read.hpp 32 KB

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