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/hpack_decoder.h"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "net/spdy/hpack/hpack_constants.h"
12 #include "net/spdy/hpack/hpack_output_stream.h"
16 using base::StringPiece
;
21 const char kCookieKey
[] = "cookie";
25 HpackDecoder::HpackDecoder(const HpackHuffmanTable
& table
)
26 : max_string_literal_size_(kDefaultMaxStringLiteralSize
),
27 regular_header_seen_(false),
28 huffman_table_(table
) {}
30 HpackDecoder::~HpackDecoder() {}
32 bool HpackDecoder::HandleControlFrameHeadersData(SpdyStreamId id
,
33 const char* headers_data
,
34 size_t headers_data_length
) {
35 decoded_block_
.clear();
37 size_t new_size
= headers_block_buffer_
.size() + headers_data_length
;
38 if (new_size
> kMaxDecodeBufferSize
) {
41 headers_block_buffer_
.insert(headers_block_buffer_
.end(), headers_data
,
42 headers_data
+ headers_data_length
);
46 bool HpackDecoder::HandleControlFrameHeadersComplete(SpdyStreamId id
,
47 size_t* compressed_len
) {
48 HpackInputStream
input_stream(max_string_literal_size_
,
49 headers_block_buffer_
);
50 regular_header_seen_
= false;
52 *compressed_len
= headers_block_buffer_
.size();
54 while (input_stream
.HasMoreData()) {
55 if (!DecodeNextOpcode(&input_stream
)) {
56 headers_block_buffer_
.clear();
60 headers_block_buffer_
.clear();
65 bool HpackDecoder::HandleHeaderRepresentation(StringPiece name
,
67 // Fail if pseudo-header follows regular header.
68 if (name
.size() > 0) {
69 if (name
[0] == kPseudoHeaderPrefix
) {
70 if (regular_header_seen_
) {
74 regular_header_seen_
= true;
78 auto it
= decoded_block_
.find(name
.as_string());
79 if (it
== decoded_block_
.end()) {
81 decoded_block_
[name
.as_string()].assign(value
.data(), value
.size());
83 // The key already exists, append |value| with appropriate delimiter.
84 string
& old_value
= it
->second
;
85 if (name
== kCookieKey
) {
86 old_value
.append("; ");
87 value
.AppendToString(&old_value
);
89 old_value
.push_back('\0');
90 old_value
.insert(old_value
.end(), value
.begin(), value
.end());
96 bool HpackDecoder::DecodeNextOpcode(HpackInputStream
* input_stream
) {
97 // Implements 7.1: Indexed Header Field Representation.
98 if (input_stream
->MatchPrefixAndConsume(kIndexedOpcode
)) {
99 return DecodeNextIndexedHeader(input_stream
);
101 // Implements 7.2.1: Literal Header Field with Incremental Indexing.
102 if (input_stream
->MatchPrefixAndConsume(kLiteralIncrementalIndexOpcode
)) {
103 return DecodeNextLiteralHeader(input_stream
, true);
105 // Implements 7.2.2: Literal Header Field without Indexing.
106 if (input_stream
->MatchPrefixAndConsume(kLiteralNoIndexOpcode
)) {
107 return DecodeNextLiteralHeader(input_stream
, false);
109 // Implements 7.2.3: Literal Header Field never Indexed.
110 // TODO(jgraettinger): Preserve the never-indexed bit.
111 if (input_stream
->MatchPrefixAndConsume(kLiteralNeverIndexOpcode
)) {
112 return DecodeNextLiteralHeader(input_stream
, false);
114 // Implements 7.3: Header Table Size Update.
115 if (input_stream
->MatchPrefixAndConsume(kHeaderTableSizeUpdateOpcode
)) {
116 return DecodeNextHeaderTableSizeUpdate(input_stream
);
118 // Unrecognized opcode.
122 bool HpackDecoder::DecodeNextHeaderTableSizeUpdate(
123 HpackInputStream
* input_stream
) {
125 if (!input_stream
->DecodeNextUint32(&size
)) {
128 if (size
> header_table_
.settings_size_bound()) {
131 header_table_
.SetMaxSize(size
);
135 bool HpackDecoder::DecodeNextIndexedHeader(HpackInputStream
* input_stream
) {
137 if (!input_stream
->DecodeNextUint32(&index
)) {
141 const HpackEntry
* entry
= header_table_
.GetByIndex(index
);
146 return HandleHeaderRepresentation(entry
->name(), entry
->value());
149 bool HpackDecoder::DecodeNextLiteralHeader(HpackInputStream
* input_stream
,
152 if (!DecodeNextName(input_stream
, &name
)) {
157 if (!DecodeNextStringLiteral(input_stream
, false, &value
)) {
161 if (!HandleHeaderRepresentation(name
, value
)) {
169 ignore_result(header_table_
.TryAddEntry(name
, value
));
173 bool HpackDecoder::DecodeNextName(HpackInputStream
* input_stream
,
174 StringPiece
* next_name
) {
175 uint32 index_or_zero
= 0;
176 if (!input_stream
->DecodeNextUint32(&index_or_zero
)) {
180 if (index_or_zero
== 0) {
181 return DecodeNextStringLiteral(input_stream
, true, next_name
);
184 const HpackEntry
* entry
= header_table_
.GetByIndex(index_or_zero
);
188 if (entry
->IsStatic()) {
189 *next_name
= entry
->name();
191 // |entry| could be evicted as part of this insertion. Preemptively copy.
192 key_buffer_
.assign(entry
->name());
193 *next_name
= key_buffer_
;
198 bool HpackDecoder::DecodeNextStringLiteral(HpackInputStream
* input_stream
,
200 StringPiece
* output
) {
201 if (input_stream
->MatchPrefixAndConsume(kStringLiteralHuffmanEncoded
)) {
202 string
* buffer
= is_key
? &key_buffer_
: &value_buffer_
;
203 bool result
= input_stream
->DecodeNextHuffmanString(huffman_table_
, buffer
);
204 *output
= StringPiece(*buffer
);
207 if (input_stream
->MatchPrefixAndConsume(kStringLiteralIdentityEncoded
)) {
208 return input_stream
->DecodeNextIdentityString(output
);