OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_framer.h" | 5 #include "net/spdy/spdy_framer.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <iterator> | 10 #include <iterator> |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 void UnpackStreamDependencyValues(uint32_t packed, | 75 void UnpackStreamDependencyValues(uint32_t packed, |
76 bool* exclusive, | 76 bool* exclusive, |
77 SpdyStreamId* parent_stream_id) { | 77 SpdyStreamId* parent_stream_id) { |
78 *exclusive = (packed >> 31) != 0; | 78 *exclusive = (packed >> 31) != 0; |
79 // Zero out the highest-order bit to get the parent stream id. | 79 // Zero out the highest-order bit to get the parent stream id. |
80 *parent_stream_id = packed & 0x7fffffff; | 80 *parent_stream_id = packed & 0x7fffffff; |
81 } | 81 } |
82 | 82 |
83 struct DictionaryIds { | 83 struct DictionaryIds { |
84 DictionaryIds() | 84 DictionaryIds() |
85 : v2_dictionary_id(CalculateDictionaryId(kV2Dictionary, kV2DictionarySize)), | 85 : v3_dictionary_id( |
86 v3_dictionary_id(CalculateDictionaryId(kV3Dictionary, kV3DictionarySize)) | 86 CalculateDictionaryId(kV3Dictionary, kV3DictionarySize)) {} |
87 {} | |
88 const uLong v2_dictionary_id; | |
89 const uLong v3_dictionary_id; | 87 const uLong v3_dictionary_id; |
90 }; | 88 }; |
91 | 89 |
92 // Adler ID for the SPDY header compressor dictionaries. Note that they are | 90 // Adler ID for the SPDY header compressor dictionaries. Note that they are |
93 // initialized lazily to avoid static initializers. | 91 // initialized lazily to avoid static initializers. |
94 base::LazyInstance<DictionaryIds>::Leaky g_dictionary_ids; | 92 base::LazyInstance<DictionaryIds>::Leaky g_dictionary_ids; |
95 | 93 |
96 // Used to indicate no flags in a SPDY flags field. | 94 // Used to indicate no flags in a SPDY flags field. |
97 const uint8_t kNoFlags = 0; | 95 const uint8_t kNoFlags = 0; |
98 | 96 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 do { \ | 128 do { \ |
131 DCHECK(state_ != SPDY_ERROR); \ | 129 DCHECK(state_ != SPDY_ERROR); \ |
132 DCHECK_EQ(previous_state_, state_); \ | 130 DCHECK_EQ(previous_state_, state_); \ |
133 previous_state_ = state_; \ | 131 previous_state_ = state_; \ |
134 state_ = newstate; \ | 132 state_ = newstate; \ |
135 } while (false) | 133 } while (false) |
136 #endif | 134 #endif |
137 | 135 |
138 SettingsFlagsAndId SettingsFlagsAndId::FromWireFormat(SpdyMajorVersion version, | 136 SettingsFlagsAndId SettingsFlagsAndId::FromWireFormat(SpdyMajorVersion version, |
139 uint32_t wire) { | 137 uint32_t wire) { |
140 if (version < SPDY3) { | |
141 ConvertFlagsAndIdForSpdy2(&wire); | |
142 } | |
143 return SettingsFlagsAndId(base::NetToHost32(wire) >> 24, | 138 return SettingsFlagsAndId(base::NetToHost32(wire) >> 24, |
144 base::NetToHost32(wire) & 0x00ffffff); | 139 base::NetToHost32(wire) & 0x00ffffff); |
145 } | 140 } |
146 | 141 |
147 SettingsFlagsAndId::SettingsFlagsAndId(uint8_t flags, uint32_t id) | 142 SettingsFlagsAndId::SettingsFlagsAndId(uint8_t flags, uint32_t id) |
148 : flags_(flags), id_(id & 0x00ffffff) { | 143 : flags_(flags), id_(id & 0x00ffffff) { |
149 LOG_IF(DFATAL, id > (1u << 24)) << "SPDY setting ID too large: " << id; | 144 LOG_IF(DFATAL, id > (1u << 24)) << "SPDY setting ID too large: " << id; |
150 } | 145 } |
151 | 146 |
152 uint32_t SettingsFlagsAndId::GetWireFormat(SpdyMajorVersion version) const { | 147 uint32_t SettingsFlagsAndId::GetWireFormat(SpdyMajorVersion version) const { |
153 uint32_t wire = | 148 return base::HostToNet32(id_ & 0x00ffffff) | base::HostToNet32(flags_ << 24); |
154 base::HostToNet32(id_ & 0x00ffffff) | base::HostToNet32(flags_ << 24); | |
155 if (version < SPDY3) { | |
156 ConvertFlagsAndIdForSpdy2(&wire); | |
157 } | |
158 return wire; | |
159 } | |
160 | |
161 // SPDY 2 had a bug in it with respect to byte ordering of id/flags field. | |
162 // This method is used to preserve buggy behavior and works on both | |
163 // little-endian and big-endian hosts. | |
164 // This method is also bidirectional (can be used to translate SPDY 2 to SPDY 3 | |
165 // as well as vice versa). | |
166 void SettingsFlagsAndId::ConvertFlagsAndIdForSpdy2(uint32_t* val) { | |
167 uint8_t* wire_array = reinterpret_cast<uint8_t*>(val); | |
168 std::swap(wire_array[0], wire_array[3]); | |
169 std::swap(wire_array[1], wire_array[2]); | |
170 } | 149 } |
171 | 150 |
172 bool SpdyFramerVisitorInterface::OnGoAwayFrameData(const char* goaway_data, | 151 bool SpdyFramerVisitorInterface::OnGoAwayFrameData(const char* goaway_data, |
173 size_t len) { | 152 size_t len) { |
174 return true; | 153 return true; |
175 } | 154 } |
176 | 155 |
177 bool SpdyFramerVisitorInterface::OnRstStreamFrameData( | 156 bool SpdyFramerVisitorInterface::OnRstStreamFrameData( |
178 const char* rst_stream_data, | 157 const char* rst_stream_data, |
179 size_t len) { | 158 size_t len) { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 size_t SpdyFramer::GetSynReplyMinimumSize() const { | 230 size_t SpdyFramer::GetSynReplyMinimumSize() const { |
252 // Size, in bytes, of a SYN_REPLY frame not including the variable-length | 231 // Size, in bytes, of a SYN_REPLY frame not including the variable-length |
253 // header block. | 232 // header block. |
254 size_t size = GetControlFrameHeaderSize(); | 233 size_t size = GetControlFrameHeaderSize(); |
255 if (protocol_version() <= SPDY3) { | 234 if (protocol_version() <= SPDY3) { |
256 // Calculated as: | 235 // Calculated as: |
257 // control frame header + 4 (stream IDs) | 236 // control frame header + 4 (stream IDs) |
258 size += 4; | 237 size += 4; |
259 } | 238 } |
260 | 239 |
261 // In SPDY 2, there were 2 unused bytes before payload. | |
262 if (protocol_version() < SPDY3) { | |
263 size += 2; | |
264 } | |
265 | |
266 return size; | 240 return size; |
267 } | 241 } |
268 | 242 |
269 size_t SpdyFramer::GetRstStreamMinimumSize() const { | 243 size_t SpdyFramer::GetRstStreamMinimumSize() const { |
270 // Size, in bytes, of a RST_STREAM frame. | 244 // Size, in bytes, of a RST_STREAM frame. |
271 if (protocol_version() <= SPDY3) { | 245 if (protocol_version() <= SPDY3) { |
272 // Calculated as: | 246 // Calculated as: |
273 // control frame header + 4 (stream id) + 4 (status code) | 247 // control frame header + 4 (stream id) + 4 (status code) |
274 return GetControlFrameHeaderSize() + 8; | 248 return GetControlFrameHeaderSize() + 8; |
275 } else { | 249 } else { |
(...skipping 28 matching lines...) Expand all Loading... |
304 } | 278 } |
305 | 279 |
306 size_t SpdyFramer::GetGoAwayMinimumSize() const { | 280 size_t SpdyFramer::GetGoAwayMinimumSize() const { |
307 // Size, in bytes, of this GOAWAY frame. Calculated as: | 281 // Size, in bytes, of this GOAWAY frame. Calculated as: |
308 // 1. Control frame header size | 282 // 1. Control frame header size |
309 size_t size = GetControlFrameHeaderSize(); | 283 size_t size = GetControlFrameHeaderSize(); |
310 | 284 |
311 // 2. Last good stream id (4 bytes) | 285 // 2. Last good stream id (4 bytes) |
312 size += 4; | 286 size += 4; |
313 | 287 |
314 // 3. SPDY 3+ GOAWAY frames also contain a status (4 bytes) | 288 // 3. GOAWAY frames also contain a status (4 bytes) |
315 if (protocol_version() >= SPDY3) { | 289 size += 4; |
316 size += 4; | |
317 } | |
318 | 290 |
319 return size; | 291 return size; |
320 } | 292 } |
321 | 293 |
322 size_t SpdyFramer::GetHeadersMinimumSize() const { | 294 size_t SpdyFramer::GetHeadersMinimumSize() const { |
323 // Size, in bytes, of a HEADERS frame not including the variable-length | 295 // Size, in bytes, of a HEADERS frame not including the variable-length |
324 // header block. | 296 // header block. |
325 size_t size = GetControlFrameHeaderSize(); | 297 size_t size = GetControlFrameHeaderSize(); |
326 if (protocol_version() <= SPDY3) { | 298 if (protocol_version() <= SPDY3) { |
327 // Calculated as: | 299 // Calculated as: |
328 // control frame header + 4 (stream IDs) | 300 // control frame header + 4 (stream IDs) |
329 size += 4; | 301 size += 4; |
330 } | 302 } |
331 | 303 |
332 // In SPDY 2, there were 2 unused bytes before payload. | |
333 if (protocol_version() <= SPDY2) { | |
334 size += 2; | |
335 } | |
336 | |
337 return size; | 304 return size; |
338 } | 305 } |
339 | 306 |
340 size_t SpdyFramer::GetWindowUpdateSize() const { | 307 size_t SpdyFramer::GetWindowUpdateSize() const { |
341 // Size, in bytes, of a WINDOW_UPDATE frame. | 308 // Size, in bytes, of a WINDOW_UPDATE frame. |
342 if (protocol_version() <= SPDY3) { | 309 if (protocol_version() <= SPDY3) { |
343 // Calculated as: | 310 // Calculated as: |
344 // control frame header + 4 (stream id) + 4 (delta) | 311 // control frame header + 4 (stream id) + 4 (delta) |
345 return GetControlFrameHeaderSize() + 8; | 312 return GetControlFrameHeaderSize() + 8; |
346 } else { | 313 } else { |
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1191 current_frame_buffer_.CopyFrom(*data, bytes_to_read); | 1158 current_frame_buffer_.CopyFrom(*data, bytes_to_read); |
1192 *data += bytes_to_read; | 1159 *data += bytes_to_read; |
1193 *len -= bytes_to_read; | 1160 *len -= bytes_to_read; |
1194 } | 1161 } |
1195 return bytes_to_read; | 1162 return bytes_to_read; |
1196 } | 1163 } |
1197 | 1164 |
1198 size_t SpdyFramer::GetSerializedLength( | 1165 size_t SpdyFramer::GetSerializedLength( |
1199 const SpdyMajorVersion spdy_version, | 1166 const SpdyMajorVersion spdy_version, |
1200 const SpdyHeaderBlock* headers) { | 1167 const SpdyHeaderBlock* headers) { |
1201 const size_t num_name_value_pairs_size = | 1168 const size_t num_name_value_pairs_size = sizeof(uint32_t); |
1202 (spdy_version < SPDY3) ? sizeof(uint16_t) : sizeof(uint32_t); | |
1203 const size_t length_of_name_size = num_name_value_pairs_size; | 1169 const size_t length_of_name_size = num_name_value_pairs_size; |
1204 const size_t length_of_value_size = num_name_value_pairs_size; | 1170 const size_t length_of_value_size = num_name_value_pairs_size; |
1205 | 1171 |
1206 size_t total_length = num_name_value_pairs_size; | 1172 size_t total_length = num_name_value_pairs_size; |
1207 for (const auto& header : *headers) { | 1173 for (const auto& header : *headers) { |
1208 // We add space for the length of the name and the length of the value as | 1174 // We add space for the length of the name and the length of the value as |
1209 // well as the length of the name and the length of the value. | 1175 // well as the length of the name and the length of the value. |
1210 total_length += length_of_name_size + header.first.size() + | 1176 total_length += length_of_name_size + header.first.size() + |
1211 length_of_value_size + header.second.size(); | 1177 length_of_value_size + header.second.size(); |
1212 } | 1178 } |
1213 return total_length; | 1179 return total_length; |
1214 } | 1180 } |
1215 | 1181 |
1216 void SpdyFramer::WriteHeaderBlock(SpdyFrameBuilder* frame, | 1182 void SpdyFramer::WriteHeaderBlock(SpdyFrameBuilder* frame, |
1217 const SpdyMajorVersion spdy_version, | 1183 const SpdyMajorVersion spdy_version, |
1218 const SpdyHeaderBlock* headers) { | 1184 const SpdyHeaderBlock* headers) { |
1219 if (spdy_version < SPDY3) { | 1185 frame->WriteUInt32(headers->size()); |
1220 frame->WriteUInt16(static_cast<uint16_t>(headers->size())); | |
1221 } else { | |
1222 frame->WriteUInt32(headers->size()); | |
1223 } | |
1224 SpdyHeaderBlock::const_iterator it; | 1186 SpdyHeaderBlock::const_iterator it; |
1225 for (it = headers->begin(); it != headers->end(); ++it) { | 1187 for (it = headers->begin(); it != headers->end(); ++it) { |
1226 if (spdy_version < SPDY3) { | 1188 frame->WriteStringPiece32(it->first); |
1227 frame->WriteStringPiece16(it->first); | 1189 frame->WriteStringPiece32(it->second); |
1228 frame->WriteStringPiece16(it->second); | |
1229 } else { | |
1230 frame->WriteStringPiece32(it->first); | |
1231 frame->WriteStringPiece32(it->second); | |
1232 } | |
1233 } | 1190 } |
1234 } | 1191 } |
1235 | 1192 |
1236 // TODO(phajdan.jr): Clean up after we no longer need | 1193 // TODO(phajdan.jr): Clean up after we no longer need |
1237 // to workaround http://crbug.com/139744. | 1194 // to workaround http://crbug.com/139744. |
1238 #if !defined(USE_SYSTEM_ZLIB) | 1195 #if !defined(USE_SYSTEM_ZLIB) |
1239 | 1196 |
1240 // These constants are used by zlib to differentiate between normal data and | 1197 // These constants are used by zlib to differentiate between normal data and |
1241 // cookie data. Cookie data is handled specially by zlib when compressing. | 1198 // cookie data. Cookie data is handled specially by zlib when compressing. |
1242 enum ZDataClass { | 1199 enum ZDataClass { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1300 n >>= 8; | 1257 n >>= 8; |
1301 } | 1258 } |
1302 WriteZ(base::StringPiece(buf, length), clas, out); | 1259 WriteZ(base::StringPiece(buf, length), clas, out); |
1303 } | 1260 } |
1304 | 1261 |
1305 // WriteHeaderBlockToZ serialises |headers| to the deflate context |z| in a | 1262 // WriteHeaderBlockToZ serialises |headers| to the deflate context |z| in a |
1306 // manner that resists the length of the compressed data from compromising | 1263 // manner that resists the length of the compressed data from compromising |
1307 // cookie data. | 1264 // cookie data. |
1308 void SpdyFramer::WriteHeaderBlockToZ(const SpdyHeaderBlock* headers, | 1265 void SpdyFramer::WriteHeaderBlockToZ(const SpdyHeaderBlock* headers, |
1309 z_stream* z) const { | 1266 z_stream* z) const { |
1310 unsigned length_length = 4; | 1267 const size_t length_length = 4; |
1311 if (protocol_version() < 3) | |
1312 length_length = 2; | |
1313 | |
1314 WriteLengthZ(headers->size(), length_length, kZStandardData, z); | 1268 WriteLengthZ(headers->size(), length_length, kZStandardData, z); |
1315 | 1269 |
1316 SpdyHeaderBlock::const_iterator it; | 1270 SpdyHeaderBlock::const_iterator it; |
1317 for (it = headers->begin(); it != headers->end(); ++it) { | 1271 for (it = headers->begin(); it != headers->end(); ++it) { |
1318 WriteLengthZ(it->first.size(), length_length, kZStandardData, z); | 1272 WriteLengthZ(it->first.size(), length_length, kZStandardData, z); |
1319 WriteZ(it->first, kZStandardData, z); | 1273 WriteZ(it->first, kZStandardData, z); |
1320 | 1274 |
1321 if (it->first == "cookie") { | 1275 if (it->first == "cookie") { |
1322 // We require the cookie values (save for the last) to end with a | 1276 // We require the cookie values (save for the last) to end with a |
1323 // semicolon and (save for the first) to start with a space. This is | 1277 // semicolon and (save for the first) to start with a space. This is |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1436 break; | 1390 break; |
1437 } | 1391 } |
1438 | 1392 |
1439 SpdyStreamId associated_to_stream_id = kInvalidStream; | 1393 SpdyStreamId associated_to_stream_id = kInvalidStream; |
1440 successful_read = reader.ReadUInt31(&associated_to_stream_id); | 1394 successful_read = reader.ReadUInt31(&associated_to_stream_id); |
1441 DCHECK(successful_read); | 1395 DCHECK(successful_read); |
1442 | 1396 |
1443 SpdyPriority priority = 0; | 1397 SpdyPriority priority = 0; |
1444 successful_read = reader.ReadUInt8(&priority); | 1398 successful_read = reader.ReadUInt8(&priority); |
1445 DCHECK(successful_read); | 1399 DCHECK(successful_read); |
1446 if (protocol_version() <= SPDY2) { | 1400 priority = priority >> 5; |
1447 priority = priority >> 6; | |
1448 } else { | |
1449 priority = priority >> 5; | |
1450 } | |
1451 | 1401 |
1452 // Seek past unused byte. | 1402 // Seek past unused byte. |
1453 reader.Seek(1); | 1403 reader.Seek(1); |
1454 | 1404 |
1455 DCHECK(reader.IsDoneReading()); | 1405 DCHECK(reader.IsDoneReading()); |
1456 if (debug_visitor_) { | 1406 if (debug_visitor_) { |
1457 debug_visitor_->OnReceiveCompressedFrame( | 1407 debug_visitor_->OnReceiveCompressedFrame( |
1458 current_frame_stream_id_, | 1408 current_frame_stream_id_, |
1459 current_frame_type_, | 1409 current_frame_type_, |
1460 current_frame_length_); | 1410 current_frame_length_); |
(...skipping 27 matching lines...) Expand all Loading... |
1488 } | 1438 } |
1489 bool successful_read = true; | 1439 bool successful_read = true; |
1490 if (protocol_version() <= SPDY3) { | 1440 if (protocol_version() <= SPDY3) { |
1491 successful_read = reader.ReadUInt31(¤t_frame_stream_id_); | 1441 successful_read = reader.ReadUInt31(¤t_frame_stream_id_); |
1492 DCHECK(successful_read); | 1442 DCHECK(successful_read); |
1493 } | 1443 } |
1494 if (current_frame_stream_id_ == 0) { | 1444 if (current_frame_stream_id_ == 0) { |
1495 set_error(SPDY_INVALID_CONTROL_FRAME); | 1445 set_error(SPDY_INVALID_CONTROL_FRAME); |
1496 break; | 1446 break; |
1497 } | 1447 } |
1498 if (protocol_version() <= SPDY2) { | |
1499 // SPDY 2 had two unused bytes here. Seek past them. | |
1500 reader.Seek(2); | |
1501 } | |
1502 if (protocol_version() > SPDY3 && | 1448 if (protocol_version() > SPDY3 && |
1503 !(current_frame_flags_ & HEADERS_FLAG_END_HEADERS) && | 1449 !(current_frame_flags_ & HEADERS_FLAG_END_HEADERS) && |
1504 current_frame_type_ == HEADERS) { | 1450 current_frame_type_ == HEADERS) { |
1505 expect_continuation_ = current_frame_stream_id_; | 1451 expect_continuation_ = current_frame_stream_id_; |
1506 end_stream_when_done_ = current_frame_flags_ & CONTROL_FLAG_FIN; | 1452 end_stream_when_done_ = current_frame_flags_ & CONTROL_FLAG_FIN; |
1507 } | 1453 } |
1508 if (protocol_version() > SPDY3 && | 1454 if (protocol_version() > SPDY3 && |
1509 current_frame_flags_ & HEADERS_FLAG_PADDED) { | 1455 current_frame_flags_ & HEADERS_FLAG_PADDED) { |
1510 uint8_t pad_payload_len = 0; | 1456 uint8_t pad_payload_len = 0; |
1511 DCHECK_EQ(remaining_padding_payload_length_, 0u); | 1457 DCHECK_EQ(remaining_padding_payload_length_, 0u); |
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2202 return original_len - len; | 2148 return original_len - len; |
2203 } | 2149 } |
2204 | 2150 |
2205 bool SpdyFramer::ParseHeaderBlockInBuffer(const char* header_data, | 2151 bool SpdyFramer::ParseHeaderBlockInBuffer(const char* header_data, |
2206 size_t header_length, | 2152 size_t header_length, |
2207 SpdyHeaderBlock* block) const { | 2153 SpdyHeaderBlock* block) const { |
2208 SpdyFrameReader reader(header_data, header_length); | 2154 SpdyFrameReader reader(header_data, header_length); |
2209 | 2155 |
2210 // Read number of headers. | 2156 // Read number of headers. |
2211 uint32_t num_headers; | 2157 uint32_t num_headers; |
2212 if (protocol_version() <= SPDY2) { | 2158 if (!reader.ReadUInt32(&num_headers)) { |
2213 uint16_t temp; | 2159 DVLOG(1) << "Unable to read number of headers."; |
2214 if (!reader.ReadUInt16(&temp)) { | 2160 return false; |
2215 DVLOG(1) << "Unable to read number of headers."; | |
2216 return false; | |
2217 } | |
2218 num_headers = temp; | |
2219 } else { | |
2220 if (!reader.ReadUInt32(&num_headers)) { | |
2221 DVLOG(1) << "Unable to read number of headers."; | |
2222 return false; | |
2223 } | |
2224 } | 2161 } |
2225 | 2162 |
2226 // Read each header. | 2163 // Read each header. |
2227 for (uint32_t index = 0; index < num_headers; ++index) { | 2164 for (uint32_t index = 0; index < num_headers; ++index) { |
2228 base::StringPiece temp; | 2165 base::StringPiece temp; |
2229 | 2166 |
2230 // Read header name. | 2167 // Read header name. |
2231 if ((protocol_version() <= SPDY2) ? !reader.ReadStringPiece16(&temp) | 2168 if (!reader.ReadStringPiece32(&temp)) { |
2232 : !reader.ReadStringPiece32(&temp)) { | |
2233 DVLOG(1) << "Unable to read header name (" << index + 1 << " of " | 2169 DVLOG(1) << "Unable to read header name (" << index + 1 << " of " |
2234 << num_headers << ")."; | 2170 << num_headers << ")."; |
2235 return false; | 2171 return false; |
2236 } | 2172 } |
2237 std::string name = temp.as_string(); | 2173 std::string name = temp.as_string(); |
2238 | 2174 |
2239 // Read header value. | 2175 // Read header value. |
2240 if ((protocol_version() <= SPDY2) ? !reader.ReadStringPiece16(&temp) | 2176 if (!reader.ReadStringPiece32(&temp)) { |
2241 : !reader.ReadStringPiece32(&temp)) { | |
2242 DVLOG(1) << "Unable to read header value (" << index + 1 << " of " | 2177 DVLOG(1) << "Unable to read header value (" << index + 1 << " of " |
2243 << num_headers << ")."; | 2178 << num_headers << ")."; |
2244 return false; | 2179 return false; |
2245 } | 2180 } |
2246 std::string value = temp.as_string(); | 2181 std::string value = temp.as_string(); |
2247 | 2182 |
2248 // Ensure no duplicates. | 2183 // Ensure no duplicates. |
2249 if (block->find(name) != block->end()) { | 2184 if (block->find(name) != block->end()) { |
2250 DVLOG(1) << "Duplicate header '" << name << "' (" << index + 1 << " of " | 2185 DVLOG(1) << "Duplicate header '" << name << "' (" << index + 1 << " of " |
2251 << num_headers << ")."; | 2186 << num_headers << ")."; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2356 } | 2291 } |
2357 | 2292 |
2358 // The size of this frame, including variable-length header block. | 2293 // The size of this frame, including variable-length header block. |
2359 size_t size = GetSynStreamMinimumSize() + | 2294 size_t size = GetSynStreamMinimumSize() + |
2360 GetSerializedLength(syn_stream.header_block()); | 2295 GetSerializedLength(syn_stream.header_block()); |
2361 | 2296 |
2362 SpdyFrameBuilder builder(size, protocol_version()); | 2297 SpdyFrameBuilder builder(size, protocol_version()); |
2363 builder.WriteControlFrameHeader(*this, SYN_STREAM, flags); | 2298 builder.WriteControlFrameHeader(*this, SYN_STREAM, flags); |
2364 builder.WriteUInt32(syn_stream.stream_id()); | 2299 builder.WriteUInt32(syn_stream.stream_id()); |
2365 builder.WriteUInt32(syn_stream.associated_to_stream_id()); | 2300 builder.WriteUInt32(syn_stream.associated_to_stream_id()); |
2366 builder.WriteUInt8(priority << ((protocol_version() <= SPDY2) ? 6 : 5)); | 2301 builder.WriteUInt8(priority << 5); |
2367 builder.WriteUInt8(0); // Unused byte. | 2302 builder.WriteUInt8(0); // Unused byte. |
2368 DCHECK_EQ(GetSynStreamMinimumSize(), builder.length()); | 2303 DCHECK_EQ(GetSynStreamMinimumSize(), builder.length()); |
2369 SerializeHeaderBlock(&builder, syn_stream); | 2304 SerializeHeaderBlock(&builder, syn_stream); |
2370 | 2305 |
2371 if (debug_visitor_) { | 2306 if (debug_visitor_) { |
2372 const size_t payload_len = | 2307 const size_t payload_len = |
2373 GetSerializedLength(protocol_version(), &(syn_stream.header_block())); | 2308 GetSerializedLength(protocol_version(), &(syn_stream.header_block())); |
2374 debug_visitor_->OnSendCompressedFrame(syn_stream.stream_id(), | 2309 debug_visitor_->OnSendCompressedFrame(syn_stream.stream_id(), |
2375 SYN_STREAM, | 2310 SYN_STREAM, |
2376 payload_len, | 2311 payload_len, |
(...skipping 18 matching lines...) Expand all Loading... |
2395 SpdyFrameBuilder builder(size, protocol_version()); | 2330 SpdyFrameBuilder builder(size, protocol_version()); |
2396 if (protocol_version() <= SPDY3) { | 2331 if (protocol_version() <= SPDY3) { |
2397 builder.WriteControlFrameHeader(*this, SYN_REPLY, flags); | 2332 builder.WriteControlFrameHeader(*this, SYN_REPLY, flags); |
2398 builder.WriteUInt32(syn_reply.stream_id()); | 2333 builder.WriteUInt32(syn_reply.stream_id()); |
2399 } else { | 2334 } else { |
2400 builder.BeginNewFrame(*this, | 2335 builder.BeginNewFrame(*this, |
2401 HEADERS, | 2336 HEADERS, |
2402 flags, | 2337 flags, |
2403 syn_reply.stream_id()); | 2338 syn_reply.stream_id()); |
2404 } | 2339 } |
2405 if (protocol_version() < SPDY3) { | |
2406 builder.WriteUInt16(0); // Unused. | |
2407 } | |
2408 DCHECK_EQ(GetSynReplyMinimumSize(), builder.length()); | 2340 DCHECK_EQ(GetSynReplyMinimumSize(), builder.length()); |
2409 SerializeHeaderBlock(&builder, syn_reply); | 2341 SerializeHeaderBlock(&builder, syn_reply); |
2410 | 2342 |
2411 if (debug_visitor_) { | 2343 if (debug_visitor_) { |
2412 const size_t payload_len = | 2344 const size_t payload_len = |
2413 GetSerializedLength(protocol_version(), &(syn_reply.header_block())); | 2345 GetSerializedLength(protocol_version(), &(syn_reply.header_block())); |
2414 debug_visitor_->OnSendCompressedFrame(syn_reply.stream_id(), | 2346 debug_visitor_->OnSendCompressedFrame(syn_reply.stream_id(), |
2415 SYN_REPLY, | 2347 SYN_REPLY, |
2416 payload_len, | 2348 payload_len, |
2417 builder.length()); | 2349 builder.length()); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2537 // Serialize the GOAWAY frame. | 2469 // Serialize the GOAWAY frame. |
2538 if (protocol_version() <= SPDY3) { | 2470 if (protocol_version() <= SPDY3) { |
2539 builder.WriteControlFrameHeader(*this, GOAWAY, kNoFlags); | 2471 builder.WriteControlFrameHeader(*this, GOAWAY, kNoFlags); |
2540 } else { | 2472 } else { |
2541 builder.BeginNewFrame(*this, GOAWAY, 0, 0); | 2473 builder.BeginNewFrame(*this, GOAWAY, 0, 0); |
2542 } | 2474 } |
2543 | 2475 |
2544 // GOAWAY frames specify the last good stream id for all SPDY versions. | 2476 // GOAWAY frames specify the last good stream id for all SPDY versions. |
2545 builder.WriteUInt32(goaway.last_good_stream_id()); | 2477 builder.WriteUInt32(goaway.last_good_stream_id()); |
2546 | 2478 |
2547 // In SPDY3 and up, GOAWAY frames also specify the error status code. | 2479 // GOAWAY frames also specify the error status code. |
2548 if (protocol_version() >= SPDY3) { | 2480 if (protocol_version() >= SPDY3) { |
2549 // TODO(jgraettinger): Merge back to server-side. | 2481 // TODO(jgraettinger): Merge back to server-side. |
2550 builder.WriteUInt32(SpdyConstants::SerializeGoAwayStatus(protocol_version(), | 2482 builder.WriteUInt32(SpdyConstants::SerializeGoAwayStatus(protocol_version(), |
2551 goaway.status())); | 2483 goaway.status())); |
2552 } | 2484 } |
2553 | 2485 |
2554 // In HTTP2 and up, GOAWAY frames may also specify opaque data. | 2486 // In HTTP2 and up, GOAWAY frames may also specify opaque data. |
2555 if ((protocol_version() > SPDY3) && (goaway.description().size() > 0)) { | 2487 if ((protocol_version() > SPDY3) && (goaway.description().size() > 0)) { |
2556 builder.WriteBytes(goaway.description().data(), | 2488 builder.WriteBytes(goaway.description().data(), |
2557 goaway.description().size()); | 2489 goaway.description().size()); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2618 SpdyFrameBuilder builder(size, protocol_version()); | 2550 SpdyFrameBuilder builder(size, protocol_version()); |
2619 if (protocol_version() <= SPDY3) { | 2551 if (protocol_version() <= SPDY3) { |
2620 builder.WriteControlFrameHeader(*this, HEADERS, flags); | 2552 builder.WriteControlFrameHeader(*this, HEADERS, flags); |
2621 builder.WriteUInt32(headers.stream_id()); | 2553 builder.WriteUInt32(headers.stream_id()); |
2622 } else { | 2554 } else { |
2623 builder.BeginNewFrame(*this, | 2555 builder.BeginNewFrame(*this, |
2624 HEADERS, | 2556 HEADERS, |
2625 flags, | 2557 flags, |
2626 headers.stream_id()); | 2558 headers.stream_id()); |
2627 } | 2559 } |
2628 if (protocol_version() <= SPDY2) { | |
2629 builder.WriteUInt16(0); // Unused. | |
2630 } | |
2631 DCHECK_EQ(GetHeadersMinimumSize(), builder.length()); | 2560 DCHECK_EQ(GetHeadersMinimumSize(), builder.length()); |
2632 | 2561 |
2633 if (protocol_version() > SPDY3) { | 2562 if (protocol_version() > SPDY3) { |
2634 int padding_payload_len = 0; | 2563 int padding_payload_len = 0; |
2635 if (headers.padded()) { | 2564 if (headers.padded()) { |
2636 builder.WriteUInt8(headers.padding_payload_len()); | 2565 builder.WriteUInt8(headers.padding_payload_len()); |
2637 padding_payload_len = headers.padding_payload_len(); | 2566 padding_payload_len = headers.padding_payload_len(); |
2638 } | 2567 } |
2639 if (headers.has_priority()) { | 2568 if (headers.has_priority()) { |
2640 builder.WriteUInt32(PackStreamDependencyValues( | 2569 builder.WriteUInt32(PackStreamDependencyValues( |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2983 header_compressor_.reset(new z_stream); | 2912 header_compressor_.reset(new z_stream); |
2984 memset(header_compressor_.get(), 0, sizeof(z_stream)); | 2913 memset(header_compressor_.get(), 0, sizeof(z_stream)); |
2985 | 2914 |
2986 int success = deflateInit2(header_compressor_.get(), | 2915 int success = deflateInit2(header_compressor_.get(), |
2987 kCompressorLevel, | 2916 kCompressorLevel, |
2988 Z_DEFLATED, | 2917 Z_DEFLATED, |
2989 kCompressorWindowSizeInBits, | 2918 kCompressorWindowSizeInBits, |
2990 kCompressorMemLevel, | 2919 kCompressorMemLevel, |
2991 Z_DEFAULT_STRATEGY); | 2920 Z_DEFAULT_STRATEGY); |
2992 if (success == Z_OK) { | 2921 if (success == Z_OK) { |
2993 const char* dictionary = (protocol_version() <= SPDY2) ? | 2922 const char* dictionary = kV3Dictionary; |
2994 kV2Dictionary : kV3Dictionary; | 2923 const int dictionary_size = kV3DictionarySize; |
2995 const int dictionary_size = (protocol_version() <= SPDY2) ? | |
2996 kV2DictionarySize : kV3DictionarySize; | |
2997 success = deflateSetDictionary(header_compressor_.get(), | 2924 success = deflateSetDictionary(header_compressor_.get(), |
2998 reinterpret_cast<const Bytef*>(dictionary), | 2925 reinterpret_cast<const Bytef*>(dictionary), |
2999 dictionary_size); | 2926 dictionary_size); |
3000 } | 2927 } |
3001 if (success != Z_OK) { | 2928 if (success != Z_OK) { |
3002 LOG(WARNING) << "deflateSetDictionary failure: " << success; | 2929 LOG(WARNING) << "deflateSetDictionary failure: " << success; |
3003 header_compressor_.reset(NULL); | 2930 header_compressor_.reset(NULL); |
3004 return NULL; | 2931 return NULL; |
3005 } | 2932 } |
3006 return header_compressor_.get(); | 2933 return header_compressor_.get(); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3073 // If we get a SYN_STREAM/SYN_REPLY/HEADERS frame with stream ID zero, we | 3000 // If we get a SYN_STREAM/SYN_REPLY/HEADERS frame with stream ID zero, we |
3074 // signal an error back in ProcessControlFrameBeforeHeaderBlock. So if we've | 3001 // signal an error back in ProcessControlFrameBeforeHeaderBlock. So if we've |
3075 // reached this method successfully, stream_id should be nonzero. | 3002 // reached this method successfully, stream_id should be nonzero. |
3076 DCHECK_LT(0u, stream_id); | 3003 DCHECK_LT(0u, stream_id); |
3077 while (decomp->avail_in > 0 && processed_successfully) { | 3004 while (decomp->avail_in > 0 && processed_successfully) { |
3078 decomp->next_out = reinterpret_cast<Bytef*>(buffer); | 3005 decomp->next_out = reinterpret_cast<Bytef*>(buffer); |
3079 decomp->avail_out = arraysize(buffer); | 3006 decomp->avail_out = arraysize(buffer); |
3080 | 3007 |
3081 int rv = inflate(decomp, Z_SYNC_FLUSH); | 3008 int rv = inflate(decomp, Z_SYNC_FLUSH); |
3082 if (rv == Z_NEED_DICT) { | 3009 if (rv == Z_NEED_DICT) { |
3083 const char* dictionary = (protocol_version() <= SPDY2) ? kV2Dictionary | 3010 const char* dictionary = kV3Dictionary; |
3084 : kV3Dictionary; | 3011 const int dictionary_size = kV3DictionarySize; |
3085 const int dictionary_size = (protocol_version() <= SPDY2) ? | |
3086 kV2DictionarySize : kV3DictionarySize; | |
3087 const DictionaryIds& ids = g_dictionary_ids.Get(); | 3012 const DictionaryIds& ids = g_dictionary_ids.Get(); |
3088 const uLong dictionary_id = (protocol_version() <= SPDY2) ? | 3013 const uLong dictionary_id = ids.v3_dictionary_id; |
3089 ids.v2_dictionary_id : ids.v3_dictionary_id; | |
3090 // Need to try again with the right dictionary. | 3014 // Need to try again with the right dictionary. |
3091 if (decomp->adler == dictionary_id) { | 3015 if (decomp->adler == dictionary_id) { |
3092 rv = inflateSetDictionary(decomp, | 3016 rv = inflateSetDictionary(decomp, |
3093 reinterpret_cast<const Bytef*>(dictionary), | 3017 reinterpret_cast<const Bytef*>(dictionary), |
3094 dictionary_size); | 3018 dictionary_size); |
3095 if (rv == Z_OK) { | 3019 if (rv == Z_OK) { |
3096 rv = inflate(decomp, Z_SYNC_FLUSH); | 3020 rv = inflate(decomp, Z_SYNC_FLUSH); |
3097 } | 3021 } |
3098 } | 3022 } |
3099 } | 3023 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3150 return kDefaultHeaderTableSizeSetting; | 3074 return kDefaultHeaderTableSizeSetting; |
3151 } else { | 3075 } else { |
3152 return hpack_encoder_->CurrentHeaderTableSizeSetting(); | 3076 return hpack_encoder_->CurrentHeaderTableSizeSetting(); |
3153 } | 3077 } |
3154 } | 3078 } |
3155 | 3079 |
3156 void SpdyFramer::SerializeHeaderBlockWithoutCompression( | 3080 void SpdyFramer::SerializeHeaderBlockWithoutCompression( |
3157 SpdyFrameBuilder* builder, | 3081 SpdyFrameBuilder* builder, |
3158 const SpdyHeaderBlock& header_block) const { | 3082 const SpdyHeaderBlock& header_block) const { |
3159 // Serialize number of headers. | 3083 // Serialize number of headers. |
3160 if (protocol_version() <= SPDY2) { | 3084 builder->WriteUInt32(header_block.size()); |
3161 builder->WriteUInt16(static_cast<uint16_t>(header_block.size())); | |
3162 } else { | |
3163 builder->WriteUInt32(header_block.size()); | |
3164 } | |
3165 | 3085 |
3166 // Serialize each header. | 3086 // Serialize each header. |
3167 for (const auto& header : header_block) { | 3087 for (const auto& header : header_block) { |
3168 if (protocol_version() <= SPDY2) { | 3088 builder->WriteStringPiece32(header.first); |
3169 builder->WriteStringPiece16(header.first); | 3089 builder->WriteStringPiece32(header.second); |
3170 builder->WriteStringPiece16(header.second); | |
3171 } else { | |
3172 builder->WriteStringPiece32(header.first); | |
3173 builder->WriteStringPiece32(header.second); | |
3174 } | |
3175 } | 3090 } |
3176 } | 3091 } |
3177 | 3092 |
3178 void SpdyFramer::SerializeHeaderBlock(SpdyFrameBuilder* builder, | 3093 void SpdyFramer::SerializeHeaderBlock(SpdyFrameBuilder* builder, |
3179 const SpdyFrameWithHeaderBlockIR& frame) { | 3094 const SpdyFrameWithHeaderBlockIR& frame) { |
3180 CHECK_GE(SPDY3, protocol_version()); | 3095 CHECK_GE(SPDY3, protocol_version()); |
3181 if (!enable_compression_) { | 3096 if (!enable_compression_) { |
3182 return SerializeHeaderBlockWithoutCompression(builder, | 3097 return SerializeHeaderBlockWithoutCompression(builder, |
3183 frame.header_block()); | 3098 frame.header_block()); |
3184 } | 3099 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3228 #else | 3143 #else |
3229 WriteHeaderBlockToZ(&frame.header_block(), compressor); | 3144 WriteHeaderBlockToZ(&frame.header_block(), compressor); |
3230 #endif // defined(USE_SYSTEM_ZLIB) | 3145 #endif // defined(USE_SYSTEM_ZLIB) |
3231 | 3146 |
3232 int compressed_size = compressed_max_size - compressor->avail_out; | 3147 int compressed_size = compressed_max_size - compressor->avail_out; |
3233 builder->Seek(compressed_size); | 3148 builder->Seek(compressed_size); |
3234 builder->RewriteLength(*this); | 3149 builder->RewriteLength(*this); |
3235 } | 3150 } |
3236 | 3151 |
3237 } // namespace net | 3152 } // namespace net |
OLD | NEW |