forked from boostorg/beast
New classes are introduced to represent HTTP messages and their associated bodies. The parser interface is reworked to use CRTP, error codes, and trait checks. New classes: * basic_headers Models field/value pairs in a HTTP message. * message Models a HTTP message, body behavior defined by template argument. Parsed message carries metadata generated during parsing. * parser Produces parsed messages. * empty_body, string_body, basic_streambuf_body Classes used to represent content bodies in various ways. New functions: * read, async_read, write, async_write Read and write HTTP messages on a socket. New concepts: * Body: Represents the HTTP Content-Body. * Field: A HTTP header field. * FieldSequence: A forward sequence of fields. * Reader: Parses a Body from a stream of bytes. * Writer: Serializes a Body to buffers. basic_parser changes: * add write methods which throw exceptions instead * error_code passed via parameter instead of return value * fold private member calls into existing callbacks * basic_parser uses CRTP instead of virtual members * add documentation on Derived requirements for CRTP impl/http-parser changes: * joyent renamed to nodejs to reflect upstream changes
187 lines
5.1 KiB
C++
187 lines
5.1 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of Beast: https://github.com/vinniefalco/Beast
|
|
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#include <BeastConfig.h>
|
|
#include <beast/unit_test/suite.h>
|
|
#include <beast/unit_test/thread.h>
|
|
#include <beast/asio/buffers_debug.h>
|
|
#include <beast/asio/placeholders.h>
|
|
#include <beast/asio/streambuf.h>
|
|
#include <beast/http.h>
|
|
#include <boost/asio.hpp>
|
|
|
|
namespace beast {
|
|
namespace http {
|
|
namespace test {
|
|
|
|
class sync_echo_http_server
|
|
{
|
|
public:
|
|
using error_code = boost::system::error_code;
|
|
using endpoint_type = boost::asio::ip::tcp::endpoint;
|
|
using address_type = boost::asio::ip::address;
|
|
using socket_type = boost::asio::ip::tcp::socket;
|
|
|
|
private:
|
|
unit_test::suite& suite_;
|
|
boost::asio::io_service ios_;
|
|
socket_type sock_;
|
|
boost::asio::ip::tcp::acceptor acceptor_;
|
|
unit_test::thread thread_;
|
|
|
|
public:
|
|
sync_echo_http_server(
|
|
endpoint_type ep, unit_test::suite& suite)
|
|
: suite_(suite)
|
|
, sock_(ios_)
|
|
, acceptor_(ios_)
|
|
{
|
|
error_code ec;
|
|
acceptor_.open(ep.protocol(), ec);
|
|
maybe_throw(ec, "open");
|
|
acceptor_.bind(ep, ec);
|
|
maybe_throw(ec, "bind");
|
|
acceptor_.listen(
|
|
boost::asio::socket_base::max_connections, ec);
|
|
maybe_throw(ec, "listen");
|
|
acceptor_.async_accept(sock_,
|
|
std::bind(&sync_echo_http_server::on_accept, this,
|
|
beast::asio::placeholders::error));
|
|
thread_ = unit_test::thread(suite_,
|
|
[&]
|
|
{
|
|
ios_.run();
|
|
});
|
|
}
|
|
|
|
~sync_echo_http_server()
|
|
{
|
|
error_code ec;
|
|
ios_.dispatch(
|
|
[&]{ acceptor_.close(ec); });
|
|
thread_.join();
|
|
}
|
|
|
|
private:
|
|
void
|
|
fail(error_code ec, std::string what)
|
|
{
|
|
suite_.log <<
|
|
what << ": " << ec.message();
|
|
}
|
|
|
|
void
|
|
maybe_throw(error_code ec, std::string what)
|
|
{
|
|
if(ec &&
|
|
ec != boost::asio::error::operation_aborted)
|
|
{
|
|
fail(ec, what);
|
|
throw ec;
|
|
}
|
|
}
|
|
|
|
void
|
|
on_accept(error_code ec)
|
|
{
|
|
if(ec == boost::asio::error::operation_aborted)
|
|
return;
|
|
maybe_throw(ec, "accept");
|
|
std::thread{&sync_echo_http_server::do_client, this,
|
|
std::move(sock_), boost::asio::io_service::work{
|
|
sock_.get_io_service()}}.detach();
|
|
acceptor_.async_accept(sock_,
|
|
std::bind(&sync_echo_http_server::on_accept, this,
|
|
beast::asio::placeholders::error));
|
|
}
|
|
|
|
void
|
|
do_client(socket_type sock, boost::asio::io_service::work)
|
|
{
|
|
error_code ec;
|
|
streambuf rb;
|
|
for(;;)
|
|
{
|
|
request<string_body> req;
|
|
read(sock, rb, req, ec);
|
|
if(ec)
|
|
break;
|
|
response<string_body> resp(
|
|
{100, "OK", req.version});
|
|
resp.body = "Completed successfully.";
|
|
write(sock, resp, ec);
|
|
if(ec)
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
class http_message_test : public unit_test::suite
|
|
{
|
|
public:
|
|
using error_code = boost::system::error_code;
|
|
using endpoint_type = boost::asio::ip::tcp::endpoint;
|
|
using address_type = boost::asio::ip::address;
|
|
using socket_type = boost::asio::ip::tcp::socket;
|
|
|
|
void
|
|
syncEcho(endpoint_type ep)
|
|
{
|
|
boost::asio::io_service ios;
|
|
socket_type sock(ios);
|
|
sock.connect(ep);
|
|
|
|
streambuf rb;
|
|
{
|
|
request<string_body> req(
|
|
{beast::http::method_t::http_get, "/", 11});
|
|
req.body = "Beast.HTTP";
|
|
req.headers.replace("Host",
|
|
ep.address().to_string() + ":" +
|
|
std::to_string(ep.port()));
|
|
write(sock, req);
|
|
}
|
|
{
|
|
response<string_body> m;
|
|
read(sock, rb, m);
|
|
}
|
|
}
|
|
|
|
void
|
|
testAsio()
|
|
{
|
|
endpoint_type ep{
|
|
address_type::from_string("127.0.0.1"), 6000};
|
|
sync_echo_http_server s(ep, *this);
|
|
syncEcho(ep);
|
|
}
|
|
|
|
void run() override
|
|
{
|
|
testAsio();
|
|
pass();
|
|
}
|
|
};
|
|
|
|
BEAST_DEFINE_TESTSUITE_MANUAL(http_message,http,beast);
|
|
|
|
} // test
|
|
} // http
|
|
} // beast
|