Boost C++ Libraries

PrevUpHomeNext

Class body_reader

boost::taar::body_reader — Incremental access to a request body while it is still arriving.

Synopsis

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


class body_reader {
public:
  // types
  typedef std::function< awaitable< std::size_t >(boost::asio::mutable_buffer)> read_function;

  // public member functions
  body_reader(read_function, std::function< bool()>, 
              std::optional< std::uint64_t >);
  awaitable< std::size_t > read_some(boost::asio::mutable_buffer);
  awaitable< std::string > read_some(std::size_t = 64 *1024);
  awaitable< std::string > read_all(std::uint64_t = 16 *1024 *1024);
  bool done() const;
  std::uint64_t bytes_read() const noexcept;
  std::optional< std::uint64_t > content_length() const noexcept;
};

Description

Handed to handlers through streaming_request. The body is never buffered by the session: each read_some pulls the next piece straight from the connection into the caller's buffer, so uploads of any size can be consumed with bounded memory. The session's body limit does not apply; the handler decides how much to accept.

If the handler returns (or throws) before done is true, the session answers and then closes the connection instead of draining what remains.

body_reader public member functions

  1. body_reader(read_function read, std::function< bool()> is_done, 
                std::optional< std::uint64_t > content_length);
  2. awaitable< std::size_t > read_some(boost::asio::mutable_buffer buffer);
    Read into buffer. Returns the number of bytes stored; 0 only when done.
  3. awaitable< std::string > read_some(std::size_t max = 64 *1024);
    Read the next piece as a string of at most max bytes; empty when done.
  4. awaitable< std::string > read_all(std::uint64_t limit = 16 *1024 *1024);
    Read everything that remains, bounded by limit.

    Throws:

    boost::taar::exception carrying boost::beast::http::error::body_limit once more than limit bytes have arrived, or the transport error if reading fails.
  5. bool done() const;
    True once the whole body has been consumed.
  6. std::uint64_t bytes_read() const noexcept;
    Bytes delivered so far.
  7. std::optional< std::uint64_t > content_length() const noexcept;
    The request's Content-Length, if it had one (chunked uploads have none).

PrevUpHomeNext