| 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/spdy_utils.h" | 5 #include "net/quic/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 "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "net/spdy/spdy_frame_builder.h" | 14 #include "net/spdy/spdy_frame_builder.h" |
| 15 #include "net/spdy/spdy_framer.h" | 15 #include "net/spdy/spdy_framer.h" |
| 16 #include "net/spdy/spdy_protocol.h" | 16 #include "net/spdy/spdy_protocol.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 using base::StringPiece; |
| 19 using std::string; | 20 using std::string; |
| 20 using std::vector; | 21 using std::vector; |
| 21 | 22 |
| 22 namespace net { | 23 namespace net { |
| 23 | 24 |
| 24 // static | 25 // static |
| 25 string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { | 26 string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { |
| 26 SpdyMajorVersion spdy_version = HTTP2; | 27 SpdyMajorVersion spdy_version = HTTP2; |
| 27 | 28 |
| 28 size_t length = SpdyFramer::GetSerializedLength(spdy_version, &headers); | 29 size_t length = SpdyFramer::GetSerializedLength(spdy_version, &headers); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 return false; | 102 return false; |
| 102 } | 103 } |
| 103 | 104 |
| 104 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec. | 105 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec. |
| 105 } | 106 } |
| 106 | 107 |
| 107 DVLOG(1) << "Successfully parsed Trailers."; | 108 DVLOG(1) << "Successfully parsed Trailers."; |
| 108 return true; | 109 return true; |
| 109 } | 110 } |
| 110 | 111 |
| 112 bool SpdyUtils::CopyAndValidateHeaders(const QuicHeaderList& header_list, |
| 113 int64_t* content_length, |
| 114 SpdyHeaderBlock* headers) { |
| 115 for (const auto& p : header_list) { |
| 116 const string& name = p.first; |
| 117 if (name.empty()) { |
| 118 DVLOG(1) << "Header name must not be empty."; |
| 119 return false; |
| 120 } |
| 121 |
| 122 if (std::any_of(name.begin(), name.end(), base::IsAsciiUpper<char>)) { |
| 123 DLOG(ERROR) << "Malformed header: Header name " << name |
| 124 << " contains upper-case characters."; |
| 125 return false; |
| 126 } |
| 127 |
| 128 if (headers->find(name) != headers->end()) { |
| 129 DLOG(ERROR) << "Duplicate header '" << name << "' found."; |
| 130 return false; |
| 131 } |
| 132 |
| 133 (*headers)[name] = p.second; |
| 134 } |
| 135 |
| 136 if (ContainsKey(*headers, "content-length")) { |
| 137 // Check whether multiple values are consistent. |
| 138 StringPiece content_length_header = (*headers)["content-length"]; |
| 139 vector<string> values = |
| 140 base::SplitString(content_length_header, base::StringPiece("\0", 1), |
| 141 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 142 for (const string& value : values) { |
| 143 int new_value; |
| 144 if (!base::StringToInt(value, &new_value) || new_value < 0) { |
| 145 DLOG(ERROR) << "Content length was either unparseable or negative."; |
| 146 return false; |
| 147 } |
| 148 if (*content_length < 0) { |
| 149 *content_length = new_value; |
| 150 continue; |
| 151 } |
| 152 if (new_value != *content_length) { |
| 153 DLOG(ERROR) << "Parsed content length " << new_value << " is " |
| 154 << "inconsistent with previously detected content length " |
| 155 << *content_length; |
| 156 return false; |
| 157 } |
| 158 } |
| 159 } |
| 160 |
| 161 DVLOG(1) << "Successfully parsed headers: " << headers->DebugString(); |
| 162 return true; |
| 163 } |
| 164 |
| 111 bool SpdyUtils::CopyAndValidateTrailers(const QuicHeaderList& header_list, | 165 bool SpdyUtils::CopyAndValidateTrailers(const QuicHeaderList& header_list, |
| 112 size_t* final_byte_offset, | 166 size_t* final_byte_offset, |
| 113 SpdyHeaderBlock* trailers) { | 167 SpdyHeaderBlock* trailers) { |
| 168 bool found_final_byte_offset = false; |
| 114 for (const auto& p : header_list) { | 169 for (const auto& p : header_list) { |
| 115 const string& name = p.first; | 170 const string& name = p.first; |
| 171 |
| 172 int new_value; |
| 173 // Pull out the final offset pseudo header which indicates the number of |
| 174 // response body bytes expected. |
| 175 if (!found_final_byte_offset && name == kFinalOffsetHeaderKey && |
| 176 base::StringToInt(p.second, &new_value)) { |
| 177 *final_byte_offset = new_value; |
| 178 found_final_byte_offset = true; |
| 179 continue; |
| 180 } |
| 181 |
| 116 if (name.empty() || name[0] == ':') { | 182 if (name.empty() || name[0] == ':') { |
| 117 DVLOG(1) << "Trailers must not be empty, and must not contain pseudo-" | 183 DVLOG(1) << "Trailers must not be empty, and must not contain pseudo-" |
| 118 << "headers. Found: '" << name << "'"; | 184 << "headers. Found: '" << name << "'"; |
| 119 return false; | 185 return false; |
| 120 } | 186 } |
| 121 | 187 |
| 122 if (std::any_of(name.begin(), name.end(), base::IsAsciiUpper<char>)) { | 188 if (std::any_of(name.begin(), name.end(), base::IsAsciiUpper<char>)) { |
| 123 DVLOG(1) << "Malformed header: Header name " << name | 189 DVLOG(1) << "Malformed header: Header name " << name |
| 124 << " contains upper-case characters."; | 190 << " contains upper-case characters."; |
| 125 return false; | 191 return false; |
| 126 } | 192 } |
| 127 | 193 |
| 128 if (trailers->find(name) != trailers->end()) { | 194 if (trailers->find(name) != trailers->end()) { |
| 129 DVLOG(1) << "Duplicate header '" << name << "' found in trailers."; | 195 DVLOG(1) << "Duplicate header '" << name << "' found in trailers."; |
| 130 return false; | 196 return false; |
| 131 } | 197 } |
| 132 | 198 |
| 133 (*trailers)[name] = p.second; | 199 (*trailers)[name] = p.second; |
| 134 } | 200 } |
| 135 | 201 |
| 136 if (trailers->empty()) { | 202 if (!found_final_byte_offset) { |
| 137 DVLOG(1) << "Request Trailers are invalid."; | |
| 138 return false; // Trailers were invalid. | |
| 139 } | |
| 140 | |
| 141 // Pull out the final offset pseudo header which indicates the number of | |
| 142 // response body bytes expected. | |
| 143 auto it = trailers->find(kFinalOffsetHeaderKey); | |
| 144 if (it == trailers->end() || !StringToSizeT(it->second, final_byte_offset)) { | |
| 145 DVLOG(1) << "Required key '" << kFinalOffsetHeaderKey << "' not present"; | 203 DVLOG(1) << "Required key '" << kFinalOffsetHeaderKey << "' not present"; |
| 146 return false; | 204 return false; |
| 147 } | 205 } |
| 148 // The final offset header is no longer needed. | |
| 149 trailers->erase(it->first); | |
| 150 | 206 |
| 151 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec. | 207 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec. |
| 152 | 208 |
| 153 DVLOG(1) << "Successfully parsed Trailers: " << trailers->DebugString(); | 209 DVLOG(1) << "Successfully parsed Trailers: " << trailers->DebugString(); |
| 154 return true; | 210 return true; |
| 155 } | 211 } |
| 156 | 212 |
| 157 // static | 213 // static |
| 158 string SpdyUtils::GetUrlFromHeaderBlock(const SpdyHeaderBlock& headers) { | 214 string SpdyUtils::GetUrlFromHeaderBlock(const SpdyHeaderBlock& headers) { |
| 159 SpdyHeaderBlock::const_iterator it = headers.find(":scheme"); | 215 SpdyHeaderBlock::const_iterator it = headers.find(":scheme"); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 183 return GURL(GetUrlFromHeaderBlock(headers)).host(); | 239 return GURL(GetUrlFromHeaderBlock(headers)).host(); |
| 184 } | 240 } |
| 185 | 241 |
| 186 // static | 242 // static |
| 187 bool SpdyUtils::UrlIsValid(const SpdyHeaderBlock& headers) { | 243 bool SpdyUtils::UrlIsValid(const SpdyHeaderBlock& headers) { |
| 188 string url(GetUrlFromHeaderBlock(headers)); | 244 string url(GetUrlFromHeaderBlock(headers)); |
| 189 return url != "" && GURL(url).is_valid(); | 245 return url != "" && GURL(url).is_valid(); |
| 190 } | 246 } |
| 191 | 247 |
| 192 } // namespace net | 248 } // namespace net |
| OLD | NEW |