message_generator.hpp 2.6 KB

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