Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:36:06

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 #ifndef BOOST_BEAST_HTTP_MESSAGE_GENERATOR_HPP
0010 #define BOOST_BEAST_HTTP_MESSAGE_GENERATOR_HPP
0011 
0012 #include <boost/beast/http/message_generator_fwd.hpp>
0013 
0014 #include <boost/beast/core/span.hpp>
0015 #include <boost/beast/http/message.hpp>
0016 #include <boost/beast/http/serializer.hpp>
0017 #include <memory>
0018 
0019 namespace boost {
0020 namespace beast {
0021 namespace http {
0022 
0023 /** Type-erased buffers generator for @ref http::message
0024    
0025     Implements the BuffersGenerator concept for any concrete instance of the
0026     @ref http::message template.
0027    
0028     @ref http::message_generator takes ownership of a message on construction,
0029     erasing the concrete type from the interface.
0030    
0031     This makes it practical for use in server applications to implement request
0032     handling:
0033    
0034     @code
0035     template <class Body, class Fields>
0036     http::message_generator handle_request(
0037         string_view doc_root,
0038         http::request<Body, Fields>&& request);
0039     @endcode
0040    
0041     The @ref beast::write and @ref beast::async_write operations are provided
0042     for BuffersGenerator. The @ref http::message::keep_alive property is made
0043     available for use after writing the message.
0044 */
0045 class message_generator
0046 {
0047 public:
0048     using const_buffers_type = span<net::const_buffer>;
0049 
0050     template <bool isRequest, class Body, class Fields>
0051     message_generator(http::message<isRequest, Body, Fields>&&);
0052 
0053     /// `BuffersGenerator`
0054     bool is_done() const {
0055         return impl_->is_done();
0056     }
0057 
0058     /// `BuffersGenerator`
0059     const_buffers_type
0060     prepare(error_code& ec)
0061     {
0062         return impl_->prepare(ec);
0063     }
0064 
0065     /// `BuffersGenerator`
0066     void
0067     consume(std::size_t n)
0068     {
0069         impl_->consume(n);
0070     }
0071 
0072     /// Returns the result of `m.keep_alive()` on the underlying message
0073     bool
0074     keep_alive() const noexcept
0075     {
0076         return impl_->keep_alive();
0077     }
0078 
0079 private:
0080     struct impl_base
0081     {
0082         virtual ~impl_base() = default;
0083         virtual bool is_done() = 0;
0084         virtual const_buffers_type prepare(error_code& ec) = 0;
0085         virtual void consume(std::size_t n) = 0;
0086         virtual bool keep_alive() const noexcept = 0;
0087     };
0088 
0089     std::unique_ptr<impl_base> impl_;
0090 
0091     template <bool isRequest, class Body, class Fields>
0092     struct generator_impl;
0093 };
0094 
0095 } // namespace http
0096 } // namespace beast
0097 } // namespace boost
0098 
0099 #include <boost/beast/http/impl/message_generator.hpp>
0100 
0101 #endif