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

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

Issue 246073007: SPDY & HPACK: Land recent internal changes (through 65328503) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase on upstream change: Expanded FRAME_TOO_LARGE/FRAME_SIZE_ERROR comment. Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « net/spdy/hpack_output_stream.h ('k') | net/spdy/hpack_output_stream_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 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_output_stream.h" 5 #include "net/spdy/hpack_output_stream.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 8
9 using base::StringPiece;
10 9
11 namespace net { 10 namespace net {
12 11
12 using base::StringPiece;
13 using std::string; 13 using std::string;
14 14
15 HpackOutputStream::HpackOutputStream(uint32 max_string_literal_size) 15 HpackOutputStream::HpackOutputStream()
16 : max_string_literal_size_(max_string_literal_size), 16 : bit_offset_(0) {}
17 bit_offset_(0) {}
18 17
19 HpackOutputStream::~HpackOutputStream() {} 18 HpackOutputStream::~HpackOutputStream() {}
20 19
21 void HpackOutputStream::AppendIndexedHeader(uint32 index_or_zero) {
22 AppendPrefix(kIndexedOpcode);
23 AppendUint32(index_or_zero);
24 }
25
26 bool HpackOutputStream::AppendLiteralHeaderNoIndexingWithName(
27 StringPiece name, StringPiece value) {
28 AppendPrefix(kLiteralNoIndexOpcode);
29 AppendBits(0x0, 8 - kLiteralNoIndexOpcode.bit_size);
30 if (!AppendStringLiteral(name))
31 return false;
32 if (!AppendStringLiteral(value))
33 return false;
34 return true;
35 }
36
37 void HpackOutputStream::TakeString(string* output) {
38 // This must hold, since all public functions cause the buffer to
39 // end on a byte boundary.
40 DCHECK_EQ(bit_offset_, 0u);
41 buffer_.swap(*output);
42 buffer_.clear();
43 bit_offset_ = 0;
44 }
45
46 void HpackOutputStream::AppendBits(uint8 bits, size_t bit_size) { 20 void HpackOutputStream::AppendBits(uint8 bits, size_t bit_size) {
47 DCHECK_GT(bit_size, 0u); 21 DCHECK_GT(bit_size, 0u);
48 DCHECK_LE(bit_size, 8u); 22 DCHECK_LE(bit_size, 8u);
49 DCHECK_EQ(bits >> bit_size, 0); 23 DCHECK_EQ(bits >> bit_size, 0);
50 size_t new_bit_offset = bit_offset_ + bit_size; 24 size_t new_bit_offset = bit_offset_ + bit_size;
51 if (bit_offset_ == 0) { 25 if (bit_offset_ == 0) {
52 // Buffer ends on a byte boundary. 26 // Buffer ends on a byte boundary.
53 DCHECK_LE(bit_size, 8u); 27 DCHECK_LE(bit_size, 8u);
54 buffer_.append(1, bits << (8 - bit_size)); 28 buffer_.append(1, bits << (8 - bit_size));
55 } else if (new_bit_offset <= 8) { 29 } else if (new_bit_offset <= 8) {
56 // Buffer does not end on a byte boundary but the given bits fit 30 // Buffer does not end on a byte boundary but the given bits fit
57 // in the remainder of the last byte. 31 // in the remainder of the last byte.
58 *buffer_.rbegin() |= bits << (8 - new_bit_offset); 32 *buffer_.rbegin() |= bits << (8 - new_bit_offset);
59 } else { 33 } else {
60 // Buffer does not end on a byte boundary and the given bits do 34 // Buffer does not end on a byte boundary and the given bits do
61 // not fit in the remainder of the last byte. 35 // not fit in the remainder of the last byte.
62 *buffer_.rbegin() |= bits >> (new_bit_offset - 8); 36 *buffer_.rbegin() |= bits >> (new_bit_offset - 8);
63 buffer_.append(1, bits << (16 - new_bit_offset)); 37 buffer_.append(1, bits << (16 - new_bit_offset));
64 } 38 }
65 bit_offset_ = new_bit_offset % 8; 39 bit_offset_ = new_bit_offset % 8;
66 } 40 }
67 41
68 void HpackOutputStream::AppendPrefix(HpackPrefix prefix) { 42 void HpackOutputStream::AppendPrefix(HpackPrefix prefix) {
69 AppendBits(prefix.bits, prefix.bit_size); 43 AppendBits(prefix.bits, prefix.bit_size);
70 } 44 }
71 45
46 void HpackOutputStream::AppendBytes(StringPiece buffer) {
47 DCHECK_EQ(bit_offset_, 0u);
48 buffer_.append(buffer.data(), buffer.size());
49 }
50
72 void HpackOutputStream::AppendUint32(uint32 I) { 51 void HpackOutputStream::AppendUint32(uint32 I) {
73 // The algorithm below is adapted from the pseudocode in 4.1.1. 52 // The algorithm below is adapted from the pseudocode in 4.1.1.
74 size_t N = 8 - bit_offset_; 53 size_t N = 8 - bit_offset_;
75 uint8 max_first_byte = static_cast<uint8>((1 << N) - 1); 54 uint8 max_first_byte = static_cast<uint8>((1 << N) - 1);
76 if (I < max_first_byte) { 55 if (I < max_first_byte) {
77 AppendBits(static_cast<uint8>(I), N); 56 AppendBits(static_cast<uint8>(I), N);
78 } else { 57 } else {
79 AppendBits(max_first_byte, N); 58 AppendBits(max_first_byte, N);
80 I -= max_first_byte; 59 I -= max_first_byte;
81 while ((I & ~0x7f) != 0) { 60 while ((I & ~0x7f) != 0) {
82 buffer_.append(1, (I & 0x7f) | 0x80); 61 buffer_.append(1, (I & 0x7f) | 0x80);
83 I >>= 7; 62 I >>= 7;
84 } 63 }
85 AppendBits(static_cast<uint8>(I), 8); 64 AppendBits(static_cast<uint8>(I), 8);
86 } 65 }
87 } 66 }
88 67
89 bool HpackOutputStream::AppendStringLiteral(base::StringPiece str) { 68 void HpackOutputStream::TakeString(string* output) {
69 // This must hold, since all public functions cause the buffer to
70 // end on a byte boundary.
90 DCHECK_EQ(bit_offset_, 0u); 71 DCHECK_EQ(bit_offset_, 0u);
91 // TODO(akalin): Implement Huffman encoding. 72 buffer_.swap(*output);
92 AppendPrefix(kStringLiteralIdentityEncoded); 73 buffer_.clear();
93 if (str.size() > max_string_literal_size_) 74 bit_offset_ = 0;
94 return false;
95 AppendUint32(static_cast<uint32>(str.size()));
96 buffer_.append(str.data(), str.size());
97 return true;
98 } 75 }
99 76
100 } // namespace net 77 } // namespace net
OLDNEW
« no previous file with comments | « net/spdy/hpack_output_stream.h ('k') | net/spdy/hpack_output_stream_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698