OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/quic/core/spdy_utils.h" | 5 #include "net/quic/core/spdy_utils.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "net/quic/platform/api/quic_text_utils.h" |
12 #include "base/strings/string_split.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "base/strings/stringprintf.h" | |
15 #include "net/spdy/spdy_flags.h" | 12 #include "net/spdy/spdy_flags.h" |
16 #include "net/spdy/spdy_frame_builder.h" | 13 #include "net/spdy/spdy_frame_builder.h" |
17 #include "net/spdy/spdy_framer.h" | 14 #include "net/spdy/spdy_framer.h" |
18 #include "net/spdy/spdy_protocol.h" | 15 #include "net/spdy/spdy_protocol.h" |
19 #include "url/gurl.h" | 16 #include "url/gurl.h" |
20 | 17 |
21 using base::StringPiece; | 18 using base::StringPiece; |
22 using base::ContainsKey; | 19 using base::ContainsKey; |
23 using std::string; | 20 using std::string; |
24 | 21 |
(...skipping 29 matching lines...) Expand all Loading... |
54 | 51 |
55 // static | 52 // static |
56 bool SpdyUtils::ExtractContentLengthFromHeaders(int64_t* content_length, | 53 bool SpdyUtils::ExtractContentLengthFromHeaders(int64_t* content_length, |
57 SpdyHeaderBlock* headers) { | 54 SpdyHeaderBlock* headers) { |
58 auto it = headers->find("content-length"); | 55 auto it = headers->find("content-length"); |
59 if (it == headers->end()) { | 56 if (it == headers->end()) { |
60 return false; | 57 return false; |
61 } else { | 58 } else { |
62 // Check whether multiple values are consistent. | 59 // Check whether multiple values are consistent. |
63 StringPiece content_length_header = it->second; | 60 StringPiece content_length_header = it->second; |
64 std::vector<string> values = | 61 std::vector<StringPiece> values = |
65 base::SplitString(content_length_header, base::StringPiece("\0", 1), | 62 QuicTextUtils::Split(content_length_header, '\0'); |
66 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 63 for (const StringPiece& value : values) { |
67 for (const string& value : values) { | |
68 int64_t new_value; | 64 int64_t new_value; |
69 if (!base::StringToInt64(value, &new_value) || new_value < 0) { | 65 if (!base::StringToInt64(value, &new_value) || new_value < 0) { |
70 DLOG(ERROR) << "Content length was either unparseable or negative."; | 66 DLOG(ERROR) << "Content length was either unparseable or negative."; |
71 return false; | 67 return false; |
72 } | 68 } |
73 if (*content_length < 0) { | 69 if (*content_length < 0) { |
74 *content_length = new_value; | 70 *content_length = new_value; |
75 continue; | 71 continue; |
76 } | 72 } |
77 if (new_value != *content_length) { | 73 if (new_value != *content_length) { |
(...skipping 25 matching lines...) Expand all Loading... |
103 if (it == trailers->end() || | 99 if (it == trailers->end() || |
104 !base::StringToSizeT(it->second, final_byte_offset)) { | 100 !base::StringToSizeT(it->second, final_byte_offset)) { |
105 DVLOG(1) << "Required key '" << kFinalOffsetHeaderKey << "' not present"; | 101 DVLOG(1) << "Required key '" << kFinalOffsetHeaderKey << "' not present"; |
106 return false; | 102 return false; |
107 } | 103 } |
108 // The final offset header is no longer needed. | 104 // The final offset header is no longer needed. |
109 trailers->erase(it->first); | 105 trailers->erase(it->first); |
110 | 106 |
111 // Trailers must not have empty keys, and must not contain pseudo headers. | 107 // Trailers must not have empty keys, and must not contain pseudo headers. |
112 for (const auto& trailer : *trailers) { | 108 for (const auto& trailer : *trailers) { |
113 base::StringPiece key = trailer.first; | 109 StringPiece key = trailer.first; |
114 base::StringPiece value = trailer.second; | 110 StringPiece value = trailer.second; |
115 if (base::StartsWith(key, ":", base::CompareCase::INSENSITIVE_ASCII)) { | 111 if (QuicTextUtils::StartsWith(key, ":")) { |
116 DVLOG(1) << "Trailers must not contain pseudo-header: '" << key << "','" | 112 DVLOG(1) << "Trailers must not contain pseudo-header: '" << key << "','" |
117 << value << "'."; | 113 << value << "'."; |
118 return false; | 114 return false; |
119 } | 115 } |
120 | 116 |
121 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec. | 117 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec. |
122 } | 118 } |
123 | 119 |
124 DVLOG(1) << "Successfully parsed Trailers."; | 120 DVLOG(1) << "Successfully parsed Trailers."; |
125 return true; | 121 return true; |
126 } | 122 } |
127 | 123 |
128 bool SpdyUtils::CopyAndValidateHeaders(const QuicHeaderList& header_list, | 124 bool SpdyUtils::CopyAndValidateHeaders(const QuicHeaderList& header_list, |
129 int64_t* content_length, | 125 int64_t* content_length, |
130 SpdyHeaderBlock* headers) { | 126 SpdyHeaderBlock* headers) { |
131 for (const auto& p : header_list) { | 127 for (const auto& p : header_list) { |
132 const string& name = p.first; | 128 const string& name = p.first; |
133 if (name.empty()) { | 129 if (name.empty()) { |
134 DVLOG(1) << "Header name must not be empty."; | 130 DVLOG(1) << "Header name must not be empty."; |
135 return false; | 131 return false; |
136 } | 132 } |
137 | 133 |
138 if (std::any_of(name.begin(), name.end(), base::IsAsciiUpper<char>)) { | 134 if (QuicTextUtils::ContainsUpperCase(name)) { |
139 DVLOG(1) << "Malformed header: Header name " << name | 135 DVLOG(1) << "Malformed header: Header name " << name |
140 << " contains upper-case characters."; | 136 << " contains upper-case characters."; |
141 return false; | 137 return false; |
142 } | 138 } |
143 | 139 |
144 headers->AppendValueOrAddHeader(name, p.second); | 140 headers->AppendValueOrAddHeader(name, p.second); |
145 } | 141 } |
146 | 142 |
147 if (ContainsKey(*headers, "content-length") && | 143 if (ContainsKey(*headers, "content-length") && |
148 !ExtractContentLengthFromHeaders(content_length, headers)) { | 144 !ExtractContentLengthFromHeaders(content_length, headers)) { |
(...skipping 20 matching lines...) Expand all Loading... |
169 found_final_byte_offset = true; | 165 found_final_byte_offset = true; |
170 continue; | 166 continue; |
171 } | 167 } |
172 | 168 |
173 if (name.empty() || name[0] == ':') { | 169 if (name.empty() || name[0] == ':') { |
174 DVLOG(1) << "Trailers must not be empty, and must not contain pseudo-" | 170 DVLOG(1) << "Trailers must not be empty, and must not contain pseudo-" |
175 << "headers. Found: '" << name << "'"; | 171 << "headers. Found: '" << name << "'"; |
176 return false; | 172 return false; |
177 } | 173 } |
178 | 174 |
179 if (std::any_of(name.begin(), name.end(), base::IsAsciiUpper<char>)) { | 175 if (QuicTextUtils::ContainsUpperCase(name)) { |
180 DVLOG(1) << "Malformed header: Header name " << name | 176 DVLOG(1) << "Malformed header: Header name " << name |
181 << " contains upper-case characters."; | 177 << " contains upper-case characters."; |
182 return false; | 178 return false; |
183 } | 179 } |
184 | 180 |
185 if (trailers->find(name) != trailers->end()) { | 181 if (trailers->find(name) != trailers->end()) { |
186 DVLOG(1) << "Duplicate header '" << name << "' found in trailers."; | 182 DVLOG(1) << "Duplicate header '" << name << "' found in trailers."; |
187 return false; | 183 return false; |
188 } | 184 } |
189 | 185 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 (*headers)[":authority"] = url.substr(start); | 247 (*headers)[":authority"] = url.substr(start); |
252 (*headers)[":path"] = "/"; | 248 (*headers)[":path"] = "/"; |
253 return true; | 249 return true; |
254 } | 250 } |
255 (*headers)[":authority"] = url.substr(start, pos - start); | 251 (*headers)[":authority"] = url.substr(start, pos - start); |
256 (*headers)[":path"] = url.substr(pos); | 252 (*headers)[":path"] = url.substr(pos); |
257 return true; | 253 return true; |
258 } | 254 } |
259 | 255 |
260 } // namespace net | 256 } // namespace net |
OLD | NEW |