Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/test/embedded_test_server/http_request.h" | 5 #include "net/test/embedded_test_server/http_request.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_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/http/http_chunked_decoder.h" | 13 #include "net/http/http_chunked_decoder.h" |
| 14 #include "url/gurl.h" | |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 namespace test_server { | 17 namespace test_server { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. | 21 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. |
| 21 | 22 |
| 22 // Helper function used to trim tokens in http request headers. | 23 // Helper function used to trim tokens in http request headers. |
| 23 std::string Trim(const std::string& value) { | 24 std::string Trim(const std::string& value) { |
| 24 std::string result; | 25 std::string result; |
| 25 base::TrimString(value, " \t", &result); | 26 base::TrimString(value, " \t", &result); |
| 26 return result; | 27 return result; |
| 27 } | 28 } |
| 28 | 29 |
| 29 } // namespace | 30 } // namespace |
| 30 | 31 |
| 31 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN), | 32 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN), |
| 32 has_content(false) { | 33 has_content(false) { |
| 33 } | 34 } |
| 34 | 35 |
| 35 HttpRequest::~HttpRequest() { | 36 HttpRequest::~HttpRequest() { |
| 36 } | 37 } |
| 37 | 38 |
| 39 GURL HttpRequest::ToURL() const { | |
| 40 return GURL("http://localhost" + relative_url); | |
|
mmenke
2015/10/19 18:07:40
Could you add a TODO to consider making this retur
svaldez
2015/10/19 21:56:15
Done.
| |
| 41 } | |
| 42 | |
| 38 HttpRequestParser::HttpRequestParser() | 43 HttpRequestParser::HttpRequestParser() |
| 39 : http_request_(new HttpRequest()), | 44 : http_request_(new HttpRequest()), |
| 40 buffer_position_(0), | 45 buffer_position_(0), |
| 41 state_(STATE_HEADERS), | 46 state_(STATE_HEADERS), |
| 42 declared_content_length_(0) { | 47 declared_content_length_(0) { |
| 43 } | 48 } |
| 44 | 49 |
| 45 HttpRequestParser::~HttpRequestParser() { | 50 HttpRequestParser::~HttpRequestParser() { |
| 46 } | 51 } |
| 47 | 52 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 std::vector<std::string> header_line_tokens = base::SplitString( | 96 std::vector<std::string> header_line_tokens = base::SplitString( |
| 92 header_line, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 97 header_line, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 93 DCHECK_EQ(3u, header_line_tokens.size()); | 98 DCHECK_EQ(3u, header_line_tokens.size()); |
| 94 // Method. | 99 // Method. |
| 95 http_request_->method_string = header_line_tokens[0]; | 100 http_request_->method_string = header_line_tokens[0]; |
| 96 http_request_->method = | 101 http_request_->method = |
| 97 GetMethodType(base::ToLowerASCII(header_line_tokens[0])); | 102 GetMethodType(base::ToLowerASCII(header_line_tokens[0])); |
| 98 // Address. | 103 // Address. |
| 99 // Don't build an absolute URL as the parser does not know (should not | 104 // Don't build an absolute URL as the parser does not know (should not |
| 100 // know) anything about the server address. | 105 // know) anything about the server address. |
| 101 http_request_->relative_url = header_line_tokens[1]; | 106 GURL url(header_line_tokens[1]); |
| 107 if (url.is_valid()) { | |
|
mmenke
2015/10/19 18:07:40
If we have a full URL, surely HttpRequest::ToURL s
svaldez
2015/10/19 21:56:15
There are cases where the "full URL" is actually n
mmenke
2015/10/21 18:45:22
Such as?
| |
| 108 http_request_->relative_url = url.path(); | |
| 109 } else { | |
| 110 if (header_line_tokens[1][0] == '/') | |
|
mmenke
2015/10/19 18:07:40
else if {
} else {
}
svaldez
2015/10/19 21:56:15
Done.
| |
| 111 http_request_->relative_url = header_line_tokens[1]; | |
| 112 else | |
| 113 http_request_->relative_url = "/" + header_line_tokens[1]; | |
| 114 } | |
| 115 | |
| 102 // Protocol. | 116 // Protocol. |
| 103 const std::string protocol = base::ToLowerASCII(header_line_tokens[2]); | 117 const std::string protocol = base::ToLowerASCII(header_line_tokens[2]); |
| 104 CHECK(protocol == "http/1.0" || protocol == "http/1.1") << | 118 CHECK(protocol == "http/1.0" || protocol == "http/1.1") << |
| 105 "Protocol not supported: " << protocol; | 119 "Protocol not supported: " << protocol; |
| 106 } | 120 } |
| 107 | 121 |
| 108 // Parse further headers. | 122 // Parse further headers. |
| 109 { | 123 { |
| 110 std::string header_name; | 124 std::string header_name; |
| 111 while (true) { | 125 while (true) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 132 } | 146 } |
| 133 } | 147 } |
| 134 | 148 |
| 135 // Headers done. Is any content data attached to the request? | 149 // Headers done. Is any content data attached to the request? |
| 136 declared_content_length_ = 0; | 150 declared_content_length_ = 0; |
| 137 if (http_request_->headers.count("Content-Length") > 0) { | 151 if (http_request_->headers.count("Content-Length") > 0) { |
| 138 http_request_->has_content = true; | 152 http_request_->has_content = true; |
| 139 const bool success = base::StringToSizeT( | 153 const bool success = base::StringToSizeT( |
| 140 http_request_->headers["Content-Length"], | 154 http_request_->headers["Content-Length"], |
| 141 &declared_content_length_); | 155 &declared_content_length_); |
| 142 DCHECK(success) << "Malformed Content-Length header's value."; | 156 if (!success) { |
| 157 declared_content_length_ = 0; | |
| 158 LOG(WARNING) << "Malformed Content-Length header's value."; | |
|
mmenke
2015/10/19 18:07:40
We actually have tests that send invalid content-l
svaldez
2015/10/19 21:56:15
Some tests send Content-Length even if there isn't
| |
| 159 } | |
| 143 } else if (http_request_->headers.count("Transfer-Encoding") > 0) { | 160 } else if (http_request_->headers.count("Transfer-Encoding") > 0) { |
| 144 if (http_request_->headers["Transfer-Encoding"] == "chunked") { | 161 if (http_request_->headers["Transfer-Encoding"] == "chunked") { |
| 145 http_request_->has_content = true; | 162 http_request_->has_content = true; |
| 146 chunked_decoder_.reset(new HttpChunkedDecoder()); | 163 chunked_decoder_.reset(new HttpChunkedDecoder()); |
| 147 state_ = STATE_CONTENT; | 164 state_ = STATE_CONTENT; |
| 148 return WAITING; | 165 return WAITING; |
| 149 } | 166 } |
| 150 } | 167 } |
| 151 if (declared_content_length_ == 0) { | 168 if (declared_content_length_ == 0) { |
| 152 // No content data, so parsing is finished. | 169 // No content data, so parsing is finished. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 } else if (token == "head") { | 234 } else if (token == "head") { |
| 218 return METHOD_HEAD; | 235 return METHOD_HEAD; |
| 219 } else if (token == "post") { | 236 } else if (token == "post") { |
| 220 return METHOD_POST; | 237 return METHOD_POST; |
| 221 } else if (token == "put") { | 238 } else if (token == "put") { |
| 222 return METHOD_PUT; | 239 return METHOD_PUT; |
| 223 } else if (token == "delete") { | 240 } else if (token == "delete") { |
| 224 return METHOD_DELETE; | 241 return METHOD_DELETE; |
| 225 } else if (token == "patch") { | 242 } else if (token == "patch") { |
| 226 return METHOD_PATCH; | 243 return METHOD_PATCH; |
| 244 } else if (token == "connect") { | |
| 245 return METHOD_CONNECT; | |
| 227 } | 246 } |
| 228 NOTREACHED() << "Method not implemented: " << token; | 247 LOG(WARNING) << "Method not implemented: " << token; |
| 229 return METHOD_UNKNOWN; | 248 return METHOD_GET; |
| 230 } | 249 } |
| 231 | 250 |
| 232 } // namespace test_server | 251 } // namespace test_server |
| 233 } // namespace net | 252 } // namespace net |
| OLD | NEW |