Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:32

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