mirror of
https://github.com/boostorg/beast.git
synced 2025-08-03 06:44:39 +02:00
Add string_view_body
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
Version 56:
|
||||
|
||||
* Add provisional IANA header field names
|
||||
* Add string_view_body
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
@@ -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]
|
||||
|
@@ -46,6 +46,7 @@
|
||||
<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__string_body">string_body</link></member>
|
||||
<member><link linkend="beast.ref.http__string_view_body">string_view_body</link></member>
|
||||
</simplelist>
|
||||
<bridgehead renderas="sect3">rfc7230</bridgehead>
|
||||
<simplelist type="vert" columns="1">
|
||||
|
@@ -24,6 +24,7 @@
|
||||
#include <beast/core/handler_ptr.hpp>
|
||||
#include <beast/core/multi_buffer.hpp>
|
||||
#include <beast/core/ostream.hpp>
|
||||
#include <beast/core/read_size.hpp>
|
||||
#include <beast/core/static_buffer.hpp>
|
||||
#include <beast/core/static_string.hpp>
|
||||
#include <beast/core/string_param.hpp>
|
||||
|
@@ -24,6 +24,7 @@
|
||||
#include <beast/http/serializer.hpp>
|
||||
#include <beast/http/status.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <beast/http/string_view_body.hpp>
|
||||
#include <beast/http/verb.hpp>
|
||||
#include <beast/http/write.hpp>
|
||||
|
||||
|
88
include/beast/http/string_view_body.hpp
Normal file
88
include/beast/http/string_view_body.hpp
Normal 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
|
@@ -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
|
||||
|
@@ -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
|
||||
|
52
test/http/string_view_body.cpp
Normal file
52
test/http/string_view_body.cpp
Normal 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
|
||||
|
Reference in New Issue
Block a user