| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_HTTP_REQUEST_H_ | 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_HTTP_REQUEST_H_ |
| 6 #define NET_TEST_EMBEDDED_TEST_SERVER_HTTP_REQUEST_H_ | 6 #define NET_TEST_EMBEDDED_TEST_SERVER_HTTP_REQUEST_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> |
| 11 #include <string> | 12 #include <string> |
| 12 | 13 |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| 20 | 20 |
| 21 class HttpChunkedDecoder; | 21 class HttpChunkedDecoder; |
| 22 | 22 |
| 23 namespace test_server { | 23 namespace test_server { |
| 24 | 24 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 // Parses the input data and produces a valid HttpRequest object. If there is | 68 // Parses the input data and produces a valid HttpRequest object. If there is |
| 69 // more than one request in one chunk, then only the first one will be parsed. | 69 // more than one request in one chunk, then only the first one will be parsed. |
| 70 // The common use is as below: | 70 // The common use is as below: |
| 71 // HttpRequestParser parser; | 71 // HttpRequestParser parser; |
| 72 // (...) | 72 // (...) |
| 73 // void OnDataChunkReceived(Socket* socket, const char* data, int size) { | 73 // void OnDataChunkReceived(Socket* socket, const char* data, int size) { |
| 74 // parser.ProcessChunk(std::string(data, size)); | 74 // parser.ProcessChunk(std::string(data, size)); |
| 75 // if (parser.ParseRequest() == HttpRequestParser::ACCEPTED) { | 75 // if (parser.ParseRequest() == HttpRequestParser::ACCEPTED) { |
| 76 // scoped_ptr<HttpRequest> request = parser.GetRequest(); | 76 // std::unique_ptr<HttpRequest> request = parser.GetRequest(); |
| 77 // (... process the request ...) | 77 // (... process the request ...) |
| 78 // } | 78 // } |
| 79 class HttpRequestParser { | 79 class HttpRequestParser { |
| 80 public: | 80 public: |
| 81 // Parsing result. | 81 // Parsing result. |
| 82 enum ParseResult { | 82 enum ParseResult { |
| 83 WAITING, // A request is not completed yet, waiting for more data. | 83 WAITING, // A request is not completed yet, waiting for more data. |
| 84 ACCEPTED, // A request has been parsed and it is ready to be processed. | 84 ACCEPTED, // A request has been parsed and it is ready to be processed. |
| 85 }; | 85 }; |
| 86 | 86 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 99 | 99 |
| 100 // Parses the http request (including data - if provided). | 100 // Parses the http request (including data - if provided). |
| 101 // If returns ACCEPTED, then it means that the whole request has been found | 101 // If returns ACCEPTED, then it means that the whole request has been found |
| 102 // in the internal buffer (and parsed). After calling GetRequest(), it will be | 102 // in the internal buffer (and parsed). After calling GetRequest(), it will be |
| 103 // ready to parse another request. | 103 // ready to parse another request. |
| 104 ParseResult ParseRequest(); | 104 ParseResult ParseRequest(); |
| 105 | 105 |
| 106 // Retrieves parsed request. Can be only called, when the parser is in | 106 // Retrieves parsed request. Can be only called, when the parser is in |
| 107 // STATE_ACCEPTED state. After calling it, the parser is ready to parse | 107 // STATE_ACCEPTED state. After calling it, the parser is ready to parse |
| 108 // another request. | 108 // another request. |
| 109 scoped_ptr<HttpRequest> GetRequest(); | 109 std::unique_ptr<HttpRequest> GetRequest(); |
| 110 | 110 |
| 111 private: | 111 private: |
| 112 HttpMethod GetMethodType(const std::string& token) const; | 112 HttpMethod GetMethodType(const std::string& token) const; |
| 113 | 113 |
| 114 // Parses headers and returns ACCEPTED if whole request was parsed. Otherwise | 114 // Parses headers and returns ACCEPTED if whole request was parsed. Otherwise |
| 115 // returns WAITING. | 115 // returns WAITING. |
| 116 ParseResult ParseHeaders(); | 116 ParseResult ParseHeaders(); |
| 117 | 117 |
| 118 // Parses request's content data and returns ACCEPTED if all of it have been | 118 // Parses request's content data and returns ACCEPTED if all of it have been |
| 119 // processed. Chunked Transfer Encoding is supported. | 119 // processed. Chunked Transfer Encoding is supported. |
| 120 ParseResult ParseContent(); | 120 ParseResult ParseContent(); |
| 121 | 121 |
| 122 // Fetches the next line from the buffer. Result does not contain \r\n. | 122 // Fetches the next line from the buffer. Result does not contain \r\n. |
| 123 // Returns an empty string for an empty line. It will assert if there is | 123 // Returns an empty string for an empty line. It will assert if there is |
| 124 // no line available. | 124 // no line available. |
| 125 std::string ShiftLine(); | 125 std::string ShiftLine(); |
| 126 | 126 |
| 127 scoped_ptr<HttpRequest> http_request_; | 127 std::unique_ptr<HttpRequest> http_request_; |
| 128 std::string buffer_; | 128 std::string buffer_; |
| 129 size_t buffer_position_; // Current position in the internal buffer. | 129 size_t buffer_position_; // Current position in the internal buffer. |
| 130 State state_; | 130 State state_; |
| 131 // Content length of the request currently being parsed. | 131 // Content length of the request currently being parsed. |
| 132 size_t declared_content_length_; | 132 size_t declared_content_length_; |
| 133 | 133 |
| 134 scoped_ptr<HttpChunkedDecoder> chunked_decoder_; | 134 std::unique_ptr<HttpChunkedDecoder> chunked_decoder_; |
| 135 | 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(HttpRequestParser); | 136 DISALLOW_COPY_AND_ASSIGN(HttpRequestParser); |
| 137 }; | 137 }; |
| 138 | 138 |
| 139 } // namespace test_server | 139 } // namespace test_server |
| 140 } // namespace net | 140 } // namespace net |
| 141 | 141 |
| 142 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_REQUEST_H_ | 142 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_REQUEST_H_ |
| OLD | NEW |