Add string_view_body

This commit is contained in:
Vinnie Falco
2017-06-12 18:39:04 -07:00
parent 914765c3cf
commit 7712ca09b2
9 changed files with 154 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
Version 56: Version 56:
* Add provisional IANA header field names * Add provisional IANA header field names
* Add string_view_body
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@@ -118,6 +118,14 @@ __Body__ requirements:
Messages with this body type may be serialized and parsed. This Messages with this body type may be serialized and parsed. This
is the type of body used in the examples. 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] [heading Usage]

View File

@@ -46,6 +46,7 @@
<member><link linkend="beast.ref.http__response_parser">response_parser</link></member> <member><link linkend="beast.ref.http__response_parser">response_parser</link></member>
<member><link linkend="beast.ref.http__serializer">serializer</link></member> <member><link linkend="beast.ref.http__serializer">serializer</link></member>
<member><link linkend="beast.ref.http__string_body">string_body</link></member> <member><link linkend="beast.ref.http__string_body">string_body</link></member>
<member><link linkend="beast.ref.http__string_view_body">string_view_body</link></member>
</simplelist> </simplelist>
<bridgehead renderas="sect3">rfc7230</bridgehead> <bridgehead renderas="sect3">rfc7230</bridgehead>
<simplelist type="vert" columns="1"> <simplelist type="vert" columns="1">

View File

@@ -24,6 +24,7 @@
#include <beast/core/handler_ptr.hpp> #include <beast/core/handler_ptr.hpp>
#include <beast/core/multi_buffer.hpp> #include <beast/core/multi_buffer.hpp>
#include <beast/core/ostream.hpp> #include <beast/core/ostream.hpp>
#include <beast/core/read_size.hpp>
#include <beast/core/static_buffer.hpp> #include <beast/core/static_buffer.hpp>
#include <beast/core/static_string.hpp> #include <beast/core/static_string.hpp>
#include <beast/core/string_param.hpp> #include <beast/core/string_param.hpp>

View File

@@ -24,6 +24,7 @@
#include <beast/http/serializer.hpp> #include <beast/http/serializer.hpp>
#include <beast/http/status.hpp> #include <beast/http/status.hpp>
#include <beast/http/string_body.hpp> #include <beast/http/string_body.hpp>
#include <beast/http/string_view_body.hpp>
#include <beast/http/verb.hpp> #include <beast/http/verb.hpp>
#include <beast/http/write.hpp> #include <beast/http/write.hpp>

View File

@@ -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 <beast/config.hpp>
#include <beast/core/string_view.hpp>
#include <beast/http/message.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/optional.hpp>
#include <utility>
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<bool isRequest, class Fields>
static
std::uint64_t
size(message<isRequest,
string_view_body, Fields> 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<bool isRequest, class Fields>
explicit
reader(message<
isRequest, string_view_body, Fields> const& m)
: body_(m.body)
{
}
void
init(error_code&)
{
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
return {{{body_.data(), body_.size()}, false}};
}
void
finish(error_code&)
{
}
};
#endif
};
} // http
} // beast
#endif

View File

@@ -62,6 +62,7 @@ unit-test http-tests :
http/serializer.cpp http/serializer.cpp
http/status.cpp http/status.cpp
http/string_body.cpp http/string_body.cpp
http/string_view_body.cpp
http/type_traits.cpp http/type_traits.cpp
http/verb.cpp http/verb.cpp
http/write.cpp http/write.cpp

View File

@@ -28,6 +28,7 @@ add_executable (http-tests
serializer.cpp serializer.cpp
status.cpp status.cpp
string_body.cpp string_body.cpp
string_view_body.cpp
type_traits.cpp type_traits.cpp
verb.cpp verb.cpp
write.cpp write.cpp

View File

@@ -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 <beast/http/string_view_body.hpp>
#include <beast/core/ostream.hpp>
#include <beast/core/static_buffer.hpp>
#include <beast/http/message.hpp>
#include <beast/http/write.hpp>
#include <beast/http/type_traits.hpp>
#include <beast/unit_test/suite.hpp>
namespace beast {
namespace http {
class string_view_body_test
: public beast::unit_test::suite
{
public:
void
run() override
{
static_assert(is_body_reader<string_view_body>::value, "");
static_assert(! is_body_writer<string_view_body>::value, "");
request<string_view_body> 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<char const*>(*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