Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5032)

Unified Diff: chrome/browser/chromeos/drive/test_servers/http_request.cc

Issue 11088073: HTTP server for testing Google Drive. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressed comments and simplified. Added tests. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3d83e7cf65a87a24f6945c8c8bf15450d58f0d55
--- /dev/null
+++ b/chrome/browser/chromeos/drive/test_servers/http_request.cc
@@ -0,0 +1,177 @@
+// 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
+
+HttpRequestParser::HttpRequestParser() : http_request_(new HttpRequest()),
+ state_(STATE_HEADERS),
+ buffer_position_(0),
+ current_content_length_(0) {
+}
+
+HttpRequestParser::~HttpRequestParser() {
+}
+
+void HttpRequestParser::ProcessChunk(const char *data, size_t length) {
+ buffer_.append(data, length);
+ DCHECK(buffer_.length() + length <= kRequestSizeLimit) <<
+ "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;
+}
+
+HttpRequestParser::ParseResult HttpRequestParser::ParseRequest() {
+ DCHECK(state_ != STATE_ACCEPTED);
+ // 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.
+ if (buffer_.find("\r\n\r\n", buffer_position_) == std::string::npos)
+ 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);
+ 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 != "") {
+ if (header_line[0] == ' ') {
+ // Continuation of the previous multi-line header.
+ http_request_->headers[header_name] += "\n" +
+ header_line.substr(1, header_line.length() - 1);
+ } 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();
+ }
+ }
+
+ // Headers done. Is any content data attached to the request?
+ if (http_request_->headers.find("Content-Length") !=
+ http_request_->headers.end()) {
+ current_content_length_ =
+ atoi(http_request_->headers["Content-Length"].c_str());
+ if (!current_content_length_)
+ return ACCEPTED;
+ }
+
+ // 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_);
+ http_request_->content.append(buffer_.data() + buffer_position_,
+ fetch_bytes);
+ buffer_position_ += fetch_bytes;
+
+ 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_,
+ 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

Powered by Google App Engine
This is Rietveld 408576698