Consolidate parsers to parser.hpp

fix #405
This commit is contained in:
Vinnie Falco
2017-06-03 08:40:53 -07:00
parent 86ce017ffe
commit 88600beab0
14 changed files with 258 additions and 327 deletions

View File

@ -6,6 +6,7 @@ API Changes:
* Tidy up chunk decorator
* Rename to buffer_cat_view
* Consolidate parsers to parser.hpp
--------------------------------------------------------------------------------

View File

@ -16,9 +16,8 @@
#include <beast/http/empty_body.hpp>
#include <beast/http/error.hpp>
#include <beast/http/fields.hpp>
#include <beast/http/header_parser.hpp>
#include <beast/http/message.hpp>
#include <beast/http/message_parser.hpp>
#include <beast/http/parser.hpp>
#include <beast/http/read.hpp>
#include <beast/http/rfc7230.hpp>
#include <beast/http/serializer.hpp>

View File

@ -1,206 +0,0 @@
//
// 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)
//
#ifndef BEAST_HTTP_HEADER_PARSER_HPP
#define BEAST_HTTP_HEADER_PARSER_HPP
#include <beast/config.hpp>
#include <beast/http/message.hpp>
#include <beast/http/basic_parser.hpp>
#include <boost/throw_exception.hpp>
#include <array>
#include <type_traits>
#include <utility>
namespace beast {
namespace http {
/** A parser for producing HTTP/1 headers.
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
on_response(int status, string_view reason,
int version, error_code&)
{
h_.status = status;
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,
string_view const&, error_code&)
{
body_ = {};
}
void
on_complete(error_code&)
{
body_ = {};
}
};
} // http
} // beast
#include <beast/http/impl/header_parser.ipp>
#endif

View File

@ -1,26 +0,0 @@
//
// 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)
//
#ifndef BEAST_HTTP_IMPL_HEADER_PARSER_IPP
#define BEAST_HTTP_IMPL_HEADER_PARSER_IPP
namespace beast {
namespace http {
template<bool isRequest, class Fields>
template<class Arg0, class... ArgN, class>
header_parser<isRequest, Fields>::
header_parser(Arg0&& arg0, ArgN&&... argn)
: h_(std::forward<Arg0>(arg0),
std::forward<ArgN>(argn)...)
{
}
} // http
} // beast
#endif

View File

@ -5,12 +5,21 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_HTTP_IMPL_MESSAGE_PARSER_IPP
#define BEAST_HTTP_IMPL_MESSAGE_PARSER_IPP
#ifndef BEAST_HTTP_IMPL_PARSER_IPP
#define BEAST_HTTP_IMPL_PARSER_IPP
namespace beast {
namespace http {
template<bool isRequest, class Fields>
template<class Arg0, class... ArgN, class>
header_parser<isRequest, Fields>::
header_parser(Arg0&& arg0, ArgN&&... argn)
: h_(std::forward<Arg0>(arg0),
std::forward<ArgN>(argn)...)
{
}
template<bool isRequest, class Body, class Fields>
template<class Arg1, class... ArgN, class>
message_parser<isRequest, Body, Fields>::

View File

@ -10,7 +10,7 @@
#include <beast/http/type_traits.hpp>
#include <beast/http/error.hpp>
#include <beast/http/message_parser.hpp>
#include <beast/http/parser.hpp>
#include <beast/http/read.hpp>
#include <beast/core/bind_handler.hpp>
#include <beast/core/handler_ptr.hpp>

View File

@ -5,21 +5,200 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_HTTP_MESSAGE_PARSER_HPP
#define BEAST_HTTP_MESSAGE_PARSER_HPP
#ifndef BEAST_HTTP_PARSER_HPP
#define BEAST_HTTP_PARSER_HPP
#include <beast/config.hpp>
#include <beast/http/basic_parser.hpp>
#include <beast/http/message.hpp>
#include <beast/http/header_parser.hpp>
#include <beast/core/detail/clamp.hpp>
#include <beast/http/type_traits.hpp>
#include <boost/optional.hpp>
#include <array>
#include <boost/throw_exception.hpp>
#include <type_traits>
#include <utility>
namespace beast {
namespace http {
/** A parser for producing HTTP/1 headers.
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
on_response(int status, string_view reason,
int version, error_code&)
{
h_.status = status;
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,
string_view const&, error_code&)
{
body_ = {};
}
void
on_complete(error_code&)
{
body_ = {};
}
};
/** A parser for producing HTTP/1 messages.
This class uses the basic HTTP/1 wire format parser to convert
@ -230,6 +409,6 @@ using response_parser = message_parser<false, Body, Fields>;
} // http
} // beast
#include <beast/http/impl/message_parser.ipp>
#include <beast/http/impl/parser.ipp>
#endif

View File

@ -10,8 +10,7 @@
#include <beast/websocket/detail/type_traits.hpp>
#include <beast/http/empty_body.hpp>
#include <beast/http/message.hpp>
#include <beast/http/header_parser.hpp>
#include <beast/http/parser.hpp>
#include <beast/http/read.hpp>
#include <beast/http/string_body.hpp>
#include <beast/http/write.hpp>

View File

@ -47,9 +47,8 @@ unit-test http-tests :
http/dynamic_body.cpp
http/error.cpp
http/fields.cpp
http/header_parser.cpp
http/message.cpp
http/message_parser.cpp
http/parser.cpp
http/read.cpp
http/rfc7230.cpp
http/serializer.cpp

View File

@ -17,9 +17,8 @@ add_executable (http-tests
empty_body.cpp
error.cpp
fields.cpp
header_parser.cpp
message.cpp
message_parser.cpp
parser.cpp
read.cpp
rfc7230.cpp
serializer.cpp

View File

@ -10,7 +10,7 @@
#include <beast/core/ostream.hpp>
#include <beast/http/fields.hpp>
#include <beast/http/message_parser.hpp>
#include <beast/http/parser.hpp>
#include <beast/http/read.hpp>
#include <beast/http/write.hpp>
#include <beast/test/string_istream.hpp>

View File

@ -1,73 +0,0 @@
//
// 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)
//
// Test that header file is self-contained.
#include <beast/http/header_parser.hpp>
#include <beast/core/flat_buffer.hpp>
#include <beast/http/read.hpp>
#include <beast/unit_test/suite.hpp>
#include <beast/test/string_istream.hpp>
#include <beast/test/yield_to.hpp>
namespace beast {
namespace http {
class header_parser_test
: public beast::unit_test::suite
, public test::enable_yield_to
{
public:
static
boost::asio::const_buffers_1
buf(string_view s)
{
return {s.data(), s.size()};
}
void
testParse()
{
{
test::string_istream is{ios_,
"GET / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"\r\n"
};
flat_buffer db{1024};
header_parser<true, fields> p;
read_some(is, db, p);
BEAST_EXPECT(p.is_header_done());
}
{
test::string_istream is{ios_,
"POST / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"Content-Length: 1\r\n"
"\r\n"
"*"
};
flat_buffer db{1024};
header_parser<true, fields> p;
read_some(is, db, p);
BEAST_EXPECT(p.is_header_done());
BEAST_EXPECT(! p.is_done());
}
}
void
run() override
{
testParse();
}
};
BEAST_DEFINE_TESTSUITE(header_parser,http,beast);
} // http
} // beast

View File

@ -6,7 +6,7 @@
//
// Test that header file is self-contained.
#include <beast/http/message_parser.hpp>
#include <beast/http/parser.hpp>
#include "test_parser.hpp"
@ -25,7 +25,58 @@
namespace beast {
namespace http {
class message_parser_test
class header_parser_test
: public beast::unit_test::suite
, public test::enable_yield_to
{
public:
static
boost::asio::const_buffers_1
buf(string_view s)
{
return {s.data(), s.size()};
}
void
testParse()
{
{
test::string_istream is{ios_,
"GET / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"\r\n"
};
flat_buffer db{1024};
header_parser<true, fields> p;
read_some(is, db, p);
BEAST_EXPECT(p.is_header_done());
}
{
test::string_istream is{ios_,
"POST / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"Content-Length: 1\r\n"
"\r\n"
"*"
};
flat_buffer db{1024};
header_parser<true, fields> p;
read_some(is, db, p);
BEAST_EXPECT(p.is_header_done());
BEAST_EXPECT(! p.is_done());
}
}
void
run() override
{
testParse();
}
};
BEAST_DEFINE_TESTSUITE(header_parser,http,beast);
class parser_test
: public beast::unit_test::suite
, public beast::test::enable_yield_to
{
@ -363,7 +414,7 @@ public:
}
};
BEAST_DEFINE_TESTSUITE(message_parser,http,beast);
BEAST_DEFINE_TESTSUITE(parser,http,beast);
} // http
} // beast

View File

@ -14,7 +14,7 @@
#include <beast/core/static_buffer.hpp>
#include <beast/http/fields.hpp>
#include <beast/http/dynamic_body.hpp>
#include <beast/http/header_parser.hpp>
#include <beast/http/parser.hpp>
#include <beast/http/string_body.hpp>
#include <beast/test/fail_stream.hpp>
#include <beast/test/pipe_stream.hpp>