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

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

Issue 11088073: HTTP server for testing Google Drive. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed for clang. 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.h
diff --git a/chrome/browser/chromeos/drive/test_servers/http_request.h b/chrome/browser/chromeos/drive/test_servers/http_request.h
new file mode 100644
index 0000000000000000000000000000000000000000..b50a1029cdaf90b89b9a549a3f5f87b1c0069340
--- /dev/null
+++ b/chrome/browser/chromeos/drive/test_servers/http_request.h
@@ -0,0 +1,109 @@
+// 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.
+
+#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_REQUEST_H_
+#define CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_REQUEST_H_
+
+#include <map>
+#include <string>
+#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/string_piece.h"
+#include "googleurl/src/gurl.h"
+
+namespace drive {
+namespace test_servers {
+
+using base::StringPiece;
+
+enum HttpMethod {
+ UNKNOWN,
+ GET,
+ HEAD,
+ POST,
+ PUT,
+ DELETE,
+};
+
+// Wraps the HTTP request. Since it can be big, use scoped_ptr to pass it
satorux1 2012/11/12 06:07:00 Wraps -> Represents ?
mtomasz 2012/11/12 12:17:44 Done.
+// instead of copying.
+struct HttpRequest {
+ HttpRequest();
+ ~HttpRequest();
+
+ GURL uri;
+ HttpMethod method;
+ std::map<std::string, std::string> headers;
+ std::string content;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(HttpRequest);
+};
+
+// Parses the input data and produces valid HttpRequest objects. The common
+// use is as below:
+// HttpRequestParser parser;
+// (...)
+// void OnDataChunkReceived(Socket* socket, const char* data, int size) {
+// parser.ProcessChunk(data, size);
+// while (parser.ParseRequest() == HttpRequestParser::ACCEPTED) {
+// scoped_ptr<HttpRequest> request = parser.GetRequest();
+// (... process the request ...)
+// }
+class HttpRequestParser {
satorux1 2012/11/12 06:07:00 I think we can write a unit test for this class.
mtomasz 2012/11/12 12:17:44 It is already done. I've extracted it to a separat
+ public:
+ // Parsing result.
+ enum ParseResult {
+ WAITING, // A request is not completed yet, waiting for more data.
+ ACCEPTED, // A request has been parsed and it is ready to be processed.
+ };
+
+ // Parser state.
+ enum State {
+ STATE_HEADERS, // Waiting for a request headers.
+ STATE_CONTENT, // Waiting for content data.
+ STATE_ACCEPTED, // Request has been parsed.
+ };
+
+ HttpRequestParser();
+ virtual ~HttpRequestParser();
+
+ // Adds chunk of data into the internal buffer.
+ virtual void ProcessChunk(const char *data, size_t length);
satorux1 2012/11/12 06:07:00 Can we use StringPiece instead? One less parameter
mtomasz 2012/11/12 12:17:44 How about std::string? Done. Please let me know wh
satorux1 2012/11/13 05:13:18 I think StringPiece is better.
mtomasz 2012/11/13 12:23:07 Done.
+
+ // Parses the http request (including data - if provided).
+ // If returns ACCEPTED, then it means that the whole request has been found
+ // in the internal buffer (and parsed). After calling GetRequest(), it will be
+ // ready to parse another request.
+ virtual ParseResult ParseRequest();
satorux1 2012/11/12 06:07:00 should this be virtual? Are there any classes inhe
mtomasz 2012/11/12 12:17:44 Done.
+
+ // Retrieves parsed request. Can be only called, when the parser is in
+ // STATE_ACCEPTED state. After calling it, the parser is ready to parse
+ // another request.
+ scoped_ptr<HttpRequest> GetRequest();
+
+ private:
+ std::string buffer_;
+ scoped_ptr<HttpRequest> http_request_;
+ State state_;
+ size_t buffer_position_; // Current position in the internal buffer.
+ size_t current_content_length_;
satorux1 2012/11/12 06:07:00 This is the content length declared in the Content
mtomasz 2012/11/12 12:17:44 Right! Done. Also, added a comment.
+
+ virtual HttpMethod GetMethodType(const std::string& token) const;
+
+ // Parses headers and returns ACCEPTED if whole request was parsed. Otherwise
+ // returns WAITING.
+ virtual ParseResult ParseHeaders();
+
+ // Parses request's content data and returns ACCEPTED if all of it have been
+ // processed. Chunked Transfer Encoding *is not* supported.
+ virtual ParseResult ParseContent();
+
+ std::string ShiftLine();
satorux1 2012/11/12 06:07:00 Function comment is missing. Does the returned str
mtomasz 2012/11/12 12:17:44 Done.
+};
+
+} // namespace test_servers
+} // namespace drive
+
+#endif // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_REQUEST_H_

Powered by Google App Engine
This is Rietveld 408576698