• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace test_server {
17 
18 // Methods of HTTP requests supported by the test HTTP server.
19 enum HttpMethod {
20   METHOD_UNKNOWN,
21   METHOD_GET,
22   METHOD_HEAD,
23   METHOD_POST,
24   METHOD_PUT,
25   METHOD_DELETE,
26   METHOD_PATCH,
27 };
28 
29 // Represents a HTTP request. Since it can be big, use scoped_ptr to pass it
30 // instead of copying. However, the struct is copyable so tests can save and
31 // examine a HTTP request.
32 struct HttpRequest {
33   HttpRequest();
34   ~HttpRequest();
35 
36   std::string relative_url;  // Starts with '/'. Example: "/test?query=foo"
37   HttpMethod method;
38   std::map<std::string, std::string> headers;
39   std::string content;
40   bool has_content;
41 };
42 
43 // Parses the input data and produces a valid HttpRequest object. If there is
44 // more than one request in one chunk, then only the first one will be parsed.
45 // The common use is as below:
46 // HttpRequestParser parser;
47 // (...)
48 // void OnDataChunkReceived(Socket* socket, const char* data, int size) {
49 //   parser.ProcessChunk(std::string(data, size));
50 //   if (parser.ParseRequest() == HttpRequestParser::ACCEPTED) {
51 //     scoped_ptr<HttpRequest> request = parser.GetRequest();
52 //     (... process the request ...)
53 //   }
54 class HttpRequestParser {
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   ~HttpRequestParser();
71 
72   // Adds chunk of data into the internal buffer.
73   void ProcessChunk(const base::StringPiece& data);
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   ParseResult ParseRequest();
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   HttpMethod GetMethodType(const std::string& token) const;
88 
89   // Parses headers and returns ACCEPTED if whole request was parsed. Otherwise
90   // returns WAITING.
91   ParseResult ParseHeaders();
92 
93   // Parses request's content data and returns ACCEPTED if all of it have been
94   // processed. Chunked Transfer Encoding *is not* supported.
95   ParseResult ParseContent();
96 
97   // Fetches the next line from the buffer. Result does not contain \r\n.
98   // Returns an empty string for an empty line. It will assert if there is
99   // no line available.
100   std::string ShiftLine();
101 
102   scoped_ptr<HttpRequest> http_request_;
103   std::string buffer_;
104   size_t buffer_position_;  // Current position in the internal buffer.
105   State state_;
106   // Content length of the request currently being parsed.
107   size_t declared_content_length_;
108 
109   DISALLOW_COPY_AND_ASSIGN(HttpRequestParser);
110 };
111 
112 }  // namespace test_server
113 }  // namespace net
114 
115 #endif  // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_REQUEST_H_
116