Files
boost_beast/include/beast/http/parser.hpp
T

323 lines
8.2 KiB
C++
Raw Normal View History

2016-11-20 07:32:41 -05:00
//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2017-06-03 08:40:53 -07:00
#ifndef BEAST_HTTP_PARSER_HPP
#define BEAST_HTTP_PARSER_HPP
2016-11-20 07:32:41 -05:00
#include <beast/config.hpp>
2017-06-03 08:40:53 -07:00
#include <beast/http/basic_parser.hpp>
2016-11-20 07:32:41 -05:00
#include <beast/http/message.hpp>
2017-06-03 08:40:53 -07:00
#include <beast/http/type_traits.hpp>
2016-11-20 07:32:41 -05:00
#include <boost/optional.hpp>
2017-06-03 08:40:53 -07:00
#include <boost/throw_exception.hpp>
2017-06-28 04:01:33 -07:00
#include <functional>
2017-06-19 11:54:53 -07:00
#include <memory>
2016-11-20 07:32:41 -05:00
#include <type_traits>
#include <utility>
namespace beast {
namespace http {
2017-06-03 08:56:21 -07:00
/** An HTTP/1 parser for producing a message.
2016-11-20 07:32:41 -05:00
This class uses the basic HTTP/1 wire format parser to convert
2017-06-19 11:54:53 -07:00
a series of octets into a @ref message using the @ref basic_fields
container to represent the fields.
2016-11-20 07:32:41 -05:00
@tparam isRequest Indicates whether a request or response
will be parsed.
2017-06-19 11:54:53 -07:00
@tparam Body The type used to represent the body. This must
meet the requirements of @b Body.
2016-11-20 07:32:41 -05:00
2017-06-19 11:54:53 -07:00
@tparam Allocator The type of allocator used with the
@ref basic_fields container.
2016-11-20 07:32:41 -05:00
@note A new instance of the parser is required for each message.
*/
2017-06-05 07:20:58 -07:00
template<
bool isRequest,
class Body,
2017-06-19 11:54:53 -07:00
class Allocator = std::allocator<char>>
2017-06-03 08:56:21 -07:00
class parser
2016-11-20 07:32:41 -05:00
: public basic_parser<isRequest,
2017-06-19 11:54:53 -07:00
parser<isRequest, Body, Allocator>>
2016-11-20 07:32:41 -05:00
{
static_assert(is_body<Body>::value,
"Body requirements not met");
static_assert(is_body_writer<Body>::value,
"BodyWriter requirements not met");
template<bool, class, class>
friend class parser;
using base_type = basic_parser<isRequest,
2017-06-19 11:54:53 -07:00
parser<isRequest, Body, Allocator>>;
2016-11-20 07:32:41 -05:00
2017-06-19 11:54:53 -07:00
message<isRequest, Body, basic_fields<Allocator>> m_;
typename Body::writer wr_;
2017-06-28 04:01:33 -07:00
std::function<void(parser&, error_code&)> cb_;
bool wr_inited_ = false;
2016-11-20 07:32:41 -05:00
public:
/// The type of message returned by the parser
2017-06-19 11:54:53 -07:00
using value_type =
message<isRequest, Body, basic_fields<Allocator>>;
2016-11-20 07:32:41 -05:00
/// Constructor
parser();
2016-11-20 07:32:41 -05:00
/// Copy constructor (disallowed)
2017-06-03 08:56:21 -07:00
parser(parser const&) = delete;
2016-11-20 07:32:41 -05:00
/// Copy assignment (disallowed)
2017-06-03 08:56:21 -07:00
parser& operator=(parser const&) = delete;
2016-11-20 07:32:41 -05:00
/** Move constructor.
After the move, the only valid operation
on the moved-from object is destruction.
*/
2017-06-05 07:20:58 -07:00
parser(parser&& other) = default;
2016-11-20 07:32:41 -05:00
/** Constructor
@param args Optional arguments forwarded to the
@ref http::header constructor.
@note This function participates in overload
resolution only if the first argument is not a
@ref parser.
2016-11-20 07:32:41 -05:00
*/
2017-05-01 08:52:02 -07:00
#if BEAST_DOXYGEN
2016-11-20 07:32:41 -05:00
template<class... Args>
explicit
2017-06-28 18:30:59 -07:00
parser(Args&&... args);
2016-11-20 07:32:41 -05:00
#else
template<class Arg1, class... ArgN,
class = typename std::enable_if<
! detail::is_parser<typename
std::decay<Arg1>::type>::value>::type>
2016-11-20 07:32:41 -05:00
explicit
2017-06-03 08:56:21 -07:00
parser(Arg1&& arg1, ArgN&&... argn);
2016-11-20 07:32:41 -05:00
#endif
/** Construct a parser from another parser, changing the Body type.
This constructs a new parser by move constructing the
header from another parser with a different body type. The
constructed-from parser must not have any parsed body octets or
initialized @b BodyWriter, otherwise an exception is generated.
@par Example
@code
// Deferred body type commitment
request_parser<empty_body> req0;
...
request_parser<string_body> req{std::move(req0)};
@endcode
If an exception is thrown, the state of the constructed-from
parser is undefined.
@param parser The other parser to construct from. After
this call returns, the constructed-from parser may only
be destroyed.
@param args Optional arguments forwarded to the message
constructor.
@throws std::invalid_argument Thrown when the constructed-from
parser has already initialized a body writer.
2017-06-19 11:54:53 -07:00
@note This function participates in overload resolution only
if the other parser uses a different body type.
*/
#if BEAST_DOXYGEN
template<class OtherBody, class... Args>
#else
2017-06-05 07:20:58 -07:00
template<class OtherBody, class... Args,
class = typename std::enable_if<
! std::is_same<Body, OtherBody>::value>::type>
#endif
explicit
2017-06-19 11:54:53 -07:00
parser(parser<isRequest, OtherBody,
Allocator>&& parser, Args&&... args);
2016-11-20 07:32:41 -05:00
/** Returns the parsed message.
2017-06-19 11:54:53 -07:00
Depending on the parser's progress,
parts of this object may be incomplete.
2016-11-20 07:32:41 -05:00
*/
value_type const&
get() const
{
return m_;
}
/** Returns the parsed message.
2017-06-19 11:54:53 -07:00
Depending on the parser's progress,
parts of this object may be incomplete.
2016-11-20 07:32:41 -05:00
*/
value_type&
get()
{
return m_;
}
/** Returns ownership of the parsed message.
Ownership is transferred to the caller.
2017-06-19 11:54:53 -07:00
Depending on the parser's progress,
parts of this object may be incomplete.
2016-11-20 07:32:41 -05:00
@par Requires
@ref value_type is @b MoveConstructible
*/
value_type
release()
{
static_assert(std::is_move_constructible<decltype(m_)>::value,
"MoveConstructible requirements not met");
return std::move(m_);
}
2017-06-28 04:01:33 -07:00
/** Set the on_header callback.
2017-06-28 18:30:59 -07:00
When the callback is set, it is called after the parser
receives a complete header. The function must be invocable with
2017-06-28 04:01:33 -07:00
this signature:
@code
void callback(
parser<isRequest, Body, Fields>& p, // `*this`
error_code& ec) // Set to the error, if any
@endcode
The callback will ensure that `!ec` is `true` if there was
no error or set to the appropriate error code if there was one.
The callback may not call @ref put or @ref put_eof, or
else the behavior is undefined.
*/
void
on_header(std::function<void(parser&, error_code&)> cb)
{
2017-06-28 18:30:59 -07:00
cb_ = std::move(cb);
2017-06-28 04:01:33 -07:00
}
2016-11-20 07:32:41 -05:00
private:
2017-06-03 09:45:09 -07:00
friend class basic_parser<isRequest, parser>;
2016-11-20 07:32:41 -05:00
void
on_request(verb method, string_view method_str,
2017-06-13 07:08:52 -07:00
string_view target, int version, error_code& ec)
2016-11-20 07:32:41 -05:00
{
2017-06-23 02:22:15 -07:00
try
{
m_.target(target);
if(method != verb::unknown)
m_.method(method);
else
m_.method_string(method_str);
ec.assign(0, ec.category());
}
catch(std::bad_alloc const&)
{
ec = error::bad_alloc;
}
2016-11-20 07:32:41 -05:00
m_.version = version;
}
void
on_response(int code,
2017-06-04 10:52:28 -07:00
string_view reason,
2017-06-13 07:08:52 -07:00
int version, error_code& ec)
2016-11-20 07:32:41 -05:00
{
m_.result(code);
2016-11-20 07:32:41 -05:00
m_.version = version;
2017-06-23 02:22:15 -07:00
try
{
m_.reason(reason);
ec.assign(0, ec.category());
}
catch(std::bad_alloc const&)
{
ec = error::bad_alloc;
}
2016-11-20 07:32:41 -05:00
}
void
2017-06-10 15:05:32 -07:00
on_field(field name, string_view name_string,
2017-06-13 07:08:52 -07:00
string_view value, error_code& ec)
2016-11-20 07:32:41 -05:00
{
2017-06-23 02:22:15 -07:00
try
{
m_.insert(name, name_string, value);
ec.assign(0, ec.category());
}
catch(std::bad_alloc const&)
{
ec = error::bad_alloc;
}
2016-11-20 07:32:41 -05:00
}
void
on_header(error_code& ec)
{
2017-06-28 04:01:33 -07:00
if(cb_)
cb_(*this, ec);
else
ec.assign(0, ec.category());
2016-11-20 07:32:41 -05:00
}
void
on_body(boost::optional<
std::uint64_t> const& content_length,
error_code& ec)
2016-11-20 07:32:41 -05:00
{
wr_.init(content_length, ec);
wr_inited_ = true;
2016-11-20 07:32:41 -05:00
}
std::size_t
2017-06-06 17:26:11 -07:00
on_data(string_view s, error_code& ec)
2016-11-20 07:32:41 -05:00
{
return wr_.put(boost::asio::buffer(
s.data(), s.size()), ec);
2016-11-20 07:32:41 -05:00
}
void
2017-06-06 17:26:11 -07:00
on_chunk(std::uint64_t,
2017-06-13 07:08:52 -07:00
string_view, error_code& ec)
2016-11-20 07:32:41 -05:00
{
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
2016-11-20 07:32:41 -05:00
}
void
on_complete(error_code& ec)
{
wr_.finish(ec);
2016-11-20 07:32:41 -05:00
}
};
2017-06-03 08:56:21 -07:00
/// An HTTP/1 parser for producing a request message.
2017-06-19 11:54:53 -07:00
template<class Body, class Allocator = std::allocator<char>>
using request_parser = parser<true, Body, Allocator>;
2017-06-03 08:56:21 -07:00
/// An HTTP/1 parser for producing a response message.
2017-06-19 11:54:53 -07:00
template<class Body, class Allocator = std::allocator<char>>
using response_parser = parser<false, Body, Allocator>;
2016-11-20 07:32:41 -05:00
} // http
} // beast
2017-06-03 08:40:53 -07:00
#include <beast/http/impl/parser.ipp>
2016-11-20 07:32:41 -05:00
#endif