OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_HTTP2_DECODER_HTTP2_STRUCTURE_DECODER_H_ | |
6 #define NET_HTTP2_DECODER_HTTP2_STRUCTURE_DECODER_H_ | |
7 | |
8 // Http2StructureDecoder is a class for decoding the fixed size structures in | |
9 // the HTTP/2 spec, defined in net/http2/http2_structures.h. This class | |
10 // is in aid of deciding whether to keep the SlowDecode methods which I | |
11 // (jamessynge) now think may not be worth their complexity. In particular, | |
12 // if most transport buffers are large, so it is rare that a structure is | |
13 // split across buffer boundaries, than the cost of buffering upon | |
14 // those rare occurrences is small, which then simplifies the callers. | |
15 | |
16 #include "base/logging.h" | |
17 #include "net/base/net_export.h" | |
18 #include "net/http2/decoder/decode_buffer.h" | |
19 #include "net/http2/decoder/decode_http2_structures.h" | |
20 #include "net/http2/decoder/decode_status.h" | |
21 #include "net/http2/http2_structures.h" | |
22 | |
23 namespace net { | |
24 namespace test { | |
25 class Http2StructureDecoderPeer; | |
26 } // namespace test | |
27 | |
28 class NET_EXPORT_PRIVATE Http2StructureDecoder { | |
29 public: | |
30 // The caller needs to keep track of whether to call Start or Resume. | |
31 // | |
32 // Start has an optimization for the case where the DecodeBuffer holds the | |
33 // entire encoded structure; in that case it decodes into *out and returns | |
34 // true, and does NOT touch the data members of the Http2StructureDecoder | |
35 // instance because the caller won't be calling Resume later. | |
36 // | |
37 // However, if the DecodeBuffer is too small to hold the entire encoded | |
38 // structure, Start copies the available bytes into the Http2StructureDecoder | |
39 // instance, and returns false to indicate that it has not been able to | |
40 // complete the decoding. | |
41 // | |
42 template <class S> | |
43 bool Start(S* out, DecodeBuffer* db) { | |
44 static_assert(S::EncodedSize() <= sizeof buffer_, "buffer_ is too small"); | |
45 DVLOG(2) << __func__ << "@" << this << ": db->Remaining=" << db->Remaining() | |
46 << "; EncodedSize=" << S::EncodedSize(); | |
47 if (db->Remaining() >= S::EncodedSize()) { | |
48 DoDecode(out, db); | |
49 return true; | |
50 } | |
51 IncompleteStart(db, S::EncodedSize()); | |
52 return false; | |
53 } | |
54 | |
55 template <class S> | |
56 bool Resume(S* out, DecodeBuffer* db) { | |
57 DVLOG(2) << __func__ << "@" << this << ": offset_=" << offset_ | |
58 << "; db->Remaining=" << db->Remaining(); | |
59 if (ResumeFillingBuffer(db, S::EncodedSize())) { | |
60 // We have the whole thing now. | |
61 DVLOG(2) << __func__ << "@" << this << " offset_=" << offset_ | |
62 << " Ready to decode from buffer_."; | |
63 DecodeBuffer buffer_db(buffer_, S::EncodedSize()); | |
64 DoDecode(out, &buffer_db); | |
65 return true; | |
66 } | |
67 DCHECK_LT(offset_, S::EncodedSize()); | |
68 return false; | |
69 } | |
70 | |
71 // A second pair of Start and Resume, where the caller has a variable, | |
72 // |remaining_payload| that is both tested for sufficiency and updated | |
73 // during decoding. Note that the decode buffer may extend beyond the | |
74 // remaining payload because the buffer may include padding. | |
75 template <class S> | |
76 DecodeStatus Start(S* out, DecodeBuffer* db, uint32_t* remaining_payload) { | |
77 static_assert(S::EncodedSize() <= sizeof buffer_, "buffer_ is too small"); | |
78 DVLOG(2) << __func__ << "@" << this | |
79 << ": *remaining_payload=" << *remaining_payload | |
80 << "; db->Remaining=" << db->Remaining() | |
81 << "; EncodedSize=" << S::EncodedSize(); | |
82 if (db->MinLengthRemaining(*remaining_payload) >= S::EncodedSize()) { | |
83 DoDecode(out, db); | |
84 *remaining_payload -= S::EncodedSize(); | |
85 return DecodeStatus::kDecodeDone; | |
86 } | |
87 return IncompleteStart(db, remaining_payload, S::EncodedSize()); | |
88 } | |
89 | |
90 template <class S> | |
91 bool Resume(S* out, DecodeBuffer* db, uint32_t* remaining_payload) { | |
92 DVLOG(3) << __func__ << "@" << this << ": offset_=" << offset_ | |
93 << "; *remaining_payload=" << *remaining_payload | |
94 << "; db->Remaining=" << db->Remaining() | |
95 << "; EncodedSize=" << S::EncodedSize(); | |
96 if (ResumeFillingBuffer(db, remaining_payload, S::EncodedSize())) { | |
97 // We have the whole thing now. | |
98 DVLOG(2) << __func__ << "@" << this << ": offset_=" << offset_ | |
99 << "; Ready to decode from buffer_."; | |
100 DecodeBuffer buffer_db(buffer_, S::EncodedSize()); | |
101 DoDecode(out, &buffer_db); | |
102 return true; | |
103 } | |
104 DCHECK_LT(offset_, S::EncodedSize()); | |
105 return false; | |
106 } | |
107 | |
108 uint32_t offset() const { return offset_; } | |
109 | |
110 private: | |
111 friend class test::Http2StructureDecoderPeer; | |
112 | |
113 uint32_t IncompleteStart(DecodeBuffer* db, uint32_t target_size); | |
114 DecodeStatus IncompleteStart(DecodeBuffer* db, | |
115 uint32_t* remaining_payload, | |
116 uint32_t target_size); | |
117 | |
118 bool ResumeFillingBuffer(DecodeBuffer* db, uint32_t target_size); | |
119 bool ResumeFillingBuffer(DecodeBuffer* db, | |
120 uint32_t* remaining_payload, | |
121 uint32_t target_size); | |
122 | |
123 uint32_t offset_; | |
124 char buffer_[Http2FrameHeader::EncodedSize()]; | |
125 }; | |
126 | |
127 } // namespace net | |
128 | |
129 #endif // NET_HTTP2_DECODER_HTTP2_STRUCTURE_DECODER_H_ | |
OLD | NEW |