Fix issue with longpress drag selection motion
[chromium-blink-merge.git] / net / spdy / hpack / hpack_decoder.cc
blobf82cb7671157c3492f8f8355443a03359e7d60a5
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"
7 #include <utility>
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"
14 namespace net {
16 using base::StringPiece;
17 using std::string;
19 namespace {
21 const char kCookieKey[] = "cookie";
23 } // namespace
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) {
39 return false;
41 headers_block_buffer_.insert(headers_block_buffer_.end(), headers_data,
42 headers_data + headers_data_length);
43 return true;
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;
51 if (compressed_len) {
52 *compressed_len = headers_block_buffer_.size();
54 while (input_stream.HasMoreData()) {
55 if (!DecodeNextOpcode(&input_stream)) {
56 headers_block_buffer_.clear();
57 return false;
60 headers_block_buffer_.clear();
62 // Emit the Cookie header, if any crumbles were encountered.
63 if (!cookie_value_.empty()) {
64 decoded_block_[kCookieKey] = cookie_value_;
65 cookie_value_.clear();
67 return true;
70 bool HpackDecoder::HandleHeaderRepresentation(StringPiece name,
71 StringPiece value) {
72 typedef std::pair<SpdyHeaderBlock::iterator, bool> InsertResult;
74 // Fail if pseudo-header follows regular header.
75 if (name.size() > 0) {
76 if (name[0] == kPseudoHeaderPrefix) {
77 if (regular_header_seen_) {
78 return false;
80 } else {
81 regular_header_seen_ = true;
85 if (name == kCookieKey) {
86 if (cookie_value_.empty()) {
87 cookie_value_.assign(value.data(), value.size());
88 } else {
89 cookie_value_ += "; ";
90 cookie_value_.insert(cookie_value_.end(), value.begin(), value.end());
92 } else {
93 InsertResult result = decoded_block_.insert(
94 std::make_pair(name.as_string(), value.as_string()));
95 if (!result.second) {
96 result.first->second.push_back('\0');
97 result.first->second.insert(result.first->second.end(), value.begin(),
98 value.end());
101 return true;
104 bool HpackDecoder::DecodeNextOpcode(HpackInputStream* input_stream) {
105 // Implements 7.1: Indexed Header Field Representation.
106 if (input_stream->MatchPrefixAndConsume(kIndexedOpcode)) {
107 return DecodeNextIndexedHeader(input_stream);
109 // Implements 7.2.1: Literal Header Field with Incremental Indexing.
110 if (input_stream->MatchPrefixAndConsume(kLiteralIncrementalIndexOpcode)) {
111 return DecodeNextLiteralHeader(input_stream, true);
113 // Implements 7.2.2: Literal Header Field without Indexing.
114 if (input_stream->MatchPrefixAndConsume(kLiteralNoIndexOpcode)) {
115 return DecodeNextLiteralHeader(input_stream, false);
117 // Implements 7.2.3: Literal Header Field never Indexed.
118 // TODO(jgraettinger): Preserve the never-indexed bit.
119 if (input_stream->MatchPrefixAndConsume(kLiteralNeverIndexOpcode)) {
120 return DecodeNextLiteralHeader(input_stream, false);
122 // Implements 7.3: Header Table Size Update.
123 if (input_stream->MatchPrefixAndConsume(kHeaderTableSizeUpdateOpcode)) {
124 return DecodeNextHeaderTableSizeUpdate(input_stream);
126 // Unrecognized opcode.
127 return false;
130 bool HpackDecoder::DecodeNextHeaderTableSizeUpdate(
131 HpackInputStream* input_stream) {
132 uint32 size = 0;
133 if (!input_stream->DecodeNextUint32(&size)) {
134 return false;
136 if (size > header_table_.settings_size_bound()) {
137 return false;
139 header_table_.SetMaxSize(size);
140 return true;
143 bool HpackDecoder::DecodeNextIndexedHeader(HpackInputStream* input_stream) {
144 uint32 index = 0;
145 if (!input_stream->DecodeNextUint32(&index)) {
146 return false;
149 const HpackEntry* entry = header_table_.GetByIndex(index);
150 if (entry == NULL) {
151 return false;
154 return HandleHeaderRepresentation(entry->name(), entry->value());
157 bool HpackDecoder::DecodeNextLiteralHeader(HpackInputStream* input_stream,
158 bool should_index) {
159 StringPiece name;
160 if (!DecodeNextName(input_stream, &name)) {
161 return false;
164 StringPiece value;
165 if (!DecodeNextStringLiteral(input_stream, false, &value)) {
166 return false;
169 if (!HandleHeaderRepresentation(name, value)) {
170 return false;
173 if (!should_index) {
174 return true;
177 ignore_result(header_table_.TryAddEntry(name, value));
178 return true;
181 bool HpackDecoder::DecodeNextName(HpackInputStream* input_stream,
182 StringPiece* next_name) {
183 uint32 index_or_zero = 0;
184 if (!input_stream->DecodeNextUint32(&index_or_zero)) {
185 return false;
188 if (index_or_zero == 0) {
189 return DecodeNextStringLiteral(input_stream, true, next_name);
192 const HpackEntry* entry = header_table_.GetByIndex(index_or_zero);
193 if (entry == NULL) {
194 return false;
196 if (entry->IsStatic()) {
197 *next_name = entry->name();
198 } else {
199 // |entry| could be evicted as part of this insertion. Preemptively copy.
200 key_buffer_.assign(entry->name());
201 *next_name = key_buffer_;
203 return true;
206 bool HpackDecoder::DecodeNextStringLiteral(HpackInputStream* input_stream,
207 bool is_key,
208 StringPiece* output) {
209 if (input_stream->MatchPrefixAndConsume(kStringLiteralHuffmanEncoded)) {
210 string* buffer = is_key ? &key_buffer_ : &value_buffer_;
211 bool result = input_stream->DecodeNextHuffmanString(huffman_table_, buffer);
212 *output = StringPiece(*buffer);
213 return result;
215 if (input_stream->MatchPrefixAndConsume(kStringLiteralIdentityEncoded)) {
216 return input_stream->DecodeNextIdentityString(output);
218 return false;
221 } // namespace net