Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:30

0001 //
0002 // Copyright (c) 2022 Seth Heeren (sgheeren 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_IMPL_MESSAGE_GENERATOR_HPP
0011 #define BOOST_BEAST_HTTP_IMPL_MESSAGE_GENERATOR_HPP
0012 
0013 #include <boost/beast/http/message_generator.hpp>
0014 #include <boost/smart_ptr/make_unique.hpp>
0015 #include <boost/beast/core/buffers_generator.hpp>
0016 
0017 namespace boost {
0018 namespace beast {
0019 namespace http {
0020 
0021 template <bool isRequest, class Body, class Fields>
0022 message_generator::message_generator(
0023     http::message<isRequest, Body, Fields>&& m)
0024     : impl_(boost::make_unique<
0025             generator_impl<isRequest, Body, Fields>>(
0026           std::move(m)))
0027 {
0028 }
0029 
0030 template <bool isRequest, class Body, class Fields>
0031 struct message_generator::generator_impl
0032     : message_generator::impl_base
0033 {
0034     explicit generator_impl(
0035         http::message<isRequest, Body, Fields>&& m)
0036         : m_(std::move(m))
0037         , sr_(m_)
0038     {
0039     }
0040 
0041     bool
0042     is_done() override
0043     {
0044         return sr_.is_done();
0045     }
0046 
0047     const_buffers_type
0048     prepare(error_code& ec) override
0049     {
0050         sr_.next(ec, visit{*this});
0051         return current_;
0052     }
0053 
0054     void
0055     consume(std::size_t n) override
0056     {
0057         sr_.consume((std::min)(n, beast::buffer_bytes(current_)));
0058     }
0059 
0060     bool
0061     keep_alive() const noexcept override
0062     {
0063         return m_.keep_alive();
0064     }
0065 
0066 private:
0067     static constexpr unsigned max_fixed_bufs = 12;
0068 
0069     http::message<isRequest, Body, Fields> m_;
0070     http::serializer<isRequest, Body, Fields> sr_;
0071 
0072     std::array<net::const_buffer, max_fixed_bufs> bs_;
0073     const_buffers_type current_ = bs_; // subspan
0074 
0075     struct visit
0076     {
0077         generator_impl& self_;
0078 
0079         template<class ConstBufferSequence>
0080         void
0081         operator()(error_code&, ConstBufferSequence const& buffers)
0082         {
0083             auto& s = self_.bs_;
0084             auto& cur = self_.current_;
0085 
0086             auto it = net::buffer_sequence_begin(buffers);
0087 
0088             std::size_t n =
0089                 std::distance(it, net::buffer_sequence_end(buffers));
0090 
0091             n = (std::min)(s.size(), n);
0092 
0093             cur = { s.data(), n };
0094             std::copy_n(it, n, cur.begin());
0095         }
0096     };
0097 
0098 };
0099 
0100 } // namespace http
0101 } // namespace beast
0102 } // namespace boost
0103 
0104 #endif