| 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   // TODO(svaldez): Use real URL from the EmbeddedTestServer. | 
|  | 41   return GURL("http://localhost" + relative_url); | 
|  | 42 } | 
|  | 43 | 
| 38 HttpRequestParser::HttpRequestParser() | 44 HttpRequestParser::HttpRequestParser() | 
| 39     : http_request_(new HttpRequest()), | 45     : http_request_(new HttpRequest()), | 
| 40       buffer_position_(0), | 46       buffer_position_(0), | 
| 41       state_(STATE_HEADERS), | 47       state_(STATE_HEADERS), | 
| 42       declared_content_length_(0) { | 48       declared_content_length_(0) { | 
| 43 } | 49 } | 
| 44 | 50 | 
| 45 HttpRequestParser::~HttpRequestParser() { | 51 HttpRequestParser::~HttpRequestParser() { | 
| 46 } | 52 } | 
| 47 | 53 | 
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 91     std::vector<std::string> header_line_tokens = base::SplitString( | 97     std::vector<std::string> header_line_tokens = base::SplitString( | 
| 92         header_line, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 98         header_line, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 
| 93     DCHECK_EQ(3u, header_line_tokens.size()); | 99     DCHECK_EQ(3u, header_line_tokens.size()); | 
| 94     // Method. | 100     // Method. | 
| 95     http_request_->method_string = header_line_tokens[0]; | 101     http_request_->method_string = header_line_tokens[0]; | 
| 96     http_request_->method = | 102     http_request_->method = | 
| 97         GetMethodType(base::ToLowerASCII(header_line_tokens[0])); | 103         GetMethodType(base::ToLowerASCII(header_line_tokens[0])); | 
| 98     // Address. | 104     // Address. | 
| 99     // Don't build an absolute URL as the parser does not know (should not | 105     // Don't build an absolute URL as the parser does not know (should not | 
| 100     // know) anything about the server address. | 106     // know) anything about the server address. | 
| 101     http_request_->relative_url = header_line_tokens[1]; | 107     GURL 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 | 
| 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."; | 
|  | 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 | 
|---|