read.hpp 32 KB

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