| 1 | // |
| 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // Official repository: https://github.com/boostorg/beast |
| 8 | // |
| 9 | |
| 10 | #ifndef BOOST_BEAST_HTTP_WRITE_HPP |
| 11 | #define BOOST_BEAST_HTTP_WRITE_HPP |
| 12 | |
| 13 | #include <boost/beast/core/detail/config.hpp> |
| 14 | #include <boost/beast/http/message.hpp> |
| 15 | #include <boost/beast/http/serializer.hpp> |
| 16 | #include <boost/beast/http/type_traits.hpp> |
| 17 | #include <boost/beast/http/detail/chunk_encode.hpp> |
| 18 | #include <boost/beast/core/error.hpp> |
| 19 | #include <boost/beast/core/stream_traits.hpp> |
| 20 | #include <boost/asio/async_result.hpp> |
| 21 | #include <iosfwd> |
| 22 | #include <limits> |
| 23 | #include <memory> |
| 24 | #include <type_traits> |
| 25 | #include <utility> |
| 26 | |
| 27 | namespace boost { |
| 28 | namespace beast { |
| 29 | namespace http { |
| 30 | |
| 31 | /** Write part of a message to a stream using a serializer. |
| 32 | |
| 33 | This function is used to write part of a message to a stream using |
| 34 | a caller-provided HTTP/1 serializer. The call will block until one |
| 35 | of the following conditions is true: |
| 36 | |
| 37 | @li One or more bytes have been transferred. |
| 38 | |
| 39 | @li The function @ref serializer::is_done returns `true` |
| 40 | |
| 41 | @li An error occurs on the stream. |
| 42 | |
| 43 | This operation is implemented in terms of one or more calls |
| 44 | to the stream's `write_some` function. |
| 45 | |
| 46 | The amount of data actually transferred is controlled by the behavior |
| 47 | of the underlying stream, subject to the buffer size limit of the |
| 48 | serializer obtained or set through a call to @ref serializer::limit. |
| 49 | Setting a limit and performing bounded work helps applications set |
| 50 | reasonable timeouts. It also allows application-level flow control |
| 51 | to function correctly. For example when using a TCP/IP based |
| 52 | stream. |
| 53 | |
| 54 | @param stream The stream to which the data is to be written. |
| 55 | The type must support the <em>SyncWriteStream</em> concept. |
| 56 | |
| 57 | @param sr The serializer to use. |
| 58 | |
| 59 | @return The number of bytes written to the stream. |
| 60 | |
| 61 | @throws system_error Thrown on failure. |
| 62 | |
| 63 | @see serializer |
| 64 | */ |
| 65 | template< |
| 66 | class SyncWriteStream, |
| 67 | bool isRequest, class Body, class Fields> |
| 68 | std::size_t |
| 69 | write_some( |
| 70 | SyncWriteStream& stream, |
| 71 | serializer<isRequest, Body, Fields>& sr); |
| 72 | |
| 73 | /** Write part of a message to a stream using a serializer. |
| 74 | |
| 75 | This function is used to write part of a message to a stream using |
| 76 | a caller-provided HTTP/1 serializer. The call will block until one |
| 77 | of the following conditions is true: |
| 78 | |
| 79 | @li One or more bytes have been transferred. |
| 80 | |
| 81 | @li The function @ref serializer::is_done returns `true` |
| 82 | |
| 83 | @li An error occurs on the stream. |
| 84 | |
| 85 | This operation is implemented in terms of one or more calls |
| 86 | to the stream's `write_some` function. |
| 87 | |
| 88 | The amount of data actually transferred is controlled by the behavior |
| 89 | of the underlying stream, subject to the buffer size limit of the |
| 90 | serializer obtained or set through a call to @ref serializer::limit. |
| 91 | Setting a limit and performing bounded work helps applications set |
| 92 | reasonable timeouts. It also allows application-level flow control |
| 93 | to function correctly. For example when using a TCP/IP based |
| 94 | stream. |
| 95 | |
| 96 | @param stream The stream to which the data is to be written. |
| 97 | The type must support the <em>SyncWriteStream</em> concept. |
| 98 | |
| 99 | @param sr The serializer to use. |
| 100 | |
| 101 | @param ec Set to indicate what error occurred, if any. |
| 102 | |
| 103 | @return The number of bytes written to the stream. |
| 104 | |
| 105 | @see async_write_some, serializer |
| 106 | */ |
| 107 | template< |
| 108 | class SyncWriteStream, |
| 109 | bool isRequest, class Body, class Fields> |
| 110 | std::size_t |
| 111 | write_some( |
| 112 | SyncWriteStream& stream, |
| 113 | serializer<isRequest, Body, Fields>& sr, |
| 114 | error_code& ec); |
| 115 | |
| 116 | /** Write part of a message to a stream asynchronously using a serializer. |
| 117 | |
| 118 | This function is used to write part of a message to a stream |
| 119 | asynchronously using a caller-provided HTTP/1 serializer. The function |
| 120 | call always returns immediately. The asynchronous operation will continue |
| 121 | until one of the following conditions is true: |
| 122 | |
| 123 | @li One or more bytes have been transferred. |
| 124 | |
| 125 | @li The function @ref serializer::is_done returns `true` |
| 126 | |
| 127 | @li An error occurs on the stream. |
| 128 | |
| 129 | This operation is implemented in terms of zero or more calls to the stream's |
| 130 | `async_write_some` function, and is known as a <em>composed operation</em>. |
| 131 | The program must ensure that the stream performs no other writes |
| 132 | until this operation completes. |
| 133 | |
| 134 | The amount of data actually transferred is controlled by the behavior |
| 135 | of the underlying stream, subject to the buffer size limit of the |
| 136 | serializer obtained or set through a call to @ref serializer::limit. |
| 137 | Setting a limit and performing bounded work helps applications set |
| 138 | reasonable timeouts. It also allows application-level flow control |
| 139 | to function correctly. For example when using a TCP/IP based |
| 140 | stream. |
| 141 | |
| 142 | @param stream The stream to which the data is to be written. |
| 143 | The type must support the <em>AsyncWriteStream</em> concept. |
| 144 | |
| 145 | @param sr The serializer to use. |
| 146 | The object must remain valid at least until the |
| 147 | handler is called; ownership is not transferred. |
| 148 | |
| 149 | @param handler The completion handler to invoke when the operation |
| 150 | completes. The implementation takes ownership of the handler by |
| 151 | performing a decay-copy. The equivalent function signature of |
| 152 | the handler must be: |
| 153 | @code |
| 154 | void handler( |
| 155 | error_code const& error, // result of operation |
| 156 | std::size_t bytes_transferred // the number of bytes written to the stream |
| 157 | ); |
| 158 | @endcode |
| 159 | If the handler has an associated immediate executor, |
| 160 | an immediate completion will be dispatched to it. |
| 161 | Otherwise, the handler will not be invoked from within |
| 162 | this function. Invocation of the handler will be performed in a |
| 163 | manner equivalent to using `net::post`. |
| 164 | |
| 165 | @par Per-Operation Cancellation |
| 166 | |
| 167 | This asynchronous operation supports cancellation for the following |
| 168 | net::cancellation_type values: |
| 169 | |
| 170 | @li @c net::cancellation_type::terminal |
| 171 | |
| 172 | if the `stream` also supports terminal cancellation, `terminal` |
| 173 | cancellation leaves the stream in an undefined state, so that only |
| 174 | closing it is guaranteed to succeed. |
| 175 | |
| 176 | @see serializer |
| 177 | */ |
| 178 | template< |
| 179 | class AsyncWriteStream, |
| 180 | bool isRequest, class Body, class Fields, |
| 181 | BOOST_BEAST_ASYNC_TPARAM2 WriteHandler = |
| 182 | net::default_completion_token_t< |
| 183 | executor_type<AsyncWriteStream>>> |
| 184 | BOOST_BEAST_ASYNC_RESULT2(WriteHandler) |
| 185 | async_write_some( |
| 186 | AsyncWriteStream& stream, |
| 187 | serializer<isRequest, Body, Fields>& sr, |
| 188 | WriteHandler&& handler = |
| 189 | net::default_completion_token_t< |
| 190 | executor_type<AsyncWriteStream>>{}); |
| 191 | |
| 192 | //------------------------------------------------------------------------------ |
| 193 | |
| 194 | /** Write a header to a stream using a serializer. |
| 195 | |
| 196 | This function is used to write a header to a stream using a |
| 197 | caller-provided HTTP/1 serializer. The call will block until one |
| 198 | of the following conditions is true: |
| 199 | |
| 200 | @li The function @ref serializer::is_header_done returns `true` |
| 201 | |
| 202 | @li An error occurs. |
| 203 | |
| 204 | This operation is implemented in terms of one or more calls |
| 205 | to the stream's `write_some` function. |
| 206 | |
| 207 | @param stream The stream to which the data is to be written. |
| 208 | The type must support the <em>SyncWriteStream</em> concept. |
| 209 | |
| 210 | @param sr The serializer to use. |
| 211 | |
| 212 | @return The number of bytes written to the stream. |
| 213 | |
| 214 | @throws system_error Thrown on failure. |
| 215 | |
| 216 | @note The implementation will call @ref serializer::split with |
| 217 | the value `true` on the serializer passed in. |
| 218 | |
| 219 | @see serializer |
| 220 | */ |
| 221 | template< |
| 222 | class SyncWriteStream, |
| 223 | bool isRequest, class Body, class Fields> |
| 224 | std::size_t |
| 225 | ( |
| 226 | SyncWriteStream& stream, |
| 227 | serializer<isRequest, Body, Fields>& sr); |
| 228 | |
| 229 | /** Write a header to a stream using a serializer. |
| 230 | |
| 231 | This function is used to write a header to a stream using a |
| 232 | caller-provided HTTP/1 serializer. The call will block until one |
| 233 | of the following conditions is true: |
| 234 | |
| 235 | @li The function @ref serializer::is_header_done returns `true` |
| 236 | |
| 237 | @li An error occurs. |
| 238 | |
| 239 | This operation is implemented in terms of one or more calls |
| 240 | to the stream's `write_some` function. |
| 241 | |
| 242 | @param stream The stream to which the data is to be written. |
| 243 | The type must support the <em>SyncWriteStream</em> concept. |
| 244 | |
| 245 | @param sr The serializer to use. |
| 246 | |
| 247 | @param ec Set to indicate what error occurred, if any. |
| 248 | |
| 249 | @return The number of bytes written to the stream. |
| 250 | |
| 251 | @note The implementation will call @ref serializer::split with |
| 252 | the value `true` on the serializer passed in. |
| 253 | |
| 254 | @see serializer |
| 255 | */ |
| 256 | template< |
| 257 | class SyncWriteStream, |
| 258 | bool isRequest, class Body, class Fields> |
| 259 | std::size_t |
| 260 | ( |
| 261 | SyncWriteStream& stream, |
| 262 | serializer<isRequest, Body, Fields>& sr, |
| 263 | error_code& ec); |
| 264 | |
| 265 | /** Write a header to a stream asynchronously using a serializer. |
| 266 | |
| 267 | This function is used to write a header to a stream asynchronously |
| 268 | using a caller-provided HTTP/1 serializer. The function call always |
| 269 | returns immediately. The asynchronous operation will continue until |
| 270 | one of the following conditions is true: |
| 271 | |
| 272 | @li The function @ref serializer::is_header_done returns `true` |
| 273 | |
| 274 | @li An error occurs. |
| 275 | |
| 276 | This operation is implemented in terms of zero or more calls to the stream's |
| 277 | `async_write_some` function, and is known as a <em>composed operation</em>. |
| 278 | The program must ensure that the stream performs no other writes |
| 279 | until this operation completes. |
| 280 | |
| 281 | @param stream The stream to which the data is to be written. |
| 282 | The type must support the <em>AsyncWriteStream</em> concept. |
| 283 | |
| 284 | @param sr The serializer to use. |
| 285 | The object must remain valid at least until the |
| 286 | handler is called; ownership is not transferred. |
| 287 | |
| 288 | @param handler The completion handler to invoke when the operation |
| 289 | completes. The implementation takes ownership of the handler by |
| 290 | performing a decay-copy. The equivalent function signature of |
| 291 | the handler must be: |
| 292 | @code |
| 293 | void handler( |
| 294 | error_code const& error, // result of operation |
| 295 | std::size_t bytes_transferred // the number of bytes written to the stream |
| 296 | ); |
| 297 | @endcode |
| 298 | If the handler has an associated immediate executor, |
| 299 | an immediate completion will be dispatched to it. |
| 300 | Otherwise, the handler will not be invoked from within |
| 301 | this function. Invocation of the handler will be performed in a |
| 302 | manner equivalent to using `net::post`. |
| 303 | |
| 304 | @note The implementation will call @ref serializer::split with |
| 305 | the value `true` on the serializer passed in. |
| 306 | |
| 307 | @par Per-Operation Cancellation |
| 308 | |
| 309 | This asynchronous operation supports cancellation for the following |
| 310 | net::cancellation_type values: |
| 311 | |
| 312 | @li @c net::cancellation_type::terminal |
| 313 | |
| 314 | if the `stream` also supports terminal cancellation, `terminal` |
| 315 | cancellation leaves the stream in an undefined state, so that only |
| 316 | closing it is guaranteed to succeed. |
| 317 | |
| 318 | @see serializer |
| 319 | */ |
| 320 | template< |
| 321 | class AsyncWriteStream, |
| 322 | bool isRequest, class Body, class Fields, |
| 323 | BOOST_BEAST_ASYNC_TPARAM2 WriteHandler = |
| 324 | net::default_completion_token_t< |
| 325 | executor_type<AsyncWriteStream>>> |
| 326 | BOOST_BEAST_ASYNC_RESULT2(WriteHandler) |
| 327 | ( |
| 328 | AsyncWriteStream& stream, |
| 329 | serializer<isRequest, Body, Fields>& sr, |
| 330 | WriteHandler&& handler = |
| 331 | net::default_completion_token_t< |
| 332 | executor_type<AsyncWriteStream>>{}); |
| 333 | |
| 334 | //------------------------------------------------------------------------------ |
| 335 | |
| 336 | /** Write a complete message to a stream using a serializer. |
| 337 | |
| 338 | This function is used to write a complete message to a stream using |
| 339 | a caller-provided HTTP/1 serializer. The call will block until one |
| 340 | of the following conditions is true: |
| 341 | |
| 342 | @li The function @ref serializer::is_done returns `true` |
| 343 | |
| 344 | @li An error occurs. |
| 345 | |
| 346 | This operation is implemented in terms of one or more calls |
| 347 | to the stream's `write_some` function. |
| 348 | |
| 349 | @param stream The stream to which the data is to be written. |
| 350 | The type must support the <em>SyncWriteStream</em> concept. |
| 351 | |
| 352 | @param sr The serializer to use. |
| 353 | |
| 354 | @return The number of bytes written to the stream. |
| 355 | |
| 356 | @throws system_error Thrown on failure. |
| 357 | |
| 358 | @see serializer |
| 359 | */ |
| 360 | template< |
| 361 | class SyncWriteStream, |
| 362 | bool isRequest, class Body, class Fields> |
| 363 | std::size_t |
| 364 | write( |
| 365 | SyncWriteStream& stream, |
| 366 | serializer<isRequest, Body, Fields>& sr); |
| 367 | |
| 368 | /** Write a complete message to a stream using a serializer. |
| 369 | |
| 370 | This function is used to write a complete message to a stream using |
| 371 | a caller-provided HTTP/1 serializer. The call will block until one |
| 372 | of the following conditions is true: |
| 373 | |
| 374 | @li The function @ref serializer::is_done returns `true` |
| 375 | |
| 376 | @li An error occurs. |
| 377 | |
| 378 | This operation is implemented in terms of one or more calls |
| 379 | to the stream's `write_some` function. |
| 380 | |
| 381 | @param stream The stream to which the data is to be written. |
| 382 | The type must support the <em>SyncWriteStream</em> concept. |
| 383 | |
| 384 | @param sr The serializer to use. |
| 385 | |
| 386 | @param ec Set to the error, if any occurred. |
| 387 | |
| 388 | @return The number of bytes written to the stream. |
| 389 | |
| 390 | @see serializer |
| 391 | */ |
| 392 | template< |
| 393 | class SyncWriteStream, |
| 394 | bool isRequest, class Body, class Fields> |
| 395 | std::size_t |
| 396 | write( |
| 397 | SyncWriteStream& stream, |
| 398 | serializer<isRequest, Body, Fields>& sr, |
| 399 | error_code& ec); |
| 400 | |
| 401 | /** Write a complete message to a stream asynchronously using a serializer. |
| 402 | |
| 403 | This function is used to write a complete message to a stream |
| 404 | asynchronously using a caller-provided HTTP/1 serializer. The |
| 405 | function call always returns immediately. The asynchronous |
| 406 | operation will continue until one of the following conditions is true: |
| 407 | |
| 408 | @li The function @ref serializer::is_done returns `true` |
| 409 | |
| 410 | @li An error occurs. |
| 411 | |
| 412 | This operation is implemented in terms of zero or more calls to the stream's |
| 413 | `async_write_some` function, and is known as a <em>composed operation</em>. |
| 414 | The program must ensure that the stream performs no other writes |
| 415 | until this operation completes. |
| 416 | |
| 417 | @param stream The stream to which the data is to be written. |
| 418 | The type must support the <em>AsyncWriteStream</em> concept. |
| 419 | |
| 420 | @param sr The serializer to use. |
| 421 | The object must remain valid at least until the |
| 422 | handler is called; ownership is not transferred. |
| 423 | |
| 424 | @param handler The completion handler to invoke when the operation |
| 425 | completes. The implementation takes ownership of the handler by |
| 426 | performing a decay-copy. The equivalent function signature of |
| 427 | the handler must be: |
| 428 | @code |
| 429 | void handler( |
| 430 | error_code const& error, // result of operation |
| 431 | std::size_t bytes_transferred // the number of bytes written to the stream |
| 432 | ); |
| 433 | @endcode |
| 434 | If the handler has an associated immediate executor, |
| 435 | an immediate completion will be dispatched to it. |
| 436 | Otherwise, the handler will not be invoked from within |
| 437 | this function. Invocation of the handler will be performed in a |
| 438 | manner equivalent to using `net::post`. |
| 439 | |
| 440 | @par Per-Operation Cancellation |
| 441 | |
| 442 | This asynchronous operation supports cancellation for the following |
| 443 | net::cancellation_type values: |
| 444 | |
| 445 | @li @c net::cancellation_type::terminal |
| 446 | |
| 447 | if the `stream` also supports terminal cancellation, `terminal` |
| 448 | cancellation leaves the stream in an undefined state, so that only |
| 449 | closing it is guaranteed to succeed. |
| 450 | |
| 451 | @see serializer |
| 452 | */ |
| 453 | template< |
| 454 | class AsyncWriteStream, |
| 455 | bool isRequest, class Body, class Fields, |
| 456 | BOOST_BEAST_ASYNC_TPARAM2 WriteHandler = |
| 457 | net::default_completion_token_t< |
| 458 | executor_type<AsyncWriteStream>>> |
| 459 | BOOST_BEAST_ASYNC_RESULT2(WriteHandler) |
| 460 | async_write( |
| 461 | AsyncWriteStream& stream, |
| 462 | serializer<isRequest, Body, Fields>& sr, |
| 463 | WriteHandler&& handler = |
| 464 | net::default_completion_token_t< |
| 465 | executor_type<AsyncWriteStream>>{}); |
| 466 | |
| 467 | //------------------------------------------------------------------------------ |
| 468 | |
| 469 | /** Write a complete message to a stream. |
| 470 | |
| 471 | This function is used to write a complete message to a stream using |
| 472 | HTTP/1. The call will block until one of the following conditions is true: |
| 473 | |
| 474 | @li The entire message is written. |
| 475 | |
| 476 | @li An error occurs. |
| 477 | |
| 478 | This operation is implemented in terms of one or more calls to the stream's |
| 479 | `write_some` function. The algorithm will use a temporary @ref serializer |
| 480 | with an empty chunk decorator to produce buffers. |
| 481 | |
| 482 | @note This function only participates in overload resolution |
| 483 | if @ref is_mutable_body_writer for <em>Body</em> returns `true`. |
| 484 | |
| 485 | @param stream The stream to which the data is to be written. |
| 486 | The type must support the <em>SyncWriteStream</em> concept. |
| 487 | |
| 488 | @param msg The message to write. |
| 489 | |
| 490 | @return The number of bytes written to the stream. |
| 491 | |
| 492 | @throws system_error Thrown on failure. |
| 493 | |
| 494 | @see message |
| 495 | */ |
| 496 | template< |
| 497 | class SyncWriteStream, |
| 498 | bool isRequest, class Body, class Fields> |
| 499 | #if BOOST_BEAST_DOXYGEN |
| 500 | std::size_t |
| 501 | #else |
| 502 | typename std::enable_if< |
| 503 | is_mutable_body_writer<Body>::value, |
| 504 | std::size_t>::type |
| 505 | #endif |
| 506 | write( |
| 507 | SyncWriteStream& stream, |
| 508 | message<isRequest, Body, Fields>& msg); |
| 509 | |
| 510 | /** Write a complete message to a stream. |
| 511 | |
| 512 | This function is used to write a complete message to a stream using |
| 513 | HTTP/1. The call will block until one of the following conditions is true: |
| 514 | |
| 515 | @li The entire message is written. |
| 516 | |
| 517 | @li An error occurs. |
| 518 | |
| 519 | This operation is implemented in terms of one or more calls to the stream's |
| 520 | `write_some` function. The algorithm will use a temporary @ref serializer |
| 521 | with an empty chunk decorator to produce buffers. |
| 522 | |
| 523 | @note This function only participates in overload resolution |
| 524 | if @ref is_mutable_body_writer for <em>Body</em> returns `false`. |
| 525 | |
| 526 | @param stream The stream to which the data is to be written. |
| 527 | The type must support the <em>SyncWriteStream</em> concept. |
| 528 | |
| 529 | @param msg The message to write. |
| 530 | |
| 531 | @return The number of bytes written to the stream. |
| 532 | |
| 533 | @throws system_error Thrown on failure. |
| 534 | |
| 535 | @see message |
| 536 | */ |
| 537 | template< |
| 538 | class SyncWriteStream, |
| 539 | bool isRequest, class Body, class Fields> |
| 540 | #if BOOST_BEAST_DOXYGEN |
| 541 | std::size_t |
| 542 | #else |
| 543 | typename std::enable_if< |
| 544 | ! is_mutable_body_writer<Body>::value, |
| 545 | std::size_t>::type |
| 546 | #endif |
| 547 | write( |
| 548 | SyncWriteStream& stream, |
| 549 | message<isRequest, Body, Fields> const& msg); |
| 550 | |
| 551 | /** Write a complete message to a stream. |
| 552 | |
| 553 | This function is used to write a complete message to a stream using |
| 554 | HTTP/1. The call will block until one of the following conditions is true: |
| 555 | |
| 556 | @li The entire message is written. |
| 557 | |
| 558 | @li An error occurs. |
| 559 | |
| 560 | This operation is implemented in terms of one or more calls to the stream's |
| 561 | `write_some` function. The algorithm will use a temporary @ref serializer |
| 562 | with an empty chunk decorator to produce buffers. |
| 563 | |
| 564 | @note This function only participates in overload resolution |
| 565 | if @ref is_mutable_body_writer for <em>Body</em> returns `true`. |
| 566 | |
| 567 | @param stream The stream to which the data is to be written. |
| 568 | The type must support the <em>SyncWriteStream</em> concept. |
| 569 | |
| 570 | @param msg The message to write. |
| 571 | |
| 572 | @param ec Set to the error, if any occurred. |
| 573 | |
| 574 | @return The number of bytes written to the stream. |
| 575 | |
| 576 | @see message |
| 577 | */ |
| 578 | template< |
| 579 | class SyncWriteStream, |
| 580 | bool isRequest, class Body, class Fields> |
| 581 | #if BOOST_BEAST_DOXYGEN |
| 582 | std::size_t |
| 583 | #else |
| 584 | typename std::enable_if< |
| 585 | is_mutable_body_writer<Body>::value, |
| 586 | std::size_t>::type |
| 587 | #endif |
| 588 | write( |
| 589 | SyncWriteStream& stream, |
| 590 | message<isRequest, Body, Fields>& msg, |
| 591 | error_code& ec); |
| 592 | |
| 593 | /** Write a complete message to a stream. |
| 594 | |
| 595 | This function is used to write a complete message to a stream using |
| 596 | HTTP/1. The call will block until one of the following conditions is true: |
| 597 | |
| 598 | @li The entire message is written. |
| 599 | |
| 600 | @li An error occurs. |
| 601 | |
| 602 | This operation is implemented in terms of one or more calls to the stream's |
| 603 | `write_some` function. The algorithm will use a temporary @ref serializer |
| 604 | with an empty chunk decorator to produce buffers. |
| 605 | |
| 606 | @note This function only participates in overload resolution |
| 607 | if @ref is_mutable_body_writer for <em>Body</em> returns `false`. |
| 608 | |
| 609 | @param stream The stream to which the data is to be written. |
| 610 | The type must support the <em>SyncWriteStream</em> concept. |
| 611 | |
| 612 | @param msg The message to write. |
| 613 | |
| 614 | @param ec Set to the error, if any occurred. |
| 615 | |
| 616 | @return The number of bytes written to the stream. |
| 617 | |
| 618 | @see message |
| 619 | */ |
| 620 | template< |
| 621 | class SyncWriteStream, |
| 622 | bool isRequest, class Body, class Fields> |
| 623 | #if BOOST_BEAST_DOXYGEN |
| 624 | std::size_t |
| 625 | #else |
| 626 | typename std::enable_if< |
| 627 | ! is_mutable_body_writer<Body>::value, |
| 628 | std::size_t>::type |
| 629 | #endif |
| 630 | write( |
| 631 | SyncWriteStream& stream, |
| 632 | message<isRequest, Body, Fields> const& msg, |
| 633 | error_code& ec); |
| 634 | |
| 635 | /** Write a complete message to a stream asynchronously. |
| 636 | |
| 637 | This function is used to write a complete message to a stream asynchronously |
| 638 | using HTTP/1. The function call always returns immediately. The asynchronous |
| 639 | operation will continue until one of the following conditions is true: |
| 640 | |
| 641 | @li The entire message is written. |
| 642 | |
| 643 | @li An error occurs. |
| 644 | |
| 645 | This operation is implemented in terms of zero or more calls to the stream's |
| 646 | `async_write_some` function, and is known as a <em>composed operation</em>. |
| 647 | The program must ensure that the stream performs no other writes |
| 648 | until this operation completes. The algorithm will use a temporary |
| 649 | @ref serializer with an empty chunk decorator to produce buffers. |
| 650 | |
| 651 | @note This function only participates in overload resolution |
| 652 | if @ref is_mutable_body_writer for <em>Body</em> returns `true`. |
| 653 | |
| 654 | @param stream The stream to which the data is to be written. |
| 655 | The type must support the <em>AsyncWriteStream</em> concept. |
| 656 | |
| 657 | @param msg The message to write. |
| 658 | The object must remain valid at least until the |
| 659 | handler is called; ownership is not transferred. |
| 660 | |
| 661 | @param handler The completion handler to invoke when the operation |
| 662 | completes. The implementation takes ownership of the handler by |
| 663 | performing a decay-copy. The equivalent function signature of |
| 664 | the handler must be: |
| 665 | @code |
| 666 | void handler( |
| 667 | error_code const& error, // result of operation |
| 668 | std::size_t bytes_transferred // the number of bytes written to the stream |
| 669 | ); |
| 670 | @endcode |
| 671 | If the handler has an associated immediate executor, |
| 672 | an immediate completion will be dispatched to it. |
| 673 | Otherwise, the handler will not be invoked from within |
| 674 | this function. Invocation of the handler will be performed in a |
| 675 | manner equivalent to using `net::post`. |
| 676 | |
| 677 | @par Per-Operation Cancellation |
| 678 | |
| 679 | This asynchronous operation supports cancellation for the following |
| 680 | net::cancellation_type values: |
| 681 | |
| 682 | @li @c net::cancellation_type::terminal |
| 683 | |
| 684 | if the `stream` also supports terminal cancellation, `terminal` |
| 685 | cancellation leaves the stream in an undefined state, so that only |
| 686 | closing it is guaranteed to succeed. |
| 687 | |
| 688 | @see message |
| 689 | */ |
| 690 | template< |
| 691 | class AsyncWriteStream, |
| 692 | bool isRequest, class Body, class Fields, |
| 693 | BOOST_BEAST_ASYNC_TPARAM2 WriteHandler = |
| 694 | net::default_completion_token_t< |
| 695 | executor_type<AsyncWriteStream>>> |
| 696 | BOOST_BEAST_ASYNC_RESULT2(WriteHandler) |
| 697 | async_write( |
| 698 | AsyncWriteStream& stream, |
| 699 | message<isRequest, Body, Fields>& msg, |
| 700 | WriteHandler&& handler = |
| 701 | net::default_completion_token_t< |
| 702 | executor_type<AsyncWriteStream>>{} |
| 703 | #ifndef BOOST_BEAST_DOXYGEN |
| 704 | , typename std::enable_if< |
| 705 | is_mutable_body_writer<Body>::value>::type* = 0 |
| 706 | #endif |
| 707 | ); |
| 708 | |
| 709 | /** Write a complete message to a stream asynchronously. |
| 710 | |
| 711 | This function is used to write a complete message to a stream asynchronously |
| 712 | using HTTP/1. The function call always returns immediately. The asynchronous |
| 713 | operation will continue until one of the following conditions is true: |
| 714 | |
| 715 | @li The entire message is written. |
| 716 | |
| 717 | @li An error occurs. |
| 718 | |
| 719 | This operation is implemented in terms of zero or more calls to the stream's |
| 720 | `async_write_some` function, and is known as a <em>composed operation</em>. |
| 721 | The program must ensure that the stream performs no other writes |
| 722 | until this operation completes. The algorithm will use a temporary |
| 723 | @ref serializer with an empty chunk decorator to produce buffers. |
| 724 | |
| 725 | @note This function only participates in overload resolution |
| 726 | if @ref is_mutable_body_writer for <em>Body</em> returns `false`. |
| 727 | |
| 728 | @param stream The stream to which the data is to be written. |
| 729 | The type must support the <em>AsyncWriteStream</em> concept. |
| 730 | |
| 731 | @param msg The message to write. |
| 732 | The object must remain valid at least until the |
| 733 | handler is called; ownership is not transferred. |
| 734 | |
| 735 | @param handler The completion handler to invoke when the operation |
| 736 | completes. The implementation takes ownership of the handler by |
| 737 | performing a decay-copy. The equivalent function signature of |
| 738 | the handler must be: |
| 739 | @code |
| 740 | void handler( |
| 741 | error_code const& error, // result of operation |
| 742 | std::size_t bytes_transferred // the number of bytes written to the stream |
| 743 | ); |
| 744 | @endcode |
| 745 | If the handler has an associated immediate executor, |
| 746 | an immediate completion will be dispatched to it. |
| 747 | Otherwise, the handler will not be invoked from within |
| 748 | this function. Invocation of the handler will be performed in a |
| 749 | manner equivalent to using `net::post`. |
| 750 | |
| 751 | @par Per-Operation Cancellation |
| 752 | |
| 753 | This asynchronous operation supports cancellation for the following |
| 754 | net::cancellation_type values: |
| 755 | |
| 756 | @li @c net::cancellation_type::terminal |
| 757 | |
| 758 | if the `stream` also supports terminal cancellation, `terminal` |
| 759 | cancellation leaves the stream in an undefined state, so that only |
| 760 | closing it is guaranteed to succeed. |
| 761 | |
| 762 | @see message |
| 763 | */ |
| 764 | template< |
| 765 | class AsyncWriteStream, |
| 766 | bool isRequest, class Body, class Fields, |
| 767 | BOOST_BEAST_ASYNC_TPARAM2 WriteHandler = |
| 768 | net::default_completion_token_t< |
| 769 | executor_type<AsyncWriteStream>>> |
| 770 | BOOST_BEAST_ASYNC_RESULT2(WriteHandler) |
| 771 | async_write( |
| 772 | AsyncWriteStream& stream, |
| 773 | message<isRequest, Body, Fields> const& msg, |
| 774 | WriteHandler&& handler = |
| 775 | net::default_completion_token_t< |
| 776 | executor_type<AsyncWriteStream>>{} |
| 777 | #ifndef BOOST_BEAST_DOXYGEN |
| 778 | , typename std::enable_if< |
| 779 | ! is_mutable_body_writer<Body>::value>::type* = 0 |
| 780 | #endif |
| 781 | ); |
| 782 | |
| 783 | |
| 784 | //------------------------------------------------------------------------------ |
| 785 | |
| 786 | /** Serialize an HTTP/1 header to a `std::ostream`. |
| 787 | |
| 788 | The function converts the header to its HTTP/1 serialized |
| 789 | representation and stores the result in the output stream. |
| 790 | |
| 791 | @param os The output stream to write to. |
| 792 | |
| 793 | @param msg The message fields to write. |
| 794 | */ |
| 795 | template<bool isRequest, class Fields> |
| 796 | std::ostream& |
| 797 | (std::ostream& os, |
| 798 | header<isRequest, Fields> const& msg); |
| 799 | |
| 800 | /** Serialize an HTTP/1 message to a `std::ostream`. |
| 801 | |
| 802 | The function converts the message to its HTTP/1 serialized |
| 803 | representation and stores the result in the output stream. |
| 804 | |
| 805 | The implementation will automatically perform chunk encoding if |
| 806 | the contents of the message indicate that chunk encoding is required. |
| 807 | |
| 808 | @param os The output stream to write to. |
| 809 | |
| 810 | @param msg The message to write. |
| 811 | */ |
| 812 | template<bool isRequest, class Body, class Fields> |
| 813 | std::ostream& |
| 814 | operator<<(std::ostream& os, |
| 815 | message<isRequest, Body, Fields> const& msg); |
| 816 | |
| 817 | } // http |
| 818 | } // beast |
| 819 | } // boost |
| 820 | |
| 821 | #include <boost/beast/http/impl/write.hpp> |
| 822 | |
| 823 | #endif |
| 824 | |