New constructors for message:

The message class now behaves like a pair with respect to the construction
of the body and headers. Additional constructors allow construction of
just the body portion from a tuple, leaving the headers default
constructed.

Previous constructors are removed as they were a notational convenience
for assembling HTTP/1 requests and responses. They are not necessary
as this library aims at library writers and not end users.
This commit is contained in:
Vinnie Falco
2016-05-07 15:18:22 -04:00
parent e0956c36c1
commit 2b69831f49
13 changed files with 334 additions and 144 deletions

View File

@@ -16,28 +16,6 @@
namespace beast {
namespace http {
template<bool isRequest, class Body, class Headers>
message_v1<isRequest, Body, Headers>::
message_v1(request_params params)
{
static_assert(isRequest, "message is not a request");
this->method = params.method;
this->url = std::move(params.url);
version = params.version;
}
template<bool isRequest, class Body, class Headers>
message_v1<isRequest, Body, Headers>::
message_v1(response_params params)
{
static_assert(! isRequest, "message is not a response");
this->status = params.status;
this->reason = std::move(params.reason);
version = params.version;
}
//------------------------------------------------------------------------------
template<bool isRequest, class Body, class Headers>
bool
is_keep_alive(message_v1<isRequest, Body, Headers> const& msg)

View File

@@ -9,8 +9,11 @@
#define BEAST_HTTP_MESSAGE_HPP
#include <beast/http/basic_headers.hpp>
#include <beast/core/detail/integer_sequence.hpp>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
namespace beast {
namespace http {
@@ -69,6 +72,79 @@ struct message
/// A container representing the body.
typename Body::value_type body;
/// Default constructor
message() = default;
/** Construct a message.
@param u An argument forwarded to the body constructor.
*/
template<class U>
explicit
message(U&& u)
: body(std::forward<U>(u))
{
}
/** Construct a message.
@param u An argument forwarded to the body constructor.
@param v An argument forwarded to the headers constructor.
*/
template<class U, class V>
explicit
message(U&& u, V&& v)
: headers(std::forward<V>(v))
, body(std::forward<U>(u))
{
}
/** Construct a message.
@param un A tuple forwarded as a parameter pack to the body constructor.
*/
template<class... Un>
message(std::piecewise_construct_t, std::tuple<Un...> un)
: message(std::piecewise_construct, un,
beast::detail::make_index_sequence<sizeof...(Un)>{})
{
}
/** Construct a message.
@param un A tuple forwarded as a parameter pack to the body constructor.
@param vn A tuple forwarded as a parameter pack to the headers constructor.
*/
template<class... Un, class... Vn>
explicit
message(std::piecewise_construct_t,
std::tuple<Un...>&& un, std::tuple<Vn...>&& vn)
: message(std::piecewise_construct, un, vn,
beast::detail::make_index_sequence<sizeof...(Un)>{},
beast::detail::make_index_sequence<sizeof...(Vn)>{})
{
}
private:
template<class... Un, size_t... IUn>
message(std::piecewise_construct_t,
std::tuple<Un...>& tu, beast::detail::index_sequence<IUn...>)
: body(std::forward<Un>(std::get<IUn>(tu))...)
{
}
template<class... Un, class... Vn,
std::size_t... IUn, std::size_t... IVn>
message(std::piecewise_construct_t,
std::tuple<Un...>& tu, std::tuple<Vn...>& tv,
beast::detail::index_sequence<IUn...>,
beast::detail::index_sequence<IVn...>)
: headers(std::forward<Vn>(std::get<IVn>(tv))...)
, body(std::forward<Un>(std::get<IUn>(tu))...)
{
}
};
#if ! GENERATING_DOCS

View File

@@ -15,24 +15,6 @@
namespace beast {
namespace http {
#if ! GENERATING_DOCS
struct request_params
{
std::string method;
std::string url;
int version;
};
struct response_params
{
int status;
std::string reason;
int version;
};
#endif
/** A HTTP/1 message.
A message can be a request or response, depending on the `isRequest`
@@ -54,15 +36,18 @@ struct message_v1 : message<isRequest, Body, Headers>
/// HTTP/1 version (10 or 11)
int version;
/// Default constructor
message_v1() = default;
/// Construct a HTTP/1 request.
/// Constructor
template<class Arg1, class... Argn>
explicit
message_v1(request_params params);
/// Construct a HTTP/1 response.
explicit
message_v1(response_params params);
message_v1(Arg1& arg1, Argn&&... argn)
: message<isRequest, Body, Headers>(
std::forward<Arg1>(arg1),
std::forward<Argn>(argn)...)
{
}
};
#if ! GENERATING_DOCS

View File

@@ -291,9 +291,9 @@ accept(http::request_v1<Body, Headers> const& req,
{
static_assert(is_SyncStream<next_layer_type>::value,
"SyncStream requirements not met");
auto const resp = build_response(req);
http::write(stream_, resp, ec);
if(resp.status != 101)
auto const res = build_response(req);
http::write(stream_, res, ec);
if(res.status != 101)
{
ec = error::handshake_failed;
// VFALCO TODO Respect keep alive setting, perform
@@ -349,11 +349,11 @@ handshake(boost::string_ref const& host,
build_request(host, resource, key), ec);
if(ec)
return;
http::response_v1<http::string_body> resp;
http::read(next_layer(), stream_.buffer(), resp, ec);
http::response_v1<http::string_body> res;
http::read(next_layer(), stream_.buffer(), res, ec);
if(ec)
return;
do_response(resp, key, ec);
do_response(res, key, ec);
}
template<class NextLayer>
@@ -855,11 +855,13 @@ build_response(http::request_v1<Body, Headers> const& req)
auto err =
[&](std::string const& text)
{
http::response_v1<http::string_body> resp(
{400, http::reason_string(400), req.version});
resp.body = text;
http::response_v1<http::string_body> res;
res.status = 400;
res.reason = http::reason_string(res.status);
res.version = req.version;
res.body = text;
// VFALCO TODO respect keep-alive here
return resp;
return res;
};
if(req.version < 11)
return err("HTTP version 1.1 required");
@@ -882,41 +884,43 @@ build_response(http::request_v1<Body, Headers> const& req)
if(! rfc2616::token_in_list(
req.headers["Upgrade"], "websocket"))
return err("Missing websocket Upgrade token");
http::response_v1<http::string_body> resp(
{101, http::reason_string(101), req.version});
resp.headers.insert("Upgrade", "websocket");
http::response_v1<http::string_body> res;
res.status = 101;
res.reason = http::reason_string(res.status);
res.version = req.version;
res.headers.insert("Upgrade", "websocket");
{
auto const key =
req.headers["Sec-WebSocket-Key"];
resp.headers.insert("Sec-WebSocket-Key", key);
resp.headers.insert("Sec-WebSocket-Accept",
res.headers.insert("Sec-WebSocket-Key", key);
res.headers.insert("Sec-WebSocket-Accept",
detail::make_sec_ws_accept(key));
}
resp.headers.replace("Server", "Beast.WSProto");
(*d_)(resp);
http::prepare(resp, http::connection::upgrade);
return resp;
res.headers.replace("Server", "Beast.WSProto");
(*d_)(res);
http::prepare(res, http::connection::upgrade);
return res;
}
template<class NextLayer>
template<class Body, class Headers>
void
stream<NextLayer>::
do_response(http::response_v1<Body, Headers> const& resp,
do_response(http::response_v1<Body, Headers> const& res,
boost::string_ref const& key, error_code& ec)
{
// VFALCO Review these error codes
auto fail = [&]{ ec = error::response_failed; };
if(resp.status != 101)
if(res.status != 101)
return fail();
if(! is_upgrade(resp))
if(! is_upgrade(res))
return fail();
if(! rfc2616::ci_equal(
resp.headers["Upgrade"], "websocket"))
res.headers["Upgrade"], "websocket"))
return fail();
if(! resp.headers.exists("Sec-WebSocket-Accept"))
if(! res.headers.exists("Sec-WebSocket-Accept"))
return fail();
if(resp.headers["Sec-WebSocket-Accept"] !=
if(res.headers["Sec-WebSocket-Accept"] !=
detail::make_sec_ws_accept(key))
return fail();
role_ = role_type::client;