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