write.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. #ifndef BOOST_BEAST_HTTP_WRITE_HPP
  10. #define BOOST_BEAST_HTTP_WRITE_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/http/message.hpp>
  13. #include <boost/beast/http/serializer.hpp>
  14. #include <boost/beast/http/type_traits.hpp>
  15. #include <boost/beast/http/detail/chunk_encode.hpp>
  16. #include <boost/beast/core/error.hpp>
  17. #include <boost/beast/core/stream_traits.hpp>
  18. #include <boost/asio/async_result.hpp>
  19. #include <iosfwd>
  20. #include <limits>
  21. #include <memory>
  22. #include <type_traits>
  23. #include <utility>
  24. namespace boost {
  25. namespace beast {
  26. namespace http {
  27. /** Write part of a message to a stream using a serializer.
  28. This function is used to write part of a message to a stream using
  29. a caller-provided HTTP/1 serializer. The call will block until one
  30. of the following conditions is true:
  31. @li One or more bytes have been transferred.
  32. @li The function @ref serializer::is_done returns `true`
  33. @li An error occurs on the stream.
  34. This operation is implemented in terms of one or more calls
  35. to the stream's `write_some` function.
  36. The amount of data actually transferred is controlled by the behavior
  37. of the underlying stream, subject to the buffer size limit of the
  38. serializer obtained or set through a call to @ref serializer::limit.
  39. Setting a limit and performing bounded work helps applications set
  40. reasonable timeouts. It also allows application-level flow control
  41. to function correctly. For example when using a TCP/IP based
  42. stream.
  43. @param stream The stream to which the data is to be written.
  44. The type must support the <em>SyncWriteStream</em> concept.
  45. @param sr The serializer to use.
  46. @return The number of bytes written to the stream.
  47. @throws system_error Thrown on failure.
  48. @see serializer
  49. */
  50. template<
  51. class SyncWriteStream,
  52. bool isRequest, class Body, class Fields>
  53. std::size_t
  54. write_some(
  55. SyncWriteStream& stream,
  56. serializer<isRequest, Body, Fields>& sr);
  57. /** Write part of a message to a stream using a serializer.
  58. This function is used to write part of a message to a stream using
  59. a caller-provided HTTP/1 serializer. The call will block until one
  60. of the following conditions is true:
  61. @li One or more bytes have been transferred.
  62. @li The function @ref serializer::is_done returns `true`
  63. @li An error occurs on the stream.
  64. This operation is implemented in terms of one or more calls
  65. to the stream's `write_some` function.
  66. The amount of data actually transferred is controlled by the behavior
  67. of the underlying stream, subject to the buffer size limit of the
  68. serializer obtained or set through a call to @ref serializer::limit.
  69. Setting a limit and performing bounded work helps applications set
  70. reasonable timeouts. It also allows application-level flow control
  71. to function correctly. For example when using a TCP/IP based
  72. stream.
  73. @param stream The stream to which the data is to be written.
  74. The type must support the <em>SyncWriteStream</em> concept.
  75. @param sr The serializer to use.
  76. @param ec Set to indicate what error occurred, if any.
  77. @return The number of bytes written to the stream.
  78. @see async_write_some, serializer
  79. */
  80. template<
  81. class SyncWriteStream,
  82. bool isRequest, class Body, class Fields>
  83. std::size_t
  84. write_some(
  85. SyncWriteStream& stream,
  86. serializer<isRequest, Body, Fields>& sr,
  87. error_code& ec);
  88. /** Write part of a message to a stream asynchronously using a serializer.
  89. This function is used to write part of a message to a stream
  90. asynchronously using a caller-provided HTTP/1 serializer. The function
  91. call always returns immediately. The asynchronous operation will continue
  92. until one of the following conditions is true:
  93. @li One or more bytes have been transferred.
  94. @li The function @ref serializer::is_done returns `true`
  95. @li An error occurs on the stream.
  96. This operation is implemented in terms of zero or more calls to the stream's
  97. `async_write_some` function, and is known as a <em>composed operation</em>.
  98. The program must ensure that the stream performs no other writes
  99. until this operation completes.
  100. The amount of data actually transferred is controlled by the behavior
  101. of the underlying stream, subject to the buffer size limit of the
  102. serializer obtained or set through a call to @ref serializer::limit.
  103. Setting a limit and performing bounded work helps applications set
  104. reasonable timeouts. It also allows application-level flow control
  105. to function correctly. For example when using a TCP/IP based
  106. stream.
  107. @param stream The stream to which the data is to be written.
  108. The type must support the <em>AsyncWriteStream</em> concept.
  109. @param sr The serializer to use.
  110. The object must remain valid at least until the
  111. handler is called; ownership is not transferred.
  112. @param handler The completion handler to invoke when the operation
  113. completes. The implementation takes ownership of the handler by
  114. performing a decay-copy. The equivalent function signature of
  115. the handler must be:
  116. @code
  117. void handler(
  118. error_code const& error, // result of operation
  119. std::size_t bytes_transferred // the number of bytes written to the stream
  120. );
  121. @endcode
  122. If the handler has an associated immediate executor,
  123. an immediate completion will be dispatched to it.
  124. Otherwise, the handler will not be invoked from within
  125. this function. Invocation of the handler will be performed in a
  126. manner equivalent to using `net::post`.
  127. @par Per-Operation Cancellation
  128. This asynchronous operation supports cancellation for the following
  129. net::cancellation_type values:
  130. @li @c net::cancellation_type::terminal
  131. if the `stream` also supports terminal cancellation, `terminal`
  132. cancellation leaves the stream in an undefined state, so that only
  133. closing it is guaranteed to succeed.
  134. @see serializer
  135. */
  136. template<
  137. class AsyncWriteStream,
  138. bool isRequest, class Body, class Fields,
  139. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  140. net::default_completion_token_t<
  141. executor_type<AsyncWriteStream>>>
  142. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  143. async_write_some(
  144. AsyncWriteStream& stream,
  145. serializer<isRequest, Body, Fields>& sr,
  146. WriteHandler&& handler =
  147. net::default_completion_token_t<
  148. executor_type<AsyncWriteStream>>{});
  149. //------------------------------------------------------------------------------
  150. /** Write a header to a stream using a serializer.
  151. This function is used to write a header to a stream using a
  152. caller-provided HTTP/1 serializer. The call will block until one
  153. of the following conditions is true:
  154. @li The function @ref serializer::is_header_done returns `true`
  155. @li An error occurs.
  156. This operation is implemented in terms of one or more calls
  157. to the stream's `write_some` function.
  158. @param stream The stream to which the data is to be written.
  159. The type must support the <em>SyncWriteStream</em> concept.
  160. @param sr The serializer to use.
  161. @return The number of bytes written to the stream.
  162. @throws system_error Thrown on failure.
  163. @note The implementation will call @ref serializer::split with
  164. the value `true` on the serializer passed in.
  165. @see serializer
  166. */
  167. template<
  168. class SyncWriteStream,
  169. bool isRequest, class Body, class Fields>
  170. std::size_t
  171. write_header(
  172. SyncWriteStream& stream,
  173. serializer<isRequest, Body, Fields>& sr);
  174. /** Write a header to a stream using a serializer.
  175. This function is used to write a header to a stream using a
  176. caller-provided HTTP/1 serializer. The call will block until one
  177. of the following conditions is true:
  178. @li The function @ref serializer::is_header_done returns `true`
  179. @li An error occurs.
  180. This operation is implemented in terms of one or more calls
  181. to the stream's `write_some` function.
  182. @param stream The stream to which the data is to be written.
  183. The type must support the <em>SyncWriteStream</em> concept.
  184. @param sr The serializer to use.
  185. @param ec Set to indicate what error occurred, if any.
  186. @return The number of bytes written to the stream.
  187. @note The implementation will call @ref serializer::split with
  188. the value `true` on the serializer passed in.
  189. @see serializer
  190. */
  191. template<
  192. class SyncWriteStream,
  193. bool isRequest, class Body, class Fields>
  194. std::size_t
  195. write_header(
  196. SyncWriteStream& stream,
  197. serializer<isRequest, Body, Fields>& sr,
  198. error_code& ec);
  199. /** Write a header to a stream asynchronously using a serializer.
  200. This function is used to write a header to a stream asynchronously
  201. using a caller-provided HTTP/1 serializer. The function call always
  202. returns immediately. The asynchronous operation will continue until
  203. one of the following conditions is true:
  204. @li The function @ref serializer::is_header_done returns `true`
  205. @li An error occurs.
  206. This operation is implemented in terms of zero or more calls to the stream's
  207. `async_write_some` function, and is known as a <em>composed operation</em>.
  208. The program must ensure that the stream performs no other writes
  209. until this operation completes.
  210. @param stream The stream to which the data is to be written.
  211. The type must support the <em>AsyncWriteStream</em> concept.
  212. @param sr The serializer to use.
  213. The object must remain valid at least until the
  214. handler is called; ownership is not transferred.
  215. @param handler The completion handler to invoke when the operation
  216. completes. The implementation takes ownership of the handler by
  217. performing a decay-copy. The equivalent function signature of
  218. the handler must be:
  219. @code
  220. void handler(
  221. error_code const& error, // result of operation
  222. std::size_t bytes_transferred // the number of bytes written to the stream
  223. );
  224. @endcode
  225. If the handler has an associated immediate executor,
  226. an immediate completion will be dispatched to it.
  227. Otherwise, the handler will not be invoked from within
  228. this function. Invocation of the handler will be performed in a
  229. manner equivalent to using `net::post`.
  230. @note The implementation will call @ref serializer::split with
  231. the value `true` on the serializer passed in.
  232. @par Per-Operation Cancellation
  233. This asynchronous operation supports cancellation for the following
  234. net::cancellation_type values:
  235. @li @c net::cancellation_type::terminal
  236. if the `stream` also supports terminal cancellation, `terminal`
  237. cancellation leaves the stream in an undefined state, so that only
  238. closing it is guaranteed to succeed.
  239. @see serializer
  240. */
  241. template<
  242. class AsyncWriteStream,
  243. bool isRequest, class Body, class Fields,
  244. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  245. net::default_completion_token_t<
  246. executor_type<AsyncWriteStream>>>
  247. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  248. async_write_header(
  249. AsyncWriteStream& stream,
  250. serializer<isRequest, Body, Fields>& sr,
  251. WriteHandler&& handler =
  252. net::default_completion_token_t<
  253. executor_type<AsyncWriteStream>>{});
  254. //------------------------------------------------------------------------------
  255. /** Write a complete message to a stream using a serializer.
  256. This function is used to write a complete message to a stream using
  257. a caller-provided HTTP/1 serializer. The call will block until one
  258. of the following conditions is true:
  259. @li The function @ref serializer::is_done returns `true`
  260. @li An error occurs.
  261. This operation is implemented in terms of one or more calls
  262. to the stream's `write_some` function.
  263. @param stream The stream to which the data is to be written.
  264. The type must support the <em>SyncWriteStream</em> concept.
  265. @param sr The serializer to use.
  266. @return The number of bytes written to the stream.
  267. @throws system_error Thrown on failure.
  268. @see serializer
  269. */
  270. template<
  271. class SyncWriteStream,
  272. bool isRequest, class Body, class Fields>
  273. std::size_t
  274. write(
  275. SyncWriteStream& stream,
  276. serializer<isRequest, Body, Fields>& sr);
  277. /** Write a complete message to a stream using a serializer.
  278. This function is used to write a complete message to a stream using
  279. a caller-provided HTTP/1 serializer. The call will block until one
  280. of the following conditions is true:
  281. @li The function @ref serializer::is_done returns `true`
  282. @li An error occurs.
  283. This operation is implemented in terms of one or more calls
  284. to the stream's `write_some` function.
  285. @param stream The stream to which the data is to be written.
  286. The type must support the <em>SyncWriteStream</em> concept.
  287. @param sr The serializer to use.
  288. @param ec Set to the error, if any occurred.
  289. @return The number of bytes written to the stream.
  290. @see serializer
  291. */
  292. template<
  293. class SyncWriteStream,
  294. bool isRequest, class Body, class Fields>
  295. std::size_t
  296. write(
  297. SyncWriteStream& stream,
  298. serializer<isRequest, Body, Fields>& sr,
  299. error_code& ec);
  300. /** Write a complete message to a stream asynchronously using a serializer.
  301. This function is used to write a complete message to a stream
  302. asynchronously using a caller-provided HTTP/1 serializer. The
  303. function call always returns immediately. The asynchronous
  304. operation will continue until one of the following conditions is true:
  305. @li The function @ref serializer::is_done returns `true`
  306. @li An error occurs.
  307. This operation is implemented in terms of zero or more calls to the stream's
  308. `async_write_some` function, and is known as a <em>composed operation</em>.
  309. The program must ensure that the stream performs no other writes
  310. until this operation completes.
  311. @param stream The stream to which the data is to be written.
  312. The type must support the <em>AsyncWriteStream</em> concept.
  313. @param sr The serializer to use.
  314. The object must remain valid at least until the
  315. handler is called; ownership is not transferred.
  316. @param handler The completion handler to invoke when the operation
  317. completes. The implementation takes ownership of the handler by
  318. performing a decay-copy. The equivalent function signature of
  319. the handler must be:
  320. @code
  321. void handler(
  322. error_code const& error, // result of operation
  323. std::size_t bytes_transferred // the number of bytes written to the stream
  324. );
  325. @endcode
  326. If the handler has an associated immediate executor,
  327. an immediate completion will be dispatched to it.
  328. Otherwise, the handler will not be invoked from within
  329. this function. Invocation of the handler will be performed in a
  330. manner equivalent to using `net::post`.
  331. @par Per-Operation Cancellation
  332. This asynchronous operation supports cancellation for the following
  333. net::cancellation_type values:
  334. @li @c net::cancellation_type::terminal
  335. if the `stream` also supports terminal cancellation, `terminal`
  336. cancellation leaves the stream in an undefined state, so that only
  337. closing it is guaranteed to succeed.
  338. @see serializer
  339. */
  340. template<
  341. class AsyncWriteStream,
  342. bool isRequest, class Body, class Fields,
  343. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  344. net::default_completion_token_t<
  345. executor_type<AsyncWriteStream>>>
  346. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  347. async_write(
  348. AsyncWriteStream& stream,
  349. serializer<isRequest, Body, Fields>& sr,
  350. WriteHandler&& handler =
  351. net::default_completion_token_t<
  352. executor_type<AsyncWriteStream>>{});
  353. //------------------------------------------------------------------------------
  354. /** Write a complete message to a stream.
  355. This function is used to write a complete message to a stream using
  356. HTTP/1. The call will block until one of the following conditions is true:
  357. @li The entire message is written.
  358. @li An error occurs.
  359. This operation is implemented in terms of one or more calls to the stream's
  360. `write_some` function. The algorithm will use a temporary @ref serializer
  361. with an empty chunk decorator to produce buffers.
  362. @note This function only participates in overload resolution
  363. if @ref is_mutable_body_writer for <em>Body</em> returns `true`.
  364. @param stream The stream to which the data is to be written.
  365. The type must support the <em>SyncWriteStream</em> concept.
  366. @param msg The message to write.
  367. @return The number of bytes written to the stream.
  368. @throws system_error Thrown on failure.
  369. @see message
  370. */
  371. template<
  372. class SyncWriteStream,
  373. bool isRequest, class Body, class Fields>
  374. #if BOOST_BEAST_DOXYGEN
  375. std::size_t
  376. #else
  377. typename std::enable_if<
  378. is_mutable_body_writer<Body>::value,
  379. std::size_t>::type
  380. #endif
  381. write(
  382. SyncWriteStream& stream,
  383. message<isRequest, Body, Fields>& msg);
  384. /** Write a complete message to a stream.
  385. This function is used to write a complete message to a stream using
  386. HTTP/1. The call will block until one of the following conditions is true:
  387. @li The entire message is written.
  388. @li An error occurs.
  389. This operation is implemented in terms of one or more calls to the stream's
  390. `write_some` function. The algorithm will use a temporary @ref serializer
  391. with an empty chunk decorator to produce buffers.
  392. @note This function only participates in overload resolution
  393. if @ref is_mutable_body_writer for <em>Body</em> returns `false`.
  394. @param stream The stream to which the data is to be written.
  395. The type must support the <em>SyncWriteStream</em> concept.
  396. @param msg The message to write.
  397. @return The number of bytes written to the stream.
  398. @throws system_error Thrown on failure.
  399. @see message
  400. */
  401. template<
  402. class SyncWriteStream,
  403. bool isRequest, class Body, class Fields>
  404. #if BOOST_BEAST_DOXYGEN
  405. std::size_t
  406. #else
  407. typename std::enable_if<
  408. ! is_mutable_body_writer<Body>::value,
  409. std::size_t>::type
  410. #endif
  411. write(
  412. SyncWriteStream& stream,
  413. message<isRequest, Body, Fields> const& msg);
  414. /** Write a complete message to a stream.
  415. This function is used to write a complete message to a stream using
  416. HTTP/1. The call will block until one of the following conditions is true:
  417. @li The entire message is written.
  418. @li An error occurs.
  419. This operation is implemented in terms of one or more calls to the stream's
  420. `write_some` function. The algorithm will use a temporary @ref serializer
  421. with an empty chunk decorator to produce buffers.
  422. @note This function only participates in overload resolution
  423. if @ref is_mutable_body_writer for <em>Body</em> returns `true`.
  424. @param stream The stream to which the data is to be written.
  425. The type must support the <em>SyncWriteStream</em> concept.
  426. @param msg The message to write.
  427. @param ec Set to the error, if any occurred.
  428. @return The number of bytes written to the stream.
  429. @see message
  430. */
  431. template<
  432. class SyncWriteStream,
  433. bool isRequest, class Body, class Fields>
  434. #if BOOST_BEAST_DOXYGEN
  435. std::size_t
  436. #else
  437. typename std::enable_if<
  438. is_mutable_body_writer<Body>::value,
  439. std::size_t>::type
  440. #endif
  441. write(
  442. SyncWriteStream& stream,
  443. message<isRequest, Body, Fields>& msg,
  444. error_code& ec);
  445. /** Write a complete message to a stream.
  446. This function is used to write a complete message to a stream using
  447. HTTP/1. The call will block until one of the following conditions is true:
  448. @li The entire message is written.
  449. @li An error occurs.
  450. This operation is implemented in terms of one or more calls to the stream's
  451. `write_some` function. The algorithm will use a temporary @ref serializer
  452. with an empty chunk decorator to produce buffers.
  453. @note This function only participates in overload resolution
  454. if @ref is_mutable_body_writer for <em>Body</em> returns `false`.
  455. @param stream The stream to which the data is to be written.
  456. The type must support the <em>SyncWriteStream</em> concept.
  457. @param msg The message to write.
  458. @param ec Set to the error, if any occurred.
  459. @return The number of bytes written to the stream.
  460. @see message
  461. */
  462. template<
  463. class SyncWriteStream,
  464. bool isRequest, class Body, class Fields>
  465. #if BOOST_BEAST_DOXYGEN
  466. std::size_t
  467. #else
  468. typename std::enable_if<
  469. ! is_mutable_body_writer<Body>::value,
  470. std::size_t>::type
  471. #endif
  472. write(
  473. SyncWriteStream& stream,
  474. message<isRequest, Body, Fields> const& msg,
  475. error_code& ec);
  476. /** Write a complete message to a stream asynchronously.
  477. This function is used to write a complete message to a stream asynchronously
  478. using HTTP/1. The function call always returns immediately. The asynchronous
  479. operation will continue until one of the following conditions is true:
  480. @li The entire message is written.
  481. @li An error occurs.
  482. This operation is implemented in terms of zero or more calls to the stream's
  483. `async_write_some` function, and is known as a <em>composed operation</em>.
  484. The program must ensure that the stream performs no other writes
  485. until this operation completes. The algorithm will use a temporary
  486. @ref serializer with an empty chunk decorator to produce buffers.
  487. @note This function only participates in overload resolution
  488. if @ref is_mutable_body_writer for <em>Body</em> returns `true`.
  489. @param stream The stream to which the data is to be written.
  490. The type must support the <em>AsyncWriteStream</em> concept.
  491. @param msg The message to write.
  492. The object must remain valid at least until the
  493. handler is called; ownership is not transferred.
  494. @param handler The completion handler to invoke when the operation
  495. completes. The implementation takes ownership of the handler by
  496. performing a decay-copy. The equivalent function signature of
  497. the handler must be:
  498. @code
  499. void handler(
  500. error_code const& error, // result of operation
  501. std::size_t bytes_transferred // the number of bytes written to the stream
  502. );
  503. @endcode
  504. If the handler has an associated immediate executor,
  505. an immediate completion will be dispatched to it.
  506. Otherwise, the handler will not be invoked from within
  507. this function. Invocation of the handler will be performed in a
  508. manner equivalent to using `net::post`.
  509. @par Per-Operation Cancellation
  510. This asynchronous operation supports cancellation for the following
  511. net::cancellation_type values:
  512. @li @c net::cancellation_type::terminal
  513. if the `stream` also supports terminal cancellation, `terminal`
  514. cancellation leaves the stream in an undefined state, so that only
  515. closing it is guaranteed to succeed.
  516. @see message
  517. */
  518. template<
  519. class AsyncWriteStream,
  520. bool isRequest, class Body, class Fields,
  521. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  522. net::default_completion_token_t<
  523. executor_type<AsyncWriteStream>>>
  524. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  525. async_write(
  526. AsyncWriteStream& stream,
  527. message<isRequest, Body, Fields>& msg,
  528. WriteHandler&& handler =
  529. net::default_completion_token_t<
  530. executor_type<AsyncWriteStream>>{}
  531. #ifndef BOOST_BEAST_DOXYGEN
  532. , typename std::enable_if<
  533. is_mutable_body_writer<Body>::value>::type* = 0
  534. #endif
  535. );
  536. /** Write a complete message to a stream asynchronously.
  537. This function is used to write a complete message to a stream asynchronously
  538. using HTTP/1. The function call always returns immediately. The asynchronous
  539. operation will continue until one of the following conditions is true:
  540. @li The entire message is written.
  541. @li An error occurs.
  542. This operation is implemented in terms of zero or more calls to the stream's
  543. `async_write_some` function, and is known as a <em>composed operation</em>.
  544. The program must ensure that the stream performs no other writes
  545. until this operation completes. The algorithm will use a temporary
  546. @ref serializer with an empty chunk decorator to produce buffers.
  547. @note This function only participates in overload resolution
  548. if @ref is_mutable_body_writer for <em>Body</em> returns `false`.
  549. @param stream The stream to which the data is to be written.
  550. The type must support the <em>AsyncWriteStream</em> concept.
  551. @param msg The message to write.
  552. The object must remain valid at least until the
  553. handler is called; ownership is not transferred.
  554. @param handler The completion handler to invoke when the operation
  555. completes. The implementation takes ownership of the handler by
  556. performing a decay-copy. The equivalent function signature of
  557. the handler must be:
  558. @code
  559. void handler(
  560. error_code const& error, // result of operation
  561. std::size_t bytes_transferred // the number of bytes written to the stream
  562. );
  563. @endcode
  564. If the handler has an associated immediate executor,
  565. an immediate completion will be dispatched to it.
  566. Otherwise, the handler will not be invoked from within
  567. this function. Invocation of the handler will be performed in a
  568. manner equivalent to using `net::post`.
  569. @par Per-Operation Cancellation
  570. This asynchronous operation supports cancellation for the following
  571. net::cancellation_type values:
  572. @li @c net::cancellation_type::terminal
  573. if the `stream` also supports terminal cancellation, `terminal`
  574. cancellation leaves the stream in an undefined state, so that only
  575. closing it is guaranteed to succeed.
  576. @see message
  577. */
  578. template<
  579. class AsyncWriteStream,
  580. bool isRequest, class Body, class Fields,
  581. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  582. net::default_completion_token_t<
  583. executor_type<AsyncWriteStream>>>
  584. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  585. async_write(
  586. AsyncWriteStream& stream,
  587. message<isRequest, Body, Fields> const& msg,
  588. WriteHandler&& handler =
  589. net::default_completion_token_t<
  590. executor_type<AsyncWriteStream>>{}
  591. #ifndef BOOST_BEAST_DOXYGEN
  592. , typename std::enable_if<
  593. ! is_mutable_body_writer<Body>::value>::type* = 0
  594. #endif
  595. );
  596. //------------------------------------------------------------------------------
  597. /** Serialize an HTTP/1 header to a `std::ostream`.
  598. The function converts the header to its HTTP/1 serialized
  599. representation and stores the result in the output stream.
  600. @param os The output stream to write to.
  601. @param msg The message fields to write.
  602. */
  603. template<bool isRequest, class Fields>
  604. std::ostream&
  605. operator<<(std::ostream& os,
  606. header<isRequest, Fields> const& msg);
  607. /** Serialize an HTTP/1 message to a `std::ostream`.
  608. The function converts the message to its HTTP/1 serialized
  609. representation and stores the result in the output stream.
  610. The implementation will automatically perform chunk encoding if
  611. the contents of the message indicate that chunk encoding is required.
  612. @param os The output stream to write to.
  613. @param msg The message to write.
  614. */
  615. template<bool isRequest, class Body, class Fields>
  616. std::ostream&
  617. operator<<(std::ostream& os,
  618. message<isRequest, Body, Fields> const& msg);
  619. } // http
  620. } // beast
  621. } // boost
  622. #include <boost/beast/http/impl/write.hpp>
  623. #endif