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 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_REQUEST_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_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_servers { | |
17 | |
18 using base::StringPiece; | |
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 | |
40 private: | |
41 DISALLOW_COPY_AND_ASSIGN(HttpRequest); | |
42 }; | |
43 | |
44 // Parses the input data and produces valid HttpRequest objects. The common | |
45 // use is as below: | |
46 // HttpRequestParser parser; | |
47 // (...) | |
48 // void OnDataChunkReceived(Socket* socket, const char* data, int size) { | |
49 // parser.ProcessChunk(std::string(data, size)); | |
satorux1
2012/11/13 05:13:18
I think StringPiece is better. It won't make a cop
mtomasz
2012/11/13 12:23:07
Done.
| |
50 // while (parser.ParseRequest() == HttpRequestParser::ACCEPTED) { | |
51 // scoped_ptr<HttpRequest> request = parser.GetRequest(); | |
52 // (... process the request ...) | |
53 // } | |
54 class HttpRequestParser { | |
55 public: | |
56 // Parsing result. | |
57 enum ParseResult { | |
58 WAITING, // A request is not completed yet, waiting for more data. | |
59 ACCEPTED, // A request has been parsed and it is ready to be processed. | |
60 }; | |
61 | |
62 // Parser state. | |
63 enum State { | |
64 STATE_HEADERS, // Waiting for a request headers. | |
65 STATE_CONTENT, // Waiting for content data. | |
66 STATE_ACCEPTED, // Request has been parsed. | |
67 }; | |
68 | |
69 HttpRequestParser(); | |
70 ~HttpRequestParser(); | |
71 | |
72 // Adds chunk of data into the internal buffer. | |
73 void ProcessChunk(const std::string& data); | |
74 | |
75 // Parses the http request (including data - if provided). | |
76 // If returns ACCEPTED, then it means that the whole request has been found | |
77 // in the internal buffer (and parsed). After calling GetRequest(), it will be | |
78 // ready to parse another request. | |
79 ParseResult ParseRequest(); | |
80 | |
81 // Retrieves parsed request. Can be only called, when the parser is in | |
82 // STATE_ACCEPTED state. After calling it, the parser is ready to parse | |
83 // another request. | |
84 scoped_ptr<HttpRequest> GetRequest(); | |
85 | |
86 private: | |
87 scoped_ptr<HttpRequest> http_request_; | |
88 std::string buffer_; | |
89 size_t buffer_position_; // Current position in the internal buffer. | |
90 State state_; | |
91 // Content length of the request currently being parsed. | |
92 size_t declared_content_length_; | |
93 | |
94 HttpMethod GetMethodType(const std::string& token) const; | |
95 | |
96 // Parses headers and returns ACCEPTED if whole request was parsed. Otherwise | |
97 // returns WAITING. | |
98 ParseResult ParseHeaders(); | |
99 | |
100 // Parses request's content data and returns ACCEPTED if all of it have been | |
101 // processed. Chunked Transfer Encoding *is not* supported. | |
102 ParseResult ParseContent(); | |
103 | |
104 // Fetches the next line from the buffer. Result does not contain \r\n. | |
105 // Returns an empty string for an empty line. It will assert if there is | |
106 // no line available. | |
107 std::string ShiftLine(); | |
108 }; | |
109 | |
110 } // namespace test_servers | |
111 } // namespace drive | |
112 | |
113 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_REQUEST_H_ | |
OLD | NEW |