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