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>
|
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 header.
|
2017-06-03 08:40:53 -07:00
|
|
|
|
|
|
|
This class uses the basic HTTP/1 wire format parser to convert
|
|
|
|
a series of octets into a @ref header.
|
|
|
|
|
|
|
|
@note A new instance of the parser is required for each message.
|
|
|
|
|
|
|
|
@tparam isRequest Indicates whether a request or response
|
|
|
|
will be parsed.
|
|
|
|
|
|
|
|
@tparam Fields The type of container used to represent the fields.
|
|
|
|
*/
|
|
|
|
template<bool isRequest, class Fields>
|
|
|
|
class header_parser
|
|
|
|
: public basic_parser<isRequest,
|
|
|
|
header_parser<isRequest, Fields>>
|
|
|
|
{
|
|
|
|
header<isRequest, Fields> h_;
|
|
|
|
string_view body_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// The type of @ref header this object produces.
|
|
|
|
using value_type = header<isRequest, Fields>;
|
|
|
|
|
|
|
|
/// Default constructor.
|
|
|
|
header_parser() = default;
|
|
|
|
|
|
|
|
/// Copy constructor.
|
|
|
|
header_parser(header_parser const&) = default;
|
|
|
|
|
|
|
|
/// Copy assignment.
|
|
|
|
header_parser& operator=(header_parser const&) = default;
|
|
|
|
|
|
|
|
/** Move constructor.
|
|
|
|
|
|
|
|
After the move, the only valid operation
|
|
|
|
on the moved-from object is destruction.
|
|
|
|
*/
|
|
|
|
header_parser(header_parser&&) = default;
|
|
|
|
|
|
|
|
/** Move assignment
|
|
|
|
|
|
|
|
After the move, the only valid operation
|
|
|
|
on the moved-from object is destruction.
|
|
|
|
*/
|
|
|
|
header_parser& operator=(header_parser&&) = default;
|
|
|
|
|
|
|
|
/** Constructor
|
|
|
|
|
|
|
|
@param args Optional arguments forwarded
|
|
|
|
forwarded to the @ref http::header constructor.
|
|
|
|
*/
|
|
|
|
#if BEAST_DOXYGEN
|
|
|
|
template<class... Args>
|
|
|
|
explicit
|
|
|
|
header_parser(Args&&... args);
|
|
|
|
#else
|
|
|
|
template<class Arg0, class... ArgN,
|
|
|
|
class = typename std::enable_if<
|
|
|
|
! std::is_convertible<typename
|
|
|
|
std::decay<Arg0>::type,
|
|
|
|
header_parser>::value>>
|
|
|
|
explicit
|
|
|
|
header_parser(Arg0&& arg0, ArgN&&... argn);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Returns parsed body octets.
|
|
|
|
|
|
|
|
This function will return the most recent buffer
|
|
|
|
of octets corresponding to the parsed body. This
|
|
|
|
buffer will become invalidated on any subsequent
|
|
|
|
call to @ref put or @ref put_eof
|
|
|
|
*/
|
|
|
|
string_view
|
|
|
|
body() const
|
|
|
|
{
|
|
|
|
return body_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the parsed header
|
|
|
|
|
|
|
|
@note The return value is undefined unless
|
|
|
|
@ref is_header_done would return `true`.
|
|
|
|
*/
|
|
|
|
value_type const&
|
|
|
|
get() const
|
|
|
|
{
|
|
|
|
return h_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the parsed header.
|
|
|
|
|
|
|
|
@note The return value is undefined unless
|
|
|
|
@ref is_header_done would return `true`.
|
|
|
|
*/
|
|
|
|
value_type&
|
|
|
|
get()
|
|
|
|
{
|
|
|
|
return h_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns ownership of the parsed header.
|
|
|
|
|
|
|
|
Ownership is transferred to the caller.
|
|
|
|
|
|
|
|
@note The return value is undefined unless
|
|
|
|
@ref is_header_done would return `true`.
|
|
|
|
|
|
|
|
Requires:
|
|
|
|
@ref value_type is @b MoveConstructible
|
|
|
|
*/
|
|
|
|
value_type
|
|
|
|
release()
|
|
|
|
{
|
|
|
|
static_assert(
|
|
|
|
std::is_move_constructible<decltype(h_)>::value,
|
|
|
|
"MoveConstructible requirements not met");
|
|
|
|
return std::move(h_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class basic_parser<isRequest, header_parser>;
|
|
|
|
|
|
|
|
void
|
|
|
|
on_request(string_view method,
|
|
|
|
string_view path, int version, error_code&)
|
|
|
|
{
|
|
|
|
h_.target(path);
|
|
|
|
h_.method(method);
|
|
|
|
h_.version = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-06-04 12:38:42 -07:00
|
|
|
on_response(int code, string_view reason,
|
2017-06-03 08:40:53 -07:00
|
|
|
int version, error_code&)
|
|
|
|
{
|
2017-06-04 12:38:42 -07:00
|
|
|
h_.result(code);
|
2017-06-03 08:40:53 -07:00
|
|
|
h_.version = version;
|
|
|
|
h_.reason(reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
on_field(string_view name,
|
|
|
|
string_view value, error_code&)
|
|
|
|
{
|
|
|
|
h_.fields.insert(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
on_header(error_code&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
on_body(boost::optional<std::
|
|
|
|
uint64_t> const&, error_code&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
on_data(string_view s, error_code&)
|
|
|
|
{
|
|
|
|
body_ = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
on_chunk(std::uint64_t,
|
2017-06-04 10:52:28 -07:00
|
|
|
string_view, error_code&)
|
2017-06-03 08:40:53 -07:00
|
|
|
{
|
|
|
|
body_ = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
on_complete(error_code&)
|
|
|
|
{
|
|
|
|
body_ = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
a series of octets into a @ref message.
|
|
|
|
|
|
|
|
@tparam isRequest Indicates whether a request or response
|
|
|
|
will be parsed.
|
|
|
|
|
|
|
|
@tparam Body The type used to represent the body.
|
|
|
|
|
|
|
|
@tparam Fields The type of container used to represent the fields.
|
|
|
|
|
|
|
|
@note A new instance of the parser is required for each message.
|
|
|
|
*/
|
2017-05-31 08:01:55 -07:00
|
|
|
template<bool isRequest, class Body, class Fields = fields>
|
2017-06-03 08:56:21 -07:00
|
|
|
class parser
|
2016-11-20 07:32:41 -05:00
|
|
|
: public basic_parser<isRequest,
|
2017-06-03 08:56:21 -07:00
|
|
|
parser<isRequest, Body, Fields>>
|
2016-11-20 07:32:41 -05:00
|
|
|
{
|
2017-05-31 08:01:55 -07:00
|
|
|
static_assert(is_body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
|
|
|
|
static_assert(is_body_writer<Body>::value,
|
|
|
|
"BodyWriter requirements not met");
|
|
|
|
|
|
|
|
using base_type = basic_parser<isRequest,
|
2017-06-03 08:56:21 -07:00
|
|
|
parser<isRequest, Body, Fields>>;
|
2016-11-20 07:32:41 -05:00
|
|
|
|
|
|
|
message<isRequest, Body, Fields> m_;
|
2017-06-03 09:45:09 -07:00
|
|
|
boost::optional<typename Body::writer> wr_;
|
2016-11-20 07:32:41 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// The type of message returned by the parser
|
|
|
|
using value_type = message<isRequest, Body, Fields>;
|
|
|
|
|
|
|
|
/// Constructor (default)
|
2017-06-03 08:56:21 -07:00
|
|
|
parser() = default;
|
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-03 08:56:21 -07:00
|
|
|
parser(parser&& other);
|
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
|
2017-06-03 08:56:21 -07:00
|
|
|
@ref http::header_parser or @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
|
|
|
|
msesage_parser(Args&&... args);
|
|
|
|
#else
|
|
|
|
template<class Arg1, class... ArgN,
|
|
|
|
class = typename std::enable_if<
|
|
|
|
! std::is_same<typename
|
|
|
|
std::decay<Arg1>::type,
|
|
|
|
header_parser<isRequest, Fields>>::value &&
|
|
|
|
! std::is_same<typename
|
2017-06-03 08:56:21 -07:00
|
|
|
std::decay<Arg1>::type, parser>::value
|
2016-11-20 07:32:41 -05:00
|
|
|
>::type>
|
|
|
|
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 message parser from a @ref header_parser.
|
|
|
|
@param parser The header parser to construct from.
|
|
|
|
@param args Optional arguments forwarded to the message
|
|
|
|
constructor.
|
|
|
|
*/
|
|
|
|
template<class... Args>
|
|
|
|
explicit
|
2017-06-03 08:56:21 -07:00
|
|
|
parser(header_parser<
|
2016-11-20 07:32:41 -05:00
|
|
|
isRequest, Fields>&& parser, Args&&... args);
|
|
|
|
|
|
|
|
/** Returns the parsed message.
|
|
|
|
|
|
|
|
Depending on the progress of the parser, portions
|
|
|
|
of this object may be incomplete.
|
|
|
|
*/
|
|
|
|
value_type const&
|
|
|
|
get() const
|
|
|
|
{
|
|
|
|
return m_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the parsed message.
|
|
|
|
|
|
|
|
Depending on the progress of the parser, portions
|
|
|
|
of this object may be incomplete.
|
|
|
|
*/
|
|
|
|
value_type&
|
|
|
|
get()
|
|
|
|
{
|
|
|
|
return m_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns ownership of the parsed message.
|
|
|
|
|
|
|
|
Ownership is transferred to the caller.
|
|
|
|
Depending on the progress of the parser, portions
|
|
|
|
of this object may be incomplete.
|
|
|
|
|
|
|
|
@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_);
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2017-06-04 10:52:28 -07:00
|
|
|
string_view method,
|
|
|
|
string_view target,
|
2016-11-20 07:32:41 -05:00
|
|
|
int version, error_code&)
|
|
|
|
{
|
2017-05-02 15:49:22 -07:00
|
|
|
m_.target(target);
|
|
|
|
m_.method(method);
|
2016-11-20 07:32:41 -05:00
|
|
|
m_.version = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-06-04 12:38:42 -07:00
|
|
|
on_response(int code,
|
2017-06-04 10:52:28 -07:00
|
|
|
string_view reason,
|
2016-11-20 07:32:41 -05:00
|
|
|
int version, error_code&)
|
|
|
|
{
|
2017-06-04 12:38:42 -07:00
|
|
|
m_.result(code);
|
2016-11-20 07:32:41 -05:00
|
|
|
m_.version = version;
|
2017-05-02 15:49:22 -07:00
|
|
|
m_.reason(reason);
|
2016-11-20 07:32:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-06-04 10:52:28 -07:00
|
|
|
on_field(string_view name,
|
|
|
|
string_view value,
|
2016-11-20 07:32:41 -05:00
|
|
|
error_code&)
|
|
|
|
{
|
|
|
|
m_.fields.insert(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
on_header(error_code& ec)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-05-31 08:01:55 -07:00
|
|
|
on_body(boost::optional<
|
|
|
|
std::uint64_t> const& content_length,
|
|
|
|
error_code& ec)
|
2016-11-20 07:32:41 -05:00
|
|
|
{
|
2017-05-28 09:05:29 -07:00
|
|
|
wr_.emplace(m_);
|
|
|
|
wr_->init(content_length, ec);
|
2016-11-20 07:32:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-06-04 10:52:28 -07:00
|
|
|
on_data(string_view s,
|
2016-11-20 07:32:41 -05:00
|
|
|
error_code& ec)
|
|
|
|
{
|
2017-05-31 08:01:55 -07:00
|
|
|
wr_->put(boost::asio::buffer(
|
|
|
|
s.data(), s.size()), ec);
|
2016-11-20 07:32:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-05-31 08:01:55 -07:00
|
|
|
on_chunk(
|
2017-06-04 10:52:28 -07:00
|
|
|
std::uint64_t, string_view,
|
2016-11-20 07:32:41 -05:00
|
|
|
error_code&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
on_complete(error_code& ec)
|
|
|
|
{
|
2017-05-28 09:05:29 -07:00
|
|
|
if(wr_)
|
2017-05-31 08:01:55 -07:00
|
|
|
wr_->finish(ec);
|
2016-11-20 07:32:41 -05:00
|
|
|
}
|
2017-05-31 08:01:55 -07:00
|
|
|
};
|
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-05-31 08:01:55 -07:00
|
|
|
template<class Body, class Fields = fields>
|
2017-06-03 08:56:21 -07:00
|
|
|
using request_parser = parser<true, Body, Fields>;
|
2016-11-20 07:32:41 -05:00
|
|
|
|
2017-06-03 08:56:21 -07:00
|
|
|
/// An HTTP/1 parser for producing a response message.
|
2017-05-31 08:01:55 -07:00
|
|
|
template<class Body, class Fields = fields>
|
2017-06-03 08:56:21 -07:00
|
|
|
using response_parser = parser<false, Body, Fields>;
|
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
|