Chromium Code Reviews| 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 // 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.
| |
| 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(data, size); | |
| 50 // while (parser.ParseRequest() == HttpRequestParser::ACCEPTED) { | |
| 51 // scoped_ptr<HttpRequest> request = parser.GetRequest(); | |
| 52 // (... process the request ...) | |
| 53 // } | |
| 54 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
| |
| 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 virtual ~HttpRequestParser(); | |
| 71 | |
| 72 // Adds chunk of data into the internal buffer. | |
| 73 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.
| |
| 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 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.
| |
| 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 std::string buffer_; | |
| 88 scoped_ptr<HttpRequest> http_request_; | |
| 89 State state_; | |
| 90 size_t buffer_position_; // Current position in the internal buffer. | |
| 91 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.
| |
| 92 | |
| 93 virtual HttpMethod GetMethodType(const std::string& token) const; | |
| 94 | |
| 95 // Parses headers and returns ACCEPTED if whole request was parsed. Otherwise | |
| 96 // returns WAITING. | |
| 97 virtual ParseResult ParseHeaders(); | |
| 98 | |
| 99 // Parses request's content data and returns ACCEPTED if all of it have been | |
| 100 // processed. Chunked Transfer Encoding *is not* supported. | |
| 101 virtual ParseResult ParseContent(); | |
| 102 | |
| 103 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.
| |
| 104 }; | |
| 105 | |
| 106 } // namespace test_servers | |
| 107 } // namespace drive | |
| 108 | |
| 109 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_REQUEST_H_ | |
| OLD | NEW |