Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:03:51

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