Boost C++ Libraries

PrevUpHomeNext

Class template chunked_response

boost::taar::chunked_response — A coroutine-based generator for chunked HTTP responses with metadata control.

Synopsis

// In header: <boost/taar/core/chunked_response.hpp>

template<typename T> 
class chunked_response {
public:
  // types
  typedef std::coroutine_handle< promise_type > handle_type;            
  typedef boost::asio::any_io_executor          executor_type;          
  typedef unspecified                           completion_handler_type;

  // member classes/structs/unions

  struct promise_type {
    // member classes/structs/unions

    struct final_awaiter {

      // public member functions
      bool await_ready() noexcept;
      void await_suspend(handle_type) noexcept;
      void await_resume() noexcept;
    };

    struct flatten_awaiter {

      // public member functions
      bool await_ready() noexcept;
      void await_suspend(handle_type);
      void await_resume() noexcept;

      // public data members
      chunked_response< T > inner;
    };

    struct yield_awaiter {

      // public member functions
      bool await_ready() noexcept;
      void await_suspend(handle_type) noexcept;
      void await_resume() noexcept;
    };

    // public member functions
    chunked_response get_return_object();
    std::suspend_always initial_suspend() noexcept;
    final_awaiter final_suspend() noexcept;
    yield_awaiter yield_value(T);
    std::suspend_never yield_value(chunked_set_status);
    std::suspend_never yield_value(chunked_set_header);
    flatten_awaiter yield_value(chunked_response< T >);
    void return_void() noexcept;
    void unhandled_exception();
    template<typename U, typename Executor> 
      auto await_transform(boost::asio::awaitable< U, Executor >);
    template<typename Executor> 
      auto await_transform(boost::asio::awaitable< void, Executor >);
    auto await_transform(boost::asio::this_coro::executor_t) noexcept;

    // public data members
    auto await_transform(Deferred &&d) -> decltype(this->await_transform(std::forward< Deferred >(d)(boost::asio::use_awaitable_t< executor_type >{}))) completion_handler_type completion_handler_;
    std::optional< executor_type > executor_;
    std::exception_ptr exception_;
    std::optional< boost::asio::cancellation_slot > cancellation_slot_;
    boost::asio::cancellation_signal internal_signal_;
    std::optional< boost::asio::cancellation_state > cancellation_state_;
    boost::beast::http::status status_;
    std::vector< chunked_set_header > headers_;
    bool data_yielded_;
    std::shared_ptr< bool > alive_;
    std::shared_ptr< boost::asio::cancellation_signal > active_co_spawn_signal_;
    std::shared_ptr< boost::asio::cancellation_signal > flattening_cancel_signal_;
    std::optional< chunked_response< T > > flattening_inner_storage_;
    chunked_response< T > * flattening_inner_;
    handle_type flattening_outer_handle_;
  };

  // public member functions
  chunked_response();
  explicit chunked_response(handle_type) noexcept;
  chunked_response(chunked_response &&) noexcept;
  chunked_response & operator=(chunked_response &&) noexcept;
  chunked_response(chunked_response const &) = delete;
  chunked_response & operator=(chunked_response const &) = delete;
  ~chunked_response();
  void set_executor(executor_type);
  void set_cancellation_slot(boost::asio::cancellation_slot);
  void rethrow_if_exception();
  boost::beast::http::status status() const;
  std::vector< chunked_set_header > const & headers() const;
  auto next();

  // private member functions
  template<typename Handler> void next_impl(Handler);
  void destroy_handle() noexcept;
};

Description

chunked_response<T> extends async_generator<T> with the ability to specify the HTTP status and response headers before the first chunk is written. Inside the body, yield:

  • chunked_set_status{...} to set the HTTP status,

  • chunked_set_header{...} to add a response header,

  • value (of type T) to write a chunk,

  • another chunked_response<T> to splice its chunks (its metadata is ignored; only the outer's metadata is written).

Metadata must precede all data yields. After the first data yield, metadata yields throw boost::taar::exception carrying error::late_chunk_metadata.

A handler returning chunked_response<T> is dispatched through the chunked-encoding path of session::http, which writes the headers, sets Transfer-Encoding: chunked, then streams each yielded value via boost::taar::chunk_body_from.

Template Parameters

  1. typename T

    The yielded chunk type. Any type accepted by boost::taar::chunk_body_from is supported.

chunked_response public member functions

  1. chunked_response();
  2. explicit chunked_response(handle_type h) noexcept;
  3. chunked_response(chunked_response && other) noexcept;
  4. chunked_response & operator=(chunked_response && other) noexcept;
  5. chunked_response(chunked_response const &) = delete;
  6. chunked_response & operator=(chunked_response const &) = delete;
  7. ~chunked_response();
  8. void set_executor(executor_type executor);
  9. void set_cancellation_slot(boost::asio::cancellation_slot slot);
  10. void rethrow_if_exception();
  11. boost::beast::http::status status() const;
  12. std::vector< chunked_set_header > const & headers() const;
  13. auto next();

chunked_response private member functions

  1. template<typename Handler> void next_impl(Handler handler);
  2. void destroy_handle() noexcept;

PrevUpHomeNext