Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:29:31

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