Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: net/http2/hpack/decoder/hpack_decoder_state.cc

Issue 2619043003: Add HpackDecoderState and HpackDecoderListener. (Closed)
Patch Set: Rebase. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "net/http2/hpack/decoder/hpack_decoder_state.h"
6
7 #include <algorithm>
8 #include <utility>
9
10 #include "base/logging.h"
11
12 using base::StringPiece;
13
14 namespace net {
15 namespace {
16
17 HpackString ExtractHpackString(HpackDecoderStringBuffer* string_buffer) {
18 if (string_buffer->IsBuffered()) {
19 return HpackString(string_buffer->ReleaseString());
20 } else {
21 auto result = HpackString(string_buffer->str());
22 string_buffer->Reset();
23 return result;
24 }
25 }
26
27 } // namespace
28
29 HpackDecoderState::HpackDecoderState(HpackDecoderListener* listener)
30 : listener_(listener),
31 final_header_table_size_(Http2SettingsInfo::DefaultHeaderTableSize()),
32 lowest_header_table_size_(final_header_table_size_),
33 require_dynamic_table_size_update_(false),
34 allow_dynamic_table_size_update_(true),
35 saw_dynamic_table_size_update_(false),
36 error_detected_(false) {
37 CHECK(listener);
38 }
39 HpackDecoderState::~HpackDecoderState() {}
40
41 void HpackDecoderState::set_listener(HpackDecoderListener* listener) {
42 CHECK(listener);
43 listener_ = listener;
44 }
45
46 void HpackDecoderState::ApplyHeaderTableSizeSetting(
47 uint32_t header_table_size) {
48 DVLOG(2) << "HpackDecoderState::ApplyHeaderTableSizeSetting("
49 << header_table_size << ")";
50 DCHECK_LE(lowest_header_table_size_, final_header_table_size_);
51 if (header_table_size < lowest_header_table_size_) {
52 lowest_header_table_size_ = header_table_size;
53 }
54 final_header_table_size_ = header_table_size;
55 DVLOG(2) << "low water mark: " << lowest_header_table_size_;
56 DVLOG(2) << "final limit: " << final_header_table_size_;
57 }
58
59 // Called to notify this object that we're starting to decode an HPACK block
60 // (e.g. a HEADERS or PUSH_PROMISE frame's header has been decoded).
61 void HpackDecoderState::OnHeaderBlockStart() {
62 DVLOG(2) << "HpackDecoderState::OnHeaderBlockStart";
63 // This instance can't be reused after an error has been detected, as we must
64 // assume that the encoder and decoder compression states are no longer
65 // synchronized.
66 DCHECK(!error_detected_);
67 DCHECK_LE(lowest_header_table_size_, final_header_table_size_);
68 allow_dynamic_table_size_update_ = true;
69 saw_dynamic_table_size_update_ = false;
70 // If the peer has acknowledged a HEADER_TABLE_SIZE smaller than that which
71 // its HPACK encoder has been using, then the next HPACK block it sends MUST
72 // start with a Dynamic Table Size Update entry that is at least as low as
73 // lowest_header_table_size_. That may be followed by another as great as
74 // final_header_table_size_, if those are different.
75 require_dynamic_table_size_update_ =
76 (lowest_header_table_size_ <
77 decoder_tables_.current_header_table_size() ||
78 final_header_table_size_ < decoder_tables_.header_table_size_limit());
79 DVLOG(2) << "HpackDecoderState::OnHeaderListStart "
80 << "require_dynamic_table_size_update_="
81 << require_dynamic_table_size_update_;
82 listener_->OnHeaderListStart();
83 }
84
85 void HpackDecoderState::OnIndexedHeader(size_t index) {
86 DVLOG(2) << "HpackDecoderState::OnIndexedHeader: " << index;
87 if (error_detected_) {
88 return;
89 }
90 if (require_dynamic_table_size_update_) {
91 ReportError("Missing dynamic table size update.");
92 return;
93 }
94 allow_dynamic_table_size_update_ = false;
95 const HpackStringPair* entry = decoder_tables_.Lookup(index);
96 if (entry != nullptr) {
97 listener_->OnHeader(HpackEntryType::kIndexedHeader, entry->name,
98 entry->value);
99 } else {
100 ReportError("Invalid index.");
101 }
102 }
103
104 void HpackDecoderState::OnNameIndexAndLiteralValue(
105 HpackEntryType entry_type,
106 size_t name_index,
107 HpackDecoderStringBuffer* value_buffer) {
108 DVLOG(2) << "HpackDecoderState::OnNameIndexAndLiteralValue " << entry_type
109 << ", " << name_index << ", " << value_buffer->str();
110 if (error_detected_) {
111 return;
112 }
113 if (require_dynamic_table_size_update_) {
114 ReportError("Missing dynamic table size update.");
115 return;
116 }
117 allow_dynamic_table_size_update_ = false;
118 const HpackStringPair* entry = decoder_tables_.Lookup(name_index);
119 if (entry != nullptr) {
120 HpackString value(ExtractHpackString(value_buffer));
121 listener_->OnHeader(entry_type, entry->name, value);
122 if (entry_type == HpackEntryType::kIndexedLiteralHeader) {
123 decoder_tables_.Insert(entry->name, value);
124 }
125 } else {
126 ReportError("Invalid name index.");
127 }
128 }
129
130 void HpackDecoderState::OnLiteralNameAndValue(
131 HpackEntryType entry_type,
132 HpackDecoderStringBuffer* name_buffer,
133 HpackDecoderStringBuffer* value_buffer) {
134 DVLOG(2) << "HpackDecoderState::OnLiteralNameAndValue " << entry_type << ", "
135 << name_buffer->str() << ", " << value_buffer->str();
136 if (error_detected_) {
137 return;
138 }
139 if (require_dynamic_table_size_update_) {
140 ReportError("Missing dynamic table size update.");
141 return;
142 }
143 allow_dynamic_table_size_update_ = false;
144 HpackString name(ExtractHpackString(name_buffer));
145 HpackString value(ExtractHpackString(value_buffer));
146 listener_->OnHeader(entry_type, name, value);
147 if (entry_type == HpackEntryType::kIndexedLiteralHeader) {
148 decoder_tables_.Insert(name, value);
149 }
150 }
151
152 void HpackDecoderState::OnDynamicTableSizeUpdate(size_t size_limit) {
153 DVLOG(2) << "HpackDecoderState::OnDynamicTableSizeUpdate " << size_limit;
154 if (error_detected_) {
155 return;
156 }
157 DCHECK_LE(lowest_header_table_size_, final_header_table_size_);
158 if (!allow_dynamic_table_size_update_) {
159 // At most two dynamic table size updates allowed at the start, and not
160 // after a header.
161 ReportError("Dynamic table size update not allowed.");
162 return;
163 }
164 if (require_dynamic_table_size_update_) {
165 // The new size must not be greater than the low water mark.
166 if (size_limit > lowest_header_table_size_) {
167 ReportError("Initial dynamic table size update is above low water mark.");
168 return;
169 }
170 require_dynamic_table_size_update_ = false;
171 } else if (size_limit > final_header_table_size_) {
172 // The new size must not be greater than the final max header table size
173 // that the peer acknowledged.
174 ReportError("Dynamic table size update is above acknowledged setting.");
175 return;
176 }
177 decoder_tables_.DynamicTableSizeUpdate(size_limit);
178 if (saw_dynamic_table_size_update_) {
179 allow_dynamic_table_size_update_ = false;
180 } else {
181 saw_dynamic_table_size_update_ = true;
182 }
183 // We no longer need to keep an eye out for a lower header table size.
184 lowest_header_table_size_ = final_header_table_size_;
185 }
186
187 void HpackDecoderState::OnHpackDecodeError(StringPiece error_message) {
188 DVLOG(2) << "HpackDecoderState::OnHpackDecodeError " << error_message;
189 if (!error_detected_) {
190 ReportError(error_message);
191 }
192 }
193
194 void HpackDecoderState::OnHeaderBlockEnd() {
195 DVLOG(2) << "HpackDecoderState::OnHeaderBlockEnd";
196 if (error_detected_) {
197 return;
198 }
199 if (require_dynamic_table_size_update_) {
200 // Apparently the HPACK block was empty, but we needed it to contain at
201 // least 1 dynamic table size update.
202 ReportError("Missing dynamic table size update.");
203 } else {
204 listener_->OnHeaderListEnd();
205 }
206 }
207
208 void HpackDecoderState::ReportError(StringPiece error_message) {
209 if (!error_detected_) {
210 listener_->OnHeaderErrorDetected(error_message);
211 error_detected_ = true;
212 }
213 }
214
215 } // namespace net
OLDNEW
« no previous file with comments | « net/http2/hpack/decoder/hpack_decoder_state.h ('k') | net/http2/hpack/decoder/hpack_decoder_state_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698