Add vector_body

fix #580
This commit is contained in:
Vinnie Falco
2017-07-08 15:12:26 -07:00
parent c6d7fa185b
commit e1de01ad13
8 changed files with 220 additions and 7 deletions

View File

@ -3,6 +3,7 @@ Version 78:
HTTP:
* Tidy up basic_string_body
* Add vector_body
--------------------------------------------------------------------------------

View File

@ -169,6 +169,13 @@ meet the requirements, or use the ones that come with the library:
is responsible for managing the lifetime of the buffer pointed to
by the string view.
]]
[[
[link beast.ref.beast__http__vector_body `vector_body`]
][
A body whose `value_type` is `std::vector`. Insertion complexity
is amortized constant time, while capacity grows geometrically.
Messages with this body type may be serialized and parsed.
]]
]
[heading Usage]

View File

@ -53,13 +53,7 @@
<member><link linkend="beast.ref.beast__http__serializer">serializer</link></member>
<member><link linkend="beast.ref.beast__http__string_body">string_body</link></member>
<member><link linkend="beast.ref.beast__http__string_view_body">string_view_body</link></member>
</simplelist>
<bridgehead renderas="sect3">rfc7230</bridgehead>
<simplelist type="vert" columns="1">
<member><link linkend="beast.ref.beast__http__ext_list">ext_list</link></member>
<member><link linkend="beast.ref.beast__http__opt_token_list">opt_token_list</link></member>
<member><link linkend="beast.ref.beast__http__param_list">param_list</link></member>
<member><link linkend="beast.ref.beast__http__token_list">token_list</link></member>
<member><link linkend="beast.ref.beast__http__vector_body">vector_body</link></member>
</simplelist>
</entry>
<entry valign="top">
@ -86,6 +80,13 @@
<member><link linkend="beast.ref.beast__http__write_header">write_header</link></member>
<member><link linkend="beast.ref.beast__http__write_some">write_some</link></member>
</simplelist>
<bridgehead renderas="sect3">rfc7230</bridgehead>
<simplelist type="vert" columns="1">
<member><link linkend="beast.ref.beast__http__ext_list">ext_list</link></member>
<member><link linkend="beast.ref.beast__http__opt_token_list">opt_token_list</link></member>
<member><link linkend="beast.ref.beast__http__param_list">param_list</link></member>
<member><link linkend="beast.ref.beast__http__token_list">token_list</link></member>
</simplelist>
</entry>
<entry valign="top">
<bridgehead renderas="sect3">Constants</bridgehead>

View File

@ -27,6 +27,7 @@
#include <beast/http/string_body.hpp>
#include <beast/http/string_view_body.hpp>
#include <beast/http/verb.hpp>
#include <beast/http/vector_body.hpp>
#include <beast/http/write.hpp>
#endif

View File

@ -0,0 +1,182 @@
//
// 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_VECTOR_BODY_HPP
#define BEAST_HTTP_VECTOR_BODY_HPP
#include <beast/config.hpp>
#include <beast/http/error.hpp>
#include <beast/http/message.hpp>
#include <beast/core/detail/type_traits.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/optional.hpp>
#include <cstdint>
#include <limits>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
namespace beast {
namespace http {
/** A @b Body using `std::vector`
This body uses `std::vector` as a memory-based container
for holding message payloads. Messages using this body type
may be serialized and parsed.
*/
template<class T, class Allocator = std::allocator<T>>
struct vector_body
{
private:
static_assert(sizeof(T) == 1 &&
std::is_integral<T>::value,
"T requirements not met");
public:
/** The type of container used for the body
This determines the type of @ref message::body
when this body type is used with a message container.
*/
using value_type = std::vector<T, Allocator>;
/** Returns the payload size of the body
When this body is used with @ref message::prepare_payload,
the Content-Length will be set to the payload size, and
any chunked Transfer-Encoding will be removed.
*/
static
std::uint64_t
size(value_type const& body)
{
return body.size();
}
/** The algorithm for serializing the body
Meets the requirements of @b BodyReader.
*/
#if BEAST_DOXYGEN
using reader = implementation_defined;
#else
class reader
{
value_type const& body_;
public:
using const_buffers_type =
boost::asio::const_buffers_1;
template<bool isRequest, class Fields>
explicit
reader(message<isRequest,
vector_body, Fields> const& msg)
: body_(msg.body)
{
}
void
init(error_code& ec)
{
ec.assign(0, ec.category());
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
ec.assign(0, ec.category());
return {{const_buffers_type{
body_.data(), body_.size()}, false}};
}
};
#endif
/** The algorithm for parsing the body
Meets the requirements of @b BodyReader.
*/
#if BEAST_DOXYGEN
using writer = implementation_defined;
#else
class writer
{
value_type& body_;
public:
template<bool isRequest, class Fields>
explicit
writer(message<isRequest,
vector_body, Fields>& m)
: body_(m.body)
{
}
void
init(boost::optional<
std::uint64_t> const& length, error_code& ec)
{
if(length)
{
if(*length > (
std::numeric_limits<std::size_t>::max)())
{
ec = error::buffer_overflow;
return;
}
try
{
body_.reserve(
static_cast<std::size_t>(*length));
}
catch(std::exception const&)
{
ec = error::buffer_overflow;
return;
}
}
ec.assign(0, ec.category());
}
template<class ConstBufferSequence>
std::size_t
put(ConstBufferSequence const& buffers,
error_code& ec)
{
using boost::asio::buffer_size;
using boost::asio::buffer_copy;
auto const n = buffer_size(buffers);
auto const len = body_.size();
try
{
body_.resize(len + n);
}
catch(std::exception const&)
{
ec = error::buffer_overflow;
return 0;
}
ec.assign(0, ec.category());
return buffer_copy(boost::asio::buffer(
&body_[0] + len, n), buffers);
}
void
finish(error_code& ec)
{
ec.assign(0, ec.category());
}
};
#endif
};
} // http
} // beast
#endif

View File

@ -32,6 +32,7 @@ add_executable (http-tests
string_body.cpp
string_view_body.cpp
type_traits.cpp
vector_body.cpp
verb.cpp
write.cpp
)

View File

@ -25,6 +25,7 @@ unit-test http-tests :
string_body.cpp
string_view_body.cpp
type_traits.cpp
vector_body.cpp
verb.cpp
write.cpp
;

19
test/http/vector_body.cpp Normal file
View File

@ -0,0 +1,19 @@
//
// 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/vector_body.hpp>
namespace beast {
namespace http {
BOOST_STATIC_ASSERT(is_body<vector_body<char>>::value);
BOOST_STATIC_ASSERT(is_body_reader<vector_body<char>>::value);
BOOST_STATIC_ASSERT(is_body_writer<vector_body<char>>::value);
} // http
} // beast