| OLD | NEW |
| 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/hpack_decoder.h" | 5 #include "net/spdy/hpack/hpack_decoder.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "net/spdy/hpack/hpack_constants.h" | 10 #include "net/spdy/hpack/hpack_constants.h" |
| 11 #include "net/spdy/hpack/hpack_output_stream.h" | 11 #include "net/spdy/hpack/hpack_output_stream.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 using base::StringPiece; | 15 using base::StringPiece; |
| 16 using std::string; | 16 using std::string; |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const char kCookieKey[] = "cookie"; | 20 const char kCookieKey[] = "cookie"; |
| 21 | 21 |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 HpackDecoder::HpackDecoder() | 24 HpackDecoder::HpackDecoder() |
| 25 : max_string_literal_size_(kDefaultMaxStringLiteralSize), | 25 : max_string_literal_size_(kDefaultMaxStringLiteralSize), |
| 26 handler_(nullptr), | 26 handler_(nullptr), |
| 27 total_header_bytes_(0), | 27 total_header_bytes_(0), |
| 28 regular_header_seen_(false), | |
| 29 header_block_started_(false), | 28 header_block_started_(false), |
| 30 total_parsed_bytes_(0) {} | 29 total_parsed_bytes_(0) {} |
| 31 | 30 |
| 32 HpackDecoder::~HpackDecoder() {} | 31 HpackDecoder::~HpackDecoder() {} |
| 33 | 32 |
| 34 bool HpackDecoder::HandleControlFrameHeadersData(const char* headers_data, | 33 bool HpackDecoder::HandleControlFrameHeadersData(const char* headers_data, |
| 35 size_t headers_data_length) { | 34 size_t headers_data_length) { |
| 36 if (!header_block_started_) { | 35 if (!header_block_started_) { |
| 37 decoded_block_.clear(); | 36 decoded_block_.clear(); |
| 38 if (handler_ != nullptr) { | 37 if (handler_ != nullptr) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 68 } | 67 } |
| 69 uint32_t parsed_bytes = input_stream.ParsedBytes(); | 68 uint32_t parsed_bytes = input_stream.ParsedBytes(); |
| 70 DCHECK_GE(headers_block_buffer_.size(), parsed_bytes); | 69 DCHECK_GE(headers_block_buffer_.size(), parsed_bytes); |
| 71 headers_block_buffer_.erase(0, parsed_bytes); | 70 headers_block_buffer_.erase(0, parsed_bytes); |
| 72 total_parsed_bytes_ += parsed_bytes; | 71 total_parsed_bytes_ += parsed_bytes; |
| 73 header_block_started_ = true; | 72 header_block_started_ = true; |
| 74 return true; | 73 return true; |
| 75 } | 74 } |
| 76 | 75 |
| 77 bool HpackDecoder::HandleControlFrameHeadersComplete(size_t* compressed_len) { | 76 bool HpackDecoder::HandleControlFrameHeadersComplete(size_t* compressed_len) { |
| 78 regular_header_seen_ = false; | |
| 79 | |
| 80 if (compressed_len != nullptr) { | 77 if (compressed_len != nullptr) { |
| 81 *compressed_len = total_parsed_bytes_; | 78 *compressed_len = total_parsed_bytes_; |
| 82 } | 79 } |
| 83 | 80 |
| 84 // Data in headers_block_buffer_ should have been parsed by | 81 // Data in headers_block_buffer_ should have been parsed by |
| 85 // HandleControlFrameHeadersData and removed. | 82 // HandleControlFrameHeadersData and removed. |
| 86 if (headers_block_buffer_.size() > 0) { | 83 if (headers_block_buffer_.size() > 0) { |
| 87 return false; | 84 return false; |
| 88 } | 85 } |
| 89 | 86 |
| 90 if (handler_ != nullptr) { | 87 if (handler_ != nullptr) { |
| 91 handler_->OnHeaderBlockEnd(total_header_bytes_); | 88 handler_->OnHeaderBlockEnd(total_header_bytes_); |
| 92 } | 89 } |
| 93 headers_block_buffer_.clear(); | 90 headers_block_buffer_.clear(); |
| 94 total_parsed_bytes_ = 0; | 91 total_parsed_bytes_ = 0; |
| 95 header_block_started_ = false; | 92 header_block_started_ = false; |
| 96 handler_ = nullptr; | 93 handler_ = nullptr; |
| 97 return true; | 94 return true; |
| 98 } | 95 } |
| 99 | 96 |
| 100 bool HpackDecoder::HandleHeaderRepresentation(StringPiece name, | 97 bool HpackDecoder::HandleHeaderRepresentation(StringPiece name, |
| 101 StringPiece value) { | 98 StringPiece value) { |
| 102 total_header_bytes_ += name.size() + value.size(); | 99 total_header_bytes_ += name.size() + value.size(); |
| 103 | 100 |
| 104 // Fail if pseudo-header follows regular header. | |
| 105 if (name.size() > 0) { | |
| 106 if (name[0] == kPseudoHeaderPrefix) { | |
| 107 if (regular_header_seen_) { | |
| 108 return false; | |
| 109 } | |
| 110 } else { | |
| 111 regular_header_seen_ = true; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 if (handler_ == nullptr) { | 101 if (handler_ == nullptr) { |
| 116 auto it = decoded_block_.find(name); | 102 auto it = decoded_block_.find(name); |
| 117 if (it == decoded_block_.end()) { | 103 if (it == decoded_block_.end()) { |
| 118 // This is a new key. | 104 // This is a new key. |
| 119 decoded_block_[name] = value; | 105 decoded_block_[name] = value; |
| 120 } else { | 106 } else { |
| 121 // The key already exists, append |value| with appropriate delimiter. | 107 // The key already exists, append |value| with appropriate delimiter. |
| 122 string new_value = it->second.as_string(); | 108 string new_value = it->second.as_string(); |
| 123 new_value.append((name == kCookieKey) ? "; " : string(1, '\0')); | 109 new_value.append((name == kCookieKey) ? "; " : string(1, '\0')); |
| 124 value.AppendToString(&new_value); | 110 value.AppendToString(&new_value); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 *output = StringPiece(*buffer); | 261 *output = StringPiece(*buffer); |
| 276 return result; | 262 return result; |
| 277 } | 263 } |
| 278 if (input_stream->MatchPrefixAndConsume(kStringLiteralIdentityEncoded)) { | 264 if (input_stream->MatchPrefixAndConsume(kStringLiteralIdentityEncoded)) { |
| 279 return input_stream->DecodeNextIdentityString(output); | 265 return input_stream->DecodeNextIdentityString(output); |
| 280 } | 266 } |
| 281 return false; | 267 return false; |
| 282 } | 268 } |
| 283 | 269 |
| 284 } // namespace net | 270 } // namespace net |
| OLD | NEW |