OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/spdy/header_coalescer.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/strings/string_util.h" | |
10 #include "net/http/http_util.h" | |
11 #include "net/spdy/platform/api/spdy_estimate_memory_usage.h" | |
12 #include "net/spdy/platform/api/spdy_string.h" | |
13 | |
14 namespace net { | |
15 | |
16 const size_t kMaxHeaderListSize = 256 * 1024; | |
17 | |
18 void HeaderCoalescer::OnHeader(SpdyStringPiece key, SpdyStringPiece value) { | |
19 if (error_seen_) { | |
20 return; | |
21 } | |
22 | |
23 if (key.empty()) { | |
24 DVLOG(1) << "Header name must not be empty."; | |
25 error_seen_ = true; | |
26 return; | |
27 } | |
28 | |
29 SpdyStringPiece key_name = key; | |
30 if (key[0] == ':') { | |
31 if (regular_header_seen_) { | |
32 error_seen_ = true; | |
33 return; | |
34 } | |
35 key_name.remove_prefix(1); | |
36 } else if (!regular_header_seen_) { | |
37 regular_header_seen_ = true; | |
38 } | |
39 | |
40 if (!HttpUtil::IsValidHeaderName(key_name)) { | |
41 error_seen_ = true; | |
42 return; | |
43 } | |
44 | |
45 // 32 byte overhead according to RFC 7540 Section 6.5.2. | |
46 header_list_size_ += key.size() + value.size() + 32; | |
47 if (header_list_size_ > kMaxHeaderListSize) { | |
48 error_seen_ = true; | |
49 return; | |
50 } | |
51 | |
52 // 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. | |
54 if (value.find("\r\n") != SpdyStringPiece::npos) { | |
55 error_seen_ = true; | |
56 return; | |
57 } | |
58 | |
59 auto iter = headers_.find(key); | |
60 if (iter == headers_.end()) { | |
61 headers_[key] = value; | |
62 } else { | |
63 // This header had multiple values, so it must be reconstructed. | |
64 SpdyStringPiece v = iter->second; | |
65 SpdyString s(v.data(), v.length()); | |
66 if (key == "cookie") { | |
67 // Obeys section 8.1.2.5 in RFC 7540 for cookie reconstruction. | |
68 s.append("; "); | |
69 } else { | |
70 SpdyStringPiece("\0", 1).AppendToString(&s); | |
71 } | |
72 value.AppendToString(&s); | |
73 headers_[key] = s; | |
74 } | |
75 } | |
76 | |
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 | |
87 } // namespace net | |
OLD | NEW |