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