message_generator.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include <boost/beast/core/span.hpp>
  12. #include <boost/beast/http/message.hpp>
  13. #include <boost/beast/http/serializer.hpp>
  14. #include <memory>
  15. namespace boost {
  16. namespace beast {
  17. namespace http {
  18. /** Type-erased buffers generator for @ref http::message
  19. Implements the BuffersGenerator concept for any concrete instance of the
  20. @ref http::message template.
  21. @ref http::message_generator takes ownership of a message on construction,
  22. erasing the concrete type from the interface.
  23. This makes it practical for use in server applications to implement request
  24. handling:
  25. @code
  26. template <class Body, class Fields>
  27. http::message_generator handle_request(
  28. string_view doc_root,
  29. http::request<Body, Fields>&& request);
  30. @endcode
  31. The @ref beast::write and @ref beast::async_write operations are provided
  32. for BuffersGenerator. The @ref http::message::keep_alive property is made
  33. available for use after writing the message.
  34. */
  35. class message_generator
  36. {
  37. public:
  38. using const_buffers_type = span<net::const_buffer>;
  39. template <bool isRequest, class Body, class Fields>
  40. message_generator(http::message<isRequest, Body, Fields>&&);
  41. /// `BuffersGenerator`
  42. bool is_done() const {
  43. return impl_->is_done();
  44. }
  45. /// `BuffersGenerator`
  46. const_buffers_type
  47. prepare(error_code& ec)
  48. {
  49. return impl_->prepare(ec);
  50. }
  51. /// `BuffersGenerator`
  52. void
  53. consume(std::size_t n)
  54. {
  55. impl_->consume(n);
  56. }
  57. /// Returns the result of `m.keep_alive()` on the underlying message
  58. bool
  59. keep_alive() const noexcept
  60. {
  61. return impl_->keep_alive();
  62. }
  63. private:
  64. struct impl_base
  65. {
  66. virtual ~impl_base() = default;
  67. virtual bool is_done() = 0;
  68. virtual const_buffers_type prepare(error_code& ec) = 0;
  69. virtual void consume(std::size_t n) = 0;
  70. virtual bool keep_alive() const noexcept = 0;
  71. };
  72. std::unique_ptr<impl_base> impl_;
  73. template <bool isRequest, class Body, class Fields>
  74. struct generator_impl;
  75. };
  76. } // namespace http
  77. } // namespace beast
  78. } // namespace boost
  79. #include <boost/beast/http/impl/message_generator.hpp>
  80. #endif