read.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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_READ_HPP
  10. #define BOOST_BEAST_HTTP_READ_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/error.hpp>
  13. #include <boost/beast/core/stream_traits.hpp>
  14. #include <boost/beast/http/basic_parser.hpp>
  15. #include <boost/beast/http/message.hpp>
  16. #include <boost/asio/async_result.hpp>
  17. namespace boost {
  18. namespace beast {
  19. namespace http {
  20. //------------------------------------------------------------------------------
  21. /** Read part of a message from a stream using a parser.
  22. This function is used to read part of a message from a stream into an
  23. instance of @ref basic_parser. The call will block until one of the
  24. following conditions is true:
  25. @li A call to @ref basic_parser::put with a non-empty buffer sequence
  26. is successful.
  27. @li An error occurs.
  28. This operation is implemented in terms of one or more calls to the stream's
  29. `read_some` function. The implementation may read additional bytes from
  30. the stream that lie past the end of the message being read. These additional
  31. bytes are stored in the dynamic buffer, which must be preserved for
  32. subsequent reads.
  33. If the end of file error is received while reading from the stream, then
  34. the error returned from this function will be:
  35. @li @ref error::end_of_stream if no bytes were parsed, or
  36. @li @ref error::partial_message if any bytes were parsed but the
  37. message was incomplete, otherwise:
  38. @li A successful result. The next attempt to read will return
  39. @ref error::end_of_stream
  40. @param stream The stream from which the data is to be read. The type must
  41. meet the <em>SyncReadStream</em> requirements.
  42. @param buffer Storage for additional bytes read by the implementation from
  43. the stream. This is both an input and an output parameter; on entry, the
  44. parser will be presented with any remaining data in the dynamic buffer's
  45. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  46. requirements.
  47. @param parser The parser to use.
  48. @return The number of bytes transferred from the stream.
  49. @throws system_error Thrown on failure.
  50. @note The function returns the total number of bytes transferred from the
  51. stream. This may be zero for the case where there is sufficient pre-existing
  52. message data in the dynamic buffer.
  53. */
  54. template<
  55. class SyncReadStream,
  56. class DynamicBuffer,
  57. bool isRequest>
  58. std::size_t
  59. read_some(
  60. SyncReadStream& stream,
  61. DynamicBuffer& buffer,
  62. basic_parser<isRequest>& parser);
  63. /** Read part of a message from a stream using a parser.
  64. This function is used to read part of a message from a stream into an
  65. instance of @ref basic_parser. The call will block until one of the
  66. following conditions is true:
  67. @li A call to @ref basic_parser::put with a non-empty buffer sequence
  68. is successful.
  69. @li An error occurs.
  70. This operation is implemented in terms of one or more calls to the stream's
  71. `read_some` function. The implementation may read additional bytes from
  72. the stream that lie past the end of the message being read. These additional
  73. bytes are stored in the dynamic buffer, which must be preserved for
  74. subsequent reads.
  75. If the end of file error is received while reading from the stream, then
  76. the error returned from this function will be:
  77. @li @ref error::end_of_stream if no bytes were parsed, or
  78. @li @ref error::partial_message if any bytes were parsed but the
  79. message was incomplete, otherwise:
  80. @li A successful result. The next attempt to read will return
  81. @ref error::end_of_stream
  82. @param stream The stream from which the data is to be read. The type must
  83. support the <em>SyncReadStream</em> requirements.
  84. @param buffer Storage for additional bytes read by the implementation from
  85. the stream. This is both an input and an output parameter; on entry, the
  86. parser will be presented with any remaining data in the dynamic buffer's
  87. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  88. requirements.
  89. @param parser The parser to use.
  90. @param ec Set to the error, if any occurred.
  91. @return The number of bytes transferred from the stream.
  92. @note The function returns the total number of bytes transferred from the
  93. stream. This may be zero for the case where there is sufficient pre-existing
  94. message data in the dynamic buffer.
  95. */
  96. template<
  97. class SyncReadStream,
  98. class DynamicBuffer,
  99. bool isRequest>
  100. std::size_t
  101. read_some(
  102. SyncReadStream& stream,
  103. DynamicBuffer& buffer,
  104. basic_parser<isRequest>& parser,
  105. error_code& ec);
  106. /** Read part of a message asynchronously from a stream using a parser.
  107. This function is used to asynchronously read part of a message from
  108. a stream into an instance of @ref basic_parser. The function call
  109. always returns immediately. The asynchronous operation will continue
  110. until one of the following conditions is true:
  111. @li A call to @ref basic_parser::put with a non-empty buffer sequence
  112. is successful.
  113. @li An error occurs.
  114. This operation is implemented in terms of zero or more calls to the
  115. next layer's `async_read_some` function, and is known as a <em>composed
  116. operation</em>. The program must ensure that the stream performs no other
  117. reads until this operation completes. The implementation may read additional
  118. bytes from the stream that lie past the end of the message being read.
  119. These additional bytes are stored in the dynamic buffer, which must be
  120. preserved for subsequent reads.
  121. If the end of file error is received while reading from the stream, then
  122. the error returned from this function will be:
  123. @li @ref error::end_of_stream if no bytes were parsed, or
  124. @li @ref error::partial_message if any bytes were parsed but the
  125. message was incomplete, otherwise:
  126. @li A successful result. The next attempt to read will return
  127. @ref error::end_of_stream
  128. @param stream The stream from which the data is to be read. The type
  129. must meet the <em>AsyncReadStream</em> requirements.
  130. @param buffer Storage for additional bytes read by the implementation from
  131. the stream. This is both an input and an output parameter; on entry, the
  132. parser will be presented with any remaining data in the dynamic buffer's
  133. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  134. requirements. The object must remain valid at least until the handler
  135. is called; ownership is not transferred.
  136. @param parser The parser to use. The object must remain valid at least until
  137. the handler is called; ownership is not transferred.
  138. @param handler The completion handler to invoke when the operation
  139. completes. The implementation takes ownership of the handler by
  140. performing a decay-copy. The equivalent function signature of
  141. the handler must be:
  142. @code
  143. void handler(
  144. error_code const& error, // result of operation
  145. std::size_t bytes_transferred // the total number of bytes transferred from the stream
  146. );
  147. @endcode
  148. Regardless of whether the asynchronous operation completes
  149. immediately or not, the handler will not be invoked from within
  150. this function. Invocation of the handler will be performed in a
  151. manner equivalent to using `net::post`.
  152. @note The completion handler will receive as a parameter the total number
  153. of bytes transferred from the stream. This may be zero for the case where
  154. there is sufficient pre-existing message data in the dynamic buffer.
  155. @par Per-Operation Cancellation
  156. This asynchronous operation supports cancellation for the following
  157. net::cancellation_type values:
  158. @li @c net::cancellation_type::terminal
  159. if the `stream` also supports terminal cancellation.
  160. `terminal` cancellation leaves the stream in an undefined state,
  161. so that only closing it is guaranteed to succeed.
  162. */
  163. template<
  164. class AsyncReadStream,
  165. class DynamicBuffer,
  166. bool isRequest,
  167. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  168. net::default_completion_token_t<
  169. executor_type<AsyncReadStream>>>
  170. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  171. async_read_some(
  172. AsyncReadStream& stream,
  173. DynamicBuffer& buffer,
  174. basic_parser<isRequest>& parser,
  175. ReadHandler&& handler =
  176. net::default_completion_token_t<
  177. executor_type<AsyncReadStream>>{});
  178. //------------------------------------------------------------------------------
  179. /** Read a complete message header from a stream using a parser.
  180. This function is used to read a complete message header from a stream
  181. into an instance of @ref basic_parser. The call will block until one of the
  182. following conditions is true:
  183. @li @ref basic_parser::is_header_done returns `true`
  184. @li An error occurs.
  185. This operation is implemented in terms of one or more calls to the stream's
  186. `read_some` function. The implementation may read additional bytes from
  187. the stream that lie past the end of the message being read. These additional
  188. bytes are stored in the dynamic buffer, which must be preserved for
  189. subsequent reads.
  190. If the end of file error is received while reading from the stream, then
  191. the error returned from this function will be:
  192. @li @ref error::end_of_stream if no bytes were parsed, or
  193. @li @ref error::partial_message if any bytes were parsed but the
  194. message was incomplete, otherwise:
  195. @li A successful result. The next attempt to read will return
  196. @ref error::end_of_stream
  197. @param stream The stream from which the data is to be read. The type must
  198. meet the <em>SyncReadStream</em> requirements.
  199. @param buffer Storage for additional bytes read by the implementation from
  200. the stream. This is both an input and an output parameter; on entry, the
  201. parser will be presented with any remaining data in the dynamic buffer's
  202. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  203. requirements.
  204. @param parser The parser to use.
  205. @return The number of bytes transferred from the stream.
  206. @throws system_error Thrown on failure.
  207. @note The function returns the total number of bytes transferred from the
  208. stream. This may be zero for the case where there is sufficient pre-existing
  209. message data in the dynamic buffer. The implementation will call
  210. @ref basic_parser::eager with the value `false` on the parser passed in.
  211. */
  212. template<
  213. class SyncReadStream,
  214. class DynamicBuffer,
  215. bool isRequest>
  216. std::size_t
  217. read_header(
  218. SyncReadStream& stream,
  219. DynamicBuffer& buffer,
  220. basic_parser<isRequest>& parser);
  221. /** Read a complete message header from a stream using a parser.
  222. This function is used to read a complete message header from a stream
  223. into an instance of @ref basic_parser. The call will block until one of the
  224. following conditions is true:
  225. @li @ref basic_parser::is_header_done returns `true`
  226. @li An error occurs.
  227. This operation is implemented in terms of one or more calls to the stream's
  228. `read_some` function. The implementation may read additional bytes from
  229. the stream that lie past the end of the message being read. These additional
  230. bytes are stored in the dynamic buffer, which must be preserved for
  231. subsequent reads.
  232. If the end of file error is received while reading from the stream, then
  233. the error returned from this function will be:
  234. @li @ref error::end_of_stream if no bytes were parsed, or
  235. @li @ref error::partial_message if any bytes were parsed but the
  236. message was incomplete, otherwise:
  237. @li A successful result. The next attempt to read will return
  238. @ref error::end_of_stream
  239. @param stream The stream from which the data is to be read. The type must
  240. meet the <em>SyncReadStream</em> requirements.
  241. @param buffer Storage for additional bytes read by the implementation from
  242. the stream. This is both an input and an output parameter; on entry, the
  243. parser will be presented with any remaining data in the dynamic buffer's
  244. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  245. requirements.
  246. @param parser The parser to use.
  247. @param ec Set to the error, if any occurred.
  248. @return The number of bytes transferred from the stream.
  249. @note The function returns the total number of bytes transferred from the
  250. stream. This may be zero for the case where there is sufficient pre-existing
  251. message data in the dynamic buffer. The implementation will call
  252. @ref basic_parser::eager with the value `false` on the parser passed in.
  253. */
  254. template<
  255. class SyncReadStream,
  256. class DynamicBuffer,
  257. bool isRequest>
  258. std::size_t
  259. read_header(
  260. SyncReadStream& stream,
  261. DynamicBuffer& buffer,
  262. basic_parser<isRequest>& parser,
  263. error_code& ec);
  264. /** Read a complete message header asynchronously from a stream using a parser.
  265. This function is used to asynchronously read a complete message header from
  266. a stream into an instance of @ref basic_parser. The function call always
  267. returns immediately. The asynchronous operation will continue until one of
  268. the following conditions is true:
  269. @li @ref basic_parser::is_header_done returns `true`
  270. @li An error occurs.
  271. This operation is implemented in terms of zero or more calls to the
  272. next layer's `async_read_some` function, and is known as a <em>composed
  273. operation</em>. The program must ensure that the stream performs no other
  274. reads until this operation completes. The implementation may read additional
  275. bytes from the stream that lie past the end of the message being read.
  276. These additional bytes are stored in the dynamic buffer, which must be
  277. preserved for subsequent reads.
  278. If the end of file error is received while reading from the stream, then
  279. the error returned from this function will be:
  280. @li @ref error::end_of_stream if no bytes were parsed, or
  281. @li @ref error::partial_message if any bytes were parsed but the
  282. message was incomplete, otherwise:
  283. @li A successful result. The next attempt to read will return
  284. @ref error::end_of_stream
  285. @param stream The stream from which the data is to be read. The type
  286. must meet the <em>AsyncReadStream</em> requirements.
  287. @param buffer Storage for additional bytes read by the implementation from
  288. the stream. This is both an input and an output parameter; on entry, the
  289. parser will be presented with any remaining data in the dynamic buffer's
  290. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  291. requirements. The object must remain valid at least until the handler
  292. is called; ownership is not transferred.
  293. @param parser The parser to use. The object must remain valid at least until
  294. the handler is called; ownership is not transferred.
  295. @param handler The completion handler to invoke when the operation
  296. completes. The implementation takes ownership of the handler by
  297. performing a decay-copy. The equivalent function signature of
  298. the handler must be:
  299. @code
  300. void handler(
  301. error_code const& error, // result of operation
  302. std::size_t bytes_transferred // the total number of bytes transferred from the stream
  303. );
  304. @endcode
  305. Regardless of whether the asynchronous operation completes
  306. immediately or not, the handler will not be invoked from within
  307. this function. Invocation of the handler will be performed in a
  308. manner equivalent to using `net::post`.
  309. @note The completion handler will receive as a parameter the total number
  310. of bytes transferred from the stream. This may be zero for the case where
  311. there is sufficient pre-existing message data in the dynamic buffer. The
  312. implementation will call @ref basic_parser::eager with the value `false`
  313. on the parser passed in.
  314. @par Per-Operation Cancellation
  315. This asynchronous operation supports cancellation for the following
  316. net::cancellation_type values:
  317. @li @c net::cancellation_type::terminal
  318. if the `stream` also supports terminal cancellation.
  319. `terminal` cancellation leaves the stream in an undefined state,
  320. so that only closing it is guaranteed to succeed.
  321. */
  322. template<
  323. class AsyncReadStream,
  324. class DynamicBuffer,
  325. bool isRequest,
  326. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  327. net::default_completion_token_t<
  328. executor_type<AsyncReadStream>>>
  329. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  330. async_read_header(
  331. AsyncReadStream& stream,
  332. DynamicBuffer& buffer,
  333. basic_parser<isRequest>& parser,
  334. ReadHandler&& handler =
  335. net::default_completion_token_t<
  336. executor_type<AsyncReadStream>>{});
  337. //------------------------------------------------------------------------------
  338. /** Read a complete message from a stream using a parser.
  339. This function is used to read a complete message from a stream into an
  340. instance of @ref basic_parser. The call will block until one of the
  341. following conditions is true:
  342. @li @ref basic_parser::is_done returns `true`
  343. @li An error occurs.
  344. This operation is implemented in terms of one or more calls to the stream's
  345. `read_some` function. The implementation may read additional bytes from
  346. the stream that lie past the end of the message being read. These additional
  347. bytes are stored in the dynamic buffer, which must be preserved for
  348. subsequent reads.
  349. If the end of file error is received while reading from the stream, then
  350. the error returned from this function will be:
  351. @li @ref error::end_of_stream if no bytes were parsed, or
  352. @li @ref error::partial_message if any bytes were parsed but the
  353. message was incomplete, otherwise:
  354. @li A successful result. The next attempt to read will return
  355. @ref error::end_of_stream
  356. @param stream The stream from which the data is to be read. The type must
  357. meet the <em>SyncReadStream</em> requirements.
  358. @param buffer Storage for additional bytes read by the implementation from
  359. the stream. This is both an input and an output parameter; on entry, the
  360. parser will be presented with any remaining data in the dynamic buffer's
  361. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  362. requirements.
  363. @param parser The parser to use.
  364. @return The number of bytes transferred from the stream.
  365. @throws system_error Thrown on failure.
  366. @note The function returns the total number of bytes transferred from the
  367. stream. This may be zero for the case where there is sufficient pre-existing
  368. message data in the dynamic buffer. The implementation will call
  369. @ref basic_parser::eager with the value `true` on the parser passed in.
  370. */
  371. template<
  372. class SyncReadStream,
  373. class DynamicBuffer,
  374. bool isRequest>
  375. std::size_t
  376. read(
  377. SyncReadStream& stream,
  378. DynamicBuffer& buffer,
  379. basic_parser<isRequest>& parser);
  380. /** Read a complete message from a stream using a parser.
  381. This function is used to read a complete message from a stream into an
  382. instance of @ref basic_parser. The call will block until one of the
  383. following conditions is true:
  384. @li @ref basic_parser::is_done returns `true`
  385. @li An error occurs.
  386. This operation is implemented in terms of one or more calls to the stream's
  387. `read_some` function. The implementation may read additional bytes from
  388. the stream that lie past the end of the message being read. These additional
  389. bytes are stored in the dynamic buffer, which must be preserved for
  390. subsequent reads.
  391. If the end of file error is received while reading from the stream, then
  392. the error returned from this function will be:
  393. @li @ref error::end_of_stream if no bytes were parsed, or
  394. @li @ref error::partial_message if any bytes were parsed but the
  395. message was incomplete, otherwise:
  396. @li A successful result. The next attempt to read will return
  397. @ref error::end_of_stream
  398. @param stream The stream from which the data is to be read. The type must
  399. meet the <em>SyncReadStream</em> requirements.
  400. @param buffer Storage for additional bytes read by the implementation from
  401. the stream. This is both an input and an output parameter; on entry, the
  402. parser will be presented with any remaining data in the dynamic buffer's
  403. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  404. requirements.
  405. @param parser The parser to use.
  406. @param ec Set to the error, if any occurred.
  407. @return The number of bytes transferred from the stream.
  408. @note The function returns the total number of bytes transferred from the
  409. stream. This may be zero for the case where there is sufficient pre-existing
  410. message data in the dynamic buffer. The implementation will call
  411. @ref basic_parser::eager with the value `true` on the parser passed in.
  412. */
  413. template<
  414. class SyncReadStream,
  415. class DynamicBuffer,
  416. bool isRequest>
  417. std::size_t
  418. read(
  419. SyncReadStream& stream,
  420. DynamicBuffer& buffer,
  421. basic_parser<isRequest>& parser,
  422. error_code& ec);
  423. /** Read a complete message asynchronously from a stream using a parser.
  424. This function is used to asynchronously read a complete message from a
  425. stream into an instance of @ref basic_parser. The function call always
  426. returns immediately. The asynchronous operation will continue until one
  427. of the following conditions is true:
  428. @li @ref basic_parser::is_done returns `true`
  429. @li An error occurs.
  430. This operation is implemented in terms of zero or more calls to the
  431. next layer's `async_read_some` function, and is known as a <em>composed
  432. operation</em>. The program must ensure that the stream performs no other
  433. reads until this operation completes. The implementation may read additional
  434. bytes from the stream that lie past the end of the message being read.
  435. These additional bytes are stored in the dynamic buffer, which must be
  436. preserved for subsequent reads.
  437. If the end of file error is received while reading from the stream, then
  438. the error returned from this function will be:
  439. @li @ref error::end_of_stream if no bytes were parsed, or
  440. @li @ref error::partial_message if any bytes were parsed but the
  441. message was incomplete, otherwise:
  442. @li A successful result. The next attempt to read will return
  443. @ref error::end_of_stream
  444. @param stream The stream from which the data is to be read. The type
  445. must meet the <em>AsyncReadStream</em> requirements.
  446. @param buffer Storage for additional bytes read by the implementation from
  447. the stream. This is both an input and an output parameter; on entry, the
  448. parser will be presented with any remaining data in the dynamic buffer's
  449. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  450. requirements. The object must remain valid at least until the handler
  451. is called; ownership is not transferred.
  452. @param parser The parser to use. The object must remain valid at least until
  453. the handler is called; ownership is not transferred.
  454. @param handler The completion handler to invoke when the operation
  455. completes. The implementation takes ownership of the handler by
  456. performing a decay-copy. The equivalent function signature of
  457. the handler must be:
  458. @code
  459. void handler(
  460. error_code const& error, // result of operation
  461. std::size_t bytes_transferred // the total number of bytes transferred from the stream
  462. );
  463. @endcode
  464. Regardless of whether the asynchronous operation completes
  465. immediately or not, the handler will not be invoked from within
  466. this function. Invocation of the handler will be performed in a
  467. manner equivalent to using `net::post`.
  468. @note The completion handler will receive as a parameter the total number
  469. of bytes transferred from the stream. This may be zero for the case where
  470. there is sufficient pre-existing message data in the dynamic buffer. The
  471. implementation will call @ref basic_parser::eager with the value `true`
  472. on the parser passed in.
  473. @par Per-Operation Cancellation
  474. This asynchronous operation supports cancellation for the following
  475. net::cancellation_type values:
  476. @li @c net::cancellation_type::terminal
  477. if the `stream` also supports terminal cancellation.
  478. `terminal` cancellation leaves the stream in an undefined state,
  479. so that only closing it is guaranteed to succeed.
  480. */
  481. template<
  482. class AsyncReadStream,
  483. class DynamicBuffer,
  484. bool isRequest,
  485. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  486. net::default_completion_token_t<
  487. executor_type<AsyncReadStream>>>
  488. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  489. async_read(
  490. AsyncReadStream& stream,
  491. DynamicBuffer& buffer,
  492. basic_parser<isRequest>& parser,
  493. ReadHandler&& handler =
  494. net::default_completion_token_t<
  495. executor_type<AsyncReadStream>>{});
  496. //------------------------------------------------------------------------------
  497. /** Read a complete message from a stream.
  498. This function is used to read a complete message from a stream into an
  499. instance of @ref message. The call will block until one of the following
  500. conditions is true:
  501. @li The entire message is read in.
  502. @li An error occurs.
  503. This operation is implemented in terms of one or more calls to the stream's
  504. `read_some` function. The implementation may read additional bytes from
  505. the stream that lie past the end of the message being read. These additional
  506. bytes are stored in the dynamic buffer, which must be preserved for
  507. subsequent reads.
  508. If the end of file error is received while reading from the stream, then
  509. the error returned from this function will be:
  510. @li @ref error::end_of_stream if no bytes were parsed, or
  511. @li @ref error::partial_message if any bytes were parsed but the
  512. message was incomplete, otherwise:
  513. @li A successful result. The next attempt to read will return
  514. @ref error::end_of_stream
  515. @param stream The stream from which the data is to be read. The type must
  516. meet the <em>SyncReadStream</em> requirements.
  517. @param buffer Storage for additional bytes read by the implementation from
  518. the stream. This is both an input and an output parameter; on entry, the
  519. parser will be presented with any remaining data in the dynamic buffer's
  520. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  521. requirements.
  522. @param msg The container in which to store the message contents. This
  523. message container should not have previous contents, otherwise the behavior
  524. is undefined. The type must be meet the <em>MoveAssignable</em> and
  525. <em>MoveConstructible</em> requirements.
  526. @return The number of bytes transferred from the stream.
  527. @throws system_error Thrown on failure.
  528. @note The function returns the total number of bytes transferred from the
  529. stream. This may be zero for the case where there is sufficient pre-existing
  530. message data in the dynamic buffer. The implementation will call
  531. @ref basic_parser::eager with the value `true` on the parser passed in.
  532. */
  533. template<
  534. class SyncReadStream,
  535. class DynamicBuffer,
  536. bool isRequest, class Body, class Allocator>
  537. std::size_t
  538. read(
  539. SyncReadStream& stream,
  540. DynamicBuffer& buffer,
  541. message<isRequest, Body, basic_fields<Allocator>>& msg);
  542. /** Read a complete message from a stream.
  543. This function is used to read a complete message from a stream into an
  544. instance of @ref message. The call will block until one of the following
  545. conditions is true:
  546. @li The entire message is read in.
  547. @li An error occurs.
  548. This operation is implemented in terms of one or more calls to the stream's
  549. `read_some` function. The implementation may read additional bytes from
  550. the stream that lie past the end of the message being read. These additional
  551. bytes are stored in the dynamic buffer, which must be preserved for
  552. subsequent reads.
  553. If the end of file error is received while reading from the stream, then
  554. the error returned from this function will be:
  555. @li @ref error::end_of_stream if no bytes were parsed, or
  556. @li @ref error::partial_message if any bytes were parsed but the
  557. message was incomplete, otherwise:
  558. @li A successful result. The next attempt to read will return
  559. @ref error::end_of_stream
  560. @param stream The stream from which the data is to be read. The type must
  561. meet the <em>SyncReadStream</em> requirements.
  562. @param buffer Storage for additional bytes read by the implementation from
  563. the stream. This is both an input and an output parameter; on entry, the
  564. parser will be presented with any remaining data in the dynamic buffer's
  565. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  566. requirements.
  567. @param msg The container in which to store the message contents. This
  568. message container should not have previous contents, otherwise the behavior
  569. is undefined. The type must be meet the <em>MoveAssignable</em> and
  570. <em>MoveConstructible</em> requirements.
  571. @param ec Set to the error, if any occurred.
  572. @return The number of bytes transferred from the stream.
  573. @note The function returns the total number of bytes transferred from the
  574. stream. This may be zero for the case where there is sufficient pre-existing
  575. message data in the dynamic buffer. The implementation will call
  576. @ref basic_parser::eager with the value `true` on the parser passed in.
  577. */
  578. template<
  579. class SyncReadStream,
  580. class DynamicBuffer,
  581. bool isRequest, class Body, class Allocator>
  582. std::size_t
  583. read(
  584. SyncReadStream& stream,
  585. DynamicBuffer& buffer,
  586. message<isRequest, Body, basic_fields<Allocator>>& msg,
  587. error_code& ec);
  588. /** Read a complete message asynchronously from a stream.
  589. This function is used to asynchronously read a complete message from a
  590. stream into an instance of @ref message. The function call always returns
  591. immediately. The asynchronous operation will continue until one of the
  592. following conditions is true:
  593. @li The entire message is read in.
  594. @li An error occurs.
  595. This operation is implemented in terms of zero or more calls to the
  596. next layer's `async_read_some` function, and is known as a <em>composed
  597. operation</em>. The program must ensure that the stream performs no other
  598. reads until this operation completes. The implementation may read additional
  599. bytes from the stream that lie past the end of the message being read.
  600. These additional bytes are stored in the dynamic buffer, which must be
  601. preserved for subsequent reads.
  602. If the end of file error is received while reading from the stream, then
  603. the error returned from this function will be:
  604. @li @ref error::end_of_stream if no bytes were parsed, or
  605. @li @ref error::partial_message if any bytes were parsed but the
  606. message was incomplete, otherwise:
  607. @li A successful result. The next attempt to read will return
  608. @ref error::end_of_stream
  609. @param stream The stream from which the data is to be read. The type
  610. must meet the <em>AsyncReadStream</em> requirements.
  611. @param buffer Storage for additional bytes read by the implementation from
  612. the stream. This is both an input and an output parameter; on entry, the
  613. parser will be presented with any remaining data in the dynamic buffer's
  614. readable bytes sequence first. The type must meet the <em>DynamicBuffer</em>
  615. requirements. The object must remain valid at least until the handler
  616. is called; ownership is not transferred.
  617. @param msg The container in which to store the message contents. This
  618. message container should not have previous contents, otherwise the behavior
  619. is undefined. The type must be meet the <em>MoveAssignable</em> and
  620. <em>MoveConstructible</em> requirements. The object must remain valid
  621. at least until the handler is called; ownership is not transferred.
  622. @param handler The completion handler to invoke when the operation
  623. completes. The implementation takes ownership of the handler by
  624. performing a decay-copy. The equivalent function signature of
  625. the handler must be:
  626. @code
  627. void handler(
  628. error_code const& error, // result of operation
  629. std::size_t bytes_transferred // the total number of bytes transferred from the stream
  630. );
  631. @endcode
  632. Regardless of whether the asynchronous operation completes
  633. immediately or not, the handler will not be invoked from within
  634. this function. Invocation of the handler will be performed in a
  635. manner equivalent to using `net::post`.
  636. @note The completion handler will receive as a parameter the total number
  637. of bytes transferred from the stream. This may be zero for the case where
  638. there is sufficient pre-existing message data in the dynamic buffer. The
  639. implementation will call @ref basic_parser::eager with the value `true`
  640. on the parser passed in.
  641. @par Per-Operation Cancellation
  642. This asynchronous operation supports cancellation for the following
  643. net::cancellation_type values:
  644. @li @c net::cancellation_type::terminal
  645. if the `stream` also supports terminal cancellation.
  646. `terminal` cancellation leaves the stream in an undefined state,
  647. so that only closing it is guaranteed to succeed.
  648. */
  649. template<
  650. class AsyncReadStream,
  651. class DynamicBuffer,
  652. bool isRequest, class Body, class Allocator,
  653. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  654. net::default_completion_token_t<
  655. executor_type<AsyncReadStream>>>
  656. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  657. async_read(
  658. AsyncReadStream& stream,
  659. DynamicBuffer& buffer,
  660. message<isRequest, Body, basic_fields<Allocator>>& msg,
  661. ReadHandler&& handler =
  662. net::default_completion_token_t<
  663. executor_type<AsyncReadStream>>{});
  664. } // http
  665. } // beast
  666. } // boost
  667. #include <boost/beast/http/impl/read.hpp>
  668. #endif