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