read.hpp 34 KB

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