// // Copyright (c) 2016-2019 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) // // Official repository: https://github.com/boostorg/beast // #ifndef BOOST_BEAST_HTTP_BASIC_DYNAMIC_BODY_HPP #define BOOST_BEAST_HTTP_BASIC_DYNAMIC_BODY_HPP #include #include #include #include #include #include #include #include #include #include namespace boost { namespace beast { namespace http { /** A Body using a DynamicBuffer This body uses a DynamicBuffer as a memory-based container for holding message payloads. Messages using this body type may be serialized and parsed. */ template struct basic_dynamic_body { static_assert( net::is_dynamic_buffer::value, "DynamicBuffer type requirements not met"); /** 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 = DynamicBuffer; /** 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& v) { return v.size(); } /** The algorithm for parsing the body Meets the requirements of BodyReader. */ #if BOOST_BEAST_DOXYGEN using reader = __implementation_defined__; #else class reader { value_type& body_; public: template explicit reader(header&, value_type& b) : body_(b) { } void init(boost::optional< std::uint64_t> const&, error_code& ec) { ec = {}; } template std::size_t put(ConstBufferSequence const& buffers, error_code& ec) { auto const n = buffer_bytes(buffers); if(beast::detail::sum_exceeds(body_.size(), n, body_.max_size())) { ec = error::buffer_overflow; return 0; } auto const mb = beast::detail::dynamic_buffer_prepare( body_, (std::min)(n, body_.max_size() - body_.size()), ec, error::buffer_overflow); if(ec) return 0; auto const bytes_transferred = net::buffer_copy(*mb, buffers); body_.commit(bytes_transferred); return bytes_transferred; } void finish(error_code& ec) { ec = {}; } }; #endif /** The algorithm for serializing the body Meets the requirements of BodyWriter. */ #if BOOST_BEAST_DOXYGEN using writer = __implementation_defined__; #else class writer { DynamicBuffer const& body_; public: using const_buffers_type = typename DynamicBuffer::const_buffers_type; template explicit writer(header const&, value_type const& b) : body_(b) { } void init(error_code& ec) { ec = {}; } boost::optional> get(error_code& ec) { ec = {}; return {{body_.data(), false}}; } }; #endif }; } // http } // beast } // boost #endif