2017-07-08 18:47:33 -07:00
|
|
|
//
|
2019-02-21 07:00:31 -08:00
|
|
|
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2017-07-08 18:47:33 -07:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
2017-07-20 13:40:34 -07:00
|
|
|
// Official repository: https://github.com/boostorg/beast
|
|
|
|
//
|
2017-07-08 18:47:33 -07:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#ifndef BOOST_BEAST_HTTP_SPAN_BODY_HPP
|
|
|
|
#define BOOST_BEAST_HTTP_SPAN_BODY_HPP
|
2017-07-08 18:47:33 -07:00
|
|
|
|
2017-10-10 07:49:03 -07:00
|
|
|
#include <boost/beast/core/detail/config.hpp>
|
2019-03-05 08:25:54 -08:00
|
|
|
#include <boost/beast/core/buffer_traits.hpp>
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/core/span.hpp>
|
|
|
|
#include <boost/beast/http/error.hpp>
|
|
|
|
#include <boost/beast/http/message.hpp>
|
2017-07-08 18:47:33 -07:00
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2017-07-08 18:47:33 -07:00
|
|
|
namespace beast {
|
|
|
|
namespace http {
|
|
|
|
|
2019-03-05 12:05:00 -08:00
|
|
|
/** A <em>Body</em> using @ref span
|
2017-07-08 18:47:33 -07:00
|
|
|
|
|
|
|
This body uses @ref span as a memory-based container for
|
|
|
|
holding message payloads. The container represents a
|
2018-09-23 14:58:03 -04:00
|
|
|
non-owning reference to a contiguous area of memory.
|
2017-07-08 18:47:33 -07:00
|
|
|
Messages using this body type may be serialized and
|
|
|
|
parsed.
|
|
|
|
|
|
|
|
Unlike @ref buffer_body, only one buffer may be provided
|
|
|
|
during a parse or serialize operation.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
struct span_body
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
static_assert(std::is_pod<T>::value,
|
|
|
|
"POD 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 = span<T>;
|
|
|
|
|
|
|
|
/** 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();
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:08:52 -07:00
|
|
|
/** The algorithm for parsing the body
|
2017-07-08 18:47:33 -07:00
|
|
|
|
2019-03-05 12:05:00 -08:00
|
|
|
Meets the requirements of <em>BodyReader</em>.
|
2017-07-08 18:47:33 -07:00
|
|
|
*/
|
2017-07-20 13:40:34 -07:00
|
|
|
#if BOOST_BEAST_DOXYGEN
|
2018-11-11 21:53:13 -08:00
|
|
|
using reader = __implementation_defined__;
|
2017-07-08 18:47:33 -07:00
|
|
|
#else
|
|
|
|
class reader
|
|
|
|
{
|
|
|
|
value_type& body_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template<bool isRequest, class Fields>
|
|
|
|
explicit
|
2017-11-13 23:34:14 +01:00
|
|
|
reader(header<isRequest, Fields>&, value_type& b)
|
|
|
|
: body_(b)
|
2017-07-08 18:47:33 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init(boost::optional<
|
|
|
|
std::uint64_t> const& length, error_code& ec)
|
|
|
|
{
|
|
|
|
if(length && *length > body_.size())
|
|
|
|
{
|
|
|
|
ec = error::buffer_overflow;
|
|
|
|
return;
|
|
|
|
}
|
2019-01-20 09:50:43 -08:00
|
|
|
ec = {};
|
2017-07-08 18:47:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class ConstBufferSequence>
|
|
|
|
std::size_t
|
|
|
|
put(ConstBufferSequence const& buffers,
|
|
|
|
error_code& ec)
|
|
|
|
{
|
2019-03-05 08:47:02 -08:00
|
|
|
auto const n = buffer_bytes(buffers);
|
2017-07-08 18:47:33 -07:00
|
|
|
auto const len = body_.size();
|
|
|
|
if(n > len)
|
|
|
|
{
|
|
|
|
ec = error::buffer_overflow;
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-20 09:50:43 -08:00
|
|
|
ec = {};
|
2019-02-02 10:53:54 -08:00
|
|
|
net::buffer_copy(net::buffer(
|
2017-07-08 18:47:33 -07:00
|
|
|
body_.data(), n), buffers);
|
|
|
|
body_ = value_type{
|
|
|
|
body_.data() + n, body_.size() - n};
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
finish(error_code& ec)
|
|
|
|
{
|
2019-01-20 09:50:43 -08:00
|
|
|
ec = {};
|
2017-07-08 18:47:33 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
2017-10-30 17:08:52 -07:00
|
|
|
|
|
|
|
/** The algorithm for serializing the body
|
|
|
|
|
2019-03-05 12:05:00 -08:00
|
|
|
Meets the requirements of <em>BodyWriter</em>.
|
2017-10-30 17:08:52 -07:00
|
|
|
*/
|
|
|
|
#if BOOST_BEAST_DOXYGEN
|
2018-11-11 21:53:13 -08:00
|
|
|
using writer = __implementation_defined__;
|
2017-10-30 17:08:52 -07:00
|
|
|
#else
|
|
|
|
class writer
|
|
|
|
{
|
|
|
|
value_type const& body_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using const_buffers_type =
|
2018-11-30 14:58:38 -08:00
|
|
|
net::const_buffer;
|
2017-10-30 17:08:52 -07:00
|
|
|
|
|
|
|
template<bool isRequest, class Fields>
|
|
|
|
explicit
|
2017-11-13 23:34:14 +01:00
|
|
|
writer(header<isRequest, Fields> const&, value_type const& b)
|
|
|
|
: body_(b)
|
2017-10-30 17:08:52 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init(error_code& ec)
|
|
|
|
{
|
2019-01-20 09:50:43 -08:00
|
|
|
ec = {};
|
2017-10-30 17:08:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<std::pair<const_buffers_type, bool>>
|
|
|
|
get(error_code& ec)
|
|
|
|
{
|
2019-01-20 09:50:43 -08:00
|
|
|
ec = {};
|
2017-10-30 17:08:52 -07:00
|
|
|
return {{
|
|
|
|
{ body_.data(),
|
|
|
|
body_.size() * sizeof(typename
|
|
|
|
value_type::value_type)},
|
|
|
|
false}};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
2017-07-08 18:47:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // http
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|
2017-07-08 18:47:33 -07:00
|
|
|
|
|
|
|
#endif
|