Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:24:16

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_VECTOR_BODY_HPP
0011 #define BOOST_BEAST_HTTP_VECTOR_BODY_HPP
0012 
0013 #include <boost/beast/http/vector_body_fwd.hpp>
0014 
0015 #include <boost/beast/core/buffer_traits.hpp>
0016 #include <boost/beast/core/detail/clamp.hpp>
0017 #include <boost/beast/core/detail/config.hpp>
0018 #include <boost/beast/http/error.hpp>
0019 #include <boost/beast/http/message.hpp>
0020 #include <boost/asio/buffer.hpp>
0021 #include <boost/optional.hpp>
0022 #include <cstdint>
0023 #include <utility>
0024 #include <vector>
0025 
0026 namespace boost {
0027 namespace beast {
0028 namespace http {
0029 
0030 /** A <em>Body</em> using `std::vector`
0031 
0032     This body uses `std::vector` as a memory-based container
0033     for holding message payloads. Messages using this body type
0034     may be serialized and parsed.
0035 */
0036 #if BOOST_BEAST_DOXYGEN
0037 template<class T, class Allocator = std::allocator<T>>
0038 #else
0039 template<class T, class Allocator>
0040 #endif
0041 struct vector_body
0042 {
0043 private:
0044     static_assert(sizeof(T) == 1,
0045         "T 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 = std::vector<T, Allocator>;
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)
0092             {
0093                 if(*length > body_.max_size())
0094                 {
0095                     BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
0096                     return;
0097                 }
0098                 body_.reserve(beast::detail::clamp(*length));
0099             }
0100             ec = {};
0101         }
0102 
0103         template<class ConstBufferSequence>
0104         std::size_t
0105         put(ConstBufferSequence const& buffers,
0106             error_code& ec)
0107         {
0108             auto const n = buffer_bytes(buffers);
0109             auto const len = body_.size();
0110             if (n > body_.max_size() - len)
0111             {
0112                 BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
0113                 return 0;
0114             }
0115 
0116             body_.resize(len + n);
0117             ec = {};
0118             return net::buffer_copy(net::buffer(
0119                 &body_[0] + len, n), buffers);
0120         }
0121 
0122         void
0123         finish(error_code& ec)
0124         {
0125             ec = {};
0126         }
0127     };
0128 #endif
0129 
0130     /** The algorithm for serializing the body
0131 
0132         Meets the requirements of <em>BodyWriter</em>.
0133     */
0134 #if BOOST_BEAST_DOXYGEN
0135     using writer = __implementation_defined__;
0136 #else
0137     class writer
0138     {
0139         value_type const& body_;
0140 
0141     public:
0142         using const_buffers_type =
0143             net::const_buffer;
0144 
0145         template<bool isRequest, class Fields>
0146         explicit
0147         writer(header<isRequest, Fields> const&, value_type const& b)
0148             : body_(b)
0149         {
0150         }
0151 
0152         void
0153         init(error_code& ec)
0154         {
0155             ec = {};
0156         }
0157 
0158         boost::optional<std::pair<const_buffers_type, bool>>
0159         get(error_code& ec)
0160         {
0161             ec = {};
0162             return {{const_buffers_type{
0163                 body_.data(), body_.size()}, false}};
0164         }
0165     };
0166 #endif
0167 };
0168 
0169 } // http
0170 } // beast
0171 } // boost
0172 
0173 #endif