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

@@ -1,21 +1,139 @@
//------------------------------------------------------------------------------
/*
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.
*/
//==============================================================================
//
// 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/message.hpp>
#include <beast/http/headers.hpp>
#include <beast/unit_test/suite.hpp>
#include <type_traits>
namespace beast {
namespace http {
class message_test : public beast::unit_test::suite
{
public:
struct Arg1
{
bool moved = false;
Arg1() = default;
Arg1(Arg1&& other)
{
other.moved = true;
}
};
struct Arg2 { };
struct Arg3 { };
// default constructible Body
struct default_body
{
using value_type = std::string;
};
// 1-arg constructible Body
struct one_arg_body
{
struct value_type
{
explicit
value_type(Arg1 const&)
{
}
explicit
value_type(Arg1&& arg)
{
Arg1 arg_(std::move(arg));
}
};
};
// 2-arg constructible Body
struct two_arg_body
{
struct value_type
{
value_type(Arg1 const&, Arg2 const&)
{
}
};
};
void testConstruction()
{
static_assert(std::is_constructible<
message<true, default_body, headers>>::value, "");
static_assert(std::is_constructible<
message<true, one_arg_body, headers>, Arg1>::value, "");
static_assert(std::is_constructible<
message<true, one_arg_body, headers>, Arg1 const>::value, "");
static_assert(std::is_constructible<
message<true, one_arg_body, headers>, Arg1 const&>::value, "");
static_assert(std::is_constructible<
message<true, one_arg_body, headers>, Arg1&&>::value, "");
static_assert(! std::is_constructible<
message<true, one_arg_body, headers>>::value, "");
static_assert(std::is_constructible<
message<true, one_arg_body, headers>,
Arg1, headers::allocator_type>::value, "");
static_assert(std::is_constructible<
message<true, one_arg_body, headers>, std::piecewise_construct_t,
std::tuple<Arg1>>::value, "");
static_assert(std::is_constructible<
message<true, two_arg_body, headers>, std::piecewise_construct_t,
std::tuple<Arg1, Arg2>>::value, "");
static_assert(std::is_constructible<
message<true, two_arg_body, headers>, std::piecewise_construct_t,
std::tuple<Arg1, Arg2>, std::tuple<headers::allocator_type>>::value, "");
{
Arg1 arg1;
message<true, one_arg_body, headers>{std::move(arg1)};
expect(arg1.moved);
}
{
headers h;
h.insert("User-Agent", "test");
message<true, one_arg_body, headers> m{Arg1{}, h};
expect(h["User-Agent"] == "test");
expect(m.headers["User-Agent"] == "test");
}
{
headers h;
h.insert("User-Agent", "test");
message<true, one_arg_body, headers> m{Arg1{}, std::move(h)};
expect(! h.exists("User-Agent"));
expect(m.headers["User-Agent"] == "test");
}
}
void run() override
{
testConstruction();
}
};
BEAST_DEFINE_TESTSUITE(message,http,beast);
} // http
} // beast

View File

@@ -1,21 +1,9 @@
//------------------------------------------------------------------------------
/*
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.
*/
//==============================================================================
//
// 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/message_v1.hpp>
@@ -29,7 +17,6 @@
namespace beast {
namespace http {
namespace test {
class sync_echo_http_server
{
@@ -122,8 +109,10 @@ private:
read(sock, rb, req, ec);
if(ec)
break;
response_v1<string_body> resp(
{100, "OK", req.version});
response_v1<string_body> resp;
resp.status = 100;
resp.reason = "OK";
resp.version = req.version;
resp.body = "Completed successfully.";
write(sock, resp, ec);
if(ec)
@@ -132,7 +121,7 @@ private:
}
};
class message_test : public beast::unit_test::suite
class message_v1_test : public beast::unit_test::suite
{
public:
using endpoint_type = boost::asio::ip::tcp::endpoint;
@@ -184,7 +173,10 @@ public:
streambuf rb;
{
request_v1<string_body> req({"GET", "/", 11});
request_v1<string_body> req;
req.method = "GET";
req.url = "/";
req.version = 11;
req.body = "Beast.HTTP";
req.headers.replace("Host",
ep.address().to_string() + ":" +
@@ -214,8 +206,7 @@ public:
}
};
BEAST_DEFINE_TESTSUITE(message,http,beast);
BEAST_DEFINE_TESTSUITE(message_v1,http,beast);
} // test
} // http
} // beast

View File

@@ -135,8 +135,10 @@ public:
{
// auto content-length HTTP/1.0
{
message_v1<true, string_body, headers> m{{
"GET", "/", 10}};
message_v1<true, string_body, headers> m;
m.method = "GET";
m.url = "/";
m.version = 10;
m.headers.insert("User-Agent", "test");
m.body = "*";
prepare(m);
@@ -150,8 +152,10 @@ public:
}
// keep-alive HTTP/1.0
{
message_v1<true, string_body, headers> m{{
"GET", "/", 10}};
message_v1<true, string_body, headers> m;
m.method = "GET";
m.url = "/";
m.version = 10;
m.headers.insert("User-Agent", "test");
m.body = "*";
prepare(m, connection::keep_alive);
@@ -166,8 +170,10 @@ public:
}
// upgrade HTTP/1.0
{
message_v1<true, string_body, headers> m{{
"GET", "/", 10}};
message_v1<true, string_body, headers> m;
m.method = "GET";
m.url = "/";
m.version = 10;
m.headers.insert("User-Agent", "test");
m.body = "*";
try
@@ -182,8 +188,10 @@ public:
}
// no content-length HTTP/1.0
{
message_v1<true, test_body, headers> m{{
"GET", "/", 10}};
message_v1<true, test_body, headers> m;
m.method = "GET";
m.url = "/";
m.version = 10;
m.headers.insert("User-Agent", "test");
m.body = "*";
prepare(m);
@@ -200,8 +208,10 @@ public:
}
// auto content-length HTTP/1.1
{
message_v1<true, string_body, headers> m{{
"GET", "/", 11}};
message_v1<true, string_body, headers> m;
m.method = "GET";
m.url = "/";
m.version = 11;
m.headers.insert("User-Agent", "test");
m.body = "*";
prepare(m);
@@ -215,8 +225,10 @@ public:
}
// close HTTP/1.1
{
message_v1<true, string_body, headers> m{{
"GET", "/", 11}};
message_v1<true, string_body, headers> m;
m.method = "GET";
m.url = "/";
m.version = 11;
m.headers.insert("User-Agent", "test");
m.body = "*";
prepare(m, connection::close);
@@ -235,8 +247,10 @@ public:
}
// upgrade HTTP/1.1
{
message_v1<true, empty_body, headers> m{{
"GET", "/", 11}};
message_v1<true, empty_body, headers> m;
m.method = "GET";
m.url = "/";
m.version = 11;
m.headers.insert("User-Agent", "test");
prepare(m, connection::upgrade);
expect(str(m) ==
@@ -248,8 +262,10 @@ public:
}
// no content-length HTTP/1.1
{
message_v1<true, test_body, headers> m{{
"GET", "/", 11}};
message_v1<true, test_body, headers> m;
m.method = "GET";
m.url = "/";
m.version = 11;
m.headers.insert("User-Agent", "test");
m.body = "*";
prepare(m);
@@ -270,8 +286,10 @@ public:
void testConvert()
{
message_v1<true, string_body, headers> m{{
"GET", "/", 11}};
message_v1<true, string_body, headers> m;
m.method = "GET";
m.url = "/";
m.version = 11;
m.headers.insert("User-Agent", "test");
m.body = "*";
prepare(m);