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

Side by Side Diff: chrome/browser/google_apis/test_server/http_request.h

Issue 11088073: HTTP server for testing Google Drive. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressed most comments. 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 unified diff | Download patch
OLDNEW
(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 #ifndef CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_REQUEST_H_
6 #define CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_REQUEST_H_
7
8 #include <map>
9 #include <string>
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/string_piece.h"
13 #include "googleurl/src/gurl.h"
14
15 namespace drive {
16 namespace test_server {
17
18 using base::StringPiece;
Lei Zhang 2012/11/14 04:24:15 nit: shouldn't be used in headers. Also not needed
satorux1 2012/11/14 04:43:36 Done.
19
20 enum HttpMethod {
21 UNKNOWN,
22 GET,
23 HEAD,
24 POST,
25 PUT,
26 DELETE,
27 };
28
29 // Represents a HTTP request. Since it can be big, use scoped_ptr to pass it
30 // instead of copying.
31 struct HttpRequest {
32 HttpRequest();
33 ~HttpRequest();
34
35 GURL uri;
36 HttpMethod method;
37 std::map<std::string, std::string> headers;
38 std::string content;
39 bool has_content;
40
41 private:
42 DISALLOW_COPY_AND_ASSIGN(HttpRequest);
43 };
44
45 // Parses the input data and produces a valid HttpRequest object. If there is
46 // more than one request in one chunk, then only the first one will be parsed.
47 // The common use is as below:
48 // HttpRequestParser parser;
49 // (...)
50 // void OnDataChunkReceived(Socket* socket, const char* data, int size) {
51 // parser.ProcessChunk(std::string(data, size));
52 // if (parser.ParseRequest() == HttpRequestParser::ACCEPTED) {
53 // scoped_ptr<HttpRequest> request = parser.GetRequest();
54 // (... process the request ...)
55 // }
56 class HttpRequestParser {
57 public:
58 // Parsing result.
59 enum ParseResult {
60 WAITING, // A request is not completed yet, waiting for more data.
61 ACCEPTED, // A request has been parsed and it is ready to be processed.
62 };
63
64 // Parser state.
65 enum State {
66 STATE_HEADERS, // Waiting for a request headers.
67 STATE_CONTENT, // Waiting for content data.
68 STATE_ACCEPTED, // Request has been parsed.
69 };
70
71 HttpRequestParser();
72 ~HttpRequestParser();
73
74 // Adds chunk of data into the internal buffer.
75 void ProcessChunk(const base::StringPiece& data);
76
77 // Parses the http request (including data - if provided).
78 // If returns ACCEPTED, then it means that the whole request has been found
79 // in the internal buffer (and parsed). After calling GetRequest(), it will be
80 // ready to parse another request.
81 ParseResult ParseRequest();
82
83 // Retrieves parsed request. Can be only called, when the parser is in
84 // STATE_ACCEPTED state. After calling it, the parser is ready to parse
85 // another request.
86 scoped_ptr<HttpRequest> GetRequest();
87
88 private:
89 scoped_ptr<HttpRequest> http_request_;
90 std::string buffer_;
91 size_t buffer_position_; // Current position in the internal buffer.
92 State state_;
93 // Content length of the request currently being parsed.
94 size_t declared_content_length_;
95
96 HttpMethod GetMethodType(const std::string& token) const;
97
98 // Parses headers and returns ACCEPTED if whole request was parsed. Otherwise
99 // returns WAITING.
100 ParseResult ParseHeaders();
101
102 // Parses request's content data and returns ACCEPTED if all of it have been
103 // processed. Chunked Transfer Encoding *is not* supported.
104 ParseResult ParseContent();
105
106 // Fetches the next line from the buffer. Result does not contain \r\n.
107 // Returns an empty string for an empty line. It will assert if there is
108 // no line available.
109 std::string ShiftLine();
110 };
111
112 } // namespace test_server
113 } // namespace drive
114
115 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_REQUEST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698