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 "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 "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "net/spdy/spdy_flags.h" | 15 #include "net/spdy/spdy_flags.h" |
16 #include "net/spdy/spdy_frame_builder.h" | 16 #include "net/spdy/spdy_frame_builder.h" |
17 #include "net/spdy/spdy_framer.h" | 17 #include "net/spdy/spdy_framer.h" |
18 #include "net/spdy/spdy_protocol.h" | 18 #include "net/spdy/spdy_protocol.h" |
19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
20 | 20 |
21 using base::StringPiece; | 21 using base::StringPiece; |
| 22 using base::ContainsKey; |
22 using std::string; | 23 using std::string; |
23 using std::vector; | 24 using std::vector; |
24 | 25 |
25 namespace net { | 26 namespace net { |
26 | 27 |
27 // static | 28 // static |
28 string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { | 29 string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { |
29 SpdyMajorVersion spdy_version = HTTP2; | 30 SpdyMajorVersion spdy_version = HTTP2; |
30 | 31 |
31 size_t length = SpdyFramer::GetSerializedLength(spdy_version, &headers); | 32 size_t length = SpdyFramer::GetSerializedLength(spdy_version, &headers); |
32 SpdyFrameBuilder builder(length, spdy_version); | 33 SpdyFrameBuilder builder(length, spdy_version); |
33 SpdyFramer framer(spdy_version); | 34 SpdyFramer framer(spdy_version); |
34 framer.SerializeHeaderBlockWithoutCompression(&builder, headers); | 35 framer.SerializeHeaderBlockWithoutCompression(&builder, headers); |
35 SpdySerializedFrame block(builder.take()); | 36 SpdySerializedFrame block(builder.take()); |
36 return string(block.data(), length); | 37 return string(block.data(), length); |
37 } | 38 } |
38 | 39 |
39 // static | 40 // static |
40 bool SpdyUtils::ParseHeaders(const char* data, | 41 bool SpdyUtils::ParseHeaders(const char* data, |
41 uint32_t data_len, | 42 uint32_t data_len, |
42 int64_t* content_length, | 43 int64_t* content_length, |
43 SpdyHeaderBlock* headers) { | 44 SpdyHeaderBlock* headers) { |
44 SpdyFramer framer(HTTP2); | 45 SpdyFramer framer(HTTP2); |
45 if (!framer.ParseHeaderBlockInBuffer(data, data_len, headers) || | 46 if (!framer.ParseHeaderBlockInBuffer(data, data_len, headers) || |
46 headers->empty()) { | 47 headers->empty()) { |
47 return false; // Headers were invalid. | 48 return false; // Headers were invalid. |
48 } | 49 } |
49 | 50 |
| 51 if (!ContainsKey(*headers, "content-length")) { |
| 52 return true; |
| 53 } |
| 54 |
50 return ExtractContentLengthFromHeaders(content_length, headers); | 55 return ExtractContentLengthFromHeaders(content_length, headers); |
51 } | 56 } |
52 | 57 |
53 // static | 58 // static |
54 bool SpdyUtils::ExtractContentLengthFromHeaders(int64_t* content_length, | 59 bool SpdyUtils::ExtractContentLengthFromHeaders(int64_t* content_length, |
55 SpdyHeaderBlock* headers) { | 60 SpdyHeaderBlock* headers) { |
56 if (base::ContainsKey(*headers, "content-length")) { | 61 if (!ContainsKey(*headers, "content-length")) { |
57 // Check whether multiple values are consistent. | 62 return false; |
58 base::StringPiece content_length_header = (*headers)["content-length"]; | 63 } |
59 vector<string> values = | 64 // Check whether multiple values are consistent. |
60 base::SplitString(content_length_header, base::StringPiece("\0", 1), | 65 base::StringPiece content_length_header = (*headers)["content-length"]; |
61 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 66 vector<string> values = |
62 for (const string& value : values) { | 67 base::SplitString(content_length_header, base::StringPiece("\0", 1), |
63 int64_t new_value; | 68 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
64 if (!base::StringToInt64(value, &new_value) || new_value < 0) { | 69 for (const string& value : values) { |
65 DLOG(ERROR) << "Content length was either unparseable or negative."; | 70 int64_t new_value; |
66 return false; | 71 if (!base::StringToInt64(value, &new_value) || new_value < 0) { |
67 } | 72 DLOG(ERROR) << "Content length was either unparseable or negative."; |
68 if (*content_length < 0) { | 73 return false; |
69 *content_length = new_value; | 74 } |
70 continue; | 75 if (*content_length < 0) { |
71 } | 76 *content_length = new_value; |
72 if (new_value != *content_length) { | 77 continue; |
73 DLOG(ERROR) << "Parsed content length " << new_value << " is " | 78 } |
74 << "inconsistent with previously detected content length " | 79 if (new_value != *content_length) { |
75 << *content_length; | 80 DLOG(ERROR) << "Parsed content length " << new_value << " is " |
76 return false; | 81 << "inconsistent with previously detected content length " |
77 } | 82 << *content_length; |
| 83 return false; |
78 } | 84 } |
79 } | 85 } |
80 return true; | 86 return true; |
81 } | 87 } |
82 | 88 |
83 // static | 89 // static |
84 bool SpdyUtils::ParseTrailers(const char* data, | 90 bool SpdyUtils::ParseTrailers(const char* data, |
85 uint32_t data_len, | 91 uint32_t data_len, |
86 size_t* final_byte_offset, | 92 size_t* final_byte_offset, |
87 SpdyHeaderBlock* trailers) { | 93 SpdyHeaderBlock* trailers) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 138 |
133 if (std::any_of(name.begin(), name.end(), base::IsAsciiUpper<char>)) { | 139 if (std::any_of(name.begin(), name.end(), base::IsAsciiUpper<char>)) { |
134 DVLOG(1) << "Malformed header: Header name " << name | 140 DVLOG(1) << "Malformed header: Header name " << name |
135 << " contains upper-case characters."; | 141 << " contains upper-case characters."; |
136 return false; | 142 return false; |
137 } | 143 } |
138 | 144 |
139 headers->AppendValueOrAddHeader(name, p.second); | 145 headers->AppendValueOrAddHeader(name, p.second); |
140 } | 146 } |
141 | 147 |
142 if (!ExtractContentLengthFromHeaders(content_length, headers)) { | 148 if (ContainsKey(*headers, "content-length") && |
| 149 !ExtractContentLengthFromHeaders(content_length, headers)) { |
143 return false; | 150 return false; |
144 } | 151 } |
145 | 152 |
146 DVLOG(1) << "Successfully parsed headers: " << headers->DebugString(); | 153 DVLOG(1) << "Successfully parsed headers: " << headers->DebugString(); |
147 return true; | 154 return true; |
148 } | 155 } |
149 | 156 |
150 bool SpdyUtils::CopyAndValidateTrailers(const QuicHeaderList& header_list, | 157 bool SpdyUtils::CopyAndValidateTrailers(const QuicHeaderList& header_list, |
151 size_t* final_byte_offset, | 158 size_t* final_byte_offset, |
152 SpdyHeaderBlock* trailers) { | 159 SpdyHeaderBlock* trailers) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 pos = url.find("/", start); | 250 pos = url.find("/", start); |
244 if (pos == string::npos) { | 251 if (pos == string::npos) { |
245 return false; | 252 return false; |
246 } | 253 } |
247 (*headers)[":authority"] = url.substr(start, pos - start); | 254 (*headers)[":authority"] = url.substr(start, pos - start); |
248 (*headers)[":path"] = url.substr(pos); | 255 (*headers)[":path"] = url.substr(pos); |
249 return true; | 256 return true; |
250 } | 257 } |
251 | 258 |
252 } // namespace net | 259 } // namespace net |
OLD | NEW |