2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_HTTP_SPAN_BODY_HPP
11 #define BOOST_BEAST_HTTP_SPAN_BODY_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/span.hpp>
15 #include <boost/beast/http/error.hpp>
16 #include <boost/beast/http/message.hpp>
17 #include <boost/optional.hpp>
23 /** A @b Body using @ref span
25 This body uses @ref span as a memory-based container for
26 holding message payloads. The container represents a
27 non-owning reference to a continguous area of memory.
28 Messages using this body type may be serialized and
31 Unlike @ref buffer_body, only one buffer may be provided
32 during a parse or serialize operation.
38 static_assert(std::is_pod<T>::value,
39 "POD requirements not met");
42 /** The type of container used for the body
44 This determines the type of @ref message::body
45 when this body type is used with a message container.
47 using value_type = span<T>;
49 /** Returns the payload size of the body
51 When this body is used with @ref message::prepare_payload,
52 the Content-Length will be set to the payload size, and
53 any chunked Transfer-Encoding will be removed.
57 size(value_type const& body)
62 /** The algorithm for parsing the body
64 Meets the requirements of @b BodyReader.
66 #if BOOST_BEAST_DOXYGEN
67 using reader = implementation_defined;
74 template<bool isRequest, class Fields>
76 reader(message<isRequest,
77 span_body, Fields>& m)
84 std::uint64_t> const& length, error_code& ec)
86 if(length && *length > body_.size())
88 ec = error::buffer_overflow;
91 ec.assign(0, ec.category());
94 template<class ConstBufferSequence>
96 put(ConstBufferSequence const& buffers,
99 using boost::asio::buffer_size;
100 using boost::asio::buffer_copy;
101 auto const n = buffer_size(buffers);
102 auto const len = body_.size();
105 ec = error::buffer_overflow;
108 ec.assign(0, ec.category());
109 buffer_copy(boost::asio::buffer(
110 body_.data(), n), buffers);
112 body_.data() + n, body_.size() - n};
117 finish(error_code& ec)
119 ec.assign(0, ec.category());
124 /** The algorithm for serializing the body
126 Meets the requirements of @b BodyWriter.
128 #if BOOST_BEAST_DOXYGEN
129 using writer = implementation_defined;
133 value_type const& body_;
136 using const_buffers_type =
137 boost::asio::const_buffer;
139 template<bool isRequest, class Fields>
141 writer(message<isRequest,
142 span_body, Fields> const& msg)
150 ec.assign(0, ec.category());
153 boost::optional<std::pair<const_buffers_type, bool>>
156 ec.assign(0, ec.category());
159 body_.size() * sizeof(typename
160 value_type::value_type)},