2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
8 #ifndef BEAST_HTTP_HEADER_PARSER_HPP
9 #define BEAST_HTTP_HEADER_PARSER_HPP
11 #include <beast/config.hpp>
12 #include <beast/http/message.hpp>
13 #include <beast/http/basic_parser.hpp>
15 #include <type_traits>
21 /** A parser for producing HTTP/1 headers.
23 This class uses the basic HTTP/1 wire format parser to convert
24 a series of octets into a @ref header.
26 @note A new instance of the parser is required for each message.
28 @tparam isRequest Indicates whether a request or response
31 @tparam Fields The type of container used to represent the fields.
33 template<bool isRequest, class Fields>
35 : public basic_parser<isRequest, false,
36 header_parser<isRequest, Fields>>
38 header<isRequest, Fields> h_;
41 using mutable_buffers_type =
42 boost::asio::null_buffers;
44 /// The type of @ref header this object produces.
45 using value_type = header<isRequest, Fields>;
48 header_parser(header_parser const&) = default;
51 header_parser& operator=(header_parser const&) = default;
55 After the move, the only valid operation
56 on the moved-from object is destruction.
58 header_parser(header_parser&&) = default;
62 @param args If present, additional arguments to be
63 forwarded to the @ref beast::http::header constructor.
65 template<class... Args>
67 header_parser(Args&&... args);
69 /** Returns the parsed header
71 Only valid if @ref got_header would return `true`.
79 /** Returns the parsed header.
81 Only valid if @ref got_header would return `true`.
89 /** Returns ownership of the parsed header.
91 Ownership is transferred to the caller. Only
92 valid if @ref got_header would return `true`.
95 @ref value_type is @b MoveConstructible
100 static_assert(std::is_move_constructible<decltype(h_)>::value,
101 "MoveConstructible requirements not met");
102 return std::move(h_);
106 friend class basic_parser<
107 isRequest, false, header_parser>;
111 boost::string_ref const& method,
112 boost::string_ref const& path,
113 int version, error_code&)
115 h_.url = std::string{
116 path.data(), path.size()};
117 h_.method = std::string{
118 method.data(), method.size()};
119 h_.version = version;
123 on_response(int status,
124 boost::string_ref const& reason,
125 int version, error_code&)
128 h_.reason = std::string{
129 reason.data(), reason.size()};
130 h_.version = version;
134 on_field(boost::string_ref const& name,
135 boost::string_ref const& value,
138 h_.fields.insert(name, value);
142 on_header(error_code&)
147 on_body(error_code& ec)
152 on_body(std::uint64_t content_length,
158 on_data(boost::string_ref const& s,
164 on_commit(std::size_t n)
166 // Can't write body data with header-only parser!
168 throw std::logic_error{
169 "invalid member function call"};
173 on_chunk(std::uint64_t n,
174 boost::string_ref const& ext,
180 on_complete(error_code&)
188 #include <beast/http/impl/header_parser.ipp>