Chromium Code Reviews| Index: chrome/browser/chromeos/drive/test_servers/http_request.cc |
| diff --git a/chrome/browser/chromeos/drive/test_servers/http_request.cc b/chrome/browser/chromeos/drive/test_servers/http_request.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..147a12f0afd17b3432231c3d11cb804a290d6ea4 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/drive/test_servers/http_request.cc |
| @@ -0,0 +1,183 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/drive/test_servers/http_request.h" |
| + |
| +#include <algorithm> |
| +#include <map> |
| +#include <string> |
| +#include "base/basictypes.h" |
| +#include "base/logging.h" |
| +#include "base/string_util.h" |
| +#include "googleurl/src/gurl.h" |
| + |
| + |
| +namespace drive { |
| +namespace test_servers { |
| + |
| +namespace { |
| +size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. |
| +} // namespace |
| + |
| +HttpRequest::HttpRequest() : method(UNKNOWN) { |
| +} |
| + |
| +HttpRequest::~HttpRequest() { |
| +} |
| + |
| +HttpRequestParser::HttpRequestParser() : http_request_(new HttpRequest()), |
| + state_(STATE_HEADERS), |
| + buffer_position_(0), |
| + current_content_length_(0) { |
|
satorux1
2012/11/12 06:07:00
Maybe:
HttpRequestParser::HttpRequestParser()
mtomasz
2012/11/12 12:17:44
Done.
|
| +} |
| + |
| +HttpRequestParser::~HttpRequestParser() { |
| +} |
| + |
| +void HttpRequestParser::ProcessChunk(const char *data, size_t length) { |
| + buffer_.append(data, length); |
|
satorux1
2012/11/12 06:07:00
If you use StringPiece for buffer_position_, it sh
mtomasz
2012/11/12 12:17:44
I am not sure if I understand correctly. So, we wi
satorux1
2012/11/13 05:13:18
Yes, but it's no worse than now. We already have t
mtomasz
2012/11/13 12:23:07
I am still not convinced. To append, the code will
satorux1
2012/11/14 01:54:31
You are right that this is tricky. Let's keep buff
|
| + DCHECK(buffer_.length() + length <= kRequestSizeLimit) << |
|
satorux1
2012/11/12 06:07:00
DCHECK_LE
mtomasz
2012/11/12 12:17:44
Why is it better? Done.
satorux1
2012/11/13 05:13:18
The failure message is better. You'll see the two
mtomasz
2012/11/13 12:23:07
Got it.
|
| + "The HTTP request is too large."; |
| +} |
| + |
| +std::string HttpRequestParser::ShiftLine() { |
| + size_t eoln_position = buffer_.find("\r\n", buffer_position_); |
| + if (eoln_position == std::string::npos) |
| + return ""; |
| + const int line_length = eoln_position - buffer_position_; |
| + std::string result = buffer_.substr(buffer_position_, line_length); |
| + buffer_position_ += line_length + 2; |
| + return result; |
|
satorux1
2012/11/12 06:07:00
I think it'd be simpler to use StringPiece for buf
mtomasz
2012/11/12 12:17:44
See above. This would be great if we could easily
|
| +} |
| + |
| +HttpRequestParser::ParseResult HttpRequestParser::ParseRequest() { |
| + DCHECK(state_ != STATE_ACCEPTED); |
|
satorux1
2012/11/12 06:07:00
DCHECK_NE
mtomasz
2012/11/12 12:17:44
Done.
|
| + // Parse the request from beginning. However, entire request may not be |
| + // available in the buffer. |
| + if (state_ == STATE_HEADERS) { |
| + if (ParseHeaders() == ACCEPTED) |
| + return ACCEPTED; |
| + } |
| + if (state_ == STATE_CONTENT) { |
| + if (ParseContent() == ACCEPTED) |
| + return ACCEPTED; |
| + } |
| + return WAITING; |
| +} |
| + |
| +HttpRequestParser::ParseResult HttpRequestParser::ParseHeaders() { |
| + // Check if the whole request input is available. |
|
satorux1
2012/11/12 06:07:00
request input is -> request headers are
mtomasz
2012/11/12 12:17:44
Done.
|
| + if (buffer_.find("\r\n\r\n", buffer_position_) == std::string::npos) |
|
satorux1
2012/11/12 06:07:00
if you use StringPiece: buffer_position_.find("\r\
mtomasz
2012/11/12 12:17:44
Ack. Let's discuss offline.
|
| + return WAITING; |
| + |
| + // Parse request's the first header line. |
| + // Request main main header, eg. GET /foobar.html HTTP/1.1 |
| + { |
| + const std::string header_line = ShiftLine(); |
| + std::vector<std::string> header_line_tokens; |
| + Tokenize(header_line, " ", &header_line_tokens); |
|
satorux1
2012/11/12 06:07:00
Can we use SplitString() instead? SplitString() is
mtomasz
2012/11/12 12:17:44
Done!
|
| + DCHECK(header_line_tokens.size() == 3); |
| + // Method. |
| + http_request_->method = GetMethodType(StringToLowerASCII( |
| + header_line_tokens[0])); |
| + // Address. |
| + const GURL host = GURL("http://localhost/"); |
| + http_request_->uri = host.Resolve(header_line_tokens[1]); |
| + // Protocol. |
| + const std::string protocol = StringToLowerASCII(header_line_tokens[2]); |
| + CHECK(protocol == "http/1.0" || protocol == "http/1.1") << |
| + "Protocol not supported: " << protocol; |
| + } |
| + |
| + // Parse further headers. |
| + { |
| + std::string header_line = ShiftLine(); |
| + std::string header_name; |
| + while (header_line != "") { |
|
satorux1
2012/11/12 06:07:00
!header_line.empty()
mtomasz
2012/11/12 12:17:44
Done.
|
| + if (header_line[0] == ' ') { |
|
satorux1
2012/11/12 06:07:00
|| header_line[0] == '\t'
mtomasz
2012/11/12 12:17:44
Done.
|
| + // Continuation of the previous multi-line header. |
| + http_request_->headers[header_name] += "\n" + |
|
satorux1
2012/11/12 06:07:00
IIRC, I think white spaces should be interpreted a
mtomasz
2012/11/12 12:17:44
You're right. Done.
|
| + header_line.substr(1, header_line.length() - 1); |
|
satorux1
2012/11/12 06:07:00
Please use size(). I think size() is more widely u
mtomasz
2012/11/12 12:17:44
Done.
|
| + } else { |
| + // New header. |
| + size_t delimiter_pos = header_line.find(":"); |
| + DCHECK(delimiter_pos != std::string::npos) << "Syntax error."; |
| + header_name = header_line.substr(0, delimiter_pos); |
| + size_t header_value_pos = delimiter_pos + 1; |
| + // Skip spaces (if any) after the colon. |
| + while (header_value_pos < header_line.length() && |
| + header_line[header_value_pos] == ' ') { |
| + header_value_pos++; |
| + } |
| + http_request_->headers[header_name] = |
| + header_line.substr(header_value_pos, |
| + header_line.length() - header_value_pos); |
| + } |
| + header_line = ShiftLine(); |
|
satorux1
2012/11/12 06:07:00
Maybe the loop is easier to read with:
while (tru
mtomasz
2012/11/12 12:17:44
Done.
|
| + } |
| + } |
| + |
| + // Headers done. Is any content data attached to the request? |
| + if (http_request_->headers.find("Content-Length") != |
|
satorux1
2012/11/12 06:07:00
more like a matter of taste, but the following is
mtomasz
2012/11/12 12:17:44
Done.
|
| + http_request_->headers.end()) { |
| + current_content_length_ = |
| + atoi(http_request_->headers["Content-Length"].c_str()); |
|
satorux1
2012/11/12 06:07:00
Please use StringToSizeT in base/string_number_con
mtomasz
2012/11/12 12:17:44
Done.
|
| + if (!current_content_length_) |
|
satorux1
2012/11/12 06:07:00
current_content_length_ == 0
mtomasz
2012/11/12 12:17:44
Done.
|
| + return ACCEPTED; |
| + } |
|
satorux1
2012/11/12 06:07:00
If Content-Length: is not present, I think we can
mtomasz
2012/11/12 12:17:44
Right! Done.
|
| + |
| + // The request has not yet been parsed, content data is still to be parsed. |
| + state_ = STATE_CONTENT; |
| + return WAITING; |
| +} |
| + |
| +HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { |
| + const size_t available_bytes = buffer_.length() - buffer_position_; |
| + const size_t fetch_bytes = std::min(available_bytes, |
| + current_content_length_); |
|
satorux1
2012/11/12 06:07:00
Are you trying to limit the size of |http_request_
mtomasz
2012/11/12 12:17:44
Fixed std::min(). Done.
As for the DCHECK, I thin
|
| + http_request_->content.append(buffer_.data() + buffer_position_, |
| + fetch_bytes); |
| + buffer_position_ += fetch_bytes; |
|
satorux1
2012/11/12 06:07:00
If you use StringPiece for buffer_position_, the c
mtomasz
2012/11/12 12:17:44
Ack. Let's discuss.
|
| + |
| + if (current_content_length_ == http_request_->content.length()) { |
| + state_ = STATE_ACCEPTED; |
| + return ACCEPTED; |
| + } |
| + |
| + return WAITING; |
| +} |
| + |
| +scoped_ptr<HttpRequest> HttpRequestParser::GetRequest() { |
| + DCHECK(state_ == STATE_ACCEPTED); |
| + scoped_ptr<HttpRequest> result = http_request_.Pass(); |
| + |
| + // Prepare for parsing a new request. |
| + state_ = STATE_HEADERS; |
| + http_request_.reset(new HttpRequest()); |
| + buffer_ = buffer_.substr(buffer_position_, |
|
satorux1
2012/11/12 06:07:00
Why not just buffer_.clear()?
At this moment, All
mtomasz
2012/11/12 12:17:44
This is in case, we get two (or more) requests in
satorux1
2012/11/13 05:13:18
I think we don't need to support the case. The ser
mtomasz
2012/11/13 12:23:07
I've just checked the rfc. In http/1.1 all connect
satorux1
2012/11/14 01:34:32
Let's remove the support for the multiple requests
mtomasz
2012/11/14 03:23:35
Done.
|
| + buffer_.length() - buffer_position_); |
| + buffer_position_ = 0; |
| + current_content_length_ = 0; |
| + |
| + return result.Pass(); |
| +} |
| + |
| +HttpMethod HttpRequestParser::GetMethodType(const std::string& token) const { |
| + if (token == "get") { |
| + return GET; |
| + } else if (token == "head") { |
| + return HEAD; |
| + } else if (token == "post") { |
| + return POST; |
| + } else if (token == "put") { |
| + return PUT; |
| + } else if (token == "delete") { |
| + return DELETE; |
| + } |
| + NOTREACHED() << "Method not implemented: " << token; |
| + return UNKNOWN; |
| +} |
| + |
| +} // namespace test_servers |
| +} // namespace drive |