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