100.00% Lines (27/27)
100.00% Functions (9/9)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) | 2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) | |||||
| 3 | // Copyright (c) 2025 Mohammad Nejati | 3 | // Copyright (c) 2025 Mohammad Nejati | |||||
| 4 | // | 4 | // | |||||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 7 | // | 7 | // | |||||
| 8 | // Official repository: https://github.com/cppalliance/http | 8 | // Official repository: https://github.com/cppalliance/http | |||||
| 9 | // | 9 | // | |||||
| 10 | 10 | |||||||
| 11 | #ifndef BOOST_HTTP_SERIALIZER_HPP | 11 | #ifndef BOOST_HTTP_SERIALIZER_HPP | |||||
| 12 | #define BOOST_HTTP_SERIALIZER_HPP | 12 | #define BOOST_HTTP_SERIALIZER_HPP | |||||
| 13 | 13 | |||||||
| 14 | #include <boost/http/config.hpp> | 14 | #include <boost/http/config.hpp> | |||||
| 15 | #include <boost/http/detail/workspace.hpp> | 15 | #include <boost/http/detail/workspace.hpp> | |||||
| 16 | #include <boost/http/error.hpp> | 16 | #include <boost/http/error.hpp> | |||||
| 17 | 17 | |||||||
| 18 | #include <boost/capy/buffers.hpp> | 18 | #include <boost/capy/buffers.hpp> | |||||
| 19 | #include <boost/http/concept/buffer_sink.hpp> | 19 | #include <boost/http/concept/buffer_sink.hpp> | |||||
| 20 | #include <boost/capy/concept/write_stream.hpp> | 20 | #include <boost/capy/concept/write_stream.hpp> | |||||
| 21 | #include <boost/capy/results.hpp> | 21 | #include <boost/capy/results.hpp> | |||||
| 22 | #include <boost/capy/task.hpp> | 22 | #include <boost/capy/task.hpp> | |||||
| 23 | #include <boost/core/span.hpp> | 23 | #include <boost/core/span.hpp> | |||||
| 24 | #include <boost/system/result.hpp> | 24 | #include <boost/system/result.hpp> | |||||
| 25 | 25 | |||||||
| 26 | #include <array> | 26 | #include <array> | |||||
| 27 | #include <cstddef> | 27 | #include <cstddef> | |||||
| 28 | #include <cstring> | 28 | #include <cstring> | |||||
| 29 | #include <type_traits> | 29 | #include <type_traits> | |||||
| 30 | #include <utility> | 30 | #include <utility> | |||||
| 31 | 31 | |||||||
| 32 | namespace boost { | 32 | namespace boost { | |||||
| 33 | namespace http { | 33 | namespace http { | |||||
| 34 | 34 | |||||||
| 35 | // Forward declaration | 35 | // Forward declaration | |||||
| 36 | class message_base; | 36 | class message_base; | |||||
| 37 | 37 | |||||||
| 38 | //------------------------------------------------ | 38 | //------------------------------------------------ | |||||
| 39 | 39 | |||||||
| 40 | /** A serializer for HTTP/1 messages. | 40 | /** A serializer for HTTP/1 messages. | |||||
| 41 | 41 | |||||||
| 42 | Transforms one or more HTTP/1 messages into bytes for | 42 | Transforms one or more HTTP/1 messages into bytes for | |||||
| 43 | transmission. Each message consists of a required header | 43 | transmission. Each message consists of a required header | |||||
| 44 | followed by an optional body. | 44 | followed by an optional body. | |||||
| 45 | 45 | |||||||
| 46 | Use @ref set_message to associate a message, then choose | 46 | Use @ref set_message to associate a message, then choose | |||||
| 47 | a body mode: | 47 | a body mode: | |||||
| 48 | 48 | |||||||
| 49 | @li @ref start — empty body (header only) | 49 | @li @ref start — empty body (header only) | |||||
| 50 | @li @ref start_writes — body via internal buffer | 50 | @li @ref start_writes — body via internal buffer | |||||
| 51 | (BufferSink path) | 51 | (BufferSink path) | |||||
| 52 | @li @ref start_buffers — body via caller-owned buffers | 52 | @li @ref start_buffers — body via caller-owned buffers | |||||
| 53 | (WriteSink path) | 53 | (WriteSink path) | |||||
| 54 | 54 | |||||||
| 55 | Alternatively, obtain a @ref sink via @ref sink_for and | 55 | Alternatively, obtain a @ref sink via @ref sink_for and | |||||
| 56 | let it start the serializer lazily on first use. | 56 | let it start the serializer lazily on first use. | |||||
| 57 | 57 | |||||||
| 58 | The caller must ensure that the associated message is not | 58 | The caller must ensure that the associated message is not | |||||
| 59 | changed or destroyed until @ref is_done returns true, | 59 | changed or destroyed until @ref is_done returns true, | |||||
| 60 | @ref reset is called, or the serializer is destroyed. | 60 | @ref reset is called, or the serializer is destroyed. | |||||
| 61 | 61 | |||||||
| 62 | @par Example | 62 | @par Example | |||||
| 63 | @code | 63 | @code | |||||
| 64 | http::serializer sr(cfg); | 64 | http::serializer sr(cfg); | |||||
| 65 | http::response res; | 65 | http::response res; | |||||
| 66 | res.set_payload_size(5); | 66 | res.set_payload_size(5); | |||||
| 67 | sr.set_message(res); | 67 | sr.set_message(res); | |||||
| 68 | 68 | |||||||
| 69 | auto sink = sr.sink_for(socket); | 69 | auto sink = sr.sink_for(socket); | |||||
| 70 | co_await sink.write_eof( | 70 | co_await sink.write_eof( | |||||
| 71 | capy::make_buffer(std::string_view("hello"))); | 71 | capy::make_buffer(std::string_view("hello"))); | |||||
| 72 | @endcode | 72 | @endcode | |||||
| 73 | 73 | |||||||
| 74 | @see @ref sink, @ref set_message. | 74 | @see @ref sink, @ref set_message. | |||||
| 75 | */ | 75 | */ | |||||
| 76 | class serializer | 76 | class serializer | |||||
| 77 | { | 77 | { | |||||
| 78 | public: | 78 | public: | |||||
| 79 | template<capy::WriteStream Stream> | 79 | template<capy::WriteStream Stream> | |||||
| 80 | class sink; | 80 | class sink; | |||||
| 81 | 81 | |||||||
| 82 | /** The type used to represent a sequence | 82 | /** The type used to represent a sequence | |||||
| 83 | of mutable buffers for streaming. | 83 | of mutable buffers for streaming. | |||||
| 84 | */ | 84 | */ | |||||
| 85 | using mutable_buffers_type = | 85 | using mutable_buffers_type = | |||||
| 86 | std::array<capy::mutable_buffer, 2>; | 86 | std::array<capy::mutable_buffer, 2>; | |||||
| 87 | 87 | |||||||
| 88 | /** The type used to represent a sequence of | 88 | /** The type used to represent a sequence of | |||||
| 89 | constant buffers that refers to the output | 89 | constant buffers that refers to the output | |||||
| 90 | area. | 90 | area. | |||||
| 91 | */ | 91 | */ | |||||
| 92 | using const_buffers_type = | 92 | using const_buffers_type = | |||||
| 93 | boost::span<capy::const_buffer const>; | 93 | boost::span<capy::const_buffer const>; | |||||
| 94 | 94 | |||||||
| 95 | /** Destructor | 95 | /** Destructor | |||||
| 96 | */ | 96 | */ | |||||
| 97 | BOOST_HTTP_DECL | 97 | BOOST_HTTP_DECL | |||||
| 98 | ~serializer(); | 98 | ~serializer(); | |||||
| 99 | 99 | |||||||
| 100 | /** Default constructor. | 100 | /** Default constructor. | |||||
| 101 | 101 | |||||||
| 102 | Constructs a serializer with no allocated state. | 102 | Constructs a serializer with no allocated state. | |||||
| 103 | The serializer must be assigned from a valid | 103 | The serializer must be assigned from a valid | |||||
| 104 | serializer before use. | 104 | serializer before use. | |||||
| 105 | 105 | |||||||
| 106 | @par Postconditions | 106 | @par Postconditions | |||||
| 107 | The serializer has no allocated state. | 107 | The serializer has no allocated state. | |||||
| 108 | */ | 108 | */ | |||||
| HITCBC | 109 | 2 | serializer() = default; | 109 | 2 | serializer() = default; | ||
| 110 | 110 | |||||||
| 111 | /** Constructor. | 111 | /** Constructor. | |||||
| 112 | 112 | |||||||
| 113 | Constructs a serializer with the provided configuration. | 113 | Constructs a serializer with the provided configuration. | |||||
| 114 | 114 | |||||||
| 115 | @par Postconditions | 115 | @par Postconditions | |||||
| 116 | @code | 116 | @code | |||||
| 117 | this->is_done() == true | 117 | this->is_done() == true | |||||
| 118 | @endcode | 118 | @endcode | |||||
| 119 | 119 | |||||||
| 120 | @param cfg Shared pointer to serializer configuration. | 120 | @param cfg Shared pointer to serializer configuration. | |||||
| 121 | 121 | |||||||
| 122 | @see @ref make_serializer_config, @ref serializer_config. | 122 | @see @ref make_serializer_config, @ref serializer_config. | |||||
| 123 | */ | 123 | */ | |||||
| 124 | BOOST_HTTP_DECL | 124 | BOOST_HTTP_DECL | |||||
| 125 | explicit | 125 | explicit | |||||
| 126 | serializer( | 126 | serializer( | |||||
| 127 | std::shared_ptr<serializer_config_impl const> cfg); | 127 | std::shared_ptr<serializer_config_impl const> cfg); | |||||
| 128 | 128 | |||||||
| 129 | /** Constructor. | 129 | /** Constructor. | |||||
| 130 | 130 | |||||||
| 131 | The states of `other` are transferred | 131 | The states of `other` are transferred | |||||
| 132 | to the newly constructed object, | 132 | to the newly constructed object, | |||||
| 133 | which includes the allocated buffer. | 133 | which includes the allocated buffer. | |||||
| 134 | After construction, the only valid | 134 | After construction, the only valid | |||||
| 135 | operations on the moved-from object | 135 | operations on the moved-from object | |||||
| 136 | are destruction and assignment. | 136 | are destruction and assignment. | |||||
| 137 | 137 | |||||||
| 138 | Buffer sequences previously obtained | 138 | Buffer sequences previously obtained | |||||
| 139 | using @ref prepare remain valid. | 139 | using @ref prepare remain valid. | |||||
| 140 | 140 | |||||||
| 141 | @par Postconditions | 141 | @par Postconditions | |||||
| 142 | @code | 142 | @code | |||||
| 143 | other.is_done() == true | 143 | other.is_done() == true | |||||
| 144 | @endcode | 144 | @endcode | |||||
| 145 | 145 | |||||||
| 146 | @par Complexity | 146 | @par Complexity | |||||
| 147 | Constant. | 147 | Constant. | |||||
| 148 | 148 | |||||||
| 149 | @param other The serializer to move from. | 149 | @param other The serializer to move from. | |||||
| 150 | */ | 150 | */ | |||||
| 151 | BOOST_HTTP_DECL | 151 | BOOST_HTTP_DECL | |||||
| 152 | serializer( | 152 | serializer( | |||||
| 153 | serializer&& other) noexcept; | 153 | serializer&& other) noexcept; | |||||
| 154 | 154 | |||||||
| 155 | /** Assignment. | 155 | /** Assignment. | |||||
| 156 | The states of `other` are transferred | 156 | The states of `other` are transferred | |||||
| 157 | to this object, which includes the | 157 | to this object, which includes the | |||||
| 158 | allocated buffer. After assignment, | 158 | allocated buffer. After assignment, | |||||
| 159 | the only valid operations on the | 159 | the only valid operations on the | |||||
| 160 | moved-from object are destruction and | 160 | moved-from object are destruction and | |||||
| 161 | assignment. | 161 | assignment. | |||||
| 162 | Buffer sequences previously obtained | 162 | Buffer sequences previously obtained | |||||
| 163 | using @ref prepare remain valid. | 163 | using @ref prepare remain valid. | |||||
| 164 | @par Complexity | 164 | @par Complexity | |||||
| 165 | Constant. | 165 | Constant. | |||||
| 166 | @param other The serializer to move from. | 166 | @param other The serializer to move from. | |||||
| 167 | @return A reference to this object. | 167 | @return A reference to this object. | |||||
| 168 | */ | 168 | */ | |||||
| 169 | BOOST_HTTP_DECL | 169 | BOOST_HTTP_DECL | |||||
| 170 | serializer& | 170 | serializer& | |||||
| 171 | operator=(serializer&& other) noexcept; | 171 | operator=(serializer&& other) noexcept; | |||||
| 172 | 172 | |||||||
| 173 | /** Reset the serializer for a new message. | 173 | /** Reset the serializer for a new message. | |||||
| 174 | 174 | |||||||
| 175 | Aborts any ongoing serialization and | 175 | Aborts any ongoing serialization and | |||||
| 176 | prepares the serializer to start | 176 | prepares the serializer to start | |||||
| 177 | serialization of a new message. | 177 | serialization of a new message. | |||||
| 178 | */ | 178 | */ | |||||
| 179 | BOOST_HTTP_DECL | 179 | BOOST_HTTP_DECL | |||||
| 180 | void | 180 | void | |||||
| 181 | reset() noexcept; | 181 | reset() noexcept; | |||||
| 182 | 182 | |||||||
| 183 | /** Set the message to serialize. | 183 | /** Set the message to serialize. | |||||
| 184 | 184 | |||||||
| 185 | Associates a message with the serializer for subsequent | 185 | Associates a message with the serializer for subsequent | |||||
| 186 | streaming operations. The message is not copied; the caller | 186 | streaming operations. The message is not copied; the caller | |||||
| 187 | must ensure it remains valid until serialization completes. | 187 | must ensure it remains valid until serialization completes. | |||||
| 188 | 188 | |||||||
| 189 | @param m The message to associate. | 189 | @param m The message to associate. | |||||
| 190 | */ | 190 | */ | |||||
| 191 | BOOST_HTTP_DECL | 191 | BOOST_HTTP_DECL | |||||
| 192 | void | 192 | void | |||||
| 193 | set_message(message_base const& m) noexcept; | 193 | set_message(message_base const& m) noexcept; | |||||
| 194 | 194 | |||||||
| 195 | /** Start serializing the associated message with an empty body. | 195 | /** Start serializing the associated message with an empty body. | |||||
| 196 | 196 | |||||||
| 197 | The message must be set beforehand using @ref set_message. | 197 | The message must be set beforehand using @ref set_message. | |||||
| 198 | Use the prepare/consume loop to pull output bytes. | 198 | Use the prepare/consume loop to pull output bytes. | |||||
| 199 | 199 | |||||||
| 200 | @par Preconditions | 200 | @par Preconditions | |||||
| 201 | A message was associated via @ref set_message. | 201 | A message was associated via @ref set_message. | |||||
| 202 | 202 | |||||||
| 203 | @par Exception Safety | 203 | @par Exception Safety | |||||
| 204 | Strong guarantee. | 204 | Strong guarantee. | |||||
| 205 | 205 | |||||||
| 206 | @throw std::logic_error if no message is associated or | 206 | @throw std::logic_error if no message is associated or | |||||
| 207 | `this->is_done() == false`. | 207 | `this->is_done() == false`. | |||||
| 208 | 208 | |||||||
| 209 | @throw std::length_error if there is insufficient internal buffer | 209 | @throw std::length_error if there is insufficient internal buffer | |||||
| 210 | space to start the operation. | 210 | space to start the operation. | |||||
| 211 | 211 | |||||||
| 212 | @see @ref set_message, @ref prepare, @ref consume. | 212 | @see @ref set_message, @ref prepare, @ref consume. | |||||
| 213 | */ | 213 | */ | |||||
| 214 | void | 214 | void | |||||
| 215 | BOOST_HTTP_DECL | 215 | BOOST_HTTP_DECL | |||||
| 216 | start(); | 216 | start(); | |||||
| 217 | 217 | |||||||
| 218 | /** Start streaming the associated message. | 218 | /** Start streaming the associated message. | |||||
| 219 | 219 | |||||||
| 220 | Low-level entry point equivalent to @ref start_writes. | 220 | Low-level entry point equivalent to @ref start_writes. | |||||
| 221 | Prefer using a @ref sink which starts lazily. | 221 | Prefer using a @ref sink which starts lazily. | |||||
| 222 | 222 | |||||||
| 223 | @par Preconditions | 223 | @par Preconditions | |||||
| 224 | A message was associated via @ref set_message. | 224 | A message was associated via @ref set_message. | |||||
| 225 | 225 | |||||||
| 226 | @par Exception Safety | 226 | @par Exception Safety | |||||
| 227 | Strong guarantee. | 227 | Strong guarantee. | |||||
| 228 | 228 | |||||||
| 229 | @throw std::logic_error if no message is associated or | 229 | @throw std::logic_error if no message is associated or | |||||
| 230 | `this->is_done() == false`. | 230 | `this->is_done() == false`. | |||||
| 231 | 231 | |||||||
| 232 | @throw std::length_error if there is insufficient internal buffer | 232 | @throw std::length_error if there is insufficient internal buffer | |||||
| 233 | space to start the operation. | 233 | space to start the operation. | |||||
| 234 | 234 | |||||||
| 235 | @see @ref start_writes, @ref sink. | 235 | @see @ref start_writes, @ref sink. | |||||
| 236 | */ | 236 | */ | |||||
| 237 | BOOST_HTTP_DECL | 237 | BOOST_HTTP_DECL | |||||
| 238 | void | 238 | void | |||||
| 239 | start_stream(); | 239 | start_stream(); | |||||
| 240 | 240 | |||||||
| 241 | /** Start the serializer in write mode. | 241 | /** Start the serializer in write mode. | |||||
| 242 | 242 | |||||||
| 243 | Prepares the serializer for write-mode streaming | 243 | Prepares the serializer for write-mode streaming | |||||
| 244 | using the message previously set via @ref set_message. | 244 | using the message previously set via @ref set_message. | |||||
| 245 | In this mode, the workspace is split into an input | 245 | In this mode, the workspace is split into an input | |||||
| 246 | buffer and an output buffer. Use @ref stream_prepare, | 246 | buffer and an output buffer. Use @ref stream_prepare, | |||||
| 247 | @ref stream_commit, and @ref stream_close to write | 247 | @ref stream_commit, and @ref stream_close to write | |||||
| 248 | body data, or use the sink's BufferSink interface. | 248 | body data, or use the sink's BufferSink interface. | |||||
| 249 | 249 | |||||||
| 250 | @par Preconditions | 250 | @par Preconditions | |||||
| 251 | A message was associated via @ref set_message. | 251 | A message was associated via @ref set_message. | |||||
| 252 | @code | 252 | @code | |||||
| 253 | this->is_done() == true | 253 | this->is_done() == true | |||||
| 254 | @endcode | 254 | @endcode | |||||
| 255 | 255 | |||||||
| 256 | @par Exception Safety | 256 | @par Exception Safety | |||||
| 257 | Strong guarantee. | 257 | Strong guarantee. | |||||
| 258 | 258 | |||||||
| 259 | @throw std::logic_error if no message is associated. | 259 | @throw std::logic_error if no message is associated. | |||||
| 260 | 260 | |||||||
| 261 | @throw std::length_error if there is insufficient internal buffer | 261 | @throw std::length_error if there is insufficient internal buffer | |||||
| 262 | space to start the operation. | 262 | space to start the operation. | |||||
| 263 | 263 | |||||||
| 264 | @see @ref set_message, @ref sink. | 264 | @see @ref set_message, @ref sink. | |||||
| 265 | */ | 265 | */ | |||||
| 266 | BOOST_HTTP_DECL | 266 | BOOST_HTTP_DECL | |||||
| 267 | void | 267 | void | |||||
| 268 | start_writes(); | 268 | start_writes(); | |||||
| 269 | 269 | |||||||
| 270 | /** Start the serializer in buffer mode. | 270 | /** Start the serializer in buffer mode. | |||||
| 271 | 271 | |||||||
| 272 | Prepares the serializer for buffer-mode streaming | 272 | Prepares the serializer for buffer-mode streaming | |||||
| 273 | using the message previously set via @ref set_message. | 273 | using the message previously set via @ref set_message. | |||||
| 274 | In this mode, the entire workspace is used for output | 274 | In this mode, the entire workspace is used for output | |||||
| 275 | buffering. The caller provides body data through the | 275 | buffering. The caller provides body data through the | |||||
| 276 | sink's WriteSink methods (write, write_eof), passing | 276 | sink's WriteSink methods (write, write_eof), passing | |||||
| 277 | their own buffers directly. | 277 | their own buffers directly. | |||||
| 278 | 278 | |||||||
| 279 | @par Preconditions | 279 | @par Preconditions | |||||
| 280 | A message was associated via @ref set_message. | 280 | A message was associated via @ref set_message. | |||||
| 281 | @code | 281 | @code | |||||
| 282 | this->is_done() == true | 282 | this->is_done() == true | |||||
| 283 | @endcode | 283 | @endcode | |||||
| 284 | 284 | |||||||
| 285 | @par Exception Safety | 285 | @par Exception Safety | |||||
| 286 | Strong guarantee. | 286 | Strong guarantee. | |||||
| 287 | 287 | |||||||
| 288 | @throw std::logic_error if no message is associated. | 288 | @throw std::logic_error if no message is associated. | |||||
| 289 | 289 | |||||||
| 290 | @throw std::length_error if there is insufficient internal buffer | 290 | @throw std::length_error if there is insufficient internal buffer | |||||
| 291 | space to start the operation. | 291 | space to start the operation. | |||||
| 292 | 292 | |||||||
| 293 | @see @ref set_message, @ref sink. | 293 | @see @ref set_message, @ref sink. | |||||
| 294 | */ | 294 | */ | |||||
| 295 | BOOST_HTTP_DECL | 295 | BOOST_HTTP_DECL | |||||
| 296 | void | 296 | void | |||||
| 297 | start_buffers(); | 297 | start_buffers(); | |||||
| 298 | 298 | |||||||
| 299 | /** Create a sink for writing body data. | 299 | /** Create a sink for writing body data. | |||||
| 300 | 300 | |||||||
| 301 | Returns a lightweight @ref sink handle that writes | 301 | Returns a lightweight @ref sink handle that writes | |||||
| 302 | serialized body data to the provided stream. The sink | 302 | serialized body data to the provided stream. The sink | |||||
| 303 | starts the serializer lazily on first use, so neither | 303 | starts the serializer lazily on first use, so neither | |||||
| 304 | @ref start_writes nor @ref start_buffers need to be | 304 | @ref start_writes nor @ref start_buffers need to be | |||||
| 305 | called beforehand. | 305 | called beforehand. | |||||
| 306 | 306 | |||||||
| 307 | The sink can be created once and reused across multiple | 307 | The sink can be created once and reused across multiple | |||||
| 308 | messages. The serializer must outlive the sink. | 308 | messages. The serializer must outlive the sink. | |||||
| 309 | 309 | |||||||
| 310 | @par Example | 310 | @par Example | |||||
| 311 | @code | 311 | @code | |||||
| 312 | http::serializer sr(cfg); | 312 | http::serializer sr(cfg); | |||||
| 313 | auto sink = sr.sink_for(socket); | 313 | auto sink = sr.sink_for(socket); | |||||
| 314 | 314 | |||||||
| 315 | http::response res; | 315 | http::response res; | |||||
| 316 | res.set_payload_size(5); | 316 | res.set_payload_size(5); | |||||
| 317 | sr.set_message(res); | 317 | sr.set_message(res); | |||||
| 318 | co_await sink.write_eof( | 318 | co_await sink.write_eof( | |||||
| 319 | capy::make_buffer(std::string_view("hello"))); | 319 | capy::make_buffer(std::string_view("hello"))); | |||||
| 320 | @endcode | 320 | @endcode | |||||
| 321 | 321 | |||||||
| 322 | @tparam Stream The output stream type satisfying | 322 | @tparam Stream The output stream type satisfying | |||||
| 323 | @ref capy::WriteStream. | 323 | @ref capy::WriteStream. | |||||
| 324 | 324 | |||||||
| 325 | @param ws The output stream to write serialized data to. | 325 | @param ws The output stream to write serialized data to. | |||||
| 326 | 326 | |||||||
| 327 | @return A @ref sink object for writing body data. | 327 | @return A @ref sink object for writing body data. | |||||
| 328 | 328 | |||||||
| 329 | @see @ref sink, @ref set_message. | 329 | @see @ref sink, @ref set_message. | |||||
| 330 | */ | 330 | */ | |||||
| 331 | template<capy::WriteStream Stream> | 331 | template<capy::WriteStream Stream> | |||||
| 332 | sink<Stream> | 332 | sink<Stream> | |||||
| 333 | sink_for(Stream& ws) noexcept; | 333 | sink_for(Stream& ws) noexcept; | |||||
| 334 | 334 | |||||||
| 335 | /** Return the output area. | 335 | /** Return the output area. | |||||
| 336 | 336 | |||||||
| 337 | This function serializes some or all of | 337 | This function serializes some or all of | |||||
| 338 | the message and returns the corresponding | 338 | the message and returns the corresponding | |||||
| 339 | output buffers. Afterward, a call to @ref | 339 | output buffers. Afterward, a call to @ref | |||||
| 340 | consume is required to report the number | 340 | consume is required to report the number | |||||
| 341 | of bytes used, if any. | 341 | of bytes used, if any. | |||||
| 342 | 342 | |||||||
| 343 | If the message includes an | 343 | If the message includes an | |||||
| 344 | `Expect: 100-continue` header and the | 344 | `Expect: 100-continue` header and the | |||||
| 345 | header section of the message has been | 345 | header section of the message has been | |||||
| 346 | consumed, the returned result will contain | 346 | consumed, the returned result will contain | |||||
| 347 | @ref error::expect_100_continue to | 347 | @ref error::expect_100_continue to | |||||
| 348 | indicate that the header part of the | 348 | indicate that the header part of the | |||||
| 349 | message is complete. The next call to @ref | 349 | message is complete. The next call to @ref | |||||
| 350 | prepare will produce output. | 350 | prepare will produce output. | |||||
| 351 | 351 | |||||||
| 352 | When the serializer is in streaming mode, | 352 | When the serializer is in streaming mode, | |||||
| 353 | the result may contain @ref error::need_data | 353 | the result may contain @ref error::need_data | |||||
| 354 | to indicate that additional input is required | 354 | to indicate that additional input is required | |||||
| 355 | to produce output. | 355 | to produce output. | |||||
| 356 | 356 | |||||||
| 357 | @par Preconditions | 357 | @par Preconditions | |||||
| 358 | @code | 358 | @code | |||||
| 359 | this->is_done() == false | 359 | this->is_done() == false | |||||
| 360 | @endcode | 360 | @endcode | |||||
| 361 | No unrecoverable error reported from previous calls. | 361 | No unrecoverable error reported from previous calls. | |||||
| 362 | 362 | |||||||
| 363 | @par Exception Safety | 363 | @par Exception Safety | |||||
| 364 | Strong guarantee. | 364 | Strong guarantee. | |||||
| 365 | 365 | |||||||
| 366 | @throw std::logic_error | 366 | @throw std::logic_error | |||||
| 367 | `this->is_done() == true`. | 367 | `this->is_done() == true`. | |||||
| 368 | 368 | |||||||
| 369 | @return A result containing @ref | 369 | @return A result containing @ref | |||||
| 370 | const_buffers_type that represents the | 370 | const_buffers_type that represents the | |||||
| 371 | output area or an error if any occurred. | 371 | output area or an error if any occurred. | |||||
| 372 | 372 | |||||||
| 373 | @see | 373 | @see | |||||
| 374 | @ref consume, | 374 | @ref consume, | |||||
| 375 | @ref is_done, | 375 | @ref is_done, | |||||
| 376 | @ref const_buffers_type. | 376 | @ref const_buffers_type. | |||||
| 377 | */ | 377 | */ | |||||
| 378 | BOOST_HTTP_DECL | 378 | BOOST_HTTP_DECL | |||||
| 379 | auto | 379 | auto | |||||
| 380 | prepare() -> | 380 | prepare() -> | |||||
| 381 | system::result< | 381 | system::result< | |||||
| 382 | const_buffers_type>; | 382 | const_buffers_type>; | |||||
| 383 | 383 | |||||||
| 384 | /** Consume bytes from the output area. | 384 | /** Consume bytes from the output area. | |||||
| 385 | 385 | |||||||
| 386 | This function should be called after one | 386 | This function should be called after one | |||||
| 387 | or more bytes contained in the buffers | 387 | or more bytes contained in the buffers | |||||
| 388 | provided in the prior call to @ref prepare | 388 | provided in the prior call to @ref prepare | |||||
| 389 | have been used. | 389 | have been used. | |||||
| 390 | 390 | |||||||
| 391 | After a call to @ref consume, callers | 391 | After a call to @ref consume, callers | |||||
| 392 | should check the return value of @ref | 392 | should check the return value of @ref | |||||
| 393 | is_done to determine if the entire message | 393 | is_done to determine if the entire message | |||||
| 394 | has been serialized. | 394 | has been serialized. | |||||
| 395 | 395 | |||||||
| 396 | @par Preconditions | 396 | @par Preconditions | |||||
| 397 | @code | 397 | @code | |||||
| 398 | this->is_done() == false | 398 | this->is_done() == false | |||||
| 399 | @endcode | 399 | @endcode | |||||
| 400 | 400 | |||||||
| 401 | @par Exception Safety | 401 | @par Exception Safety | |||||
| 402 | Strong guarantee. | 402 | Strong guarantee. | |||||
| 403 | 403 | |||||||
| 404 | @throw std::logic_error | 404 | @throw std::logic_error | |||||
| 405 | `this->is_done() == true`. | 405 | `this->is_done() == true`. | |||||
| 406 | 406 | |||||||
| 407 | @param n The number of bytes to consume. | 407 | @param n The number of bytes to consume. | |||||
| 408 | If `n` is greater than the size of the | 408 | If `n` is greater than the size of the | |||||
| 409 | buffer returned from @ref prepared the | 409 | buffer returned from @ref prepared the | |||||
| 410 | entire output sequence is consumed and no | 410 | entire output sequence is consumed and no | |||||
| 411 | error is issued. | 411 | error is issued. | |||||
| 412 | 412 | |||||||
| 413 | @see | 413 | @see | |||||
| 414 | @ref prepare, | 414 | @ref prepare, | |||||
| 415 | @ref is_done, | 415 | @ref is_done, | |||||
| 416 | @ref const_buffers_type. | 416 | @ref const_buffers_type. | |||||
| 417 | */ | 417 | */ | |||||
| 418 | BOOST_HTTP_DECL | 418 | BOOST_HTTP_DECL | |||||
| 419 | void | 419 | void | |||||
| 420 | consume(std::size_t n); | 420 | consume(std::size_t n); | |||||
| 421 | 421 | |||||||
| 422 | /** Return true if serialization is complete. | 422 | /** Return true if serialization is complete. | |||||
| 423 | */ | 423 | */ | |||||
| 424 | BOOST_HTTP_DECL | 424 | BOOST_HTTP_DECL | |||||
| 425 | bool | 425 | bool | |||||
| 426 | is_done() const noexcept; | 426 | is_done() const noexcept; | |||||
| 427 | 427 | |||||||
| 428 | /** Return true if serialization has not yet started. | 428 | /** Return true if serialization has not yet started. | |||||
| 429 | */ | 429 | */ | |||||
| 430 | BOOST_HTTP_DECL | 430 | BOOST_HTTP_DECL | |||||
| 431 | bool | 431 | bool | |||||
| 432 | is_start() const noexcept; | 432 | is_start() const noexcept; | |||||
| 433 | 433 | |||||||
| 434 | /** Return the available capacity for streaming. | 434 | /** Return the available capacity for streaming. | |||||
| 435 | 435 | |||||||
| 436 | Returns the number of bytes that can be written | 436 | Returns the number of bytes that can be written | |||||
| 437 | to the serializer's internal buffer. | 437 | to the serializer's internal buffer. | |||||
| 438 | 438 | |||||||
| 439 | @par Preconditions | 439 | @par Preconditions | |||||
| 440 | The serializer is in streaming mode (after calling | 440 | The serializer is in streaming mode (after calling | |||||
| 441 | @ref start_stream). | 441 | @ref start_stream). | |||||
| 442 | 442 | |||||||
| 443 | @par Exception Safety | 443 | @par Exception Safety | |||||
| 444 | Strong guarantee. | 444 | Strong guarantee. | |||||
| 445 | 445 | |||||||
| 446 | @throw std::logic_error if not in streaming mode. | 446 | @throw std::logic_error if not in streaming mode. | |||||
| 447 | */ | 447 | */ | |||||
| 448 | BOOST_HTTP_DECL | 448 | BOOST_HTTP_DECL | |||||
| 449 | std::size_t | 449 | std::size_t | |||||
| 450 | stream_capacity() const; | 450 | stream_capacity() const; | |||||
| 451 | 451 | |||||||
| 452 | /** Prepare a buffer for writing stream data. | 452 | /** Prepare a buffer for writing stream data. | |||||
| 453 | 453 | |||||||
| 454 | Returns a mutable buffer sequence representing | 454 | Returns a mutable buffer sequence representing | |||||
| 455 | the writable bytes. Use @ref stream_commit to make the | 455 | the writable bytes. Use @ref stream_commit to make the | |||||
| 456 | written data available to the serializer. | 456 | written data available to the serializer. | |||||
| 457 | 457 | |||||||
| 458 | All buffer sequences previously obtained | 458 | All buffer sequences previously obtained | |||||
| 459 | using @ref stream_prepare are invalidated. | 459 | using @ref stream_prepare are invalidated. | |||||
| 460 | 460 | |||||||
| 461 | @par Preconditions | 461 | @par Preconditions | |||||
| 462 | The serializer is in streaming mode. | 462 | The serializer is in streaming mode. | |||||
| 463 | 463 | |||||||
| 464 | @par Exception Safety | 464 | @par Exception Safety | |||||
| 465 | Strong guarantee. | 465 | Strong guarantee. | |||||
| 466 | 466 | |||||||
| 467 | @return An instance of @ref mutable_buffers_type. | 467 | @return An instance of @ref mutable_buffers_type. | |||||
| 468 | The underlying memory is owned by the serializer. | 468 | The underlying memory is owned by the serializer. | |||||
| 469 | 469 | |||||||
| 470 | @throw std::logic_error if not in streaming mode. | 470 | @throw std::logic_error if not in streaming mode. | |||||
| 471 | 471 | |||||||
| 472 | @see | 472 | @see | |||||
| 473 | @ref stream_commit, | 473 | @ref stream_commit, | |||||
| 474 | @ref stream_capacity. | 474 | @ref stream_capacity. | |||||
| 475 | */ | 475 | */ | |||||
| 476 | BOOST_HTTP_DECL | 476 | BOOST_HTTP_DECL | |||||
| 477 | mutable_buffers_type | 477 | mutable_buffers_type | |||||
| 478 | stream_prepare(); | 478 | stream_prepare(); | |||||
| 479 | 479 | |||||||
| 480 | /** Commit data to the serializer stream. | 480 | /** Commit data to the serializer stream. | |||||
| 481 | 481 | |||||||
| 482 | Makes `n` bytes available to the serializer. | 482 | Makes `n` bytes available to the serializer. | |||||
| 483 | 483 | |||||||
| 484 | All buffer sequences previously obtained | 484 | All buffer sequences previously obtained | |||||
| 485 | using @ref stream_prepare are invalidated. | 485 | using @ref stream_prepare are invalidated. | |||||
| 486 | 486 | |||||||
| 487 | @par Preconditions | 487 | @par Preconditions | |||||
| 488 | The serializer is in streaming mode and | 488 | The serializer is in streaming mode and | |||||
| 489 | `n <= stream_capacity()`. | 489 | `n <= stream_capacity()`. | |||||
| 490 | 490 | |||||||
| 491 | @par Exception Safety | 491 | @par Exception Safety | |||||
| 492 | Strong guarantee. | 492 | Strong guarantee. | |||||
| 493 | Exceptions thrown on invalid input. | 493 | Exceptions thrown on invalid input. | |||||
| 494 | 494 | |||||||
| 495 | @param n The number of bytes to commit. | 495 | @param n The number of bytes to commit. | |||||
| 496 | 496 | |||||||
| 497 | @throw std::invalid_argument if `n > stream_capacity()`. | 497 | @throw std::invalid_argument if `n > stream_capacity()`. | |||||
| 498 | 498 | |||||||
| 499 | @throw std::logic_error if not in streaming mode. | 499 | @throw std::logic_error if not in streaming mode. | |||||
| 500 | 500 | |||||||
| 501 | @see | 501 | @see | |||||
| 502 | @ref stream_prepare, | 502 | @ref stream_prepare, | |||||
| 503 | @ref stream_capacity. | 503 | @ref stream_capacity. | |||||
| 504 | */ | 504 | */ | |||||
| 505 | BOOST_HTTP_DECL | 505 | BOOST_HTTP_DECL | |||||
| 506 | void | 506 | void | |||||
| 507 | stream_commit(std::size_t n); | 507 | stream_commit(std::size_t n); | |||||
| 508 | 508 | |||||||
| 509 | /** Close the stream. | 509 | /** Close the stream. | |||||
| 510 | 510 | |||||||
| 511 | Notifies the serializer that the message body | 511 | Notifies the serializer that the message body | |||||
| 512 | has ended. After calling this function, no more | 512 | has ended. After calling this function, no more | |||||
| 513 | data can be written to the stream. | 513 | data can be written to the stream. | |||||
| 514 | 514 | |||||||
| 515 | @par Preconditions | 515 | @par Preconditions | |||||
| 516 | The serializer is in streaming mode. | 516 | The serializer is in streaming mode. | |||||
| 517 | 517 | |||||||
| 518 | @par Postconditions | 518 | @par Postconditions | |||||
| 519 | The stream is closed. | 519 | The stream is closed. | |||||
| 520 | */ | 520 | */ | |||||
| 521 | BOOST_HTTP_DECL | 521 | BOOST_HTTP_DECL | |||||
| 522 | void | 522 | void | |||||
| 523 | stream_close() noexcept; | 523 | stream_close() noexcept; | |||||
| 524 | 524 | |||||||
| 525 | private: | 525 | private: | |||||
| 526 | class impl; | 526 | class impl; | |||||
| 527 | 527 | |||||||
| 528 | BOOST_HTTP_DECL | 528 | BOOST_HTTP_DECL | |||||
| 529 | detail::workspace& | 529 | detail::workspace& | |||||
| 530 | ws(); | 530 | ws(); | |||||
| 531 | 531 | |||||||
| 532 | impl* impl_ = nullptr; | 532 | impl* impl_ = nullptr; | |||||
| 533 | }; | 533 | }; | |||||
| 534 | 534 | |||||||
| 535 | //------------------------------------------------ | 535 | //------------------------------------------------ | |||||
| 536 | 536 | |||||||
| 537 | /** A sink adapter for writing HTTP message bodies. | 537 | /** A sink adapter for writing HTTP message bodies. | |||||
| 538 | 538 | |||||||
| 539 | Wraps a @ref serializer and a @ref capy::WriteStream to | 539 | Wraps a @ref serializer and a @ref capy::WriteStream to | |||||
| 540 | provide two interfaces for body writing: | 540 | provide two interfaces for body writing: | |||||
| 541 | 541 | |||||||
| 542 | @li **BufferSink** (@ref prepare / @ref commit / | 542 | @li **BufferSink** (@ref prepare / @ref commit / | |||||
| 543 | @ref commit_eof) — write directly into the serializer's | 543 | @ref commit_eof) — write directly into the serializer's | |||||
| 544 | internal buffer (zero-copy). Triggers @ref start_writes | 544 | internal buffer (zero-copy). Triggers @ref start_writes | |||||
| 545 | lazily. | 545 | lazily. | |||||
| 546 | @li **WriteSink** (@ref write / @ref write_eof) — pass | 546 | @li **WriteSink** (@ref write / @ref write_eof) — pass | |||||
| 547 | caller-owned buffers; the sink copies data through the | 547 | caller-owned buffers; the sink copies data through the | |||||
| 548 | serializer. Triggers @ref start_buffers lazily. | 548 | serializer. Triggers @ref start_buffers lazily. | |||||
| 549 | 549 | |||||||
| 550 | Both interfaces handle chunked framing, compression, and | 550 | Both interfaces handle chunked framing, compression, and | |||||
| 551 | Content-Length validation automatically. | 551 | Content-Length validation automatically. | |||||
| 552 | 552 | |||||||
| 553 | The sink is a lightweight handle that can be created once | 553 | The sink is a lightweight handle that can be created once | |||||
| 554 | and reused across multiple messages. The serializer and | 554 | and reused across multiple messages. The serializer and | |||||
| 555 | stream must outlive the sink. | 555 | stream must outlive the sink. | |||||
| 556 | 556 | |||||||
| 557 | @tparam Stream The underlying stream type satisfying | 557 | @tparam Stream The underlying stream type satisfying | |||||
| 558 | @ref capy::WriteStream. | 558 | @ref capy::WriteStream. | |||||
| 559 | 559 | |||||||
| 560 | @par Thread Safety | 560 | @par Thread Safety | |||||
| 561 | Distinct objects: Safe. | 561 | Distinct objects: Safe. | |||||
| 562 | Shared objects: Unsafe. | 562 | Shared objects: Unsafe. | |||||
| 563 | 563 | |||||||
| 564 | @par Example | 564 | @par Example | |||||
| 565 | @code | 565 | @code | |||||
| 566 | capy::task<> | 566 | capy::task<> | |||||
| 567 | send_response(capy::WriteStream auto& socket) | 567 | send_response(capy::WriteStream auto& socket) | |||||
| 568 | { | 568 | { | |||||
| 569 | http::serializer sr(cfg); | 569 | http::serializer sr(cfg); | |||||
| 570 | auto sink = sr.sink_for(socket); | 570 | auto sink = sr.sink_for(socket); | |||||
| 571 | 571 | |||||||
| 572 | http::response res; | 572 | http::response res; | |||||
| 573 | res.set_payload_size(5); | 573 | res.set_payload_size(5); | |||||
| 574 | sr.set_message(res); | 574 | sr.set_message(res); | |||||
| 575 | 575 | |||||||
| 576 | // WriteSink: pass your own buffer | 576 | // WriteSink: pass your own buffer | |||||
| 577 | co_await sink.write_eof( | 577 | co_await sink.write_eof( | |||||
| 578 | capy::make_buffer(std::string_view("hello"))); | 578 | capy::make_buffer(std::string_view("hello"))); | |||||
| 579 | } | 579 | } | |||||
| 580 | @endcode | 580 | @endcode | |||||
| 581 | 581 | |||||||
| 582 | @see @ref http::BufferSink, @ref http::any_buffer_sink, | 582 | @see @ref http::BufferSink, @ref http::any_buffer_sink, | |||||
| 583 | @ref serializer. | 583 | @ref serializer. | |||||
| 584 | */ | 584 | */ | |||||
| 585 | template<capy::WriteStream Stream> | 585 | template<capy::WriteStream Stream> | |||||
| 586 | class serializer::sink | 586 | class serializer::sink | |||||
| 587 | { | 587 | { | |||||
| 588 | Stream* stream_ = nullptr; | 588 | Stream* stream_ = nullptr; | |||||
| 589 | serializer* sr_ = nullptr; | 589 | serializer* sr_ = nullptr; | |||||
| 590 | 590 | |||||||
| 591 | public: | 591 | public: | |||||
| 592 | /** Constructor. | 592 | /** Constructor. | |||||
| 593 | 593 | |||||||
| 594 | A default-constructed sink is in an empty state. | 594 | A default-constructed sink is in an empty state. | |||||
| 595 | */ | 595 | */ | |||||
| 596 | sink() noexcept = default; | 596 | sink() noexcept = default; | |||||
| 597 | 597 | |||||||
| 598 | /** Constructor. | 598 | /** Constructor. | |||||
| 599 | 599 | |||||||
| 600 | @param stream The underlying stream to write serialized data to. | 600 | @param stream The underlying stream to write serialized data to. | |||||
| 601 | @param sr The serializer performing HTTP framing. | 601 | @param sr The serializer performing HTTP framing. | |||||
| 602 | */ | 602 | */ | |||||
| HITCBC | 603 | 143 | sink( | 603 | 143 | sink( | ||
| 604 | Stream& stream, | 604 | Stream& stream, | |||||
| 605 | serializer& sr) noexcept | 605 | serializer& sr) noexcept | |||||
| HITCBC | 606 | 143 | : stream_(&stream) | 606 | 143 | : stream_(&stream) | ||
| HITCBC | 607 | 143 | , sr_(&sr) | 607 | 143 | , sr_(&sr) | ||
| 608 | { | 608 | { | |||||
| HITCBC | 609 | 143 | } | 609 | 143 | } | ||
| 610 | 610 | |||||||
| 611 | /** Prepare writable buffers. | 611 | /** Prepare writable buffers. | |||||
| 612 | 612 | |||||||
| 613 | Fills the provided span with mutable buffer descriptors | 613 | Fills the provided span with mutable buffer descriptors | |||||
| 614 | pointing to the serializer's internal storage. This | 614 | pointing to the serializer's internal storage. This | |||||
| 615 | operation is synchronous. Lazily starts the serializer | 615 | operation is synchronous. Lazily starts the serializer | |||||
| 616 | in write mode if not already started. | 616 | in write mode if not already started. | |||||
| 617 | 617 | |||||||
| 618 | @param dest Span of mutable_buffer to fill. | 618 | @param dest Span of mutable_buffer to fill. | |||||
| 619 | 619 | |||||||
| 620 | @return A span of filled buffers. | 620 | @return A span of filled buffers. | |||||
| 621 | */ | 621 | */ | |||||
| 622 | std::span<capy::mutable_buffer> | 622 | std::span<capy::mutable_buffer> | |||||
| HITCBC | 623 | 63 | prepare(std::span<capy::mutable_buffer> dest) | 623 | 63 | prepare(std::span<capy::mutable_buffer> dest) | ||
| 624 | { | 624 | { | |||||
| HITCBC | 625 | 63 | if(sr_->is_start()) | 625 | 63 | if(sr_->is_start()) | ||
| HITCBC | 626 | 57 | sr_->start_writes(); | 626 | 57 | sr_->start_writes(); | ||
| HITCBC | 627 | 63 | auto bufs = sr_->stream_prepare(); | 627 | 63 | auto bufs = sr_->stream_prepare(); | ||
| HITCBC | 628 | 63 | std::size_t count = 0; | 628 | 63 | std::size_t count = 0; | ||
| HITCBC | 629 | 126 | for(auto const& b : bufs) | 629 | 126 | for(auto const& b : bufs) | ||
| 630 | { | 630 | { | |||||
| HITCBC | 631 | 126 | if(count >= dest.size() || b.size() == 0) | 631 | 126 | if(count >= dest.size() || b.size() == 0) | ||
| HITCBC | 632 | 63 | break; | 632 | 63 | break; | ||
| HITCBC | 633 | 63 | dest[count++] = b; | 633 | 63 | dest[count++] = b; | ||
| 634 | } | 634 | } | |||||
| HITCBC | 635 | 126 | return dest.first(count); | 635 | 126 | return dest.first(count); | ||
| 636 | } | 636 | } | |||||
| 637 | 637 | |||||||
| 638 | /** Commit bytes written to the prepared buffers. | 638 | /** Commit bytes written to the prepared buffers. | |||||
| 639 | 639 | |||||||
| 640 | Commits `n` bytes written to the buffers returned by the | 640 | Commits `n` bytes written to the buffers returned by the | |||||
| 641 | most recent call to @ref prepare. The operation flushes | 641 | most recent call to @ref prepare. The operation flushes | |||||
| 642 | serialized output to the underlying stream. | 642 | serialized output to the underlying stream. | |||||
| 643 | 643 | |||||||
| 644 | @param n The number of bytes to commit. | 644 | @param n The number of bytes to commit. | |||||
| 645 | 645 | |||||||
| 646 | @return An awaitable yielding `(error_code)`. | 646 | @return An awaitable yielding `(error_code)`. | |||||
| 647 | */ | 647 | */ | |||||
| 648 | auto | 648 | auto | |||||
| HITCBC | 649 | 63 | commit(std::size_t n) | 649 | 63 | commit(std::size_t n) | ||
| 650 | -> capy::task<capy::results<>> | 650 | -> capy::task<capy::results<>> | |||||
| 651 | { | 651 | { | |||||
| 652 | if(sr_->is_start()) | 652 | if(sr_->is_start()) | |||||
| 653 | sr_->start_writes(); | 653 | sr_->start_writes(); | |||||
| 654 | sr_->stream_commit(n); | 654 | sr_->stream_commit(n); | |||||
| 655 | 655 | |||||||
| 656 | while(!sr_->is_done()) | 656 | while(!sr_->is_done()) | |||||
| 657 | { | 657 | { | |||||
| 658 | auto cbs = sr_->prepare(); | 658 | auto cbs = sr_->prepare(); | |||||
| 659 | if(cbs.has_error()) | 659 | if(cbs.has_error()) | |||||
| 660 | { | 660 | { | |||||
| 661 | if(cbs.error() == error::need_data) | 661 | if(cbs.error() == error::need_data) | |||||
| 662 | break; | 662 | break; | |||||
| 663 | co_return {std::error_code(cbs.error())}; | 663 | co_return {std::error_code(cbs.error())}; | |||||
| 664 | } | 664 | } | |||||
| 665 | 665 | |||||||
| 666 | if(capy::buffers_empty(*cbs)) | 666 | if(capy::buffers_empty(*cbs)) | |||||
| 667 | { | 667 | { | |||||
| 668 | // advance state machine | 668 | // advance state machine | |||||
| 669 | sr_->consume(0); | 669 | sr_->consume(0); | |||||
| 670 | continue; | 670 | continue; | |||||
| 671 | } | 671 | } | |||||
| 672 | 672 | |||||||
| 673 | auto [ec, written] = co_await stream_->write_some(*cbs); | 673 | auto [ec, written] = co_await stream_->write_some(*cbs); | |||||
| 674 | sr_->consume(written); | 674 | sr_->consume(written); | |||||
| 675 | 675 | |||||||
| 676 | if(ec) | 676 | if(ec) | |||||
| 677 | co_return {ec}; | 677 | co_return {ec}; | |||||
| 678 | } | 678 | } | |||||
| 679 | 679 | |||||||
| 680 | co_return {}; | 680 | co_return {}; | |||||
| HITCBC | 681 | 126 | } | 681 | 126 | } | ||
| 682 | 682 | |||||||
| 683 | /** Commit final bytes and signal end-of-stream. | 683 | /** Commit final bytes and signal end-of-stream. | |||||
| 684 | 684 | |||||||
| 685 | Commits `n` bytes written to the buffers returned by the | 685 | Commits `n` bytes written to the buffers returned by the | |||||
| 686 | most recent call to @ref prepare and closes the body stream, | 686 | most recent call to @ref prepare and closes the body stream, | |||||
| 687 | flushing any remaining serializer output to the underlying | 687 | flushing any remaining serializer output to the underlying | |||||
| 688 | stream. For chunked encoding, this writes the final | 688 | stream. For chunked encoding, this writes the final | |||||
| 689 | zero-length chunk. | 689 | zero-length chunk. | |||||
| 690 | 690 | |||||||
| 691 | @param n The number of bytes to commit. | 691 | @param n The number of bytes to commit. | |||||
| 692 | 692 | |||||||
| 693 | @return An awaitable yielding `(error_code)`. | 693 | @return An awaitable yielding `(error_code)`. | |||||
| 694 | 694 | |||||||
| 695 | @post The serializer's `is_done()` returns `true` on success. | 695 | @post The serializer's `is_done()` returns `true` on success. | |||||
| 696 | */ | 696 | */ | |||||
| 697 | auto | 697 | auto | |||||
| HITCBC | 698 | 35 | commit_eof(std::size_t n) | 698 | 35 | commit_eof(std::size_t n) | ||
| 699 | -> capy::task<capy::results<>> | 699 | -> capy::task<capy::results<>> | |||||
| 700 | { | 700 | { | |||||
| 701 | if(sr_->is_start()) | 701 | if(sr_->is_start()) | |||||
| 702 | sr_->start_writes(); | 702 | sr_->start_writes(); | |||||
| 703 | sr_->stream_commit(n); | 703 | sr_->stream_commit(n); | |||||
| 704 | sr_->stream_close(); | 704 | sr_->stream_close(); | |||||
| 705 | 705 | |||||||
| 706 | while(!sr_->is_done()) | 706 | while(!sr_->is_done()) | |||||
| 707 | { | 707 | { | |||||
| 708 | auto cbs = sr_->prepare(); | 708 | auto cbs = sr_->prepare(); | |||||
| 709 | if(cbs.has_error()) | 709 | if(cbs.has_error()) | |||||
| 710 | { | 710 | { | |||||
| 711 | if(cbs.error() == error::need_data) | 711 | if(cbs.error() == error::need_data) | |||||
| 712 | continue; | 712 | continue; | |||||
| 713 | co_return {std::error_code(cbs.error())}; | 713 | co_return {std::error_code(cbs.error())}; | |||||
| 714 | } | 714 | } | |||||
| 715 | 715 | |||||||
| 716 | if(capy::buffers_empty(*cbs)) | 716 | if(capy::buffers_empty(*cbs)) | |||||
| 717 | { | 717 | { | |||||
| 718 | // advance state machine | 718 | // advance state machine | |||||
| 719 | sr_->consume(0); | 719 | sr_->consume(0); | |||||
| 720 | continue; | 720 | continue; | |||||
| 721 | } | 721 | } | |||||
| 722 | 722 | |||||||
| 723 | auto [ec, written] = co_await stream_->write_some(*cbs); | 723 | auto [ec, written] = co_await stream_->write_some(*cbs); | |||||
| 724 | sr_->consume(written); | 724 | sr_->consume(written); | |||||
| 725 | 725 | |||||||
| 726 | if(ec) | 726 | if(ec) | |||||
| 727 | co_return {ec}; | 727 | co_return {ec}; | |||||
| 728 | } | 728 | } | |||||
| 729 | 729 | |||||||
| 730 | co_return {}; | 730 | co_return {}; | |||||
| HITCBC | 731 | 70 | } | 731 | 70 | } | ||
| 732 | 732 | |||||||
| 733 | /** Write body data from caller-owned buffers. | 733 | /** Write body data from caller-owned buffers. | |||||
| 734 | 734 | |||||||
| 735 | Lazily starts the serializer in buffer mode if not | 735 | Lazily starts the serializer in buffer mode if not | |||||
| 736 | already started. Writes all data from the provided | 736 | already started. Writes all data from the provided | |||||
| 737 | buffers through the serializer to the underlying stream. | 737 | buffers through the serializer to the underlying stream. | |||||
| 738 | 738 | |||||||
| 739 | @param buffers The buffer sequence containing body data. | 739 | @param buffers The buffer sequence containing body data. | |||||
| 740 | 740 | |||||||
| 741 | @return An awaitable yielding `(error_code, std::size_t)`. | 741 | @return An awaitable yielding `(error_code, std::size_t)`. | |||||
| 742 | The size_t is the total number of body bytes written. | 742 | The size_t is the total number of body bytes written. | |||||
| 743 | */ | 743 | */ | |||||
| 744 | template<class ConstBufferSequence> | 744 | template<class ConstBufferSequence> | |||||
| 745 | auto | 745 | auto | |||||
| HITCBC | 746 | 72 | write(ConstBufferSequence const& buffers) | 746 | 72 | write(ConstBufferSequence const& buffers) | ||
| 747 | -> capy::task<capy::results<std::size_t>> | 747 | -> capy::task<capy::results<std::size_t>> | |||||
| 748 | { | 748 | { | |||||
| 749 | if(sr_->is_start()) | 749 | if(sr_->is_start()) | |||||
| 750 | sr_->start_buffers(); | 750 | sr_->start_buffers(); | |||||
| 751 | 751 | |||||||
| 752 | // Drain header first | 752 | // Drain header first | |||||
| 753 | while(!sr_->is_done()) | 753 | while(!sr_->is_done()) | |||||
| 754 | { | 754 | { | |||||
| 755 | auto cbs = sr_->prepare(); | 755 | auto cbs = sr_->prepare(); | |||||
| 756 | if(cbs.has_error()) | 756 | if(cbs.has_error()) | |||||
| 757 | { | 757 | { | |||||
| 758 | if(cbs.error() == error::need_data) | 758 | if(cbs.error() == error::need_data) | |||||
| 759 | break; | 759 | break; | |||||
| 760 | co_return {cbs.error(), 0}; | 760 | co_return {cbs.error(), 0}; | |||||
| 761 | } | 761 | } | |||||
| 762 | 762 | |||||||
| 763 | if(capy::buffers_empty(*cbs)) | 763 | if(capy::buffers_empty(*cbs)) | |||||
| 764 | { | 764 | { | |||||
| 765 | // advance state machine | 765 | // advance state machine | |||||
| 766 | sr_->consume(0); | 766 | sr_->consume(0); | |||||
| 767 | continue; | 767 | continue; | |||||
| 768 | } | 768 | } | |||||
| 769 | 769 | |||||||
| 770 | auto [ec, written] = co_await stream_->write_some(*cbs); | 770 | auto [ec, written] = co_await stream_->write_some(*cbs); | |||||
| 771 | sr_->consume(written); | 771 | sr_->consume(written); | |||||
| 772 | 772 | |||||||
| 773 | if(ec) | 773 | if(ec) | |||||
| 774 | co_return {ec, 0}; | 774 | co_return {ec, 0}; | |||||
| 775 | } | 775 | } | |||||
| 776 | 776 | |||||||
| 777 | // Write body data through stream_prepare/commit | 777 | // Write body data through stream_prepare/commit | |||||
| 778 | std::size_t total = 0; | 778 | std::size_t total = 0; | |||||
| 779 | for(auto it = capy::buffer_sequence_begin(buffers); | 779 | for(auto it = capy::buffer_sequence_begin(buffers); | |||||
| 780 | it != capy::buffer_sequence_end(buffers); ++it) | 780 | it != capy::buffer_sequence_end(buffers); ++it) | |||||
| 781 | { | 781 | { | |||||
| 782 | capy::const_buffer src = *it; | 782 | capy::const_buffer src = *it; | |||||
| 783 | while(src.size() != 0) | 783 | while(src.size() != 0) | |||||
| 784 | { | 784 | { | |||||
| 785 | auto mbp = sr_->stream_prepare(); | 785 | auto mbp = sr_->stream_prepare(); | |||||
| 786 | std::size_t copied = 0; | 786 | std::size_t copied = 0; | |||||
| 787 | for(auto const& mb : mbp) | 787 | for(auto const& mb : mbp) | |||||
| 788 | { | 788 | { | |||||
| 789 | auto chunk = (std::min)( | 789 | auto chunk = (std::min)( | |||||
| 790 | mb.size(), src.size()); | 790 | mb.size(), src.size()); | |||||
| 791 | if(chunk == 0) | 791 | if(chunk == 0) | |||||
| 792 | break; | 792 | break; | |||||
| 793 | std::memcpy(mb.data(), | 793 | std::memcpy(mb.data(), | |||||
| 794 | src.data(), chunk); | 794 | src.data(), chunk); | |||||
| 795 | src += chunk; | 795 | src += chunk; | |||||
| 796 | copied += chunk; | 796 | copied += chunk; | |||||
| 797 | } | 797 | } | |||||
| 798 | sr_->stream_commit(copied); | 798 | sr_->stream_commit(copied); | |||||
| 799 | total += copied; | 799 | total += copied; | |||||
| 800 | 800 | |||||||
| 801 | // Drain output | 801 | // Drain output | |||||
| 802 | while(!sr_->is_done()) | 802 | while(!sr_->is_done()) | |||||
| 803 | { | 803 | { | |||||
| 804 | auto cbs = sr_->prepare(); | 804 | auto cbs = sr_->prepare(); | |||||
| 805 | if(cbs.has_error()) | 805 | if(cbs.has_error()) | |||||
| 806 | { | 806 | { | |||||
| 807 | if(cbs.error() == error::need_data) | 807 | if(cbs.error() == error::need_data) | |||||
| 808 | break; | 808 | break; | |||||
| 809 | co_return {cbs.error(), total}; | 809 | co_return {cbs.error(), total}; | |||||
| 810 | } | 810 | } | |||||
| 811 | 811 | |||||||
| 812 | if(capy::buffers_empty(*cbs)) | 812 | if(capy::buffers_empty(*cbs)) | |||||
| 813 | { | 813 | { | |||||
| 814 | // advance state machine | 814 | // advance state machine | |||||
| 815 | sr_->consume(0); | 815 | sr_->consume(0); | |||||
| 816 | continue; | 816 | continue; | |||||
| 817 | } | 817 | } | |||||
| 818 | 818 | |||||||
| 819 | auto [ec, written] = | 819 | auto [ec, written] = | |||||
| 820 | co_await stream_->write_some(*cbs); | 820 | co_await stream_->write_some(*cbs); | |||||
| 821 | sr_->consume(written); | 821 | sr_->consume(written); | |||||
| 822 | 822 | |||||||
| 823 | if(ec) | 823 | if(ec) | |||||
| 824 | co_return {ec, total}; | 824 | co_return {ec, total}; | |||||
| 825 | } | 825 | } | |||||
| 826 | } | 826 | } | |||||
| 827 | } | 827 | } | |||||
| 828 | 828 | |||||||
| 829 | co_return {std::error_code(), total}; | 829 | co_return {std::error_code(), total}; | |||||
| HITCBC | 830 | 144 | } | 830 | 144 | } | ||
| 831 | 831 | |||||||
| 832 | /** Write final body data and signal end-of-stream. | 832 | /** Write final body data and signal end-of-stream. | |||||
| 833 | 833 | |||||||
| 834 | Lazily starts the serializer in buffer mode if not | 834 | Lazily starts the serializer in buffer mode if not | |||||
| 835 | already started. Writes all data from the provided | 835 | already started. Writes all data from the provided | |||||
| 836 | buffers and then closes the body stream, flushing | 836 | buffers and then closes the body stream, flushing | |||||
| 837 | any remaining output to the underlying stream. | 837 | any remaining output to the underlying stream. | |||||
| 838 | 838 | |||||||
| 839 | @param buffers The buffer sequence containing final body data. | 839 | @param buffers The buffer sequence containing final body data. | |||||
| 840 | 840 | |||||||
| 841 | @return An awaitable yielding `(error_code, std::size_t)`. | 841 | @return An awaitable yielding `(error_code, std::size_t)`. | |||||
| 842 | The size_t is the total number of body bytes written. | 842 | The size_t is the total number of body bytes written. | |||||
| 843 | 843 | |||||||
| 844 | @post The serializer's `is_done()` returns `true` on success. | 844 | @post The serializer's `is_done()` returns `true` on success. | |||||
| 845 | */ | 845 | */ | |||||
| 846 | template<class ConstBufferSequence> | 846 | template<class ConstBufferSequence> | |||||
| 847 | auto | 847 | auto | |||||
| HITCBC | 848 | 44 | write_eof(ConstBufferSequence const& buffers) | 848 | 44 | write_eof(ConstBufferSequence const& buffers) | ||
| 849 | -> capy::task<capy::results<std::size_t>> | 849 | -> capy::task<capy::results<std::size_t>> | |||||
| 850 | { | 850 | { | |||||
| 851 | auto [ec, n] = co_await write(buffers); | 851 | auto [ec, n] = co_await write(buffers); | |||||
| 852 | if(ec) | 852 | if(ec) | |||||
| 853 | co_return {ec, n}; | 853 | co_return {ec, n}; | |||||
| 854 | 854 | |||||||
| 855 | sr_->stream_close(); | 855 | sr_->stream_close(); | |||||
| 856 | 856 | |||||||
| 857 | while(!sr_->is_done()) | 857 | while(!sr_->is_done()) | |||||
| 858 | { | 858 | { | |||||
| 859 | auto cbs = sr_->prepare(); | 859 | auto cbs = sr_->prepare(); | |||||
| 860 | if(cbs.has_error()) | 860 | if(cbs.has_error()) | |||||
| 861 | { | 861 | { | |||||
| 862 | if(cbs.error() == error::need_data) | 862 | if(cbs.error() == error::need_data) | |||||
| 863 | continue; | 863 | continue; | |||||
| 864 | co_return {cbs.error(), n}; | 864 | co_return {cbs.error(), n}; | |||||
| 865 | } | 865 | } | |||||
| 866 | 866 | |||||||
| 867 | if(capy::buffers_empty(*cbs)) | 867 | if(capy::buffers_empty(*cbs)) | |||||
| 868 | { | 868 | { | |||||
| 869 | // advance state machine | 869 | // advance state machine | |||||
| 870 | sr_->consume(0); | 870 | sr_->consume(0); | |||||
| 871 | continue; | 871 | continue; | |||||
| 872 | } | 872 | } | |||||
| 873 | 873 | |||||||
| 874 | auto [ec2, written] = co_await stream_->write_some(*cbs); | 874 | auto [ec2, written] = co_await stream_->write_some(*cbs); | |||||
| 875 | sr_->consume(written); | 875 | sr_->consume(written); | |||||
| 876 | 876 | |||||||
| 877 | if(ec2) | 877 | if(ec2) | |||||
| 878 | co_return {ec2, n}; | 878 | co_return {ec2, n}; | |||||
| 879 | } | 879 | } | |||||
| 880 | 880 | |||||||
| 881 | co_return {std::error_code(), n}; | 881 | co_return {std::error_code(), n}; | |||||
| HITCBC | 882 | 88 | } | 882 | 88 | } | ||
| 883 | 883 | |||||||
| 884 | /** Signal end-of-stream with no additional data. | 884 | /** Signal end-of-stream with no additional data. | |||||
| 885 | 885 | |||||||
| 886 | Lazily starts the serializer in buffer mode if not | 886 | Lazily starts the serializer in buffer mode if not | |||||
| 887 | already started. Closes the body stream and flushes | 887 | already started. Closes the body stream and flushes | |||||
| 888 | any remaining output to the underlying stream. | 888 | any remaining output to the underlying stream. | |||||
| 889 | 889 | |||||||
| 890 | @return An awaitable yielding `(error_code)`. | 890 | @return An awaitable yielding `(error_code)`. | |||||
| 891 | 891 | |||||||
| 892 | @post The serializer's `is_done()` returns `true` on success. | 892 | @post The serializer's `is_done()` returns `true` on success. | |||||
| 893 | */ | 893 | */ | |||||
| 894 | auto | 894 | auto | |||||
| HITCBC | 895 | 16 | write_eof() | 895 | 16 | write_eof() | ||
| 896 | -> capy::task<capy::results<>> | 896 | -> capy::task<capy::results<>> | |||||
| 897 | { | 897 | { | |||||
| 898 | if(sr_->is_start()) | 898 | if(sr_->is_start()) | |||||
| 899 | sr_->start_buffers(); | 899 | sr_->start_buffers(); | |||||
| 900 | 900 | |||||||
| 901 | sr_->stream_close(); | 901 | sr_->stream_close(); | |||||
| 902 | 902 | |||||||
| 903 | while(!sr_->is_done()) | 903 | while(!sr_->is_done()) | |||||
| 904 | { | 904 | { | |||||
| 905 | auto cbs = sr_->prepare(); | 905 | auto cbs = sr_->prepare(); | |||||
| 906 | if(cbs.has_error()) | 906 | if(cbs.has_error()) | |||||
| 907 | { | 907 | { | |||||
| 908 | if(cbs.error() == error::need_data) | 908 | if(cbs.error() == error::need_data) | |||||
| 909 | continue; | 909 | continue; | |||||
| 910 | co_return {std::error_code(cbs.error())}; | 910 | co_return {std::error_code(cbs.error())}; | |||||
| 911 | } | 911 | } | |||||
| 912 | 912 | |||||||
| 913 | if(capy::buffers_empty(*cbs)) | 913 | if(capy::buffers_empty(*cbs)) | |||||
| 914 | { | 914 | { | |||||
| 915 | // advance state machine | 915 | // advance state machine | |||||
| 916 | sr_->consume(0); | 916 | sr_->consume(0); | |||||
| 917 | continue; | 917 | continue; | |||||
| 918 | } | 918 | } | |||||
| 919 | 919 | |||||||
| 920 | auto [ec, written] = co_await stream_->write_some(*cbs); | 920 | auto [ec, written] = co_await stream_->write_some(*cbs); | |||||
| 921 | sr_->consume(written); | 921 | sr_->consume(written); | |||||
| 922 | 922 | |||||||
| 923 | if(ec) | 923 | if(ec) | |||||
| 924 | co_return {ec}; | 924 | co_return {ec}; | |||||
| 925 | } | 925 | } | |||||
| 926 | 926 | |||||||
| 927 | co_return {}; | 927 | co_return {}; | |||||
| HITCBC | 928 | 32 | } | 928 | 32 | } | ||
| 929 | }; | 929 | }; | |||||
| 930 | 930 | |||||||
| 931 | //------------------------------------------------ | 931 | //------------------------------------------------ | |||||
| 932 | 932 | |||||||
| 933 | template<capy::WriteStream Stream> | 933 | template<capy::WriteStream Stream> | |||||
| 934 | serializer::sink<Stream> | 934 | serializer::sink<Stream> | |||||
| HITCBC | 935 | 143 | serializer::sink_for(Stream& ws) noexcept | 935 | 143 | serializer::sink_for(Stream& ws) noexcept | ||
| 936 | { | 936 | { | |||||
| HITCBC | 937 | 143 | return sink<Stream>(ws, *this); | 937 | 143 | return sink<Stream>(ws, *this); | ||
| 938 | } | 938 | } | |||||
| 939 | 939 | |||||||
| 940 | } // http | 940 | } // http | |||||
| 941 | } // boost | 941 | } // boost | |||||
| 942 | 942 | |||||||
| 943 | #endif | 943 | #endif | |||||