]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | #ifndef BEAST_HTTP_HEADER_PARSER_HPP | |
9 | #define BEAST_HTTP_HEADER_PARSER_HPP | |
10 | ||
11 | #include <beast/config.hpp> | |
12 | #include <beast/http/message.hpp> | |
13 | #include <beast/http/basic_parser.hpp> | |
14 | #include <array> | |
15 | #include <type_traits> | |
16 | #include <utility> | |
17 | ||
18 | namespace beast { | |
19 | namespace http { | |
20 | ||
21 | /** A parser for producing HTTP/1 headers. | |
22 | ||
23 | This class uses the basic HTTP/1 wire format parser to convert | |
24 | a series of octets into a @ref header. | |
25 | ||
26 | @note A new instance of the parser is required for each message. | |
27 | ||
28 | @tparam isRequest Indicates whether a request or response | |
29 | will be parsed. | |
30 | ||
31 | @tparam Fields The type of container used to represent the fields. | |
32 | */ | |
33 | template<bool isRequest, class Fields> | |
34 | class header_parser | |
35 | : public basic_parser<isRequest, false, | |
36 | header_parser<isRequest, Fields>> | |
37 | { | |
38 | header<isRequest, Fields> h_; | |
39 | ||
40 | public: | |
41 | using mutable_buffers_type = | |
42 | boost::asio::null_buffers; | |
43 | ||
44 | /// The type of @ref header this object produces. | |
45 | using value_type = header<isRequest, Fields>; | |
46 | ||
47 | /// Copy constructor. | |
48 | header_parser(header_parser const&) = default; | |
49 | ||
50 | /// Copy assignment. | |
51 | header_parser& operator=(header_parser const&) = default; | |
52 | ||
53 | /** Move constructor. | |
54 | ||
55 | After the move, the only valid operation | |
56 | on the moved-from object is destruction. | |
57 | */ | |
58 | header_parser(header_parser&&) = default; | |
59 | ||
60 | /** Constructor | |
61 | ||
62 | @param args If present, additional arguments to be | |
63 | forwarded to the @ref beast::http::header constructor. | |
64 | */ | |
65 | template<class... Args> | |
66 | explicit | |
67 | header_parser(Args&&... args); | |
68 | ||
69 | /** Returns the parsed header | |
70 | ||
71 | Only valid if @ref got_header would return `true`. | |
72 | */ | |
73 | value_type const& | |
74 | get() const | |
75 | { | |
76 | return h_; | |
77 | } | |
78 | ||
79 | /** Returns the parsed header. | |
80 | ||
81 | Only valid if @ref got_header would return `true`. | |
82 | */ | |
83 | value_type& | |
84 | get() | |
85 | { | |
86 | return h_; | |
87 | } | |
88 | ||
89 | /** Returns ownership of the parsed header. | |
90 | ||
91 | Ownership is transferred to the caller. Only | |
92 | valid if @ref got_header would return `true`. | |
93 | ||
94 | Requires: | |
95 | @ref value_type is @b MoveConstructible | |
96 | */ | |
97 | value_type | |
98 | release() | |
99 | { | |
100 | static_assert(std::is_move_constructible<decltype(h_)>::value, | |
101 | "MoveConstructible requirements not met"); | |
102 | return std::move(h_); | |
103 | } | |
104 | ||
105 | private: | |
106 | friend class basic_parser< | |
107 | isRequest, false, header_parser>; | |
108 | ||
109 | void | |
110 | on_request( | |
111 | boost::string_ref const& method, | |
112 | boost::string_ref const& path, | |
113 | int version, error_code&) | |
114 | { | |
115 | h_.url = std::string{ | |
116 | path.data(), path.size()}; | |
117 | h_.method = std::string{ | |
118 | method.data(), method.size()}; | |
119 | h_.version = version; | |
120 | } | |
121 | ||
122 | void | |
123 | on_response(int status, | |
124 | boost::string_ref const& reason, | |
125 | int version, error_code&) | |
126 | { | |
127 | h_.status = status; | |
128 | h_.reason = std::string{ | |
129 | reason.data(), reason.size()}; | |
130 | h_.version = version; | |
131 | } | |
132 | ||
133 | void | |
134 | on_field(boost::string_ref const& name, | |
135 | boost::string_ref const& value, | |
136 | error_code&) | |
137 | { | |
138 | h_.fields.insert(name, value); | |
139 | } | |
140 | ||
141 | void | |
142 | on_header(error_code&) | |
143 | { | |
144 | } | |
145 | ||
146 | void | |
147 | on_body(error_code& ec) | |
148 | { | |
149 | } | |
150 | ||
151 | void | |
152 | on_body(std::uint64_t content_length, | |
153 | error_code& ec) | |
154 | { | |
155 | } | |
156 | ||
157 | void | |
158 | on_data(boost::string_ref const& s, | |
159 | error_code& ec) | |
160 | { | |
161 | } | |
162 | ||
163 | void | |
164 | on_commit(std::size_t n) | |
165 | { | |
166 | // Can't write body data with header-only parser! | |
167 | BOOST_ASSERT(false); | |
168 | throw std::logic_error{ | |
169 | "invalid member function call"}; | |
170 | } | |
171 | ||
172 | void | |
173 | on_chunk(std::uint64_t n, | |
174 | boost::string_ref const& ext, | |
175 | error_code& ec) | |
176 | { | |
177 | } | |
178 | ||
179 | void | |
180 | on_complete(error_code&) | |
181 | { | |
182 | } | |
183 | }; | |
184 | ||
185 | } // http | |
186 | } // beast | |
187 | ||
188 | #include <beast/http/impl/header_parser.ipp> | |
189 | ||
190 | #endif |