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 |
| 30 // instead of copying. |
| 31 struct HttpRequest { |
| 32 HttpRequest() : method(UNKNOWN) {} |
| 33 |
| 34 GURL uri; |
| 35 HttpMethod method; |
| 36 std::map<std::string, std::string> headers; |
| 37 std::string content; |
| 38 |
| 39 private: |
| 40 DISALLOW_COPY_AND_ASSIGN(HttpRequest); |
| 41 }; |
| 42 |
| 43 // Parses the input data and produces valid HttpRequest objects. The common |
| 44 // use is as below: |
| 45 // HttpRequestParser parser; |
| 46 // (...) |
| 47 // void OnDataChunkReceived(Socket* socket, const char* data, int size) { |
| 48 // parser.ProcessChunk(data, size); |
| 49 // while (parser.ParseRequest() == HttpRequestParser::ACCEPTED) { |
| 50 // scoped_ptr<HttpRequest> request = parser.GetRequest(); |
| 51 // (... process the request ...) |
| 52 // } |
| 53 class HttpRequestParser { |
| 54 public: |
| 55 // Parsing result. |
| 56 enum ParseResult { |
| 57 WAITING, // A request is not completed yet, waiting for more data. |
| 58 ACCEPTED, // A request has been parsed and it is ready to be processed. |
| 59 }; |
| 60 |
| 61 // Parser state. |
| 62 enum State { |
| 63 STATE_HEADERS, // Waiting for a request headers. |
| 64 STATE_CONTENT, // Waiting for content data. |
| 65 STATE_ACCEPTED, // Request has been parsed. |
| 66 }; |
| 67 |
| 68 HttpRequestParser(); |
| 69 virtual ~HttpRequestParser(); |
| 70 |
| 71 // Adds chunk of data into the internal buffer. |
| 72 virtual void ProcessChunk(const char *data, size_t length); |
| 73 |
| 74 // Parses the http request (including data - if provided). |
| 75 // If returns ACCEPTED, then it means that the whole request has been found |
| 76 // in the internal buffer (and parsed). After calling GetRequest(), it will be |
| 77 // ready to parse another request. |
| 78 virtual ParseResult ParseRequest(); |
| 79 |
| 80 // Retrieves parsed request. Can be only called, when the parser is in |
| 81 // STATE_ACCEPTED state. After calling it, the parser is ready to parse |
| 82 // another request. |
| 83 scoped_ptr<HttpRequest> GetRequest(); |
| 84 |
| 85 private: |
| 86 std::string buffer_; |
| 87 scoped_ptr<HttpRequest> http_request_; |
| 88 State state_; |
| 89 size_t buffer_position_; // Current position in the internal buffer. |
| 90 size_t current_content_length_; |
| 91 |
| 92 virtual HttpMethod GetMethodType(const std::string& token) const; |
| 93 |
| 94 // Parses headers and returns ACCEPTED if whole request was parsed. Otherwise |
| 95 // returns WAITING. |
| 96 virtual ParseResult ParseHeaders(); |
| 97 |
| 98 // Parses request's content data and returns ACCEPTED if all of it have been |
| 99 // processed. Chunked Transfer Encoding *is not* supported. |
| 100 virtual ParseResult ParseContent(); |
| 101 |
| 102 std::string ShiftLine(); |
| 103 }; |
| 104 |
| 105 } // namespace test_servers |
| 106 } // namespace drive |
| 107 |
| 108 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_TEST_SERVERS_HTTP_REQUEST_H_ |
OLD | NEW |