Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:34:09

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