Files
boost_beast/doc/concept/BodyReader.qbk
T

165 lines
5.4 KiB
Plaintext
Raw Normal View History

2017-05-08 12:41:45 -07:00
[/
Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
]
2017-06-04 17:25:55 -07:00
[section:BodyReader BodyReader]
2017-05-08 12:41:45 -07:00
2017-05-28 09:05:29 -07:00
A [*BodyReader] provides an online algorithm to obtain a sequence of zero
2017-05-08 12:41:45 -07:00
or more buffers from a body during serialization. The implementation creates
2017-05-28 09:05:29 -07:00
an instance of this type when needed, and calls into it one or more times to
retrieve buffers holding body octets. The interface of [*BodyReader] is
intended to obtain buffers for these scenarios:
2017-05-08 12:41:45 -07:00
* A body that does not entirely fit in memory.
* A body produced incrementally from coroutine output.
* A body represented by zero or more buffers already in memory.
2017-05-28 09:05:29 -07:00
* A body whose size is not known ahead of time.
* Body data generated dynamically from other threads.
2017-05-08 12:41:45 -07:00
* Body data computed algorithmically.
In this table:
2017-05-28 09:05:29 -07:00
* `X` denotes a type meeting the requirements of [*BodyReader].
* `B` denotes a __Body__ where
`std::is_same<X, B::reader>::value == true`.
2017-05-08 12:41:45 -07:00
* `a` denotes a value of type `X`.
* `m` denotes a value of type `message const&` where
2017-05-28 09:05:29 -07:00
`std::is_same<decltype(m.body), Body::value_type>:value == true`.
2017-05-08 12:41:45 -07:00
* `ec` is a value of type [link beast.ref.error_code `error_code&`].
2017-05-28 09:05:29 -07:00
* `R<T>` is the type `boost::optional<std::pair<T, bool>>`.
2017-05-08 12:41:45 -07:00
2017-05-28 09:05:29 -07:00
[table BodyReader requirements
[[expression] [type] [semantics, pre/post-conditions]]
2017-05-08 12:41:45 -07:00
[
[`X::const_buffers_type`]
[]
[
2017-05-28 09:05:29 -07:00
A type which meets the requirements of __ConstBufferSequence__.
2017-05-08 12:41:45 -07:00
This is the type of buffer returned by `X::get`.
]
2017-06-07 04:36:45 -07:00
][
2017-05-28 09:05:29 -07:00
[`X::is_deferred`]
[]
[
2017-06-02 19:12:39 -07:00
The type `std::true_type` if the serialization implementation
should only attempt to retrieve buffers from the reader after
the header has been serialized. Otherwise, if this type is
`std::false_type` the implementation will activate an
optimization: the first buffer produced during serialization
will contain both the header and some or all of this body.
Implementations of [*BodyReader] for which initialization is
expensive, should use `std::false_type` here, to reduce the
latency experienced by the remote host when expecting to read
the HTTP header.
2017-05-28 09:05:29 -07:00
]
2017-06-07 04:36:45 -07:00
][
2017-05-08 12:41:45 -07:00
[`X(m);`]
[]
[
Constructible from `m`. The lifetime of `m` is guaranteed
to end no earlier than after the `X` is destroyed.
]
2017-06-07 04:36:45 -07:00
][
2017-05-08 12:41:45 -07:00
[`a.init(ec)`]
2017-05-28 09:05:29 -07:00
[]
2017-05-08 12:41:45 -07:00
[
Called immediately after construction. If the function sets an
error code in `ec`, the serialization is aborted and the error
is propagated to the caller.
]
2017-06-07 04:36:45 -07:00
][
2017-05-08 12:41:45 -07:00
[`a.get(ec)`]
2017-05-28 09:05:29 -07:00
[`R<X::const_buffers_type>`]
2017-05-08 12:41:45 -07:00
[
Called repeatedly after `init` succeeds. This function returns
`boost::none` if all buffers representing the body have been
returned in previous calls or if it sets `ec` to indicate an
error. Otherwise, if there are buffers remaining the function
should return a pair with the first element containing a non-zero
length buffer sequence representing the next set of octets in
the body, while the second element is a `bool` meaning `true`
if there may be additional buffers returned on a subsequent call,
or `false` if the buffer returned on this call is the last
buffer representing the body.
]
2017-06-07 04:36:45 -07:00
][
2017-05-28 09:05:29 -07:00
[`is_body_reader<B>`]
2017-05-08 12:41:45 -07:00
[`std::true_type`]
[
2017-05-28 09:05:29 -07:00
An alias for `std::true_type` for `B`, otherwise an alias
for `std::false_type`.
2017-05-08 12:41:45 -07:00
]
]
]
[note
2017-06-07 04:36:45 -07:00
Definitions for required [*BodyReader] member functions may
be declared inline so the generated code can become part of
the implementation.
2017-05-08 12:41:45 -07:00
]
Exemplar:
```
2017-05-28 09:05:29 -07:00
struct reader
2017-05-08 12:41:45 -07:00
{
public:
/** Controls when the implementation requests buffers.
If false, the implementation will request the first buffer
2017-06-02 19:12:39 -07:00
immediately and try to serialize both the header and some
or all of the body in a single buffer.
2017-05-08 12:41:45 -07:00
*/
using is_deferred = std::false_type;
2017-06-02 19:12:39 -07:00
/// The type of buffer returned by `get`.
2017-05-08 12:41:45 -07:00
using const_buffers_type = boost::asio::const_buffers_1;
2017-05-28 09:05:29 -07:00
/** Construct the reader.
2017-05-08 12:41:45 -07:00
2017-06-02 19:12:39 -07:00
@param msg The message whose body is to be retrieved.
2017-05-08 12:41:45 -07:00
*/
template<bool isRequest, class Body, class Headers>
explicit
2017-05-28 09:05:29 -07:00
reader(message<isRequest, Body, Headers> const& msg);
2017-05-08 12:41:45 -07:00
2017-05-28 09:05:29 -07:00
/** Initialization.
2017-05-08 12:41:45 -07:00
Called once immediately after construction.
2017-05-28 09:05:29 -07:00
@param ec Set to the error, if any occurred.
2017-05-08 12:41:45 -07:00
*/
void
init(error_code& ec);
/** Returns the next buffer in the body.
@li If the return value is `boost::none` (unseated optional) and
`ec` does not contain an error, this indicates the end of the
body, no more buffers are present.
@li If the optional contains a value, the first element of the
pair represents a @b ConstBufferSequence containing one or
more octets of the body data. The second element indicates
whether or not there are additional octets of body data.
A value of `true` means there is more data, and that the
implementation will perform a subsequent call to `get`.
A value of `false` means there is no more body data.
@li If `ec` contains an error code, the return value is ignored.
*/
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec);
};
```
[endsect]