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/spdy_headers_block_parser.h" | 5 #include "net/spdy/spdy_headers_block_parser.h" |
6 | 6 |
7 #include "base/sys_byteorder.h" | 7 #include "base/sys_byteorder.h" |
8 | 8 |
9 namespace net { | 9 namespace net { |
10 namespace { | 10 namespace { |
11 | 11 |
12 // 0 is invalid according to both the SPDY 3.1 and HTTP/2 specifications. | 12 // 0 is invalid according to both the SPDY 3.1 and HTTP/2 specifications. |
13 const SpdyStreamId kInvalidStreamId = 0; | 13 const SpdyStreamId kInvalidStreamId = 0; |
14 | 14 |
15 } // anonymous namespace | 15 } // anonymous namespace |
16 | 16 |
| 17 namespace { |
| 18 const size_t kLengthFieldSize = sizeof(uint32_t); |
| 19 } // anonymous namespace |
| 20 |
17 const size_t SpdyHeadersBlockParser::kMaximumFieldLength = 16 * 1024; | 21 const size_t SpdyHeadersBlockParser::kMaximumFieldLength = 16 * 1024; |
18 | 22 |
19 SpdyHeadersBlockParser::SpdyHeadersBlockParser( | 23 SpdyHeadersBlockParser::SpdyHeadersBlockParser( |
20 SpdyMajorVersion spdy_version, | 24 SpdyMajorVersion spdy_version, |
21 SpdyHeadersHandlerInterface* handler) | 25 SpdyHeadersHandlerInterface* handler) |
22 : state_(READING_HEADER_BLOCK_LEN), | 26 : state_(READING_HEADER_BLOCK_LEN), |
23 length_field_size_(LengthFieldSizeForVersion(spdy_version)), | 27 max_headers_in_block_(MaxNumberOfHeaders()), |
24 max_headers_in_block_(MaxNumberOfHeadersForVersion(spdy_version)), | |
25 total_bytes_received_(0), | 28 total_bytes_received_(0), |
26 remaining_key_value_pairs_for_frame_(0), | 29 remaining_key_value_pairs_for_frame_(0), |
27 handler_(handler), | 30 handler_(handler), |
28 stream_id_(kInvalidStreamId), | 31 stream_id_(kInvalidStreamId), |
29 error_(NO_PARSER_ERROR), | 32 error_(NO_PARSER_ERROR), |
30 spdy_version_(spdy_version) { | 33 spdy_version_(spdy_version) { |
31 // The handler that we set must not be NULL. | 34 // The handler that we set must not be NULL. |
32 DCHECK(handler_ != NULL); | 35 DCHECK(handler_ != NULL); |
33 } | 36 } |
34 | 37 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 void SpdyHeadersBlockParser::ParseFieldLength(Reader* reader) { | 158 void SpdyHeadersBlockParser::ParseFieldLength(Reader* reader) { |
156 ParseLength(reader, &next_field_length_); | 159 ParseLength(reader, &next_field_length_); |
157 if (error_ == NO_PARSER_ERROR && next_field_length_ > kMaximumFieldLength) { | 160 if (error_ == NO_PARSER_ERROR && next_field_length_ > kMaximumFieldLength) { |
158 error_ = HEADER_FIELD_TOO_LARGE; | 161 error_ = HEADER_FIELD_TOO_LARGE; |
159 } | 162 } |
160 } | 163 } |
161 | 164 |
162 void SpdyHeadersBlockParser::ParseLength(Reader* reader, | 165 void SpdyHeadersBlockParser::ParseLength(Reader* reader, |
163 uint32_t* parsed_length) { | 166 uint32_t* parsed_length) { |
164 char buffer[] = {0, 0, 0, 0}; | 167 char buffer[] = {0, 0, 0, 0}; |
165 if (!reader->ReadN(length_field_size_, buffer)) { | 168 if (!reader->ReadN(kLengthFieldSize, buffer)) { |
166 error_ = NEED_MORE_DATA; | 169 error_ = NEED_MORE_DATA; |
167 return; | 170 return; |
168 } | 171 } |
169 // Convert from network to host order and return the parsed out integer. | 172 // Convert from network to host order and return the parsed out integer. |
170 if (length_field_size_ == sizeof(uint32_t)) { | 173 *parsed_length = |
171 *parsed_length = | 174 base::NetToHost32(*reinterpret_cast<const uint32_t*>(buffer)); |
172 base::NetToHost32(*reinterpret_cast<const uint32_t *>(buffer)); | |
173 } else { | |
174 *parsed_length = | |
175 base::NetToHost16(*reinterpret_cast<const uint16_t *>(buffer)); | |
176 } | |
177 } | 175 } |
178 | 176 |
179 size_t SpdyHeadersBlockParser::LengthFieldSizeForVersion( | 177 size_t SpdyHeadersBlockParser::MaxNumberOfHeaders() { |
180 SpdyMajorVersion spdy_version) { | |
181 if (spdy_version < SPDY3) { | |
182 return sizeof(uint16_t); | |
183 } | |
184 return sizeof(uint32_t); | |
185 } | |
186 | |
187 size_t SpdyHeadersBlockParser::MaxNumberOfHeadersForVersion( | |
188 SpdyMajorVersion spdy_version) { | |
189 // Account for the length of the header block field. | 178 // Account for the length of the header block field. |
190 size_t max_bytes_for_headers = | 179 size_t max_bytes_for_headers = kMaximumFieldLength - kLengthFieldSize; |
191 kMaximumFieldLength - LengthFieldSizeForVersion(spdy_version); | |
192 | 180 |
193 // A minimal size header is twice the length field size (and has a | 181 // A minimal size header is twice the length field size (and has a |
194 // zero-lengthed key and a zero-lengthed value). | 182 // zero-lengthed key and a zero-lengthed value). |
195 return max_bytes_for_headers / (2 * LengthFieldSizeForVersion(spdy_version)); | 183 return max_bytes_for_headers / (2 * kLengthFieldSize); |
196 } | 184 } |
197 | 185 |
198 } // namespace net | 186 } // namespace net |
OLD | NEW |