From 7712ca09b2c3e617882729f81fb8c54c8793d898 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 12 Jun 2017 18:39:04 -0700 Subject: [PATCH] Add string_view_body --- CHANGELOG.md | 1 + doc/5_02_message.qbk | 8 +++ doc/quickref.xml | 1 + include/beast/core.hpp | 1 + include/beast/http.hpp | 1 + include/beast/http/string_view_body.hpp | 88 +++++++++++++++++++++++++ test/Jamfile | 1 + test/http/CMakeLists.txt | 1 + test/http/string_view_body.cpp | 52 +++++++++++++++ 9 files changed, 154 insertions(+) create mode 100644 include/beast/http/string_view_body.hpp create mode 100644 test/http/string_view_body.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 66055a21..aae16525 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version 56: * Add provisional IANA header field names +* Add string_view_body -------------------------------------------------------------------------------- diff --git a/doc/5_02_message.qbk b/doc/5_02_message.qbk index a26a2f10..14c8226a 100644 --- a/doc/5_02_message.qbk +++ b/doc/5_02_message.qbk @@ -118,6 +118,14 @@ __Body__ requirements: Messages with this body type may be serialized and parsed. This is the type of body used in the examples. ]] +[[ + [link beast.ref.http__string_view_body `string_view_body`] +][ + A body whose `value_type` is [link beast.ref.string_view `string_view`]. + Messages with this body type may be serialized only, and the caller + is responsible for managing the lifetime of the buffer pointed to + by the string view. +]] ] [heading Usage] diff --git a/doc/quickref.xml b/doc/quickref.xml index b0a85a72..c7118174 100644 --- a/doc/quickref.xml +++ b/doc/quickref.xml @@ -46,6 +46,7 @@ response_parser serializer string_body + string_view_body rfc7230 diff --git a/include/beast/core.hpp b/include/beast/core.hpp index f7577253..b4b6f862 100644 --- a/include/beast/core.hpp +++ b/include/beast/core.hpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/include/beast/http.hpp b/include/beast/http.hpp index fbdc3034..898edc3a 100644 --- a/include/beast/http.hpp +++ b/include/beast/http.hpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include diff --git a/include/beast/http/string_view_body.hpp b/include/beast/http/string_view_body.hpp new file mode 100644 index 00000000..b05c2c90 --- /dev/null +++ b/include/beast/http/string_view_body.hpp @@ -0,0 +1,88 @@ +// +// Copyright (c) 2013-2017 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) +// + +#ifndef BEAST_HTTP_STRING_VIEW_BODY +#define BEAST_HTTP_STRING_VIEW_BODY + +#include +#include +#include +#include +#include +#include + +namespace beast { +namespace http { + +/** A readable HTTP message body represented by a @ref string_view. + + The application must ensure that the memory pointed to + by the string view remains valid for the lifetime of + any attempted operations. + + Meets the requirements of @b Body. +*/ +struct string_view_body +{ + /// The type of the body member when used in a message. + using value_type = string_view; + + /// Returns the content length of this body in a message. + template + static + std::uint64_t + size(message const& m) + { + return m.body.size(); + } + +#if BEAST_DOXYGEN + /// The algorithm to obtain buffers representing the body + using reader = implementation_defined; +#else + class reader + { + string_view body_; + + public: + using is_deferred = std::false_type; + + using const_buffers_type = + boost::asio::const_buffers_1; + + template + explicit + reader(message< + isRequest, string_view_body, Fields> const& m) + : body_(m.body) + { + } + + void + init(error_code&) + { + } + + boost::optional> + get(error_code& ec) + { + return {{{body_.data(), body_.size()}, false}}; + } + + void + finish(error_code&) + { + } + }; +#endif +}; + +} // http +} // beast + +#endif diff --git a/test/Jamfile b/test/Jamfile index dd42c5b3..e149a636 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -62,6 +62,7 @@ unit-test http-tests : http/serializer.cpp http/status.cpp http/string_body.cpp + http/string_view_body.cpp http/type_traits.cpp http/verb.cpp http/write.cpp diff --git a/test/http/CMakeLists.txt b/test/http/CMakeLists.txt index 4f302ff3..1d306a55 100644 --- a/test/http/CMakeLists.txt +++ b/test/http/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable (http-tests serializer.cpp status.cpp string_body.cpp + string_view_body.cpp type_traits.cpp verb.cpp write.cpp diff --git a/test/http/string_view_body.cpp b/test/http/string_view_body.cpp new file mode 100644 index 00000000..3f5eb1a7 --- /dev/null +++ b/test/http/string_view_body.cpp @@ -0,0 +1,52 @@ +// +// Copyright (c) 2013-2017 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 + +#include +#include +#include +#include +#include +#include + +namespace beast { +namespace http { + +class string_view_body_test + : public beast::unit_test::suite +{ +public: + void + run() override + { + static_assert(is_body_reader::value, ""); + static_assert(! is_body_writer::value, ""); + request req{"Hello, world!"}; + req.version = 11; + req.method(verb::post); + req.target("/"); + req.prepare(); + static_buffer_n<512> b; + ostream(b) << req; + string_view const s{ + boost::asio::buffer_cast(*b.data().begin()), + boost::asio::buffer_size(*b.data().begin())}; + BEAST_EXPECT(s == + "POST / HTTP/1.1\r\n" + "Content-Length: 13\r\n" + "\r\n" + "Hello, world!"); + } +}; + +BEAST_DEFINE_TESTSUITE(string_view_body,http,beast); + +} // http +} // beast +