| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/google_apis/test_server/http_request.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/strings/string_split.h" | |
| 13 | |
| 14 namespace google_apis { | |
| 15 namespace test_server { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. | |
| 20 | |
| 21 // Helper function used to trim tokens in http request headers. | |
| 22 std::string Trim(const std::string& value) { | |
| 23 std::string result; | |
| 24 TrimString(value, " \t", &result); | |
| 25 return result; | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 HttpRequest::HttpRequest() : method(METHOD_UNKNOWN), | |
| 31 has_content(false) { | |
| 32 } | |
| 33 | |
| 34 HttpRequest::~HttpRequest() { | |
| 35 } | |
| 36 | |
| 37 HttpRequestParser::HttpRequestParser() | |
| 38 : http_request_(new HttpRequest()), | |
| 39 buffer_position_(0), | |
| 40 state_(STATE_HEADERS), | |
| 41 declared_content_length_(0) { | |
| 42 } | |
| 43 | |
| 44 HttpRequestParser::~HttpRequestParser() { | |
| 45 } | |
| 46 | |
| 47 void HttpRequestParser::ProcessChunk(const base::StringPiece& data) { | |
| 48 data.AppendToString(&buffer_); | |
| 49 DCHECK_LE(buffer_.size() + data.size(), kRequestSizeLimit) << | |
| 50 "The HTTP request is too large."; | |
| 51 } | |
| 52 | |
| 53 std::string HttpRequestParser::ShiftLine() { | |
| 54 size_t eoln_position = buffer_.find("\r\n", buffer_position_); | |
| 55 DCHECK_NE(std::string::npos, eoln_position); | |
| 56 const int line_length = eoln_position - buffer_position_; | |
| 57 std::string result = buffer_.substr(buffer_position_, line_length); | |
| 58 buffer_position_ += line_length + 2; | |
| 59 return result; | |
| 60 } | |
| 61 | |
| 62 HttpRequestParser::ParseResult HttpRequestParser::ParseRequest() { | |
| 63 DCHECK_NE(STATE_ACCEPTED, state_); | |
| 64 // Parse the request from beginning. However, entire request may not be | |
| 65 // available in the buffer. | |
| 66 if (state_ == STATE_HEADERS) { | |
| 67 if (ParseHeaders() == ACCEPTED) | |
| 68 return ACCEPTED; | |
| 69 } | |
| 70 // This should not be 'else if' of the previous block, as |state_| can be | |
| 71 // changed in ParseHeaders(). | |
| 72 if (state_ == STATE_CONTENT) { | |
| 73 if (ParseContent() == ACCEPTED) | |
| 74 return ACCEPTED; | |
| 75 } | |
| 76 return WAITING; | |
| 77 } | |
| 78 | |
| 79 HttpRequestParser::ParseResult HttpRequestParser::ParseHeaders() { | |
| 80 // Check if the all request headers are available. | |
| 81 if (buffer_.find("\r\n\r\n", buffer_position_) == std::string::npos) | |
| 82 return WAITING; | |
| 83 | |
| 84 // Parse request's the first header line. | |
| 85 // Request main main header, eg. GET /foobar.html HTTP/1.1 | |
| 86 { | |
| 87 const std::string header_line = ShiftLine(); | |
| 88 std::vector<std::string> header_line_tokens; | |
| 89 base::SplitString(header_line, ' ', &header_line_tokens); | |
| 90 DCHECK_EQ(3u, header_line_tokens.size()); | |
| 91 // Method. | |
| 92 http_request_->method = GetMethodType(StringToLowerASCII( | |
| 93 header_line_tokens[0])); | |
| 94 // Address. | |
| 95 // Don't build an absolute URL as the parser does not know (should not | |
| 96 // know) anything about the server address. | |
| 97 http_request_->relative_url = header_line_tokens[1]; | |
| 98 // Protocol. | |
| 99 const std::string protocol = StringToLowerASCII(header_line_tokens[2]); | |
| 100 CHECK(protocol == "http/1.0" || protocol == "http/1.1") << | |
| 101 "Protocol not supported: " << protocol; | |
| 102 } | |
| 103 | |
| 104 // Parse further headers. | |
| 105 { | |
| 106 std::string header_name; | |
| 107 while (true) { | |
| 108 std::string header_line = ShiftLine(); | |
| 109 if (header_line.empty()) | |
| 110 break; | |
| 111 | |
| 112 if (header_line[0] == ' ' || header_line[0] == '\t') { | |
| 113 // Continuation of the previous multi-line header. | |
| 114 std::string header_value = | |
| 115 Trim(header_line.substr(1, header_line.size() - 1)); | |
| 116 http_request_->headers[header_name] += " " + header_value; | |
| 117 } else { | |
| 118 // New header. | |
| 119 size_t delimiter_pos = header_line.find(":"); | |
| 120 DCHECK_NE(std::string::npos, delimiter_pos) << "Syntax error."; | |
| 121 header_name = Trim(header_line.substr(0, delimiter_pos)); | |
| 122 std::string header_value = Trim(header_line.substr( | |
| 123 delimiter_pos + 1, | |
| 124 header_line.size() - delimiter_pos - 1)); | |
| 125 http_request_->headers[header_name] = header_value; | |
| 126 } | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 // Headers done. Is any content data attached to the request? | |
| 131 declared_content_length_ = 0; | |
| 132 if (http_request_->headers.count("Content-Length") > 0) { | |
| 133 http_request_->has_content = true; | |
| 134 const bool success = base::StringToSizeT( | |
| 135 http_request_->headers["Content-Length"], | |
| 136 &declared_content_length_); | |
| 137 DCHECK(success) << "Malformed Content-Length header's value."; | |
| 138 } | |
| 139 if (declared_content_length_ == 0) { | |
| 140 // No content data, so parsing is finished. | |
| 141 state_ = STATE_ACCEPTED; | |
| 142 return ACCEPTED; | |
| 143 } | |
| 144 | |
| 145 // The request has not yet been parsed yet, content data is still to be | |
| 146 // processed. | |
| 147 state_ = STATE_CONTENT; | |
| 148 return WAITING; | |
| 149 } | |
| 150 | |
| 151 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { | |
| 152 const size_t available_bytes = buffer_.size() - buffer_position_; | |
| 153 const size_t fetch_bytes = std::min( | |
| 154 available_bytes, | |
| 155 declared_content_length_ - http_request_->content.size()); | |
| 156 http_request_->content.append(buffer_.data() + buffer_position_, | |
| 157 fetch_bytes); | |
| 158 buffer_position_ += fetch_bytes; | |
| 159 | |
| 160 if (declared_content_length_ == http_request_->content.size()) { | |
| 161 state_ = STATE_ACCEPTED; | |
| 162 return ACCEPTED; | |
| 163 } | |
| 164 | |
| 165 state_ = STATE_CONTENT; | |
| 166 return WAITING; | |
| 167 } | |
| 168 | |
| 169 scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() { | |
| 170 DCHECK_EQ(STATE_ACCEPTED, state_); | |
| 171 scoped_ptr<HttpRequest> result = http_request_.Pass(); | |
| 172 | |
| 173 // Prepare for parsing a new request. | |
| 174 state_ = STATE_HEADERS; | |
| 175 http_request_.reset(new HttpRequest()); | |
| 176 buffer_.clear(); | |
| 177 buffer_position_ = 0; | |
| 178 declared_content_length_ = 0; | |
| 179 | |
| 180 return result.Pass(); | |
| 181 } | |
| 182 | |
| 183 HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const { | |
| 184 if (token == "get") { | |
| 185 return METHOD_GET; | |
| 186 } else if (token == "head") { | |
| 187 return METHOD_HEAD; | |
| 188 } else if (token == "post") { | |
| 189 return METHOD_POST; | |
| 190 } else if (token == "put") { | |
| 191 return METHOD_PUT; | |
| 192 } else if (token == "delete") { | |
| 193 return METHOD_DELETE; | |
| 194 } else if (token == "patch") { | |
| 195 return METHOD_PATCH; | |
| 196 } | |
| 197 NOTREACHED() << "Method not implemented: " << token; | |
| 198 return METHOD_UNKNOWN; | |
| 199 } | |
| 200 | |
| 201 } // namespace test_server | |
| 202 } // namespace google_apis | |
| OLD | NEW |