Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:36:06

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_trivial<T>::value &&
0043         std::is_standard_layout<T>::value,
0044             "POD requirements not met");
0045 
0046 public:
0047     /** The type of container used for the body
0048 
0049         This determines the type of @ref message::body
0050         when this body type is used with a message container.
0051     */
0052     using value_type = span<T>;
0053 
0054     /** Returns the payload size of the body
0055 
0056         When this body is used with @ref message::prepare_payload,
0057         the Content-Length will be set to the payload size, and
0058         any chunked Transfer-Encoding will be removed.
0059     */
0060     static
0061     std::uint64_t
0062     size(value_type const& body)
0063     {
0064         return body.size();
0065     }
0066 
0067     /** The algorithm for parsing the body
0068 
0069         Meets the requirements of <em>BodyReader</em>.
0070     */
0071 #if BOOST_BEAST_DOXYGEN
0072     using reader = __implementation_defined__;
0073 #else
0074     class reader
0075     {
0076         value_type& body_;
0077 
0078     public:
0079         template<bool isRequest, class Fields>
0080         explicit
0081         reader(header<isRequest, Fields>&, value_type& b)
0082             : body_(b)
0083         {
0084         }
0085 
0086         void
0087         init(boost::optional<
0088             std::uint64_t> const& length, error_code& ec)
0089         {
0090             if(length && *length > body_.size())
0091             {
0092                 BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
0093                 return;
0094             }
0095             ec = {};
0096         }
0097 
0098         template<class ConstBufferSequence>
0099         std::size_t
0100         put(ConstBufferSequence const& buffers,
0101             error_code& ec)
0102         {
0103             auto const n = buffer_bytes(buffers);
0104             auto const len = body_.size();
0105             if(n > len)
0106             {
0107                 BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
0108                 return 0;
0109             }
0110             ec = {};
0111             net::buffer_copy(net::buffer(
0112                 body_.data(), n), buffers);
0113             body_ = value_type{
0114                 body_.data() + n, body_.size() - n};
0115             return n;
0116         }
0117 
0118         void
0119         finish(error_code& ec)
0120         {
0121             ec = {};
0122         }
0123     };
0124 #endif
0125 
0126     /** The algorithm for serializing the body
0127 
0128         Meets the requirements of <em>BodyWriter</em>.
0129     */
0130 #if BOOST_BEAST_DOXYGEN
0131     using writer = __implementation_defined__;
0132 #else
0133     class writer
0134     {
0135         value_type const& body_;
0136 
0137     public:
0138         using const_buffers_type =
0139             net::const_buffer;
0140 
0141         template<bool isRequest, class Fields>
0142         explicit
0143         writer(header<isRequest, Fields> const&, value_type const& b)
0144             : body_(b)
0145         {
0146         }
0147 
0148         void
0149         init(error_code& ec)
0150         {
0151             ec = {};
0152         }
0153 
0154         boost::optional<std::pair<const_buffers_type, bool>>
0155         get(error_code& ec)
0156         {
0157             ec = {};
0158             return {{
0159                 { body_.data(),
0160                   body_.size() * sizeof(typename
0161                     value_type::value_type)},
0162                 false}};
0163         }
0164     };
0165 #endif
0166 };
0167 
0168 } // http
0169 } // beast
0170 } // boost
0171 
0172 #endif