Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / asio / example / cpp11 / http / server / request_parser.hpp
1 //
2 // request_parser.hpp
3 // ~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef HTTP_REQUEST_PARSER_HPP
12 #define HTTP_REQUEST_PARSER_HPP
13
14 #include <tuple>
15
16 namespace http {
17 namespace server {
18
19 struct request;
20
21 /// Parser for incoming requests.
22 class request_parser
23 {
24 public:
25   /// Construct ready to parse the request method.
26   request_parser();
27
28   /// Reset to initial parser state.
29   void reset();
30
31   /// Result of parse.
32   enum result_type { good, bad, indeterminate };
33
34   /// Parse some data. The enum return value is good when a complete request has
35   /// been parsed, bad if the data is invalid, indeterminate when more data is
36   /// required. The InputIterator return value indicates how much of the input
37   /// has been consumed.
38   template <typename InputIterator>
39   std::tuple<result_type, InputIterator> parse(request& req,
40       InputIterator begin, InputIterator end)
41   {
42     while (begin != end)
43     {
44       result_type result = consume(req, *begin++);
45       if (result == good || result == bad)
46         return std::make_tuple(result, begin);
47     }
48     return std::make_tuple(indeterminate, begin);
49   }
50
51 private:
52   /// Handle the next character of input.
53   result_type consume(request& req, char input);
54
55   /// Check if a byte is an HTTP character.
56   static bool is_char(int c);
57
58   /// Check if a byte is an HTTP control character.
59   static bool is_ctl(int c);
60
61   /// Check if a byte is defined as an HTTP tspecial character.
62   static bool is_tspecial(int c);
63
64   /// Check if a byte is a digit.
65   static bool is_digit(int c);
66
67   /// The current state of the parser.
68   enum state
69   {
70     method_start,
71     method,
72     uri,
73     http_version_h,
74     http_version_t_1,
75     http_version_t_2,
76     http_version_p,
77     http_version_slash,
78     http_version_major_start,
79     http_version_major,
80     http_version_minor_start,
81     http_version_minor,
82     expecting_newline_1,
83     header_line_start,
84     header_lws,
85     header_name,
86     space_before_header_value,
87     header_value,
88     expecting_newline_2,
89     expecting_newline_3
90   } state_;
91 };
92
93 } // namespace server
94 } // namespace http
95
96 #endif // HTTP_REQUEST_PARSER_HPP