Add headers_parser:

This allows just the HTTP headers to be parsed, and
the choice of body to be deferred to a subsequent
call to parse.
This commit is contained in:
Vinnie Falco committed 2016-10-20 17:22:39 -04:00
1 parent 2f90627dd6
commit d107ba1c1d
10 files changed
+522 -56

No files matched your search

+1
View File
@@ -48,6 +48,7 @@ unit-test http-tests :
http/concepts.cpp
http/empty_body.cpp
http/headers.cpp
http/headers_parser_v1.cpp
http/message.cpp
http/parse.cpp
http/parse_error.cpp
+1
View File
@@ -17,6 +17,7 @@ add_executable (http-tests
concepts.cpp
empty_body.cpp
headers.cpp
headers_parser_v1.cpp
message.cpp
parse.cpp
parse_error.cpp
+90
View File
@@ -0,0 +1,90 @@
//
// Copyright (c) 2013-2016 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/headers_parser_v1.hpp>
#include <beast/http/headers.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
namespace beast {
namespace http {
class headers_parser_v1_test : public beast::unit_test::suite
{
public:
void testParser()
{
{
error_code ec;
headers_parser_v1<true, headers> p;
BEAST_EXPECT(! p.complete());
auto const n = p.write(boost::asio::buffer(
"GET / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"\r\n"
), ec);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.complete());
BEAST_EXPECT(n == 36);
}
{
error_code ec;
headers_parser_v1<true, headers> p;
BEAST_EXPECT(! p.complete());
auto const n = p.write(boost::asio::buffer(
"GET / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"Content-Length: 5\r\n"
"\r\n"
"*****"
), ec);
BEAST_EXPECT(n == 55);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.complete());
}
{
error_code ec;
headers_parser_v1<false, headers> p;
BEAST_EXPECT(! p.complete());
auto const n = p.write(boost::asio::buffer(
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"\r\n"
), ec);
BEAST_EXPECT(n == 33);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.complete());
}
{
error_code ec;
headers_parser_v1<false, headers> p;
BEAST_EXPECT(! p.complete());
auto const n = p.write(boost::asio::buffer(
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"Content-Length: 5\r\n"
"\r\n"
"*****"
), ec);
BEAST_EXPECT(n == 52);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.complete());
}
}
void run() override
{
testParser();
}
};
BEAST_DEFINE_TESTSUITE(headers_parser_v1,http,beast);
} // http
} // beast
+38 -1
View File
@@ -8,14 +8,21 @@
// Test that header file is self-contained.
#include <beast/http/parser_v1.hpp>
#include <beast/core/streambuf.hpp>
#include <beast/http/headers.hpp>
#include <beast/http/headers_parser_v1.hpp>
#include <beast/http/parse.hpp>
#include <beast/http/string_body.hpp>
#include <beast/test/string_stream.hpp>
#include <beast/test/yield_to.hpp>
#include <beast/unit_test/suite.hpp>
namespace beast {
namespace http {
class parser_v1_test : public beast::unit_test::suite
class parser_v1_test
: public beast::unit_test::suite
, public test::enable_yield_to
{
public:
void testRegressions()
@@ -46,6 +53,35 @@ public:
}
}
void testWithBody()
{
test::string_stream ss{ios_,
"GET / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"Content-Length: 1\r\n"
"\r\n"
"*"};
streambuf rb;
headers_parser_v1<true, headers> p0;
parse(ss, rb, p0);
request_headers<headers> const& reqh = p0.get();
BEAST_EXPECT(reqh.method == "GET");
BEAST_EXPECT(reqh.url == "/");
BEAST_EXPECT(reqh.version == 11);
BEAST_EXPECT(reqh.headers["User-Agent"] == "test");
BEAST_EXPECT(reqh.headers["Content-Length"] == "1");
parser_v1<true, string_body, headers> p =
with_body<string_body>(p0);
BEAST_EXPECT(p.get().method == "GET");
BEAST_EXPECT(p.get().url == "/");
BEAST_EXPECT(p.get().version == 11);
BEAST_EXPECT(p.get().headers["User-Agent"] == "test");
BEAST_EXPECT(p.get().headers["Content-Length"] == "1");
parse(ss, rb, p);
request<string_body, headers> req = p.release();
BEAST_EXPECT(req.body == "*");
}
void run() override
{
using boost::asio::buffer;
@@ -104,6 +140,7 @@ public:
}
testRegressions();
testWithBody();
}
};