Chromium Code Reviews| 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 <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 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 "net/spdy/spdy_frame_builder.h" | 13 #include "net/spdy/spdy_frame_builder.h" |
| 14 #include "net/spdy/spdy_framer.h" | 14 #include "net/spdy/spdy_framer.h" |
| 15 #include "net/spdy/spdy_protocol.h" | 15 #include "net/spdy/spdy_protocol.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 using std::string; | 18 using std::string; |
| 19 using std::vector; | 19 using std::vector; |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 | 22 |
| 23 bool ascii_isupper(unsigned char c) { | |
|
ramant (doing other things)
2016/04/11 18:11:57
nit: consider adding this to strings/string_util.h
| |
| 24 return c >= 'A' && c <= 'Z'; | |
| 25 } | |
| 26 | |
| 23 // static | 27 // static |
| 24 string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { | 28 string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { |
| 25 SpdyMajorVersion spdy_version = HTTP2; | 29 SpdyMajorVersion spdy_version = HTTP2; |
| 26 | 30 |
| 27 size_t length = SpdyFramer::GetSerializedLength(spdy_version, &headers); | 31 size_t length = SpdyFramer::GetSerializedLength(spdy_version, &headers); |
| 28 SpdyFrameBuilder builder(length, spdy_version); | 32 SpdyFrameBuilder builder(length, spdy_version); |
| 29 SpdyFramer framer(spdy_version); | 33 SpdyFramer framer(spdy_version); |
| 30 framer.SerializeHeaderBlockWithoutCompression(&builder, headers); | 34 framer.SerializeHeaderBlockWithoutCompression(&builder, headers); |
| 31 SpdySerializedFrame block(builder.take()); | 35 SpdySerializedFrame block(builder.take()); |
| 32 return string(block.data(), length); | 36 return string(block.data(), length); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 return false; | 104 return false; |
| 101 } | 105 } |
| 102 | 106 |
| 103 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec. | 107 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec. |
| 104 } | 108 } |
| 105 | 109 |
| 106 DVLOG(1) << "Successfully parsed Trailers."; | 110 DVLOG(1) << "Successfully parsed Trailers."; |
| 107 return true; | 111 return true; |
| 108 } | 112 } |
| 109 | 113 |
| 114 bool SpdyUtils::CopyAndValidateTrailers(const QuicHeaderList& header_list, | |
| 115 size_t* final_byte_offset, | |
| 116 SpdyHeaderBlock* trailers) { | |
| 117 for (const auto& p : header_list) { | |
| 118 const string& name = p.first; | |
| 119 if (name.empty() || name[0] == ':') { | |
| 120 DVLOG(1) << "Trailers must not be empty, and must not contain pseudo-" | |
| 121 << "headers. Found: '" << name << "'"; | |
| 122 return false; | |
| 123 } | |
| 124 | |
| 125 if (std::any_of(name.begin(), name.end(), ascii_isupper)) { | |
| 126 DVLOG(1) << "Malformed header: Header name " << name | |
| 127 << " contains upper-case characters."; | |
|
ramant (doing other things)
2016/04/11 18:11:57
nit: indent line# 127
Zhongyi Shi
2016/04/11 18:27:31
Already fixed in final.
| |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 if (trailers->find(name) != trailers->end()) { | |
| 132 DVLOG(1) << "Duplicate header '" << name << "' found in trailers."; | |
| 133 return false; | |
| 134 } | |
| 135 | |
| 136 (*trailers)[name] = p.second; | |
| 137 } | |
| 138 | |
| 139 if (trailers->empty()) { | |
| 140 DVLOG(1) << "Request Trailers are invalid."; | |
| 141 return false; // Trailers were invalid. | |
| 142 } | |
| 143 | |
| 144 // Pull out the final offset pseudo header which indicates the number of | |
| 145 // response body bytes expected. | |
| 146 auto it = trailers->find(kFinalOffsetHeaderKey); | |
| 147 if (it == trailers->end() || !StringToSizeT(it->second, final_byte_offset)) { | |
| 148 DVLOG(1) << "Required key '" << kFinalOffsetHeaderKey << "' not present"; | |
| 149 return false; | |
| 150 } | |
| 151 // The final offset header is no longer needed. | |
| 152 trailers->erase(it->first); | |
| 153 | |
| 154 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec. | |
| 155 | |
| 156 DVLOG(1) << "Successfully parsed Trailers: " << trailers->DebugString(); | |
| 157 return true; | |
| 158 } | |
| 159 | |
| 110 // static | 160 // static |
| 111 string SpdyUtils::GetUrlFromHeaderBlock(const SpdyHeaderBlock& headers) { | 161 string SpdyUtils::GetUrlFromHeaderBlock(const SpdyHeaderBlock& headers) { |
| 112 SpdyHeaderBlock::const_iterator it = headers.find(":scheme"); | 162 SpdyHeaderBlock::const_iterator it = headers.find(":scheme"); |
| 113 if (it == headers.end()) { | 163 if (it == headers.end()) { |
| 114 return ""; | 164 return ""; |
| 115 } | 165 } |
| 116 std::string url = it->second.as_string(); | 166 std::string url = it->second.as_string(); |
| 117 | 167 |
| 118 url.append("://"); | 168 url.append("://"); |
| 119 | 169 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 136 return GURL(GetUrlFromHeaderBlock(headers)).host(); | 186 return GURL(GetUrlFromHeaderBlock(headers)).host(); |
| 137 } | 187 } |
| 138 | 188 |
| 139 // static | 189 // static |
| 140 bool SpdyUtils::UrlIsValid(const SpdyHeaderBlock& headers) { | 190 bool SpdyUtils::UrlIsValid(const SpdyHeaderBlock& headers) { |
| 141 string url(GetUrlFromHeaderBlock(headers)); | 191 string url(GetUrlFromHeaderBlock(headers)); |
| 142 return url != "" && GURL(url).is_valid(); | 192 return url != "" && GURL(url).is_valid(); |
| 143 } | 193 } |
| 144 | 194 |
| 145 } // namespace net | 195 } // namespace net |
| OLD | NEW |