read.hpp 28 KB

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