Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08: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_STRING_BODY_HPP
0011 #define BOOST_BEAST_HTTP_STRING_BODY_HPP
0012 
0013 #include <boost/beast/http/string_body_fwd.hpp>
0014 
0015 #include <boost/beast/core/buffer_traits.hpp>
0016 #include <boost/beast/core/buffers_range.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/asio/buffer.hpp>
0022 #include <boost/optional.hpp>
0023 #include <cstdint>
0024 #include <string>
0025 #include <utility>
0026 
0027 namespace boost {
0028 namespace beast {
0029 namespace http {
0030 
0031 /** A <em>Body</em> using `std::basic_string`
0032 
0033     This body uses `std::basic_string` as a memory-based container
0034     for holding message payloads. Messages using this body type
0035     may be serialized and parsed.
0036 */
0037 #if BOOST_BEAST_DOXYGEN
0038 template<
0039     class CharT,
0040     class Traits = std::char_traits<CharT>,
0041     class Allocator = std::allocator<CharT>>
0042 #else
0043 template<
0044     class CharT,
0045     class Traits,
0046     class Allocator>
0047 #endif
0048 struct basic_string_body
0049 {
0050 private:
0051     static_assert(
0052         std::is_integral<CharT>::value &&
0053             sizeof(CharT) == 1,
0054         "CharT requirements not met");
0055 
0056 public:
0057     /** The type of container used for the body
0058 
0059         This determines the type of @ref message::body
0060         when this body type is used with a message container.
0061     */
0062     using value_type =
0063         std::basic_string<CharT, Traits, Allocator>;
0064 
0065     /** Returns the payload size of the body
0066 
0067         When this body is used with @ref message::prepare_payload,
0068         the Content-Length will be set to the payload size, and
0069         any chunked Transfer-Encoding will be removed.
0070     */
0071     static
0072     std::uint64_t
0073     size(value_type const& body)
0074     {
0075         return body.size();
0076     }
0077 
0078     /** The algorithm for parsing the body
0079 
0080         Meets the requirements of <em>BodyReader</em>.
0081     */
0082 #if BOOST_BEAST_DOXYGEN
0083     using reader = __implementation_defined__;
0084 #else
0085     class reader
0086     {
0087         value_type& body_;
0088 
0089     public:
0090         template<bool isRequest, class Fields>
0091         explicit
0092         reader(header<isRequest, Fields>&, value_type& b)
0093             : body_(b)
0094         {
0095         }
0096 
0097         void
0098         init(boost::optional<
0099             std::uint64_t> const& length, error_code& ec)
0100         {
0101             if(length)
0102             {
0103                 if(*length > body_.max_size())
0104                 {
0105                     BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
0106                     return;
0107                 }
0108                 body_.reserve(beast::detail::clamp(*length));
0109             }
0110             ec = {};
0111         }
0112 
0113         template<class ConstBufferSequence>
0114         std::size_t
0115         put(ConstBufferSequence const& buffers,
0116             error_code& ec)
0117         {
0118             auto const extra = buffer_bytes(buffers);
0119             auto const size = body_.size();
0120             if (extra > body_.max_size() - size)
0121             {
0122                 BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
0123                 return 0;
0124             }
0125 
0126             body_.resize(size + extra);
0127             ec = {};
0128             CharT* dest = &body_[size];
0129             for(auto b : beast::buffers_range_ref(buffers))
0130             {
0131                 Traits::copy(dest, static_cast<
0132                     CharT const*>(b.data()), b.size());
0133                 dest += b.size();
0134             }
0135             return extra;
0136         }
0137 
0138         void
0139         finish(error_code& ec)
0140         {
0141             ec = {};
0142         }
0143     };
0144 #endif
0145 
0146     /** The algorithm for serializing the body
0147 
0148         Meets the requirements of <em>BodyWriter</em>.
0149     */
0150 #if BOOST_BEAST_DOXYGEN
0151     using writer = __implementation_defined__;
0152 #else
0153     class writer
0154     {
0155         value_type const& body_;
0156 
0157     public:
0158         using const_buffers_type =
0159             net::const_buffer;
0160 
0161         template<bool isRequest, class Fields>
0162         explicit
0163         writer(header<isRequest, Fields> const&, value_type const& b)
0164             : body_(b)
0165         {
0166         }
0167 
0168         void
0169         init(error_code& ec)
0170         {
0171             ec = {};
0172         }
0173 
0174         boost::optional<std::pair<const_buffers_type, bool>>
0175         get(error_code& ec)
0176         {
0177             ec = {};
0178             return {{const_buffers_type{
0179                 body_.data(), body_.size()}, false}};
0180         }
0181     };
0182 #endif
0183 };
0184 
0185 #if BOOST_BEAST_DOXYGEN
0186 /// A <em>Body</em> using `std::string`
0187 using string_body = basic_string_body<char>;
0188 #endif
0189 
0190 } // http
0191 } // beast
0192 } // boost
0193 
0194 #endif