Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-16 08:38:33

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_HTTP_SPAN_BODY_HPP
0011 #define BOOST_BEAST_HTTP_SPAN_BODY_HPP
0012 
0013 #include <boost/beast/http/span_body_fwd.hpp>
0014 
0015 #include <boost/beast/core/buffer_traits.hpp>
0016 #include <boost/beast/core/detail/config.hpp>
0017 #include <boost/beast/core/span.hpp>
0018 #include <boost/beast/http/error.hpp>
0019 #include <boost/beast/http/message.hpp>
0020 #include <boost/optional.hpp>
0021 
0022 namespace boost {
0023 namespace beast {
0024 namespace http {
0025 
0026 /** A <em>Body</em> using @ref span
0027 
0028     This body uses @ref span as a memory-based container for
0029     holding message payloads. The container represents a
0030     non-owning reference to a contiguous area of memory.
0031     Messages using this body type may be serialized and
0032     parsed.
0033 
0034     Unlike @ref buffer_body, only one buffer may be provided
0035     during a parse or serialize operation.
0036 */
0037 template<class T>
0038 struct span_body
0039 {
0040 private:
0041     static_assert(
0042         std::is_trivially_default_constructible<T>::value &&
0043         std::is_trivially_copyable<T>::value &&
0044         std::is_standard_layout<T>::value,
0045             "POD requirements not met");
0046 
0047 public:
0048     /** The type of container used for the body
0049 
0050         This determines the type of @ref message::body
0051         when this body type is used with a message container.
0052     */
0053     using value_type = span<T>;
0054 
0055     /** Returns the payload size of the body
0056 
0057         When this body is used with @ref message::prepare_payload,
0058         the Content-Length will be set to the payload size, and
0059         any chunked Transfer-Encoding will be removed.
0060     */
0061     static
0062     std::uint64_t
0063     size(value_type const& body)
0064     {
0065         return body.size();
0066     }
0067 
0068     /** The algorithm for parsing the body
0069 
0070         Meets the requirements of <em>BodyReader</em>.
0071     */
0072 #if BOOST_BEAST_DOXYGEN
0073     using reader = __implementation_defined__;
0074 #else
0075     class reader
0076     {
0077         value_type& body_;
0078 
0079     public:
0080         template<bool isRequest, class Fields>
0081         explicit
0082         reader(header<isRequest, Fields>&, value_type& b)
0083             : body_(b)
0084         {
0085         }
0086 
0087         void
0088         init(boost::optional<
0089             std::uint64_t> const& length, error_code& ec)
0090         {
0091             if(length && *length > body_.size())
0092             {
0093                 BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
0094                 return;
0095             }
0096             ec = {};
0097         }
0098 
0099         template<class ConstBufferSequence>
0100         std::size_t
0101         put(ConstBufferSequence const& buffers,
0102             error_code& ec)
0103         {
0104             auto const n = buffer_bytes(buffers);
0105             auto const len = body_.size();
0106             if(n > len)
0107             {
0108                 BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
0109                 return 0;
0110             }
0111             ec = {};
0112             net::buffer_copy(net::buffer(
0113                 body_.data(), n), buffers);
0114             body_ = value_type{
0115                 body_.data() + n, body_.size() - n};
0116             return n;
0117         }
0118 
0119         void
0120         finish(error_code& ec)
0121         {
0122             ec = {};
0123         }
0124     };
0125 #endif
0126 
0127     /** The algorithm for serializing the body
0128 
0129         Meets the requirements of <em>BodyWriter</em>.
0130     */
0131 #if BOOST_BEAST_DOXYGEN
0132     using writer = __implementation_defined__;
0133 #else
0134     class writer
0135     {
0136         value_type const& body_;
0137 
0138     public:
0139         using const_buffers_type =
0140             net::const_buffer;
0141 
0142         template<bool isRequest, class Fields>
0143         explicit
0144         writer(header<isRequest, Fields> const&, value_type const& b)
0145             : body_(b)
0146         {
0147         }
0148 
0149         void
0150         init(error_code& ec)
0151         {
0152             ec = {};
0153         }
0154 
0155         boost::optional<std::pair<const_buffers_type, bool>>
0156         get(error_code& ec)
0157         {
0158             ec = {};
0159             return {{
0160                 { body_.data(),
0161                   body_.size() * sizeof(typename
0162                     value_type::value_type)},
0163                 false}};
0164         }
0165     };
0166 #endif
0167 };
0168 
0169 } // http
0170 } // beast
0171 } // boost
0172 
0173 #endif