| 1 | // |
| 2 | // Copyright (c) 2022 Seth Heeren (sgheeren at gmail dot com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // Official repository: https://github.com/boostorg/beast |
| 8 | // |
| 9 | #ifndef BOOST_BEAST_HTTP_MESSAGE_GENERATOR_HPP |
| 10 | #define BOOST_BEAST_HTTP_MESSAGE_GENERATOR_HPP |
| 11 | |
| 12 | #include <boost/beast/core/span.hpp> |
| 13 | #include <boost/beast/http/message.hpp> |
| 14 | #include <boost/beast/http/serializer.hpp> |
| 15 | #include <memory> |
| 16 | |
| 17 | namespace boost { |
| 18 | namespace beast { |
| 19 | namespace http { |
| 20 | |
| 21 | /** Type-erased buffers generator for @ref http::message |
| 22 | |
| 23 | Implements the BuffersGenerator concept for any concrete instance of the |
| 24 | @ref http::message template. |
| 25 | |
| 26 | @ref http::message_generator takes ownership of a message on construction, |
| 27 | erasing the concrete type from the interface. |
| 28 | |
| 29 | This makes it practical for use in server applications to implement request |
| 30 | handling: |
| 31 | |
| 32 | @code |
| 33 | template <class Body, class Fields> |
| 34 | http::message_generator handle_request( |
| 35 | string_view doc_root, |
| 36 | http::request<Body, Fields>&& request); |
| 37 | @endcode |
| 38 | |
| 39 | The @ref beast::write and @ref beast::async_write operations are provided |
| 40 | for BuffersGenerator. The @ref http::message::keep_alive property is made |
| 41 | available for use after writing the message. |
| 42 | */ |
| 43 | class message_generator |
| 44 | { |
| 45 | public: |
| 46 | using const_buffers_type = span<net::const_buffer>; |
| 47 | |
| 48 | template <bool isRequest, class Body, class Fields> |
| 49 | message_generator(http::message<isRequest, Body, Fields>&&); |
| 50 | |
| 51 | /// `BuffersGenerator` |
| 52 | bool is_done() const { |
| 53 | return impl_->is_done(); |
| 54 | } |
| 55 | |
| 56 | /// `BuffersGenerator` |
| 57 | const_buffers_type |
| 58 | prepare(error_code& ec) |
| 59 | { |
| 60 | return impl_->prepare(ec); |
| 61 | } |
| 62 | |
| 63 | /// `BuffersGenerator` |
| 64 | void |
| 65 | consume(std::size_t n) |
| 66 | { |
| 67 | impl_->consume(n); |
| 68 | } |
| 69 | |
| 70 | /// Returns the result of `m.keep_alive()` on the underlying message |
| 71 | bool |
| 72 | keep_alive() const noexcept |
| 73 | { |
| 74 | return impl_->keep_alive(); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | struct impl_base |
| 79 | { |
| 80 | virtual ~impl_base() = default; |
| 81 | virtual bool is_done() = 0; |
| 82 | virtual const_buffers_type prepare(error_code& ec) = 0; |
| 83 | virtual void consume(std::size_t n) = 0; |
| 84 | virtual bool keep_alive() const noexcept = 0; |
| 85 | }; |
| 86 | |
| 87 | std::unique_ptr<impl_base> impl_; |
| 88 | |
| 89 | template <bool isRequest, class Body, class Fields> |
| 90 | struct generator_impl; |
| 91 | }; |
| 92 | |
| 93 | } // namespace http |
| 94 | } // namespace beast |
| 95 | } // namespace boost |
| 96 | |
| 97 | #include <boost/beast/http/impl/message_generator.hpp> |
| 98 | |
| 99 | #endif |
| 100 | |