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
3 // found in the LICENSE file.
5 #include "net/spdy/hpack_decoder.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "net/spdy/hpack_constants.h"
10 #include "net/spdy/hpack_output_stream.h"
14 using base::StringPiece
;
19 const char kCookieKey
[] = "cookie";
23 HpackDecoder::HpackDecoder(const HpackHuffmanTable
& table
)
24 : max_string_literal_size_(kDefaultMaxStringLiteralSize
),
25 huffman_table_(table
) {}
27 HpackDecoder::~HpackDecoder() {}
29 bool HpackDecoder::HandleControlFrameHeadersData(SpdyStreamId id
,
30 const char* headers_data
,
31 size_t headers_data_length
) {
32 decoded_block_
.clear();
34 size_t new_size
= headers_block_buffer_
.size() + headers_data_length
;
35 if (new_size
> kMaxDecodeBufferSize
) {
38 headers_block_buffer_
.insert(headers_block_buffer_
.end(),
40 headers_data
+ headers_data_length
);
44 bool HpackDecoder::HandleControlFrameHeadersComplete(SpdyStreamId id
) {
45 HpackInputStream
input_stream(max_string_literal_size_
,
46 headers_block_buffer_
);
47 while (input_stream
.HasMoreData()) {
48 if (!DecodeNextOpcode(&input_stream
)) {
49 headers_block_buffer_
.clear();
53 headers_block_buffer_
.clear();
55 // Emit the Cookie header, if any crumbles were encountered.
56 if (!cookie_value_
.empty()) {
57 decoded_block_
[kCookieKey
] = cookie_value_
;
58 cookie_value_
.clear();
63 void HpackDecoder::HandleHeaderRepresentation(StringPiece name
,
65 typedef std::pair
<std::map
<string
, string
>::iterator
, bool> InsertResult
;
67 if (name
== kCookieKey
) {
68 if (cookie_value_
.empty()) {
69 cookie_value_
.assign(value
.data(), value
.size());
71 cookie_value_
+= "; ";
72 cookie_value_
.insert(cookie_value_
.end(), value
.begin(), value
.end());
75 InsertResult result
= decoded_block_
.insert(
76 std::make_pair(name
.as_string(), value
.as_string()));
78 result
.first
->second
.push_back('\0');
79 result
.first
->second
.insert(result
.first
->second
.end(),
86 bool HpackDecoder::DecodeNextOpcode(HpackInputStream
* input_stream
) {
87 // Implements 7.1: Indexed Header Field Representation.
88 if (input_stream
->MatchPrefixAndConsume(kIndexedOpcode
)) {
89 return DecodeNextIndexedHeader(input_stream
);
91 // Implements 7.2.1: Literal Header Field with Incremental Indexing.
92 if (input_stream
->MatchPrefixAndConsume(kLiteralIncrementalIndexOpcode
)) {
93 return DecodeNextLiteralHeader(input_stream
, true);
95 // Implements 7.2.2: Literal Header Field without Indexing.
96 if (input_stream
->MatchPrefixAndConsume(kLiteralNoIndexOpcode
)) {
97 return DecodeNextLiteralHeader(input_stream
, false);
99 // Implements 7.2.3: Literal Header Field never Indexed.
100 // TODO(jgraettinger): Preserve the never-indexed bit.
101 if (input_stream
->MatchPrefixAndConsume(kLiteralNeverIndexOpcode
)) {
102 return DecodeNextLiteralHeader(input_stream
, false);
104 // Implements 7.3: Header Table Size Update.
105 if (input_stream
->MatchPrefixAndConsume(kHeaderTableSizeUpdateOpcode
)) {
106 return DecodeNextHeaderTableSizeUpdate(input_stream
);
108 // Unrecognized opcode.
112 bool HpackDecoder::DecodeNextHeaderTableSizeUpdate(
113 HpackInputStream
* input_stream
) {
115 if (!input_stream
->DecodeNextUint32(&size
)) {
118 if (size
> header_table_
.settings_size_bound()) {
121 header_table_
.SetMaxSize(size
);
125 bool HpackDecoder::DecodeNextIndexedHeader(HpackInputStream
* input_stream
) {
127 if (!input_stream
->DecodeNextUint32(&index
))
130 HpackEntry
* entry
= header_table_
.GetByIndex(index
);
134 HandleHeaderRepresentation(entry
->name(), entry
->value());
138 bool HpackDecoder::DecodeNextLiteralHeader(HpackInputStream
* input_stream
,
141 if (!DecodeNextName(input_stream
, &name
))
145 if (!DecodeNextStringLiteral(input_stream
, false, &value
))
148 HandleHeaderRepresentation(name
, value
);
153 ignore_result(header_table_
.TryAddEntry(name
, value
));
157 bool HpackDecoder::DecodeNextName(
158 HpackInputStream
* input_stream
, StringPiece
* next_name
) {
159 uint32 index_or_zero
= 0;
160 if (!input_stream
->DecodeNextUint32(&index_or_zero
))
163 if (index_or_zero
== 0)
164 return DecodeNextStringLiteral(input_stream
, true, next_name
);
166 const HpackEntry
* entry
= header_table_
.GetByIndex(index_or_zero
);
169 } else if (entry
->IsStatic()) {
170 *next_name
= entry
->name();
172 // |entry| could be evicted as part of this insertion. Preemptively copy.
173 key_buffer_
.assign(entry
->name());
174 *next_name
= key_buffer_
;
179 bool HpackDecoder::DecodeNextStringLiteral(HpackInputStream
* input_stream
,
181 StringPiece
* output
) {
182 if (input_stream
->MatchPrefixAndConsume(kStringLiteralHuffmanEncoded
)) {
183 string
* buffer
= is_key
? &key_buffer_
: &value_buffer_
;
184 bool result
= input_stream
->DecodeNextHuffmanString(huffman_table_
, buffer
);
185 *output
= StringPiece(*buffer
);
187 } else if (input_stream
->MatchPrefixAndConsume(
188 kStringLiteralIdentityEncoded
)) {
189 return input_stream
->DecodeNextIdentityString(output
);