From b0e52dd24616bbfe18e9ed45282d2e6ea6402fc9 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 3 Jul 2017 20:33:54 -0700 Subject: [PATCH] Refactor header and message constructors: fix #581 * request and response headers now have convenience constructors so important fields like method, target, result, and version may be set upon construction. Actions Required: * Evaluate each message constructor call site and adjust the constructor argument list as needed. --- CHANGELOG.md | 9 + example/http-client/http_client.cpp | 5 +- example/http-crawl/http_crawl.cpp | 5 +- .../http_server_threaded.cpp | 9 +- example/server-framework/http_async_port.hpp | 2 +- example/server-framework/http_sync_port.hpp | 3 +- include/beast/http/impl/message.ipp | 132 +++++++-- include/beast/http/message.hpp | 269 +++++++++++++----- test/http/message.cpp | 193 ++++++++----- test/http/string_view_body.cpp | 3 +- test/http/write.cpp | 67 ++--- 11 files changed, 487 insertions(+), 210 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bd26372..33c7df6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ HTTP: * basic_parser optimizations * Add basic_parser tests +API Changes: + +* Refactor header and message constructors + +Actions Required: + +* Evaluate each message constructor call site and + adjust the constructor argument list as needed. + -------------------------------------------------------------------------------- Version 72: diff --git a/example/http-client/http_client.cpp b/example/http-client/http_client.cpp index 5440c7f1..5c3d0184 100644 --- a/example/http-client/http_client.cpp +++ b/example/http-client/http_client.cpp @@ -48,10 +48,7 @@ int main() return fail("connect", ec); // Set up an HTTP GET request message - http::request req; - req.method(http::verb::get); - req.target("/"); - req.version = 11; + http::request req{http::verb::get, "/", 11}; req.set(http::field::host, host + ":" + std::to_string(sock.remote_endpoint().port())); req.set(http::field::user_agent, BEAST_VERSION_STRING); diff --git a/example/http-crawl/http_crawl.cpp b/example/http-crawl/http_crawl.cpp index 8cee781b..03ae5172 100644 --- a/example/http-crawl/http_crawl.cpp +++ b/example/http-crawl/http_crawl.cpp @@ -75,10 +75,7 @@ main(int, char const*[]) } // Set up an HTTP GET request - http::request req; - req.version = 11; - req.method(http::verb::get); - req.target("/"); + http::request req{http::verb::get, "/", 11}; req.set(http::field::host, host + std::string(":") + std::to_string(ep.port())); req.set(http::field::user_agent, BEAST_VERSION_STRING); diff --git a/example/http-server-threaded/http_server_threaded.cpp b/example/http-server-threaded/http_server_threaded.cpp index 84a00763..540a261b 100644 --- a/example/http-server-threaded/http_server_threaded.cpp +++ b/example/http-server-threaded/http_server_threaded.cpp @@ -60,8 +60,7 @@ private: http::response client_error(http::status result, beast::string_view text) { - http::response res; - res.result(result); + http::response res{result, 11}; res.set(http::field::server, BEAST_VERSION_STRING); res.set(http::field::content_type, "text/plain"); res.set(http::field::connection, "close"); @@ -75,8 +74,7 @@ private: beast::http::response not_found() const { - beast::http::response res; - res.result(beast::http::status::not_found); + beast::http::response res{beast::http::status::not_found, 11}; res.set(beast::http::field::server, BEAST_VERSION_STRING); res.set(beast::http::field::content_type, "text/html"); res.set(http::field::connection, "close"); @@ -90,8 +88,7 @@ private: beast::http::response server_error(beast::error_code const& ec) const { - beast::http::response res; - res.result(beast::http::status::internal_server_error); + beast::http::response res{beast::http::status::internal_server_error, 11}; res.set(beast::http::field::server, BEAST_VERSION_STRING); res.set(beast::http::field::content_type, "text/html"); res.set(http::field::connection, "close"); diff --git a/example/server-framework/http_async_port.hpp b/example/server-framework/http_async_port.hpp index 54db9266..7231ca7c 100644 --- a/example/server-framework/http_async_port.hpp +++ b/example/server-framework/http_async_port.hpp @@ -264,7 +264,7 @@ protected: // We construct the dynamic body with a 1MB limit // to prevent vulnerability to buffer attacks. // - parser_.emplace(1024 * 1024); + parser_.emplace(std::piecewise_construct, std::make_tuple(1024 * 1024)); // Read just the header beast::http::async_read_header( diff --git a/example/server-framework/http_sync_port.hpp b/example/server-framework/http_sync_port.hpp index 6f581059..69fb1a68 100644 --- a/example/server-framework/http_sync_port.hpp +++ b/example/server-framework/http_sync_port.hpp @@ -188,7 +188,8 @@ private: // We construct the dynamic body with a 1MB limit // to prevent vulnerability to buffer attacks. // - beast::http::request_parser parser(1024* 1024); + beast::http::request_parser parser( + std::piecewise_construct, std::make_tuple(1024* 1024)); // Read the header first beast::http::read_header(impl().stream(), buffer_, parser, ec); diff --git a/include/beast/http/impl/message.ipp b/include/beast/http/impl/message.ipp index 37216f03..af3c524a 100644 --- a/include/beast/http/impl/message.ipp +++ b/include/beast/http/impl/message.ipp @@ -111,6 +111,19 @@ header(Arg1&& arg1, ArgN&&... argn) std::forward(argn)...) { } + +#if 0 +template +template +header:: +header(status result, unsigned version_, Args&&... args) + : Fields(std::forward(args)...) + , version(version_) + , result_(result) +{ +} +#endif + template inline status @@ -189,23 +202,118 @@ swap( //------------------------------------------------------------------------------ template -template +template message:: -message(header_type&& h, Args&&... args) +message(header_type&& h, BodyArgs&&... body_args) : header_type(std::move(h)) - , body(std::forward(args)...) + , body(std::forward(body_args)...) { } template -template +template message:: -message(header_type const& h, Args&&... args) +message(header_type const& h, BodyArgs&&... body_args) : header_type(h) - , body(std::forward(args)...) + , body(std::forward(body_args)...) { } +template +template +message:: +message(verb method, string_view target, Version version) + : header_type(method, target, version) +{ +} + +template +template +message:: +message(verb method, string_view target, + Version version, BodyArg&& body_arg) + : header_type(method, target, version) + , body(std::forward(body_arg)) +{ +} + +template +template +message:: +message( + verb method, string_view target, Version version, + BodyArg&& body_arg, + FieldsArg&& fields_arg) + : header_type(method, target, version, + std::forward(fields_arg)) + , body(std::forward(body_arg)) +{ +} + +template +template +message:: +message(status result, Version version) + : header_type(result, version) +{ +} + +template +template +message:: +message(status result, Version version, + BodyArg&& body_arg) + : header_type(result, version) + , body(std::forward(body_arg)) +{ +} + +template +template +message:: +message(status result, Version version, + BodyArg&& body_arg, FieldsArg&& fields_arg) + : header_type(result, version, + std::forward(fields_arg)) + , body(std::forward(body_arg)) +{ +} + +template +message:: +message(std::piecewise_construct_t) +{ +} + +template +template +message:: +message(std::piecewise_construct_t, + std::tuple body_args) + : message(std::piecewise_construct, + body_args, + beast::detail::make_index_sequence< + sizeof...(BodyArgs)>{}) +{ +} + +template +template +message:: +message(std::piecewise_construct_t, + std::tuple body_args, + std::tuple fields_args) + : message(std::piecewise_construct, + body_args, + fields_args, + beast::detail::make_index_sequence< + sizeof...(BodyArgs)>{}, + beast::detail::make_index_sequence< + sizeof...(FieldsArgs)>{}) +{ +} + +#if 0 template template message:: @@ -223,17 +331,6 @@ message(BodyArg&& body_arg, HeaderArg&& header_arg) { } -template -template -message:: -message(std::piecewise_construct_t, - std::tuple body_args) - : message(std::piecewise_construct, body_args, - beast::detail::make_index_sequence< - sizeof...(BodyArgs)>{}) -{ -} - template template message:: @@ -248,6 +345,7 @@ message(std::piecewise_construct_t, sizeof...(HeaderArgs)>{}) { } +#endif template boost::optional diff --git a/include/beast/http/message.hpp b/include/beast/http/message.hpp index ec7692a7..93a39ff3 100644 --- a/include/beast/http/message.hpp +++ b/include/beast/http/message.hpp @@ -158,8 +158,8 @@ struct header : Fields void target(string_view s); - // VFALCO Don't move these declarations around, - // otherwise the documentation will be wrong. + // VFALCO Don't rearrange these declarations or + // ifdefs, or else the documentation will break. /** Constructor @@ -168,7 +168,8 @@ struct header : Fields @note This constructor participates in overload resolution if and only if the first parameter is - not convertible to @ref header. + not convertible to @ref header, @ref verb, or + @ref status. */ #if BEAST_DOXYGEN template @@ -178,9 +179,13 @@ struct header : Fields #else template 0) || ! std::is_convertible< - typename std::decay::type, - header>::value>::type> + ! std::is_convertible::type, header>::value && + ! std::is_convertible::type, verb>::value && + ! std::is_convertible::type, header>::value + >::type> explicit header(Arg1&& arg1, ArgN&&... argn); @@ -193,6 +198,19 @@ private: void swap(header& m1, header& m2); + template + header( + verb method, + string_view target_, + unsigned version_, + FieldsArgs&&... fields_args) + : Fields(std::forward(fields_args)...) + , version(version_) + , method_(method) + { + target(target_); + } + verb method_ = verb::unknown; }; @@ -207,11 +225,7 @@ struct header : Fields "Fields requirements not met"); /// Indicates if the header is a request or response. -#if BEAST_DOXYGEN - using is_request = std::integral_constant; -#else using is_request = std::false_type; -#endif /// The type representing the fields. using fields_type = Fields; @@ -225,7 +239,8 @@ struct header : Fields unsigned minor = version % 10; @endcode - Newly constructed headers will use HTTP/1.1 by default. + Newly constructed headers will use HTTP/1.1 by default + unless otherwise specified. */ unsigned version = 11; @@ -251,13 +266,16 @@ struct header : Fields @note This constructor participates in overload resolution if and only if the first parameter is - not convertible to @ref header. + not convertible to @ref header, @ref verb, or + @ref status. */ template 0) || ! std::is_convertible< - typename std::decay::type, - header>::value>::type> + ! std::is_convertible::type, status>::value && + ! std::is_convertible::type, header>::value + >::type> explicit header(Arg1&& arg1, ArgN&&... argn); #endif @@ -347,6 +365,17 @@ private: void swap(header& m1, header& m2); + template + header( + status result, + unsigned version_, + FieldsArgs&&... fields_args) + : Fields(std::forward(fields_args)...) + , version(version_) + , result_(result) + { + } + status result_ = status::ok; #endif }; @@ -403,82 +432,184 @@ struct message : header /// A value representing the body. typename Body::value_type body; - /// Default constructor + /// Constructor message() = default; - /// Move constructor + /// Constructor message(message&&) = default; - /// Copy constructor + /// Constructor message(message const&) = default; - /// Move assignment + /// Assignment message& operator=(message&&) = default; - /// Copy assignment + /// Assignment message& operator=(message const&) = default; - /** Constructor. + /** Constructor @param h The header to move construct from. - @param args Optional arguments forwarded - to the body constructor. + @param body_args Optional arguments forwarded + to the `body` constructor. */ - template + template explicit - message(header_type&& h, Args&&... args); + message(header_type&& h, BodyArgs&&... body_args); /** Constructor. @param h The header to copy construct from. - @param args Optional arguments forwarded - to the body constructor. + @param body_args Optional arguments forwarded + to the `body` constructor. */ - template + template explicit - message(header_type const& h, Args&&... args); + message(header_type const& h, BodyArgs&&... body_args); - /** Construct a message. + /** Constructor - @param body_arg An argument forwarded to the body constructor. + @param method The request-method to use - @note This constructor participates in overload resolution - only if `body_arg` is not convertible to `header_type`. + @param target The request-target. + + @param version The HTTP-version + + @note This function is only available when `isRequest == true`. */ - template::type, header_type>::value>::type +#if BEAST_DOXYGEN + message(verb method, string_view target, unsigned version); +#else + template::value>::type> + message(verb method, string_view target, Version version); #endif - > + + /** Constructor + + @param method The request-method to use + + @param target The request-target. + + @param version The HTTP-version + + @param body_arg An argument forwarded to the `body` constructor. + + @note This function is only available when `isRequest == true`. + */ +#if BEAST_DOXYGEN + template + message(verb method, string_view target, + unsigned version, BodyArg&& body_arg); +#else + template::value>::type> + message(verb method, string_view target, + Version version, BodyArg&& body_arg); +#endif + + /** Constructor + + @param method The request-method to use + + @param target The request-target. + + @param version The HTTP-version + + @param body_arg An argument forwarded to the `body` constructor. + + @param fields_arg An argument forwarded to the `Fields` constructor. + + @note This function is only available when `isRequest == true`. + */ +#if BEAST_DOXYGEN + template + message(verb method, string_view target, unsigned version, + BodyArg&& body_arg, FieldsArg&& fields_arg); +#else + template::value>::type> + message(verb method, string_view target, Version version, + BodyArg&& body_arg, FieldsArg&& fields_arg); +#endif + + /** Constructor + + @param result The status-code for the response + + @param version The HTTP-version + + @note This member is only available when `isRequest == false`. + */ +#if BEAST_DOXYGEN + message(status result, unsigned version); +#else + template::value>::type> + message(status result, Version version); +#endif + + /** Constructor + + @param result The status-code for the response + + @param version The HTTP-version + + @param body_arg An argument forwarded to the `body` constructor. + + @note This member is only available when `isRequest == false`. + */ +#if BEAST_DOXYGEN + template + message(status result, unsigned version, BodyArg&& body_arg); +#else + template::value>::type> + message(status result, Version version, BodyArg&& body_arg); +#endif + + /** Constructor + + @param result The status-code for the response + + @param version The HTTP-version + + @param body_arg An argument forwarded to the `body` constructor. + + @param fields_arg An argument forwarded to the `Fields` base class constructor. + + @note This member is only available when `isRequest == false`. + */ +#if BEAST_DOXYGEN + template + message(status result, unsigned version, + BodyArg&& body_arg, FieldsArg&& fields_arg); +#else + template::value>::type> + message(status result, Version version, + BodyArg&& body_arg, FieldsArg&& fields_arg); +#endif + + /** Constructor + + The header and body are default-constructed. + */ explicit - message(BodyArg&& body_arg); + message(std::piecewise_construct_t); /** Construct a message. - @param body_arg An argument forwarded to the body constructor. - - @param header_arg An argument forwarded to the header constructor. - - @note This constructor participates in overload resolution - only if `body_arg` is not convertible to `header_type`. - */ - template::type, - header_type>::value>::type -#endif - > - message(BodyArg&& body_arg, HeaderArg&& header_arg); - - /** Construct a message. - - @param body_args A tuple forwarded as a parameter pack to the body constructor. + @param body_args A tuple forwarded as a parameter + pack to the body constructor. */ template message(std::piecewise_construct_t, @@ -486,14 +617,16 @@ struct message : header /** Construct a message. - @param body_args A tuple forwarded as a parameter pack to the body constructor. + @param body_args A tuple forwarded as a parameter + pack to the body constructor. - @param header_args A tuple forwarded as a parameter pack to the fields constructor. + @param fields_args A tuple forwarded as a parameter + pack to the `Fields` constructor. */ - template + template message(std::piecewise_construct_t, - std::tuple&& body_args, - std::tuple&& header_args); + std::tuple body_args, + std::tuple fields_args); /// Returns the header portion of the message header_type const& @@ -524,7 +657,6 @@ struct message : header boost::optional payload_size() const; - /** Prepare the message payload fields for the body. This function will adjust the Content-Length and @@ -533,10 +665,7 @@ struct message : header @par Example @code - request req; - req.version = 11; - req.method(verb::upgrade); - req.target("/"); + request req{verb::post, "/"}; req.set(field::user_agent, "Beast"); req.body = "Hello, world!"; req.prepare_payload(); diff --git a/test/http/message.cpp b/test/http/message.cpp index d9469755..b4aa8ac5 100644 --- a/test/http/message.cpp +++ b/test/http/message.cpp @@ -8,6 +8,7 @@ // Test that header file is self-contained. #include +#include #include #include #include @@ -70,62 +71,78 @@ public: }; }; + // 0-arg + BOOST_STATIC_ASSERT(std::is_constructible< + request>::value); + + // 1-arg + BOOST_STATIC_ASSERT(! std::is_constructible + >::value); + + //BOOST_STATIC_ASSERT(! std::is_constructible, + // verb, string_view, unsigned>::value); + + BOOST_STATIC_ASSERT(std::is_constructible, + verb, string_view, unsigned, Arg1>::value); + + BOOST_STATIC_ASSERT(std::is_constructible, + verb, string_view, unsigned, Arg1&&>::value); + + BOOST_STATIC_ASSERT(std::is_constructible, + verb, string_view, unsigned, Arg1 const>::value); + + BOOST_STATIC_ASSERT(std::is_constructible, + verb, string_view, unsigned, Arg1 const&>::value); + + // 1-arg + fields + BOOST_STATIC_ASSERT(std::is_constructible, + verb, string_view, unsigned, Arg1, fields::allocator_type>::value); + + BOOST_STATIC_ASSERT(std::is_constructible, std::piecewise_construct_t, + std::tuple>::value); + + BOOST_STATIC_ASSERT(std::is_constructible, std::piecewise_construct_t, + std::tuple>::value); + + BOOST_STATIC_ASSERT(std::is_constructible, std::piecewise_construct_t, + std::tuple, std::tuple>::value); + + // special members + BOOST_STATIC_ASSERT(std::is_copy_constructible>::value); + BOOST_STATIC_ASSERT(std::is_move_constructible>::value); + BOOST_STATIC_ASSERT(std::is_copy_assignable>::value); + BOOST_STATIC_ASSERT(std::is_move_assignable>::value); + BOOST_STATIC_ASSERT(std::is_copy_constructible>::value); + BOOST_STATIC_ASSERT(std::is_move_constructible>::value); + BOOST_STATIC_ASSERT(std::is_copy_assignable>::value); + BOOST_STATIC_ASSERT(std::is_move_assignable>::value); + void testMessage() { - BOOST_STATIC_ASSERT(std::is_constructible< - request>::value); - - BOOST_STATIC_ASSERT(std::is_constructible< - request, Arg1>::value); - - BOOST_STATIC_ASSERT(std::is_constructible< - request, Arg1 const>::value); - - BOOST_STATIC_ASSERT(std::is_constructible< - request, Arg1 const&>::value); - - BOOST_STATIC_ASSERT(std::is_constructible< - request, Arg1&&>::value); - - BOOST_STATIC_ASSERT(! std::is_constructible< - request>::value); - - BOOST_STATIC_ASSERT(std::is_constructible< - request, - Arg1, fields::allocator_type>::value); - - BOOST_STATIC_ASSERT(std::is_constructible< - request, std::piecewise_construct_t, - std::tuple>::value); - - BOOST_STATIC_ASSERT(std::is_constructible< - request, std::piecewise_construct_t, - std::tuple>::value); - - BOOST_STATIC_ASSERT(std::is_constructible< - request, std::piecewise_construct_t, - std::tuple, std::tuple>::value); - { Arg1 arg1; - request{std::move(arg1)}; + request{verb::get, "/", 11, std::move(arg1)}; BEAST_EXPECT(arg1.moved); } { header h; - h.insert(field::user_agent, "test"); - request m{Arg1{}, h}; - BEAST_EXPECT(h["User-Agent"] == "test"); - BEAST_EXPECT(m["User-Agent"] == "test"); + h.set(field::user_agent, "test"); + BEAST_EXPECT(h[field::user_agent] == "test"); + request m{std::move(h)}; + BEAST_EXPECT(m[field::user_agent] == "test"); + BEAST_EXPECT(h.count(field::user_agent) == 0); } { - header h; - h.insert(field::user_agent, "test"); - request m{Arg1{}, std::move(h)}; - BEAST_EXPECT(! h.count(http::field::user_agent)); + request h{verb::get, "/", 10}; + h.set(field::user_agent, "test"); + request m{std::move(h.base()), Arg1{}}; BEAST_EXPECT(m["User-Agent"] == "test"); + BEAST_EXPECT(h.count(http::field::user_agent) == 0); + BEAST_EXPECT(m.method() == verb::get); + BEAST_EXPECT(m.target() == "/"); + BEAST_EXPECT(m.version == 10); } // swap @@ -166,31 +183,79 @@ public: } }; + struct token {}; + + struct test_fields + { + std::string target; + + test_fields() = delete; + test_fields(token) {} + void set_method_impl(string_view) {} + void set_target_impl(string_view s) { target = s.to_string(); } + void set_reason_impl(string_view) {} + string_view get_method_impl() const { return {}; } + string_view get_target_impl() const { return target; } + string_view get_reason_impl() const { return {}; } + void prepare_payload_impl(bool, boost::optional) {} + }; + void - testHeaders() + testMessageCtors() { { - using req_type = header; - BOOST_STATIC_ASSERT(std::is_copy_constructible::value); - BOOST_STATIC_ASSERT(std::is_move_constructible::value); - BOOST_STATIC_ASSERT(std::is_copy_assignable::value); - BOOST_STATIC_ASSERT(std::is_move_assignable::value); - - using res_type = header; - BOOST_STATIC_ASSERT(std::is_copy_constructible::value); - BOOST_STATIC_ASSERT(std::is_move_constructible::value); - BOOST_STATIC_ASSERT(std::is_copy_assignable::value); - BOOST_STATIC_ASSERT(std::is_move_assignable::value); + request req; + BEAST_EXPECT(req.version == 11); + BEAST_EXPECT(req.method() == verb::unknown); + BEAST_EXPECT(req.target() == ""); } - { - MoveFields h; - header r{std::move(h)}; - BEAST_EXPECT(h.moved_from); - BEAST_EXPECT(r.moved_to); - request m{std::move(r)}; - BEAST_EXPECT(r.moved_from); - BEAST_EXPECT(m.moved_to); + request req{verb::get, "/", 11}; + BEAST_EXPECT(req.version == 11); + BEAST_EXPECT(req.method() == verb::get); + BEAST_EXPECT(req.target() == "/"); + } + { + request req{verb::get, "/", 11, "Hello"}; + BEAST_EXPECT(req.version == 11); + BEAST_EXPECT(req.method() == verb::get); + BEAST_EXPECT(req.target() == "/"); + BEAST_EXPECT(req.body == "Hello"); + } + { + request req{ + verb::get, "/", 11, "Hello", token{}}; + BEAST_EXPECT(req.version == 11); + BEAST_EXPECT(req.method() == verb::get); + BEAST_EXPECT(req.target() == "/"); + BEAST_EXPECT(req.body == "Hello"); + } + { + response res; + BEAST_EXPECT(res.version == 11); + BEAST_EXPECT(res.result() == status::ok); + BEAST_EXPECT(res.reason() == "OK"); + } + { + response res{status::bad_request, 10}; + BEAST_EXPECT(res.version == 10); + BEAST_EXPECT(res.result() == status::bad_request); + BEAST_EXPECT(res.reason() == "Bad Request"); + } + { + response res{status::bad_request, 10, "Hello"}; + BEAST_EXPECT(res.version == 10); + BEAST_EXPECT(res.result() == status::bad_request); + BEAST_EXPECT(res.reason() == "Bad Request"); + BEAST_EXPECT(res.body == "Hello"); + } + { + response res{ + status::bad_request, 10, "Hello", token{}}; + BEAST_EXPECT(res.version == 10); + BEAST_EXPECT(res.result() == status::bad_request); + BEAST_EXPECT(res.reason() == "Bad Request"); + BEAST_EXPECT(res.body == "Hello"); } } @@ -292,7 +357,7 @@ public: run() override { testMessage(); - testHeaders(); + testMessageCtors(); testSwap(); testSpecialMembers(); testMethod(); diff --git a/test/http/string_view_body.cpp b/test/http/string_view_body.cpp index f709a8b9..e6a00a7f 100644 --- a/test/http/string_view_body.cpp +++ b/test/http/string_view_body.cpp @@ -27,10 +27,11 @@ public: { static_assert(is_body_reader::value, ""); static_assert(! is_body_writer::value, ""); - request req{"Hello, world!"}; + request req; req.version = 11; req.method(verb::post); req.target("/"); + req.body = "Hello, world!"; req.prepare_payload(); static_buffer_n<512> b; ostream(b) << req; diff --git a/test/http/write.cpp b/test/http/write.cpp index c36352b4..9d494014 100644 --- a/test/http/write.cpp +++ b/test/http/write.cpp @@ -278,9 +278,8 @@ public: response m; m.version = 10; m.result(status::ok); - m.reason("OK"); - m.insert(field::server, "test"); - m.insert("Content-Length", "5"); + m.set(field::server, "test"); + m.set(field::content_length, "5"); m.body = "*****"; error_code ec; test::string_ostream ss{ios_}; @@ -297,9 +296,8 @@ public: response m; m.version = 11; m.result(status::ok); - m.reason("OK"); - m.insert(field::server, "test"); - m.insert("Transfer-Encoding", "chunked"); + m.set(field::server, "test"); + m.set(field::transfer_encoding, "chunked"); m.body = "*****"; error_code ec; test::string_ostream ss(ios_); @@ -327,11 +325,8 @@ public: test::fail_counter fc(n); test::fail_stream< test::string_ostream> fs(fc, ios_); - request m{fc}; - m.method(verb::get); - m.target("/"); - m.version = 10; - m.insert(field::user_agent, "test"); + request m(verb::get, "/", 10, fc); + m.set(field::user_agent, "test"); m.set(field::connection, "keep-alive"); m.set(field::content_length, "5"); m.body = "*****"; @@ -360,12 +355,9 @@ public: test::fail_counter fc(n); test::fail_stream< test::string_ostream> fs(fc, ios_); - request m{fc}; - m.method(verb::get); - m.target("/"); - m.version = 10; - m.insert(field::user_agent, "test"); - m.insert("Transfer-Encoding", "chunked"); + request m{verb::get, "/", 10, fc}; + m.set(field::user_agent, "test"); + m.set(field::transfer_encoding, "chunked"); m.body = "*****"; error_code ec = test::error::fail_error; write(fs, m, ec); @@ -393,12 +385,9 @@ public: test::fail_counter fc(n); test::fail_stream< test::string_ostream> fs(fc, ios_); - request m{fc}; - m.method(verb::get); - m.target("/"); - m.version = 10; - m.insert(field::user_agent, "test"); - m.insert("Transfer-Encoding", "chunked"); + request m{verb::get, "/", 10, fc}; + m.set(field::user_agent, "test"); + m.set(field::transfer_encoding, "chunked"); m.body = "*****"; error_code ec = test::error::fail_error; async_write(fs, m, do_yield[ec]); @@ -426,11 +415,8 @@ public: test::fail_counter fc(n); test::fail_stream< test::string_ostream> fs(fc, ios_); - request m{fc}; - m.method(verb::get); - m.target("/"); - m.version = 10; - m.insert(field::user_agent, "test"); + request m{verb::get, "/", 10, fc}; + m.set(field::user_agent, "test"); m.set(field::connection, "keep-alive"); m.set(field::content_length, "5"); m.body = "*****"; @@ -456,11 +442,8 @@ public: test::fail_counter fc(n); test::fail_stream< test::string_ostream> fs(fc, ios_); - request m{fc}; - m.method(verb::get); - m.target("/"); - m.version = 10; - m.insert(field::user_agent, "test"); + request m{verb::get, "/", 10, fc}; + m.set(field::user_agent, "test"); m.set(field::connection, "keep-alive"); m.set(field::content_length, "5"); m.body = "*****"; @@ -491,7 +474,7 @@ public: m.method(verb::get); m.target("/"); m.version = 10; - m.insert(field::user_agent, "test"); + m.set(field::user_agent, "test"); m.body = "*"; m.prepare_payload(); BEAST_EXPECT(str(m) == @@ -508,7 +491,7 @@ public: m.method(verb::get); m.target("/"); m.version = 10; - m.insert(field::user_agent, "test"); + m.set(field::user_agent, "test"); m.body = "*"; m.prepare_payload(); test::string_ostream ss(ios_); @@ -528,7 +511,7 @@ public: m.method(verb::get); m.target("/"); m.version = 11; - m.insert(field::user_agent, "test"); + m.set(field::user_agent, "test"); m.body = "*"; m.prepare_payload(); BEAST_EXPECT(str(m) == @@ -545,7 +528,7 @@ public: m.method(verb::get); m.target("/"); m.version = 11; - m.insert(field::user_agent, "test"); + m.set(field::user_agent, "test"); m.body = "*"; m.prepare_payload(); test::string_ostream ss(ios_); @@ -570,7 +553,7 @@ public: m.method(verb::get); m.target("/"); m.version = 11; - m.insert(field::user_agent, "test"); + m.set(field::user_agent, "test"); m.body = "*"; BEAST_EXPECT(boost::lexical_cast(m) == "GET / HTTP/1.1\r\nUser-Agent: test\r\n\r\n*"); @@ -600,7 +583,7 @@ public: m.method(verb::get); m.version = 11; m.target("/"); - m.insert("Content-Length", 5); + m.set("Content-Length", 5); m.body = "*****"; async_write(os, m, handler{}); BEAST_EXPECT(handler::count() > 0); @@ -622,7 +605,7 @@ public: m.method(verb::get); m.version = 11; m.target("/"); - m.insert("Content-Length", 5); + m.set("Content-Length", 5); m.body = "*****"; async_write(is, m, handler{}); BEAST_EXPECT(handler::count() > 0); @@ -704,7 +687,7 @@ public: m0.version = 11; m0.result(status::ok); m0.reason("OK"); - m0.insert(field::server, "test"); + m0.set(field::server, "test"); m0.body.s = "Hello, world!\n"; { @@ -761,7 +744,7 @@ public: } } { - m0.insert("Transfer-Encoding", "chunked"); + m0.set("Transfer-Encoding", "chunked"); { auto m = m0; error_code ec;