OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/hpack_decoder.h" | 5 #include "net/spdy/hpack_decoder.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "net/spdy/hpack_constants.h" | 9 #include "net/spdy/hpack_constants.h" |
10 #include "net/spdy/hpack_output_stream.h" | 10 #include "net/spdy/hpack_output_stream.h" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 using base::StringPiece; | 14 using base::StringPiece; |
15 using std::string; | 15 using std::string; |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 const uint8 kNoState = 0; | |
20 // Set on entries added to the reference set during this decoding. | |
21 const uint8 kReferencedThisEncoding = 1; | |
22 | |
23 const char kCookieKey[] = "cookie"; | 19 const char kCookieKey[] = "cookie"; |
24 | 20 |
25 } // namespace | 21 } // namespace |
26 | 22 |
27 HpackDecoder::HpackDecoder(const HpackHuffmanTable& table) | 23 HpackDecoder::HpackDecoder(const HpackHuffmanTable& table) |
28 : max_string_literal_size_(kDefaultMaxStringLiteralSize), | 24 : max_string_literal_size_(kDefaultMaxStringLiteralSize), |
29 huffman_table_(table) {} | 25 huffman_table_(table) {} |
30 | 26 |
31 HpackDecoder::~HpackDecoder() {} | 27 HpackDecoder::~HpackDecoder() {} |
32 | 28 |
(...skipping 16 matching lines...) Expand all Loading... |
49 HpackInputStream input_stream(max_string_literal_size_, | 45 HpackInputStream input_stream(max_string_literal_size_, |
50 headers_block_buffer_); | 46 headers_block_buffer_); |
51 while (input_stream.HasMoreData()) { | 47 while (input_stream.HasMoreData()) { |
52 if (!DecodeNextOpcode(&input_stream)) { | 48 if (!DecodeNextOpcode(&input_stream)) { |
53 headers_block_buffer_.clear(); | 49 headers_block_buffer_.clear(); |
54 return false; | 50 return false; |
55 } | 51 } |
56 } | 52 } |
57 headers_block_buffer_.clear(); | 53 headers_block_buffer_.clear(); |
58 | 54 |
59 // Emit everything in the reference set that hasn't already been emitted. | |
60 // Also clear entry state for the next decoded headers block. | |
61 // TODO(jgraettinger): We may need to revisit the order in which headers | |
62 // are emitted (b/14051713). | |
63 for (HpackHeaderTable::OrderedEntrySet::const_iterator it = | |
64 header_table_.reference_set().begin(); | |
65 it != header_table_.reference_set().end(); ++it) { | |
66 HpackEntry* entry = *it; | |
67 | |
68 if (entry->state() == kNoState) { | |
69 HandleHeaderRepresentation(entry->name(), entry->value()); | |
70 } else { | |
71 entry->set_state(kNoState); | |
72 } | |
73 } | |
74 // Emit the Cookie header, if any crumbles were encountered. | 55 // Emit the Cookie header, if any crumbles were encountered. |
75 if (!cookie_value_.empty()) { | 56 if (!cookie_value_.empty()) { |
76 decoded_block_[kCookieKey] = cookie_value_; | 57 decoded_block_[kCookieKey] = cookie_value_; |
77 cookie_value_.clear(); | 58 cookie_value_.clear(); |
78 } | 59 } |
79 return true; | 60 return true; |
80 } | 61 } |
81 | 62 |
82 void HpackDecoder::HandleHeaderRepresentation(StringPiece name, | 63 void HpackDecoder::HandleHeaderRepresentation(StringPiece name, |
83 StringPiece value) { | 64 StringPiece value) { |
(...skipping 12 matching lines...) Expand all Loading... |
96 if (!result.second) { | 77 if (!result.second) { |
97 result.first->second.push_back('\0'); | 78 result.first->second.push_back('\0'); |
98 result.first->second.insert(result.first->second.end(), | 79 result.first->second.insert(result.first->second.end(), |
99 value.begin(), | 80 value.begin(), |
100 value.end()); | 81 value.end()); |
101 } | 82 } |
102 } | 83 } |
103 } | 84 } |
104 | 85 |
105 bool HpackDecoder::DecodeNextOpcode(HpackInputStream* input_stream) { | 86 bool HpackDecoder::DecodeNextOpcode(HpackInputStream* input_stream) { |
106 // Implements 4.2: Indexed Header Field Representation. | 87 // Implements 7.1: Indexed Header Field Representation. |
107 if (input_stream->MatchPrefixAndConsume(kIndexedOpcode)) { | 88 if (input_stream->MatchPrefixAndConsume(kIndexedOpcode)) { |
108 return DecodeNextIndexedHeader(input_stream); | 89 return DecodeNextIndexedHeader(input_stream); |
109 } | 90 } |
110 // Implements 4.3.1: Literal Header Field without Indexing. | 91 // Implements 7.2.1: Literal Header Field with Incremental Indexing. |
| 92 if (input_stream->MatchPrefixAndConsume(kLiteralIncrementalIndexOpcode)) { |
| 93 return DecodeNextLiteralHeader(input_stream, true); |
| 94 } |
| 95 // Implements 7.2.2: Literal Header Field without Indexing. |
111 if (input_stream->MatchPrefixAndConsume(kLiteralNoIndexOpcode)) { | 96 if (input_stream->MatchPrefixAndConsume(kLiteralNoIndexOpcode)) { |
112 return DecodeNextLiteralHeader(input_stream, false); | 97 return DecodeNextLiteralHeader(input_stream, false); |
113 } | 98 } |
114 // Implements 4.3.2: Literal Header Field with Incremental Indexing. | 99 // Implements 7.2.3: Literal Header Field never Indexed. |
115 if (input_stream->MatchPrefixAndConsume(kLiteralIncrementalIndexOpcode)) { | |
116 return DecodeNextLiteralHeader(input_stream, true); | |
117 } | |
118 // Implements 4.3.3: Literal Header Field never Indexed. | |
119 // TODO(jgraettinger): Preserve the never-indexed bit. | 100 // TODO(jgraettinger): Preserve the never-indexed bit. |
120 if (input_stream->MatchPrefixAndConsume(kLiteralNeverIndexOpcode)) { | 101 if (input_stream->MatchPrefixAndConsume(kLiteralNeverIndexOpcode)) { |
121 return DecodeNextLiteralHeader(input_stream, false); | 102 return DecodeNextLiteralHeader(input_stream, false); |
122 } | 103 } |
123 // Implements 4.4: Encoding context update. | 104 // Implements 7.3: Header Table Size Update. |
124 if (input_stream->MatchPrefixAndConsume(kEncodingContextOpcode)) { | 105 if (input_stream->MatchPrefixAndConsume(kHeaderTableSizeUpdateOpcode)) { |
125 return DecodeNextContextUpdate(input_stream); | 106 return DecodeNextHeaderTableSizeUpdate(input_stream); |
126 } | 107 } |
127 // Unrecognized opcode. | 108 // Unrecognized opcode. |
128 return false; | 109 return false; |
129 } | 110 } |
130 | 111 |
131 bool HpackDecoder::DecodeNextContextUpdate(HpackInputStream* input_stream) { | 112 bool HpackDecoder::DecodeNextHeaderTableSizeUpdate( |
132 if (input_stream->MatchPrefixAndConsume(kEncodingContextEmptyReferenceSet)) { | 113 HpackInputStream* input_stream) { |
133 header_table_.ClearReferenceSet(); | 114 uint32 size = 0; |
134 return true; | 115 if (!input_stream->DecodeNextUint32(&size)) { |
| 116 return false; |
135 } | 117 } |
136 if (input_stream->MatchPrefixAndConsume(kEncodingContextNewMaximumSize)) { | 118 if (size > header_table_.settings_size_bound()) { |
137 uint32 size = 0; | 119 return false; |
138 if (!input_stream->DecodeNextUint32(&size)) { | |
139 return false; | |
140 } | |
141 if (size > header_table_.settings_size_bound()) { | |
142 return false; | |
143 } | |
144 header_table_.SetMaxSize(size); | |
145 return true; | |
146 } | 120 } |
147 // Unrecognized encoding context update. | 121 header_table_.SetMaxSize(size); |
148 return false; | 122 return true; |
149 } | 123 } |
150 | 124 |
151 bool HpackDecoder::DecodeNextIndexedHeader(HpackInputStream* input_stream) { | 125 bool HpackDecoder::DecodeNextIndexedHeader(HpackInputStream* input_stream) { |
152 uint32 index = 0; | 126 uint32 index = 0; |
153 if (!input_stream->DecodeNextUint32(&index)) | 127 if (!input_stream->DecodeNextUint32(&index)) |
154 return false; | 128 return false; |
155 | 129 |
156 HpackEntry* entry = header_table_.GetByIndex(index); | 130 HpackEntry* entry = header_table_.GetByIndex(index); |
157 if (entry == NULL) | 131 if (entry == NULL) |
158 return false; | 132 return false; |
159 | 133 |
160 if (entry->IsStatic()) { | 134 HandleHeaderRepresentation(entry->name(), entry->value()); |
161 HandleHeaderRepresentation(entry->name(), entry->value()); | |
162 | |
163 HpackEntry* new_entry = header_table_.TryAddEntry( | |
164 entry->name(), entry->value()); | |
165 if (new_entry) { | |
166 header_table_.Toggle(new_entry); | |
167 new_entry->set_state(kReferencedThisEncoding); | |
168 } | |
169 } else { | |
170 entry->set_state(kNoState); | |
171 if (header_table_.Toggle(entry)) { | |
172 HandleHeaderRepresentation(entry->name(), entry->value()); | |
173 entry->set_state(kReferencedThisEncoding); | |
174 } | |
175 } | |
176 return true; | 135 return true; |
177 } | 136 } |
178 | 137 |
179 bool HpackDecoder::DecodeNextLiteralHeader(HpackInputStream* input_stream, | 138 bool HpackDecoder::DecodeNextLiteralHeader(HpackInputStream* input_stream, |
180 bool should_index) { | 139 bool should_index) { |
181 StringPiece name; | 140 StringPiece name; |
182 if (!DecodeNextName(input_stream, &name)) | 141 if (!DecodeNextName(input_stream, &name)) |
183 return false; | 142 return false; |
184 | 143 |
185 StringPiece value; | 144 StringPiece value; |
186 if (!DecodeNextStringLiteral(input_stream, false, &value)) | 145 if (!DecodeNextStringLiteral(input_stream, false, &value)) |
187 return false; | 146 return false; |
188 | 147 |
189 HandleHeaderRepresentation(name, value); | 148 HandleHeaderRepresentation(name, value); |
190 | 149 |
191 if (!should_index) | 150 if (!should_index) |
192 return true; | 151 return true; |
193 | 152 |
194 HpackEntry* new_entry = header_table_.TryAddEntry(name, value); | 153 ignore_result(header_table_.TryAddEntry(name, value)); |
195 if (new_entry) { | |
196 header_table_.Toggle(new_entry); | |
197 new_entry->set_state(kReferencedThisEncoding); | |
198 } | |
199 return true; | 154 return true; |
200 } | 155 } |
201 | 156 |
202 bool HpackDecoder::DecodeNextName( | 157 bool HpackDecoder::DecodeNextName( |
203 HpackInputStream* input_stream, StringPiece* next_name) { | 158 HpackInputStream* input_stream, StringPiece* next_name) { |
204 uint32 index_or_zero = 0; | 159 uint32 index_or_zero = 0; |
205 if (!input_stream->DecodeNextUint32(&index_or_zero)) | 160 if (!input_stream->DecodeNextUint32(&index_or_zero)) |
206 return false; | 161 return false; |
207 | 162 |
208 if (index_or_zero == 0) | 163 if (index_or_zero == 0) |
(...skipping 22 matching lines...) Expand all Loading... |
231 return result; | 186 return result; |
232 } else if (input_stream->MatchPrefixAndConsume( | 187 } else if (input_stream->MatchPrefixAndConsume( |
233 kStringLiteralIdentityEncoded)) { | 188 kStringLiteralIdentityEncoded)) { |
234 return input_stream->DecodeNextIdentityString(output); | 189 return input_stream->DecodeNextIdentityString(output); |
235 } else { | 190 } else { |
236 return false; | 191 return false; |
237 } | 192 } |
238 } | 193 } |
239 | 194 |
240 } // namespace net | 195 } // namespace net |
OLD | NEW |