![]() |
boost::taar::segmented_body — A Beast body made of segments: owned text, windows of shared immutable buffers, and byte windows of one open file.
// In header: <boost/taar/core/segmented_body.hpp>
struct segmented_body {
// member classes/structs/unions
// The body's value: the ordered segments and the file they may refer to.
class value_type {
public:
// member classes/structs/unions
struct buffer_window {
// public data members
std::shared_ptr< std::string const > data;
std::uint64_t offset;
std::uint64_t length;
};
struct file_window {
// public data members
std::uint64_t offset;
std::uint64_t length;
};
// public member functions
void attach_file(boost::beast::file);
bool has_file() const noexcept;
void append_text(std::string);
void append_buffer(std::shared_ptr< std::string const >, std::uint64_t,
std::uint64_t);
void append_file_range(std::uint64_t, std::uint64_t);
std::uint64_t size() const noexcept;
bool empty() const noexcept;
};
// Serializes the segments in order; file windows are read in chunks.
class writer {
public:
// types
typedef boost::asio::const_buffer const_buffers_type; // The buffer sequence type produced by get().
// public member functions
template<bool isRequest, typename Fields>
writer(boost::beast::http::header< isRequest, Fields > const &,
value_type const &);
void init(boost::system::error_code &);
boost::optional< std::pair< const_buffers_type, bool > >
get(boost::system::error_code &);
// private member functions
bool more() const noexcept;
};
// public static functions
static std::uint64_t size(value_type const &) noexcept;
};It serves every response of htdocs with one type: a whole file, a single byte range, a multipart/byteranges sequence (text part headers between file windows) or a cached file kept in memory, without copying file data into the message. File windows are read on demand while the body is written, so the file is never loaded as a whole.
The body is read-only during serialization (the writer takes the value by const reference), so a response<segmented_body> can be written as an rvalue or converted to a message_generator. An empty value writes nothing; a response describing a body it does not carry (HEAD, 304) sets Content-Length itself.
segmented_body::value_type body; boost::beast::file file; file.open("movie.mp4", boost::beast::file_mode::scan, ec); body.attach_file(std::move(file)); body.append_file_range(0, 1024);
segmented_body public static functionsstatic std::uint64_t size(value_type const & body) noexcept;Payload size, for
prepare_payload().