2 Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
8 [section:Writer Writer requirements]
10 A `Writer` serializes the message body. The implementation creates an instance
11 of this type when serializing a message, and calls into it zero or more times
12 to provide buffers containing the data. The interface of `Writer` is intended
13 to allow serialization in these scenarios:
15 * A body that does not entirely fit in memory.
16 * A body produced incrementally from coroutine output.
17 * A body represented by zero or more buffers already in memory.
18 * A body as a series of buffers when the content size is not known ahead of time.
19 * Body data generated on demand from other threads.
20 * Body data computed algorithmically.
24 * `X` denotes a type meeting the requirements of `Writer`.
26 * `a` denotes a value of type `X`.
28 * `m` denotes a value of type `message const&` where
29 `std::is_same<decltype(m.body), Body::value_type>:value == true`.
31 * `ec` is a value of type [link beast.ref.error_code `error_code&`]
33 * `wf` is a [*write function]: a function object of unspecified type provided
34 by the implementation which accepts any value meeting the requirements
35 of __ConstBufferSequence__ as its single parameter.
37 [table Writer requirements
38 [[operation] [type] [semantics, pre/post-conditions]]
43 `a` is constructible from `m`. The lifetime of `m` is guaranteed
44 to end no earlier than after `a` is destroyed. This function must
52 Called immediately after construction. If the function sets an
53 error code in `ec`, the serialization is aborted and the error
54 is propagated to the caller. This function must be `noexcept`.
58 [`a.content_length()`]
61 If this member is present, it is called after initialization
62 and before calls to provide buffers. The serialized message will
63 have the Content-Length field set to the value returned from
64 this function. If this member is absent, the serialized message
65 body will be chunk-encoded for HTTP versions 1.1 and later, else
66 the serialized message body will be sent unmodified, with the
67 error `boost::asio::error::eof` returned to the caller, to notify
68 they should close the connection to indicate the end of the message.
69 This function must be `noexcept`.
76 Called repeatedly after `init` succeeds. `wf` is a function object
77 which takes as its single parameter any value meeting the requirements
78 of __ConstBufferSequence__. Buffers provided to this write function
79 must remain valid until the next member function of `writer` is
80 invoked (which may be the destructor). This function returns `true`
81 to indicate all message body data has been written, or `false` if
82 there is more body data.
88 Definitions for required `Writer` member functions should be declared
89 inline so the generated code can become part of the implementation.
97 /** Construct the writer.
99 The msg object is guaranteed to exist for the lifetime of the writer.
104 @param msg The message whose body is to be written.
106 template<bool isRequest, class Body, class Headers>
108 writer(message<isRequest, Body, Headers> const& msg) noexcept;
110 /** Initialize the writer.
112 Called once immediately after construction.
113 The writer can perform initialization which may fail.
115 @param ec Contains the error code if any errors occur.
118 init(error_code& ec) noexcept;
120 /** Returns the content length.
122 If this member is present, the implementation will set the
123 Content-Length field accordingly. If absent, the implementation will
124 use chunk-encoding or terminate the connection to indicate the end
128 content_length() noexcept;
130 /** Write zero or one buffer representing the message body.
134 If return value is `true`:
135 * Callee made zero or one calls to `write`.
136 * There is no more data remaining to write.
138 If return value is `false`:
139 * Callee does not take ownership of resume.
140 * Callee made one call to `write`.
142 @param ec Set to indicate an error. This will cause an
143 asynchronous write operation to complete with the error.
145 @param write A functor the writer will call to provide the next
146 set of buffers. Ownership of the buffers is not transferred,
147 the writer must guarantee that the buffers remain valid until the
148 next member function is invoked, which may be the destructor.
150 @return `true` if there is no more data to send,
151 `false` when there may be more data.
153 template<class WriteFunction>
157 WriteFunction&& wf) noexcept;