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

Side by Side Diff: net/spdy/spdy_framer.cc

Issue 1263803002: Replace SpdyNameValueBlock typedef with SpdyHeaderBlock. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 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
« no previous file with comments | « net/spdy/spdy_framer.h ('k') | net/spdy/spdy_framer_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 #include <string> 9 #include <string>
10 10
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 return SpdyConstants::GetDataFrameMinimumSize(protocol_version()); 225 return SpdyConstants::GetDataFrameMinimumSize(protocol_version());
226 } 226 }
227 227
228 // Size, in bytes, of the control frame header. 228 // Size, in bytes, of the control frame header.
229 size_t SpdyFramer::GetControlFrameHeaderSize() const { 229 size_t SpdyFramer::GetControlFrameHeaderSize() const {
230 return SpdyConstants::GetControlFrameHeaderSize(protocol_version()); 230 return SpdyConstants::GetControlFrameHeaderSize(protocol_version());
231 } 231 }
232 232
233 size_t SpdyFramer::GetSynStreamMinimumSize() const { 233 size_t SpdyFramer::GetSynStreamMinimumSize() const {
234 // Size, in bytes, of a SYN_STREAM frame not including the variable-length 234 // Size, in bytes, of a SYN_STREAM frame not including the variable-length
235 // name-value block. 235 // header block.
236 if (protocol_version() <= SPDY3) { 236 if (protocol_version() <= SPDY3) {
237 // Calculated as: 237 // Calculated as:
238 // control frame header + 2 * 4 (stream IDs) + 1 (priority) 238 // control frame header + 2 * 4 (stream IDs) + 1 (priority)
239 // + 1 (unused, was credential slot) 239 // + 1 (unused, was credential slot)
240 return GetControlFrameHeaderSize() + 10; 240 return GetControlFrameHeaderSize() + 10;
241 } else { 241 } else {
242 return GetControlFrameHeaderSize() + 242 return GetControlFrameHeaderSize() +
243 kPriorityDependencyPayloadSize + 243 kPriorityDependencyPayloadSize +
244 kPriorityWeightPayloadSize; 244 kPriorityWeightPayloadSize;
245 } 245 }
246 } 246 }
247 247
248 size_t SpdyFramer::GetSynReplyMinimumSize() const { 248 size_t SpdyFramer::GetSynReplyMinimumSize() const {
249 // Size, in bytes, of a SYN_REPLY frame not including the variable-length 249 // Size, in bytes, of a SYN_REPLY frame not including the variable-length
250 // name-value block. 250 // header block.
251 size_t size = GetControlFrameHeaderSize(); 251 size_t size = GetControlFrameHeaderSize();
252 if (protocol_version() <= SPDY3) { 252 if (protocol_version() <= SPDY3) {
253 // Calculated as: 253 // Calculated as:
254 // control frame header + 4 (stream IDs) 254 // control frame header + 4 (stream IDs)
255 size += 4; 255 size += 4;
256 } 256 }
257 257
258 // In SPDY 2, there were 2 unused bytes before payload. 258 // In SPDY 2, there were 2 unused bytes before payload.
259 if (protocol_version() < SPDY3) { 259 if (protocol_version() < SPDY3) {
260 size += 2; 260 size += 2;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 // 3. SPDY 3+ GOAWAY frames also contain a status (4 bytes) 311 // 3. SPDY 3+ GOAWAY frames also contain a status (4 bytes)
312 if (protocol_version() >= SPDY3) { 312 if (protocol_version() >= SPDY3) {
313 size += 4; 313 size += 4;
314 } 314 }
315 315
316 return size; 316 return size;
317 } 317 }
318 318
319 size_t SpdyFramer::GetHeadersMinimumSize() const { 319 size_t SpdyFramer::GetHeadersMinimumSize() const {
320 // Size, in bytes, of a HEADERS frame not including the variable-length 320 // Size, in bytes, of a HEADERS frame not including the variable-length
321 // name-value block. 321 // header block.
322 size_t size = GetControlFrameHeaderSize(); 322 size_t size = GetControlFrameHeaderSize();
323 if (protocol_version() <= SPDY3) { 323 if (protocol_version() <= SPDY3) {
324 // Calculated as: 324 // Calculated as:
325 // control frame header + 4 (stream IDs) 325 // control frame header + 4 (stream IDs)
326 size += 4; 326 size += 4;
327 } 327 }
328 328
329 // In SPDY 2, there were 2 unused bytes before payload. 329 // In SPDY 2, there were 2 unused bytes before payload.
330 if (protocol_version() <= SPDY2) { 330 if (protocol_version() <= SPDY2) {
331 size += 2; 331 size += 2;
(...skipping 862 matching lines...) Expand 10 before | Expand all | Expand 10 after
1194 1194
1195 size_t SpdyFramer::GetSerializedLength( 1195 size_t SpdyFramer::GetSerializedLength(
1196 const SpdyMajorVersion spdy_version, 1196 const SpdyMajorVersion spdy_version,
1197 const SpdyHeaderBlock* headers) { 1197 const SpdyHeaderBlock* headers) {
1198 const size_t num_name_value_pairs_size 1198 const size_t num_name_value_pairs_size
1199 = (spdy_version < SPDY3) ? sizeof(uint16) : sizeof(uint32); 1199 = (spdy_version < SPDY3) ? sizeof(uint16) : sizeof(uint32);
1200 const size_t length_of_name_size = num_name_value_pairs_size; 1200 const size_t length_of_name_size = num_name_value_pairs_size;
1201 const size_t length_of_value_size = num_name_value_pairs_size; 1201 const size_t length_of_value_size = num_name_value_pairs_size;
1202 1202
1203 size_t total_length = num_name_value_pairs_size; 1203 size_t total_length = num_name_value_pairs_size;
1204 for (SpdyHeaderBlock::const_iterator it = headers->begin(); 1204 for (const auto& header : *headers) {
1205 it != headers->end();
1206 ++it) {
1207 // We add space for the length of the name and the length of the value as 1205 // We add space for the length of the name and the length of the value as
1208 // well as the length of the name and the length of the value. 1206 // well as the length of the name and the length of the value.
1209 total_length += length_of_name_size + it->first.size() + 1207 total_length += length_of_name_size + header.first.size() +
1210 length_of_value_size + it->second.size(); 1208 length_of_value_size + header.second.size();
1211 } 1209 }
1212 return total_length; 1210 return total_length;
1213 } 1211 }
1214 1212
1215 void SpdyFramer::WriteHeaderBlock(SpdyFrameBuilder* frame, 1213 void SpdyFramer::WriteHeaderBlock(SpdyFrameBuilder* frame,
1216 const SpdyMajorVersion spdy_version, 1214 const SpdyMajorVersion spdy_version,
1217 const SpdyHeaderBlock* headers) { 1215 const SpdyHeaderBlock* headers) {
1218 if (spdy_version < SPDY3) { 1216 if (spdy_version < SPDY3) {
1219 frame->WriteUInt16(static_cast<uint16>(headers->size())); 1217 frame->WriteUInt16(static_cast<uint16>(headers->size()));
1220 } else { 1218 } else {
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after
1754 CHANGE_STATE(SPDY_FRAME_COMPLETE); 1752 CHANGE_STATE(SPDY_FRAME_COMPLETE);
1755 } 1753 }
1756 1754
1757 return processed_bytes; 1755 return processed_bytes;
1758 } 1756 }
1759 1757
1760 void SpdyFramer::DeliverHpackBlockAsSpdy3Block() { 1758 void SpdyFramer::DeliverHpackBlockAsSpdy3Block() {
1761 DCHECK_LT(SPDY3, protocol_version()); 1759 DCHECK_LT(SPDY3, protocol_version());
1762 DCHECK_EQ(remaining_padding_payload_length_, remaining_data_length_); 1760 DCHECK_EQ(remaining_padding_payload_length_, remaining_data_length_);
1763 1761
1764 const SpdyNameValueBlock& block = GetHpackDecoder()->decoded_block(); 1762 const SpdyHeaderBlock& block = GetHpackDecoder()->decoded_block();
1765 if (block.empty()) { 1763 if (block.empty()) {
1766 // Special-case this to make tests happy. 1764 // Special-case this to make tests happy.
1767 ProcessControlFrameHeaderBlock(NULL, 0, false); 1765 ProcessControlFrameHeaderBlock(NULL, 0, false);
1768 return; 1766 return;
1769 } 1767 }
1770 SpdyFrameBuilder builder( 1768 SpdyFrameBuilder builder(
1771 GetSerializedLength(protocol_version(), &block), 1769 GetSerializedLength(protocol_version(), &block),
1772 SPDY3); 1770 SPDY3);
1773 1771
1774 SerializeNameValueBlockWithoutCompression(&builder, block); 1772 SerializeHeaderBlockWithoutCompression(&builder, block);
1775 scoped_ptr<SpdyFrame> frame(builder.take()); 1773 scoped_ptr<SpdyFrame> frame(builder.take());
1776 1774
1777 // Preserve padding length, and reset it after the re-entrant call. 1775 // Preserve padding length, and reset it after the re-entrant call.
1778 size_t remaining_padding = remaining_padding_payload_length_; 1776 size_t remaining_padding = remaining_padding_payload_length_;
1779 1777
1780 remaining_padding_payload_length_ = 0; 1778 remaining_padding_payload_length_ = 0;
1781 remaining_data_length_ = frame->size(); 1779 remaining_data_length_ = frame->size();
1782 1780
1783 ProcessControlFrameHeaderBlock(frame->data(), frame->size(), false); 1781 ProcessControlFrameHeaderBlock(frame->data(), frame->size(), false);
1784 1782
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after
2340 flags |= CONTROL_FLAG_UNIDIRECTIONAL; 2338 flags |= CONTROL_FLAG_UNIDIRECTIONAL;
2341 } 2339 }
2342 2340
2343 // Sanitize priority. 2341 // Sanitize priority.
2344 uint8 priority = syn_stream.priority(); 2342 uint8 priority = syn_stream.priority();
2345 if (priority > GetLowestPriority()) { 2343 if (priority > GetLowestPriority()) {
2346 DLOG(DFATAL) << "Priority out-of-bounds."; 2344 DLOG(DFATAL) << "Priority out-of-bounds.";
2347 priority = GetLowestPriority(); 2345 priority = GetLowestPriority();
2348 } 2346 }
2349 2347
2350 // The size of this frame, including variable-length name-value block. 2348 // The size of this frame, including variable-length header block.
2351 size_t size = GetSynStreamMinimumSize() + 2349 size_t size = GetSynStreamMinimumSize() +
2352 GetSerializedLength(syn_stream.name_value_block()); 2350 GetSerializedLength(syn_stream.header_block());
2353 2351
2354 SpdyFrameBuilder builder(size, protocol_version()); 2352 SpdyFrameBuilder builder(size, protocol_version());
2355 builder.WriteControlFrameHeader(*this, SYN_STREAM, flags); 2353 builder.WriteControlFrameHeader(*this, SYN_STREAM, flags);
2356 builder.WriteUInt32(syn_stream.stream_id()); 2354 builder.WriteUInt32(syn_stream.stream_id());
2357 builder.WriteUInt32(syn_stream.associated_to_stream_id()); 2355 builder.WriteUInt32(syn_stream.associated_to_stream_id());
2358 builder.WriteUInt8(priority << ((protocol_version() <= SPDY2) ? 6 : 5)); 2356 builder.WriteUInt8(priority << ((protocol_version() <= SPDY2) ? 6 : 5));
2359 builder.WriteUInt8(0); // Unused byte where credential slot used to be. 2357 builder.WriteUInt8(0); // Unused byte where credential slot used to be.
2360 DCHECK_EQ(GetSynStreamMinimumSize(), builder.length()); 2358 DCHECK_EQ(GetSynStreamMinimumSize(), builder.length());
2361 SerializeNameValueBlock(&builder, syn_stream); 2359 SerializeHeaderBlock(&builder, syn_stream);
2362 2360
2363 if (debug_visitor_) { 2361 if (debug_visitor_) {
2364 const size_t payload_len = 2362 const size_t payload_len =
2365 GetSerializedLength(protocol_version(), 2363 GetSerializedLength(protocol_version(), &(syn_stream.header_block()));
2366 &(syn_stream.name_value_block()));
2367 debug_visitor_->OnSendCompressedFrame(syn_stream.stream_id(), 2364 debug_visitor_->OnSendCompressedFrame(syn_stream.stream_id(),
2368 SYN_STREAM, 2365 SYN_STREAM,
2369 payload_len, 2366 payload_len,
2370 builder.length()); 2367 builder.length());
2371 } 2368 }
2372 2369
2373 return builder.take(); 2370 return builder.take();
2374 } 2371 }
2375 2372
2376 SpdySerializedFrame* SpdyFramer::SerializeSynReply( 2373 SpdySerializedFrame* SpdyFramer::SerializeSynReply(
2377 const SpdySynReplyIR& syn_reply) { 2374 const SpdySynReplyIR& syn_reply) {
2378 DCHECK_GE(SPDY3, protocol_version()); 2375 DCHECK_GE(SPDY3, protocol_version());
2379 uint8 flags = 0; 2376 uint8 flags = 0;
2380 if (syn_reply.fin()) { 2377 if (syn_reply.fin()) {
2381 flags |= CONTROL_FLAG_FIN; 2378 flags |= CONTROL_FLAG_FIN;
2382 } 2379 }
2383 2380
2384 // The size of this frame, including variable-length name-value block. 2381 // The size of this frame, including variable-length header block.
2385 const size_t size = GetSynReplyMinimumSize() + 2382 const size_t size =
2386 GetSerializedLength(syn_reply.name_value_block()); 2383 GetSynReplyMinimumSize() + GetSerializedLength(syn_reply.header_block());
2387 2384
2388 SpdyFrameBuilder builder(size, protocol_version()); 2385 SpdyFrameBuilder builder(size, protocol_version());
2389 if (protocol_version() <= SPDY3) { 2386 if (protocol_version() <= SPDY3) {
2390 builder.WriteControlFrameHeader(*this, SYN_REPLY, flags); 2387 builder.WriteControlFrameHeader(*this, SYN_REPLY, flags);
2391 builder.WriteUInt32(syn_reply.stream_id()); 2388 builder.WriteUInt32(syn_reply.stream_id());
2392 } else { 2389 } else {
2393 builder.BeginNewFrame(*this, 2390 builder.BeginNewFrame(*this,
2394 HEADERS, 2391 HEADERS,
2395 flags, 2392 flags,
2396 syn_reply.stream_id()); 2393 syn_reply.stream_id());
2397 } 2394 }
2398 if (protocol_version() < SPDY3) { 2395 if (protocol_version() < SPDY3) {
2399 builder.WriteUInt16(0); // Unused. 2396 builder.WriteUInt16(0); // Unused.
2400 } 2397 }
2401 DCHECK_EQ(GetSynReplyMinimumSize(), builder.length()); 2398 DCHECK_EQ(GetSynReplyMinimumSize(), builder.length());
2402 SerializeNameValueBlock(&builder, syn_reply); 2399 SerializeHeaderBlock(&builder, syn_reply);
2403 2400
2404 if (debug_visitor_) { 2401 if (debug_visitor_) {
2405 const size_t payload_len = GetSerializedLength( 2402 const size_t payload_len =
2406 protocol_version(), &(syn_reply.name_value_block())); 2403 GetSerializedLength(protocol_version(), &(syn_reply.header_block()));
2407 debug_visitor_->OnSendCompressedFrame(syn_reply.stream_id(), 2404 debug_visitor_->OnSendCompressedFrame(syn_reply.stream_id(),
2408 SYN_REPLY, 2405 SYN_REPLY,
2409 payload_len, 2406 payload_len,
2410 builder.length()); 2407 builder.length());
2411 } 2408 }
2412 2409
2413 return builder.take(); 2410 return builder.take();
2414 } 2411 }
2415 2412
2416 SpdySerializedFrame* SpdyFramer::SerializeRstStream( 2413 SpdySerializedFrame* SpdyFramer::SerializeRstStream(
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
2572 // This will get overwritten if we overflow into a CONTINUATION frame. 2569 // This will get overwritten if we overflow into a CONTINUATION frame.
2573 flags |= HEADERS_FLAG_END_HEADERS; 2570 flags |= HEADERS_FLAG_END_HEADERS;
2574 if (headers.has_priority()) { 2571 if (headers.has_priority()) {
2575 flags |= HEADERS_FLAG_PRIORITY; 2572 flags |= HEADERS_FLAG_PRIORITY;
2576 } 2573 }
2577 if (headers.padded()) { 2574 if (headers.padded()) {
2578 flags |= HEADERS_FLAG_PADDED; 2575 flags |= HEADERS_FLAG_PADDED;
2579 } 2576 }
2580 } 2577 }
2581 2578
2582 // The size of this frame, including padding (if there is any) 2579 // The size of this frame, including padding (if there is any) and
2583 // and variable-length name-value block. 2580 // variable-length header block.
2584 size_t size = GetHeadersMinimumSize(); 2581 size_t size = GetHeadersMinimumSize();
2585 2582
2586 if (protocol_version() > SPDY3 && headers.padded()) { 2583 if (protocol_version() > SPDY3 && headers.padded()) {
2587 size += kPadLengthFieldSize; 2584 size += kPadLengthFieldSize;
2588 size += headers.padding_payload_len(); 2585 size += headers.padding_payload_len();
2589 } 2586 }
2590 2587
2591 SpdyPriority priority = static_cast<SpdyPriority>(headers.priority()); 2588 SpdyPriority priority = static_cast<SpdyPriority>(headers.priority());
2592 if (headers.has_priority()) { 2589 if (headers.has_priority()) {
2593 if (headers.priority() > GetLowestPriority()) { 2590 if (headers.priority() > GetLowestPriority()) {
2594 DLOG(DFATAL) << "Priority out-of-bounds."; 2591 DLOG(DFATAL) << "Priority out-of-bounds.";
2595 priority = GetLowestPriority(); 2592 priority = GetLowestPriority();
2596 } 2593 }
2597 size += 5; 2594 size += 5;
2598 } 2595 }
2599 2596
2600 string hpack_encoding; 2597 string hpack_encoding;
2601 if (protocol_version() > SPDY3) { 2598 if (protocol_version() > SPDY3) {
2602 if (enable_compression_) { 2599 if (enable_compression_) {
2603 GetHpackEncoder()->EncodeHeaderSet( 2600 GetHpackEncoder()->EncodeHeaderSet(headers.header_block(),
2604 headers.name_value_block(), &hpack_encoding); 2601 &hpack_encoding);
2605 } else { 2602 } else {
2606 GetHpackEncoder()->EncodeHeaderSetWithoutCompression( 2603 GetHpackEncoder()->EncodeHeaderSetWithoutCompression(
2607 headers.name_value_block(), &hpack_encoding); 2604 headers.header_block(), &hpack_encoding);
2608 } 2605 }
2609 size += hpack_encoding.size(); 2606 size += hpack_encoding.size();
2610 if (size > kMaxControlFrameSize) { 2607 if (size > kMaxControlFrameSize) {
2611 size += GetNumberRequiredContinuationFrames(size) * 2608 size += GetNumberRequiredContinuationFrames(size) *
2612 GetContinuationMinimumSize(); 2609 GetContinuationMinimumSize();
2613 flags &= ~HEADERS_FLAG_END_HEADERS; 2610 flags &= ~HEADERS_FLAG_END_HEADERS;
2614 } 2611 }
2615 } else { 2612 } else {
2616 size += GetSerializedLength(headers.name_value_block()); 2613 size += GetSerializedLength(headers.header_block());
2617 } 2614 }
2618 2615
2619 SpdyFrameBuilder builder(size, protocol_version()); 2616 SpdyFrameBuilder builder(size, protocol_version());
2620 if (protocol_version() <= SPDY3) { 2617 if (protocol_version() <= SPDY3) {
2621 builder.WriteControlFrameHeader(*this, HEADERS, flags); 2618 builder.WriteControlFrameHeader(*this, HEADERS, flags);
2622 builder.WriteUInt32(headers.stream_id()); 2619 builder.WriteUInt32(headers.stream_id());
2623 } else { 2620 } else {
2624 builder.BeginNewFrame(*this, 2621 builder.BeginNewFrame(*this,
2625 HEADERS, 2622 HEADERS,
2626 flags, 2623 flags,
(...skipping 14 matching lines...) Expand all
2641 builder.WriteUInt32(PackStreamDependencyValues( 2638 builder.WriteUInt32(PackStreamDependencyValues(
2642 headers.exclusive(), headers.parent_stream_id())); 2639 headers.exclusive(), headers.parent_stream_id()));
2643 builder.WriteUInt8(MapPriorityToWeight(priority)); 2640 builder.WriteUInt8(MapPriorityToWeight(priority));
2644 } 2641 }
2645 WritePayloadWithContinuation(&builder, 2642 WritePayloadWithContinuation(&builder,
2646 hpack_encoding, 2643 hpack_encoding,
2647 headers.stream_id(), 2644 headers.stream_id(),
2648 HEADERS, 2645 HEADERS,
2649 padding_payload_len); 2646 padding_payload_len);
2650 } else { 2647 } else {
2651 SerializeNameValueBlock(&builder, headers); 2648 SerializeHeaderBlock(&builder, headers);
2652 } 2649 }
2653 2650
2654 if (debug_visitor_) { 2651 if (debug_visitor_) {
2655 // HTTP2 uses HPACK for header compression. However, continue to 2652 // HTTP2 uses HPACK for header compression. However, continue to
2656 // use GetSerializedLength() for an apples-to-apples comparision of 2653 // use GetSerializedLength() for an apples-to-apples comparision of
2657 // compression performance between HPACK and SPDY w/ deflate. 2654 // compression performance between HPACK and SPDY w/ deflate.
2658 const size_t payload_len = 2655 const size_t payload_len =
2659 GetSerializedLength(protocol_version(), 2656 GetSerializedLength(protocol_version(), &(headers.header_block()));
2660 &(headers.name_value_block()));
2661 debug_visitor_->OnSendCompressedFrame(headers.stream_id(), 2657 debug_visitor_->OnSendCompressedFrame(headers.stream_id(),
2662 HEADERS, 2658 HEADERS,
2663 payload_len, 2659 payload_len,
2664 builder.length()); 2660 builder.length());
2665 } 2661 }
2666 2662
2667 return builder.take(); 2663 return builder.take();
2668 } 2664 }
2669 2665
2670 SpdySerializedFrame* SpdyFramer::SerializeWindowUpdate( 2666 SpdySerializedFrame* SpdyFramer::SerializeWindowUpdate(
(...skipping 30 matching lines...) Expand all
2701 size_t size = GetPushPromiseMinimumSize(); 2697 size_t size = GetPushPromiseMinimumSize();
2702 2698
2703 if (push_promise.padded()) { 2699 if (push_promise.padded()) {
2704 flags |= PUSH_PROMISE_FLAG_PADDED; 2700 flags |= PUSH_PROMISE_FLAG_PADDED;
2705 size += kPadLengthFieldSize; 2701 size += kPadLengthFieldSize;
2706 size += push_promise.padding_payload_len(); 2702 size += push_promise.padding_payload_len();
2707 } 2703 }
2708 2704
2709 string hpack_encoding; 2705 string hpack_encoding;
2710 if (enable_compression_) { 2706 if (enable_compression_) {
2711 GetHpackEncoder()->EncodeHeaderSet( 2707 GetHpackEncoder()->EncodeHeaderSet(push_promise.header_block(),
2712 push_promise.name_value_block(), &hpack_encoding); 2708 &hpack_encoding);
2713 } else { 2709 } else {
2714 GetHpackEncoder()->EncodeHeaderSetWithoutCompression( 2710 GetHpackEncoder()->EncodeHeaderSetWithoutCompression(
2715 push_promise.name_value_block(), &hpack_encoding); 2711 push_promise.header_block(), &hpack_encoding);
2716 } 2712 }
2717 size += hpack_encoding.size(); 2713 size += hpack_encoding.size();
2718 if (size > kMaxControlFrameSize) { 2714 if (size > kMaxControlFrameSize) {
2719 size += GetNumberRequiredContinuationFrames(size) * 2715 size += GetNumberRequiredContinuationFrames(size) *
2720 GetContinuationMinimumSize(); 2716 GetContinuationMinimumSize();
2721 flags &= ~PUSH_PROMISE_FLAG_END_PUSH_PROMISE; 2717 flags &= ~PUSH_PROMISE_FLAG_END_PUSH_PROMISE;
2722 } 2718 }
2723 2719
2724 SpdyFrameBuilder builder(size, protocol_version()); 2720 SpdyFrameBuilder builder(size, protocol_version());
2725 builder.BeginNewFrame(*this, 2721 builder.BeginNewFrame(*this,
(...skipping 17 matching lines...) Expand all
2743 hpack_encoding, 2739 hpack_encoding,
2744 push_promise.stream_id(), 2740 push_promise.stream_id(),
2745 PUSH_PROMISE, 2741 PUSH_PROMISE,
2746 padding_payload_len); 2742 padding_payload_len);
2747 2743
2748 if (debug_visitor_) { 2744 if (debug_visitor_) {
2749 // HTTP2 uses HPACK for header compression. However, continue to 2745 // HTTP2 uses HPACK for header compression. However, continue to
2750 // use GetSerializedLength() for an apples-to-apples comparision of 2746 // use GetSerializedLength() for an apples-to-apples comparision of
2751 // compression performance between HPACK and SPDY w/ deflate. 2747 // compression performance between HPACK and SPDY w/ deflate.
2752 const size_t payload_len = 2748 const size_t payload_len =
2753 GetSerializedLength(protocol_version(), 2749 GetSerializedLength(protocol_version(), &(push_promise.header_block()));
2754 &(push_promise.name_value_block()));
2755 debug_visitor_->OnSendCompressedFrame(push_promise.stream_id(), 2750 debug_visitor_->OnSendCompressedFrame(push_promise.stream_id(),
2756 PUSH_PROMISE, 2751 PUSH_PROMISE,
2757 payload_len, 2752 payload_len,
2758 builder.length()); 2753 builder.length());
2759 } 2754 }
2760 2755
2761 return builder.take(); 2756 return builder.take();
2762 } 2757 }
2763 2758
2764 // TODO(jgraettinger): This implementation is incorrect. The continuation 2759 // TODO(jgraettinger): This implementation is incorrect. The continuation
2765 // frame continues a previously-begun HPACK encoding; it doesn't begin a 2760 // frame continues a previously-begun HPACK encoding; it doesn't begin a
2766 // new one. Figure out whether it makes sense to keep SerializeContinuation(). 2761 // new one. Figure out whether it makes sense to keep SerializeContinuation().
2767 SpdyFrame* SpdyFramer::SerializeContinuation( 2762 SpdyFrame* SpdyFramer::SerializeContinuation(
2768 const SpdyContinuationIR& continuation) { 2763 const SpdyContinuationIR& continuation) {
2769 CHECK_LT(SPDY3, protocol_version()); 2764 CHECK_LT(SPDY3, protocol_version());
2770 uint8 flags = 0; 2765 uint8 flags = 0;
2771 if (continuation.end_headers()) { 2766 if (continuation.end_headers()) {
2772 flags |= HEADERS_FLAG_END_HEADERS; 2767 flags |= HEADERS_FLAG_END_HEADERS;
2773 } 2768 }
2774 2769
2775 // The size of this frame, including variable-length name-value block. 2770 // The size of this frame, including variable-length name-value block.
2776 size_t size = GetContinuationMinimumSize(); 2771 size_t size = GetContinuationMinimumSize();
2777 string hpack_encoding; 2772 string hpack_encoding;
2778 if (enable_compression_) { 2773 if (enable_compression_) {
2779 GetHpackEncoder()->EncodeHeaderSet( 2774 GetHpackEncoder()->EncodeHeaderSet(continuation.header_block(),
2780 continuation.name_value_block(), &hpack_encoding); 2775 &hpack_encoding);
2781 } else { 2776 } else {
2782 GetHpackEncoder()->EncodeHeaderSetWithoutCompression( 2777 GetHpackEncoder()->EncodeHeaderSetWithoutCompression(
2783 continuation.name_value_block(), &hpack_encoding); 2778 continuation.header_block(), &hpack_encoding);
2784 } 2779 }
2785 size += hpack_encoding.size(); 2780 size += hpack_encoding.size();
2786 2781
2787 SpdyFrameBuilder builder(size, protocol_version()); 2782 SpdyFrameBuilder builder(size, protocol_version());
2788 builder.BeginNewFrame(*this, CONTINUATION, flags, 2783 builder.BeginNewFrame(*this, CONTINUATION, flags,
2789 continuation.stream_id()); 2784 continuation.stream_id());
2790 DCHECK_EQ(GetContinuationMinimumSize(), builder.length()); 2785 DCHECK_EQ(GetContinuationMinimumSize(), builder.length());
2791 2786
2792 builder.WriteBytes(&hpack_encoding[0], hpack_encoding.size()); 2787 builder.WriteBytes(&hpack_encoding[0], hpack_encoding.size());
2793 return builder.take(); 2788 return builder.take();
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
3145 header_table_size_bound_ = value; 3140 header_table_size_bound_ = value;
3146 GetHpackEncoder()->ApplyHeaderTableSizeSetting(value); 3141 GetHpackEncoder()->ApplyHeaderTableSizeSetting(value);
3147 GetHpackDecoder()->ApplyHeaderTableSizeSetting(value); 3142 GetHpackDecoder()->ApplyHeaderTableSizeSetting(value);
3148 } 3143 }
3149 3144
3150 // Return size bound of the header compression table. 3145 // Return size bound of the header compression table.
3151 size_t SpdyFramer::header_table_size_bound() const { 3146 size_t SpdyFramer::header_table_size_bound() const {
3152 return header_table_size_bound_; 3147 return header_table_size_bound_;
3153 } 3148 }
3154 3149
3155 void SpdyFramer::SerializeNameValueBlockWithoutCompression( 3150 void SpdyFramer::SerializeHeaderBlockWithoutCompression(
3156 SpdyFrameBuilder* builder, 3151 SpdyFrameBuilder* builder,
3157 const SpdyNameValueBlock& name_value_block) const { 3152 const SpdyHeaderBlock& header_block) const {
3158 // Serialize number of headers. 3153 // Serialize number of headers.
3159 if (protocol_version() <= SPDY2) { 3154 if (protocol_version() <= SPDY2) {
3160 builder->WriteUInt16(static_cast<uint16>(name_value_block.size())); 3155 builder->WriteUInt16(static_cast<uint16>(header_block.size()));
3161 } else { 3156 } else {
3162 builder->WriteUInt32(name_value_block.size()); 3157 builder->WriteUInt32(header_block.size());
3163 } 3158 }
3164 3159
3165 // Serialize each header. 3160 // Serialize each header.
3166 for (SpdyHeaderBlock::const_iterator it = name_value_block.begin(); 3161 for (const auto& header : header_block) {
3167 it != name_value_block.end();
3168 ++it) {
3169 if (protocol_version() <= SPDY2) { 3162 if (protocol_version() <= SPDY2) {
3170 builder->WriteStringPiece16(it->first); 3163 builder->WriteStringPiece16(header.first);
3171 builder->WriteStringPiece16(it->second); 3164 builder->WriteStringPiece16(header.second);
3172 } else { 3165 } else {
3173 builder->WriteStringPiece32(it->first); 3166 builder->WriteStringPiece32(header.first);
3174 builder->WriteStringPiece32(it->second); 3167 builder->WriteStringPiece32(header.second);
3175 } 3168 }
3176 } 3169 }
3177 } 3170 }
3178 3171
3179 void SpdyFramer::SerializeNameValueBlock( 3172 void SpdyFramer::SerializeHeaderBlock(SpdyFrameBuilder* builder,
3180 SpdyFrameBuilder* builder, 3173 const SpdyFrameWithHeaderBlockIR& frame) {
3181 const SpdyFrameWithNameValueBlockIR& frame) {
3182 CHECK_GE(SPDY3, protocol_version()); 3174 CHECK_GE(SPDY3, protocol_version());
3183 if (!enable_compression_) { 3175 if (!enable_compression_) {
3184 return SerializeNameValueBlockWithoutCompression(builder, 3176 return SerializeHeaderBlockWithoutCompression(builder,
3185 frame.name_value_block()); 3177 frame.header_block());
3186 } 3178 }
3187 3179
3188 // First build an uncompressed version to be fed into the compressor. 3180 // First build an uncompressed version to be fed into the compressor.
3189 const size_t uncompressed_len = GetSerializedLength( 3181 const size_t uncompressed_len =
3190 protocol_version(), &(frame.name_value_block())); 3182 GetSerializedLength(protocol_version(), &(frame.header_block()));
3191 SpdyFrameBuilder uncompressed_builder(uncompressed_len, protocol_version()); 3183 SpdyFrameBuilder uncompressed_builder(uncompressed_len, protocol_version());
3192 SerializeNameValueBlockWithoutCompression(&uncompressed_builder, 3184 SerializeHeaderBlockWithoutCompression(&uncompressed_builder,
3193 frame.name_value_block()); 3185 frame.header_block());
3194 scoped_ptr<SpdyFrame> uncompressed_payload(uncompressed_builder.take()); 3186 scoped_ptr<SpdyFrame> uncompressed_payload(uncompressed_builder.take());
3195 3187
3196 z_stream* compressor = GetHeaderCompressor(); 3188 z_stream* compressor = GetHeaderCompressor();
3197 if (!compressor) { 3189 if (!compressor) {
3198 LOG(DFATAL) << "Could not obtain compressor."; 3190 LOG(DFATAL) << "Could not obtain compressor.";
3199 return; 3191 return;
3200 } 3192 }
3201 // Create an output frame. 3193 // Create an output frame.
3202 // Since we'll be performing lots of flushes when compressing the data, 3194 // Since we'll be performing lots of flushes when compressing the data,
3203 // zlib's lower bounds may be insufficient. 3195 // zlib's lower bounds may be insufficient.
(...skipping 17 matching lines...) Expand all
3221 // to workaround http://crbug.com/139744. 3213 // to workaround http://crbug.com/139744.
3222 #if defined(USE_SYSTEM_ZLIB) 3214 #if defined(USE_SYSTEM_ZLIB)
3223 int rv = deflate(compressor, Z_SYNC_FLUSH); 3215 int rv = deflate(compressor, Z_SYNC_FLUSH);
3224 if (rv != Z_OK) { // How can we know that it compressed everything? 3216 if (rv != Z_OK) { // How can we know that it compressed everything?
3225 // This shouldn't happen, right? 3217 // This shouldn't happen, right?
3226 LOG(WARNING) << "deflate failure: " << rv; 3218 LOG(WARNING) << "deflate failure: " << rv;
3227 // TODO(akalin): Upstream this return. 3219 // TODO(akalin): Upstream this return.
3228 return; 3220 return;
3229 } 3221 }
3230 #else 3222 #else
3231 WriteHeaderBlockToZ(&frame.name_value_block(), compressor); 3223 WriteHeaderBlockToZ(&frame.header_block(), compressor);
3232 #endif // defined(USE_SYSTEM_ZLIB) 3224 #endif // defined(USE_SYSTEM_ZLIB)
3233 3225
3234 int compressed_size = compressed_max_size - compressor->avail_out; 3226 int compressed_size = compressed_max_size - compressor->avail_out;
3235 builder->Seek(compressed_size); 3227 builder->Seek(compressed_size);
3236 builder->RewriteLength(*this); 3228 builder->RewriteLength(*this);
3237 } 3229 }
3238 3230
3239 } // namespace net 3231 } // namespace net
OLDNEW
« no previous file with comments | « net/spdy/spdy_framer.h ('k') | net/spdy/spdy_framer_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698