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 #include <map> |
| 9 #include <string> |
| 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/string_number_conversions.h" |
| 13 #include "base/string_split.h" |
| 14 #include "base/string_util.h" |
| 15 #include "googleurl/src/gurl.h" |
| 16 |
| 17 |
| 18 namespace drive { |
| 19 namespace test_server { |
| 20 |
| 21 namespace { |
| 22 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. |
| 23 |
| 24 // Helper function used to trim tokens in http request headers. |
| 25 std::string Trim(const std::string& value) { |
| 26 std::string result; |
| 27 TrimString(value, " \t", &result); |
| 28 return result; |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 HttpRequest::HttpRequest() : method(UNKNOWN) { |
| 34 } |
| 35 |
| 36 HttpRequest::~HttpRequest() { |
| 37 } |
| 38 |
| 39 HttpRequestParser::HttpRequestParser() |
| 40 : http_request_(new HttpRequest()), |
| 41 buffer_position_(0), |
| 42 state_(STATE_HEADERS), |
| 43 declared_content_length_(0) { |
| 44 } |
| 45 |
| 46 HttpRequestParser::~HttpRequestParser() { |
| 47 } |
| 48 |
| 49 void HttpRequestParser::ProcessChunk(const base::StringPiece& data) { |
| 50 data.AppendToString(&buffer_); |
| 51 DCHECK_LE(buffer_.size() + data.size(), kRequestSizeLimit) << |
| 52 "The HTTP request is too large."; |
| 53 } |
| 54 |
| 55 std::string HttpRequestParser::ShiftLine() { |
| 56 size_t eoln_position = buffer_.find("\r\n", buffer_position_); |
| 57 DCHECK_NE(std::string::npos, eoln_position); |
| 58 const int line_length = eoln_position - buffer_position_; |
| 59 std::string result = buffer_.substr(buffer_position_, line_length); |
| 60 buffer_position_ += line_length + 2; |
| 61 return result; |
| 62 } |
| 63 |
| 64 HttpRequestParser::ParseResult HttpRequestParser::ParseRequest() { |
| 65 DCHECK_NE(state_, STATE_ACCEPTED); |
| 66 // Parse the request from beginning. However, entire request may not be |
| 67 // available in the buffer. |
| 68 if (state_ == STATE_HEADERS) { |
| 69 if (ParseHeaders() == ACCEPTED) |
| 70 return ACCEPTED; |
| 71 } |
| 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(header_line_tokens.size(), 3u); |
| 91 // Method. |
| 92 http_request_->method = GetMethodType(StringToLowerASCII( |
| 93 header_line_tokens[0])); |
| 94 // Address. |
| 95 const GURL host = GURL("http://localhost/"); |
| 96 http_request_->uri = host.Resolve(header_line_tokens[1]); |
| 97 // Protocol. |
| 98 const std::string protocol = StringToLowerASCII(header_line_tokens[2]); |
| 99 CHECK(protocol == "http/1.0" || protocol == "http/1.1") << |
| 100 "Protocol not supported: " << protocol; |
| 101 } |
| 102 |
| 103 // Parse further headers. |
| 104 { |
| 105 std::string header_name; |
| 106 while (true) { |
| 107 std::string header_line = ShiftLine(); |
| 108 if (header_line.empty()) |
| 109 break; |
| 110 |
| 111 if (header_line[0] == ' ' || header_line[0] == '\t') { |
| 112 // Continuation of the previous multi-line header. |
| 113 std::string header_value = |
| 114 Trim(header_line.substr(1, header_line.size() - 1)); |
| 115 http_request_->headers[header_name] += " " + header_value; |
| 116 } else { |
| 117 // New header. |
| 118 size_t delimiter_pos = header_line.find(":"); |
| 119 DCHECK_NE(delimiter_pos, std::string::npos) << "Syntax error."; |
| 120 header_name = Trim(header_line.substr(0, delimiter_pos)); |
| 121 std::string header_value = Trim(header_line.substr( |
| 122 delimiter_pos + 1, |
| 123 header_line.size() - delimiter_pos - 1)); |
| 124 http_request_->headers[header_name] = header_value; |
| 125 } |
| 126 } |
| 127 } |
| 128 |
| 129 // Headers done. Is any content data attached to the request? |
| 130 declared_content_length_ = 0; |
| 131 if (http_request_->headers.count("Content-Length") > 0) { |
| 132 DCHECK(base::StringToSizeT(http_request_->headers["Content-Length"], |
| 133 &declared_content_length_)) << |
| 134 "Malformed Content-Length header's value."; |
| 135 } |
| 136 if (declared_content_length_ == 0) { |
| 137 // No content data, so parsing is finished. |
| 138 state_ = STATE_ACCEPTED; |
| 139 return ACCEPTED; |
| 140 } |
| 141 |
| 142 // The request has not yet been parsed yet, content data is still to be |
| 143 // processed. |
| 144 state_ = STATE_CONTENT; |
| 145 return WAITING; |
| 146 } |
| 147 |
| 148 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { |
| 149 const size_t available_bytes = buffer_.size() - buffer_position_; |
| 150 const size_t fetch_bytes = std::min( |
| 151 available_bytes, |
| 152 declared_content_length_ - http_request_->content.size()); |
| 153 http_request_->content.append(buffer_.data() + buffer_position_, |
| 154 fetch_bytes); |
| 155 buffer_position_ += fetch_bytes; |
| 156 |
| 157 if (declared_content_length_ == http_request_->content.size()) { |
| 158 state_ = STATE_ACCEPTED; |
| 159 return ACCEPTED; |
| 160 } |
| 161 |
| 162 state_ = STATE_CONTENT; |
| 163 return WAITING; |
| 164 } |
| 165 |
| 166 scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() { |
| 167 DCHECK_EQ(state_, STATE_ACCEPTED); |
| 168 scoped_ptr<HttpRequest> result = http_request_.Pass(); |
| 169 |
| 170 // Prepare for parsing a new request. |
| 171 state_ = STATE_HEADERS; |
| 172 http_request_.reset(new HttpRequest()); |
| 173 buffer_ = buffer_.substr(buffer_position_, |
| 174 buffer_.size() - buffer_position_); |
| 175 buffer_position_ = 0; |
| 176 declared_content_length_ = 0; |
| 177 |
| 178 return result.Pass(); |
| 179 } |
| 180 |
| 181 HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const { |
| 182 if (token == "get") { |
| 183 return GET; |
| 184 } else if (token == "head") { |
| 185 return HEAD; |
| 186 } else if (token == "post") { |
| 187 return POST; |
| 188 } else if (token == "put") { |
| 189 return PUT; |
| 190 } else if (token == "delete") { |
| 191 return DELETE; |
| 192 } |
| 193 NOTREACHED() << "Method not implemented: " << token; |
| 194 return UNKNOWN; |
| 195 } |
| 196 |
| 197 } // namespace test_server |
| 198 } // namespace drive |
OLD | NEW |