| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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/chromium/header_coalescer.h" | 5 #include "net/spdy/chromium/header_coalescer.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/values.h" |
| 13 #include "net/http/http_log_util.h" |
| 10 #include "net/http/http_util.h" | 14 #include "net/http/http_util.h" |
| 11 #include "net/spdy/platform/api/spdy_estimate_memory_usage.h" | 15 #include "net/spdy/platform/api/spdy_estimate_memory_usage.h" |
| 12 #include "net/spdy/platform/api/spdy_string.h" | 16 #include "net/spdy/platform/api/spdy_string.h" |
| 13 | 17 |
| 14 namespace net { | 18 namespace net { |
| 19 namespace { |
| 20 std::unique_ptr<base::Value> ElideNetLogHeaderCallback( |
| 21 SpdyStringPiece header_name, |
| 22 SpdyStringPiece header_value, |
| 23 NetLogCaptureMode capture_mode) { |
| 24 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 25 dict->SetString("header_name", header_name); |
| 26 dict->SetString("header_value", ElideHeaderValueForNetLog( |
| 27 capture_mode, header_name.as_string(), |
| 28 header_value.as_string())); |
| 29 return std::move(dict); |
| 30 } |
| 31 |
| 32 } // namespace |
| 15 | 33 |
| 16 const size_t kMaxHeaderListSize = 256 * 1024; | 34 const size_t kMaxHeaderListSize = 256 * 1024; |
| 17 | 35 |
| 36 HeaderCoalescer::HeaderCoalescer(const NetLogWithSource& net_log) |
| 37 : net_log_(net_log) {} |
| 38 |
| 18 void HeaderCoalescer::OnHeader(SpdyStringPiece key, SpdyStringPiece value) { | 39 void HeaderCoalescer::OnHeader(SpdyStringPiece key, SpdyStringPiece value) { |
| 19 if (error_seen_) { | 40 if (error_seen_) |
| 20 return; | 41 return; |
| 42 if (!AddHeader(key, value)) { |
| 43 error_seen_ = true; |
| 44 if (net_log_.IsCapturing()) { |
| 45 net_log_.AddEvent(NetLogEventType::HTTP2_SESSION_RECV_INVALID_HEADER, |
| 46 base::Bind(&ElideNetLogHeaderCallback, key, value)); |
| 47 } |
| 21 } | 48 } |
| 49 } |
| 22 | 50 |
| 51 SpdyHeaderBlock HeaderCoalescer::release_headers() { |
| 52 DCHECK(headers_valid_); |
| 53 headers_valid_ = false; |
| 54 return std::move(headers_); |
| 55 } |
| 56 |
| 57 size_t HeaderCoalescer::EstimateMemoryUsage() const { |
| 58 return SpdyEstimateMemoryUsage(headers_); |
| 59 } |
| 60 |
| 61 bool HeaderCoalescer::AddHeader(SpdyStringPiece key, SpdyStringPiece value) { |
| 23 if (key.empty()) { | 62 if (key.empty()) { |
| 24 DVLOG(1) << "Header name must not be empty."; | 63 DVLOG(1) << "Header name must not be empty."; |
| 25 error_seen_ = true; | 64 return false; |
| 26 return; | |
| 27 } | 65 } |
| 28 | 66 |
| 29 SpdyStringPiece key_name = key; | 67 SpdyStringPiece key_name = key; |
| 30 if (key[0] == ':') { | 68 if (key[0] == ':') { |
| 31 if (regular_header_seen_) { | 69 if (regular_header_seen_) { |
| 32 error_seen_ = true; | 70 return false; |
| 33 return; | |
| 34 } | 71 } |
| 35 key_name.remove_prefix(1); | 72 key_name.remove_prefix(1); |
| 36 } else if (!regular_header_seen_) { | 73 } else if (!regular_header_seen_) { |
| 37 regular_header_seen_ = true; | 74 regular_header_seen_ = true; |
| 38 } | 75 } |
| 39 | 76 |
| 40 if (!HttpUtil::IsValidHeaderName(key_name)) { | 77 if (!HttpUtil::IsValidHeaderName(key_name)) { |
| 41 error_seen_ = true; | 78 return false; |
| 42 return; | |
| 43 } | 79 } |
| 44 | 80 |
| 45 // 32 byte overhead according to RFC 7540 Section 6.5.2. | 81 // 32 byte overhead according to RFC 7540 Section 6.5.2. |
| 46 header_list_size_ += key.size() + value.size() + 32; | 82 header_list_size_ += key.size() + value.size() + 32; |
| 47 if (header_list_size_ > kMaxHeaderListSize) { | 83 if (header_list_size_ > kMaxHeaderListSize) |
| 48 error_seen_ = true; | 84 return false; |
| 49 return; | |
| 50 } | |
| 51 | 85 |
| 52 // End of line delimiter is forbidden according to RFC 7230 Section 3.2. | 86 // End of line delimiter is forbidden according to RFC 7230 Section 3.2. |
| 53 // Line folding, RFC 7230 Section 3.2.4., is a special case of this. | 87 // Line folding, RFC 7230 Section 3.2.4., is a special case of this. |
| 54 if (value.find("\r\n") != SpdyStringPiece::npos) { | 88 if (value.find("\r\n") != SpdyStringPiece::npos) |
| 55 error_seen_ = true; | 89 return false; |
| 56 return; | |
| 57 } | |
| 58 | 90 |
| 59 auto iter = headers_.find(key); | 91 auto iter = headers_.find(key); |
| 60 if (iter == headers_.end()) { | 92 if (iter == headers_.end()) { |
| 61 headers_[key] = value; | 93 headers_[key] = value; |
| 62 } else { | 94 } else { |
| 63 // This header had multiple values, so it must be reconstructed. | 95 // This header had multiple values, so it must be reconstructed. |
| 64 SpdyStringPiece v = iter->second; | 96 SpdyStringPiece v = iter->second; |
| 65 SpdyString s(v.data(), v.length()); | 97 SpdyString s(v.data(), v.length()); |
| 66 if (key == "cookie") { | 98 if (key == "cookie") { |
| 67 // Obeys section 8.1.2.5 in RFC 7540 for cookie reconstruction. | 99 // Obeys section 8.1.2.5 in RFC 7540 for cookie reconstruction. |
| 68 s.append("; "); | 100 s.append("; "); |
| 69 } else { | 101 } else { |
| 70 SpdyStringPiece("\0", 1).AppendToString(&s); | 102 SpdyStringPiece("\0", 1).AppendToString(&s); |
| 71 } | 103 } |
| 72 value.AppendToString(&s); | 104 value.AppendToString(&s); |
| 73 headers_[key] = s; | 105 headers_[key] = s; |
| 74 } | 106 } |
| 107 return true; |
| 75 } | 108 } |
| 76 | 109 |
| 77 SpdyHeaderBlock HeaderCoalescer::release_headers() { | |
| 78 DCHECK(headers_valid_); | |
| 79 headers_valid_ = false; | |
| 80 return std::move(headers_); | |
| 81 } | |
| 82 | |
| 83 size_t HeaderCoalescer::EstimateMemoryUsage() const { | |
| 84 return SpdyEstimateMemoryUsage(headers_); | |
| 85 } | |
| 86 | 110 |
| 87 } // namespace net | 111 } // namespace net |
| OLD | NEW |