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