write.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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_IMPL_WRITE_HPP
  10. #define BOOST_BEAST_HTTP_IMPL_WRITE_HPP
  11. #include <boost/beast/http/type_traits.hpp>
  12. #include <boost/beast/core/async_base.hpp>
  13. #include <boost/beast/core/bind_handler.hpp>
  14. #include <boost/beast/core/buffers_range.hpp>
  15. #include <boost/beast/core/make_printable.hpp>
  16. #include <boost/beast/core/stream_traits.hpp>
  17. #include <boost/beast/core/detail/is_invocable.hpp>
  18. #include <boost/asio/coroutine.hpp>
  19. #include <boost/asio/post.hpp>
  20. #include <boost/asio/write.hpp>
  21. #include <boost/optional.hpp>
  22. #include <boost/throw_exception.hpp>
  23. #include <ostream>
  24. #include <sstream>
  25. namespace boost {
  26. namespace beast {
  27. namespace http {
  28. namespace detail {
  29. template<
  30. class Handler,
  31. class Stream,
  32. bool isRequest, class Body, class Fields>
  33. class write_some_op
  34. : public beast::async_base<
  35. Handler, beast::executor_type<Stream>>
  36. {
  37. Stream& s_;
  38. serializer<isRequest,Body, Fields>& sr_;
  39. class lambda
  40. {
  41. write_some_op& op_;
  42. public:
  43. bool invoked = false;
  44. explicit
  45. lambda(write_some_op& op)
  46. : op_(op)
  47. {
  48. }
  49. template<class ConstBufferSequence>
  50. void
  51. operator()(
  52. error_code& ec,
  53. ConstBufferSequence const& buffers)
  54. {
  55. BOOST_ASIO_HANDLER_LOCATION((
  56. __FILE__, __LINE__,
  57. "http::async_write_some"));
  58. invoked = true;
  59. ec = {};
  60. op_.s_.async_write_some(
  61. buffers, std::move(op_));
  62. }
  63. };
  64. public:
  65. template<class Handler_>
  66. write_some_op(
  67. Handler_&& h,
  68. Stream& s,
  69. serializer<isRequest, Body, Fields>& sr)
  70. : async_base<
  71. Handler, beast::executor_type<Stream>>(
  72. std::forward<Handler_>(h), s.get_executor())
  73. , s_(s)
  74. , sr_(sr)
  75. {
  76. (*this)();
  77. }
  78. void
  79. operator()()
  80. {
  81. error_code ec;
  82. if(! sr_.is_done())
  83. {
  84. lambda f{*this};
  85. sr_.next(ec, f);
  86. if(ec)
  87. {
  88. BOOST_ASSERT(! f.invoked);
  89. BOOST_ASIO_HANDLER_LOCATION((
  90. __FILE__, __LINE__,
  91. "http::async_write_some"));
  92. return net::post(
  93. s_.get_executor(),
  94. beast::bind_front_handler(
  95. std::move(*this), ec, 0));
  96. }
  97. if(f.invoked)
  98. {
  99. // *this is now moved-from,
  100. return;
  101. }
  102. // What else could it be?
  103. BOOST_ASSERT(sr_.is_done());
  104. }
  105. BOOST_ASIO_HANDLER_LOCATION((
  106. __FILE__, __LINE__,
  107. "http::async_write_some"));
  108. return net::post(
  109. s_.get_executor(),
  110. beast::bind_front_handler(
  111. std::move(*this), ec, 0));
  112. }
  113. void
  114. operator()(
  115. error_code ec,
  116. std::size_t bytes_transferred)
  117. {
  118. if(! ec)
  119. sr_.consume(bytes_transferred);
  120. this->complete_now(ec, bytes_transferred);
  121. }
  122. };
  123. //------------------------------------------------------------------------------
  124. struct serializer_is_header_done
  125. {
  126. template<
  127. bool isRequest, class Body, class Fields>
  128. bool
  129. operator()(
  130. serializer<isRequest, Body, Fields>& sr) const
  131. {
  132. return sr.is_header_done();
  133. }
  134. };
  135. struct serializer_is_done
  136. {
  137. template<
  138. bool isRequest, class Body, class Fields>
  139. bool
  140. operator()(
  141. serializer<isRequest, Body, Fields>& sr) const
  142. {
  143. return sr.is_done();
  144. }
  145. };
  146. //------------------------------------------------------------------------------
  147. template<
  148. class Handler,
  149. class Stream,
  150. class Predicate,
  151. bool isRequest, class Body, class Fields>
  152. class write_op
  153. : public beast::async_base<
  154. Handler, beast::executor_type<Stream>>
  155. , public asio::coroutine
  156. {
  157. Stream& s_;
  158. serializer<isRequest, Body, Fields>& sr_;
  159. std::size_t bytes_transferred_ = 0;
  160. public:
  161. template<class Handler_>
  162. write_op(
  163. Handler_&& h,
  164. Stream& s,
  165. serializer<isRequest, Body, Fields>& sr)
  166. : async_base<
  167. Handler, beast::executor_type<Stream>>(
  168. std::forward<Handler_>(h), s.get_executor())
  169. , s_(s)
  170. , sr_(sr)
  171. {
  172. (*this)();
  173. }
  174. void
  175. operator()(
  176. error_code ec = {},
  177. std::size_t bytes_transferred = 0)
  178. {
  179. BOOST_ASIO_CORO_REENTER(*this)
  180. {
  181. if(Predicate{}(sr_))
  182. {
  183. BOOST_ASIO_CORO_YIELD
  184. {
  185. BOOST_ASIO_HANDLER_LOCATION((
  186. __FILE__, __LINE__,
  187. "http::async_write"));
  188. net::post(
  189. s_.get_executor(),
  190. std::move(*this));
  191. }
  192. goto upcall;
  193. }
  194. for(;;)
  195. {
  196. BOOST_ASIO_CORO_YIELD
  197. {
  198. BOOST_ASIO_HANDLER_LOCATION((
  199. __FILE__, __LINE__,
  200. "http::async_write"));
  201. beast::http::async_write_some(
  202. s_, sr_, std::move(*this));
  203. }
  204. bytes_transferred_ += bytes_transferred;
  205. if(ec)
  206. goto upcall;
  207. if(Predicate{}(sr_))
  208. break;
  209. }
  210. upcall:
  211. this->complete_now(ec, bytes_transferred_);
  212. }
  213. }
  214. };
  215. //------------------------------------------------------------------------------
  216. template<
  217. class Handler,
  218. class Stream,
  219. bool isRequest, class Body, class Fields>
  220. class write_msg_op
  221. : public beast::stable_async_base<
  222. Handler, beast::executor_type<Stream>>
  223. {
  224. Stream& s_;
  225. serializer<isRequest, Body, Fields>& sr_;
  226. public:
  227. template<
  228. class Handler_,
  229. class... Args>
  230. write_msg_op(
  231. Handler_&& h,
  232. Stream& s,
  233. Args&&... args)
  234. : stable_async_base<
  235. Handler, beast::executor_type<Stream>>(
  236. std::forward<Handler_>(h), s.get_executor())
  237. , s_(s)
  238. , sr_(beast::allocate_stable<
  239. serializer<isRequest, Body, Fields>>(
  240. *this, std::forward<Args>(args)...))
  241. {
  242. (*this)();
  243. }
  244. void
  245. operator()()
  246. {
  247. BOOST_ASIO_HANDLER_LOCATION((
  248. __FILE__, __LINE__,
  249. "http::async_write(msg)"));
  250. async_write(s_, sr_, std::move(*this));
  251. }
  252. void
  253. operator()(
  254. error_code ec, std::size_t bytes_transferred)
  255. {
  256. this->complete_now(ec, bytes_transferred);
  257. }
  258. };
  259. struct run_write_some_op
  260. {
  261. template<
  262. class WriteHandler,
  263. class Stream,
  264. bool isRequest, class Body, class Fields>
  265. void
  266. operator()(
  267. WriteHandler&& h,
  268. Stream* s,
  269. serializer<isRequest, Body, Fields>* sr)
  270. {
  271. // If you get an error on the following line it means
  272. // that your handler does not meet the documented type
  273. // requirements for the handler.
  274. static_assert(
  275. beast::detail::is_invocable<WriteHandler,
  276. void(error_code, std::size_t)>::value,
  277. "WriteHandler type requirements not met");
  278. write_some_op<
  279. typename std::decay<WriteHandler>::type,
  280. Stream,
  281. isRequest, Body, Fields>(
  282. std::forward<WriteHandler>(h), *s, *sr);
  283. }
  284. };
  285. struct run_write_op
  286. {
  287. template<
  288. class WriteHandler,
  289. class Stream,
  290. class Predicate,
  291. bool isRequest, class Body, class Fields>
  292. void
  293. operator()(
  294. WriteHandler&& h,
  295. Stream* s,
  296. Predicate const&,
  297. serializer<isRequest, Body, Fields>* sr)
  298. {
  299. // If you get an error on the following line it means
  300. // that your handler does not meet the documented type
  301. // requirements for the handler.
  302. static_assert(
  303. beast::detail::is_invocable<WriteHandler,
  304. void(error_code, std::size_t)>::value,
  305. "WriteHandler type requirements not met");
  306. write_op<
  307. typename std::decay<WriteHandler>::type,
  308. Stream,
  309. Predicate,
  310. isRequest, Body, Fields>(
  311. std::forward<WriteHandler>(h), *s, *sr);
  312. }
  313. };
  314. struct run_write_msg_op
  315. {
  316. template<
  317. class WriteHandler,
  318. class Stream,
  319. bool isRequest, class Body, class Fields,
  320. class... Args>
  321. void
  322. operator()(
  323. WriteHandler&& h,
  324. Stream* s,
  325. message<isRequest, Body, Fields>* m,
  326. std::false_type,
  327. Args&&... args)
  328. {
  329. // If you get an error on the following line it means
  330. // that your handler does not meet the documented type
  331. // requirements for the handler.
  332. static_assert(
  333. beast::detail::is_invocable<WriteHandler,
  334. void(error_code, std::size_t)>::value,
  335. "WriteHandler type requirements not met");
  336. write_msg_op<
  337. typename std::decay<WriteHandler>::type,
  338. Stream,
  339. isRequest, Body, Fields>(
  340. std::forward<WriteHandler>(h), *s, *m,
  341. std::forward<Args>(args)...);
  342. }
  343. template<
  344. class WriteHandler,
  345. class Stream,
  346. bool isRequest, class Body, class Fields,
  347. class... Args>
  348. void
  349. operator()(
  350. WriteHandler&& h,
  351. Stream* s,
  352. message<isRequest, Body, Fields> const* m,
  353. std::true_type,
  354. Args&&... args)
  355. {
  356. // If you get an error on the following line it means
  357. // that your handler does not meet the documented type
  358. // requirements for the handler.
  359. static_assert(
  360. beast::detail::is_invocable<WriteHandler,
  361. void(error_code, std::size_t)>::value,
  362. "WriteHandler type requirements not met");
  363. write_msg_op<
  364. typename std::decay<WriteHandler>::type,
  365. Stream,
  366. isRequest, Body, Fields>(
  367. std::forward<WriteHandler>(h), *s, *m,
  368. std::forward<Args>(args)...);
  369. }
  370. };
  371. //------------------------------------------------------------------------------
  372. template<class Stream>
  373. class write_some_lambda
  374. {
  375. Stream& stream_;
  376. public:
  377. bool invoked = false;
  378. std::size_t bytes_transferred = 0;
  379. explicit
  380. write_some_lambda(Stream& stream)
  381. : stream_(stream)
  382. {
  383. }
  384. template<class ConstBufferSequence>
  385. void
  386. operator()(error_code& ec,
  387. ConstBufferSequence const& buffers)
  388. {
  389. invoked = true;
  390. bytes_transferred =
  391. stream_.write_some(buffers, ec);
  392. }
  393. };
  394. template<class Stream>
  395. class write_lambda
  396. {
  397. Stream& stream_;
  398. public:
  399. bool invoked = false;
  400. std::size_t bytes_transferred = 0;
  401. explicit
  402. write_lambda(Stream& stream)
  403. : stream_(stream)
  404. {
  405. }
  406. template<class ConstBufferSequence>
  407. void
  408. operator()(error_code& ec,
  409. ConstBufferSequence const& buffers)
  410. {
  411. invoked = true;
  412. bytes_transferred = net::write(
  413. stream_, buffers, ec);
  414. }
  415. };
  416. template<
  417. class SyncWriteStream,
  418. bool isRequest, class Body, class Fields>
  419. std::size_t
  420. write_some_impl(
  421. SyncWriteStream& stream,
  422. serializer<isRequest, Body, Fields>& sr,
  423. error_code& ec)
  424. {
  425. if(! sr.is_done())
  426. {
  427. write_some_lambda<SyncWriteStream> f{stream};
  428. sr.next(ec, f);
  429. if(ec)
  430. return f.bytes_transferred;
  431. if(f.invoked)
  432. sr.consume(f.bytes_transferred);
  433. return f.bytes_transferred;
  434. }
  435. ec = {};
  436. return 0;
  437. }
  438. template<
  439. class AsyncWriteStream,
  440. bool isRequest, class Body, class Fields,
  441. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
  442. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  443. async_write_some_impl(
  444. AsyncWriteStream& stream,
  445. serializer<isRequest, Body, Fields>& sr,
  446. WriteHandler&& handler)
  447. {
  448. return net::async_initiate<
  449. WriteHandler,
  450. void(error_code, std::size_t)>(
  451. run_write_some_op{},
  452. handler,
  453. &stream,
  454. &sr);
  455. }
  456. } // detail
  457. //------------------------------------------------------------------------------
  458. template<
  459. class SyncWriteStream,
  460. bool isRequest, class Body, class Fields>
  461. std::size_t
  462. write_some(
  463. SyncWriteStream& stream,
  464. serializer<isRequest, Body, Fields>& sr)
  465. {
  466. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  467. "SyncWriteStream type requirements not met");
  468. static_assert(is_body<Body>::value,
  469. "Body type requirements not met");
  470. static_assert(is_body_writer<Body>::value,
  471. "BodyWriter type requirements not met");
  472. error_code ec;
  473. auto const bytes_transferred =
  474. write_some(stream, sr, ec);
  475. if(ec)
  476. BOOST_THROW_EXCEPTION(system_error{ec});
  477. return bytes_transferred;
  478. }
  479. template<
  480. class SyncWriteStream,
  481. bool isRequest, class Body, class Fields>
  482. std::size_t
  483. write_some(
  484. SyncWriteStream& stream,
  485. serializer<isRequest, Body, Fields>& sr,
  486. error_code& ec)
  487. {
  488. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  489. "SyncWriteStream type requirements not met");
  490. static_assert(is_body<Body>::value,
  491. "Body type requirements not met");
  492. static_assert(is_body_writer<Body>::value,
  493. "BodyWriter type requirements not met");
  494. return detail::write_some_impl(stream, sr, ec);
  495. }
  496. template<
  497. class AsyncWriteStream,
  498. bool isRequest, class Body, class Fields,
  499. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
  500. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  501. async_write_some(
  502. AsyncWriteStream& stream,
  503. serializer<isRequest, Body, Fields>& sr,
  504. WriteHandler&& handler)
  505. {
  506. static_assert(is_async_write_stream<
  507. AsyncWriteStream>::value,
  508. "AsyncWriteStream type requirements not met");
  509. static_assert(is_body<Body>::value,
  510. "Body type requirements not met");
  511. static_assert(is_body_writer<Body>::value,
  512. "BodyWriter type requirements not met");
  513. return detail::async_write_some_impl(stream, sr,
  514. std::forward<WriteHandler>(handler));
  515. }
  516. //------------------------------------------------------------------------------
  517. template<
  518. class SyncWriteStream,
  519. bool isRequest, class Body, class Fields>
  520. std::size_t
  521. write_header(SyncWriteStream& stream,
  522. serializer<isRequest, Body, Fields>& sr)
  523. {
  524. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  525. "SyncWriteStream type requirements not met");
  526. static_assert(is_body<Body>::value,
  527. "Body type requirements not met");
  528. static_assert(is_body_writer<Body>::value,
  529. "BodyWriter type requirements not met");
  530. error_code ec;
  531. auto const bytes_transferred =
  532. write_header(stream, sr, ec);
  533. if(ec)
  534. BOOST_THROW_EXCEPTION(system_error{ec});
  535. return bytes_transferred;
  536. }
  537. template<
  538. class SyncWriteStream,
  539. bool isRequest, class Body, class Fields>
  540. std::size_t
  541. write_header(
  542. SyncWriteStream& stream,
  543. serializer<isRequest, Body, Fields>& sr,
  544. error_code& ec)
  545. {
  546. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  547. "SyncWriteStream type requirements not met");
  548. static_assert(is_body<Body>::value,
  549. "Body type requirements not met");
  550. static_assert(is_body_writer<Body>::value,
  551. "BodyWriter type requirements not met");
  552. sr.split(true);
  553. std::size_t bytes_transferred = 0;
  554. if(! sr.is_header_done())
  555. {
  556. detail::write_lambda<SyncWriteStream> f{stream};
  557. do
  558. {
  559. sr.next(ec, f);
  560. bytes_transferred += f.bytes_transferred;
  561. if(ec)
  562. return bytes_transferred;
  563. BOOST_ASSERT(f.invoked);
  564. sr.consume(f.bytes_transferred);
  565. }
  566. while(! sr.is_header_done());
  567. }
  568. else
  569. {
  570. ec = {};
  571. }
  572. return bytes_transferred;
  573. }
  574. template<
  575. class AsyncWriteStream,
  576. bool isRequest, class Body, class Fields,
  577. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
  578. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  579. async_write_header(
  580. AsyncWriteStream& stream,
  581. serializer<isRequest, Body, Fields>& sr,
  582. WriteHandler&& handler)
  583. {
  584. static_assert(is_async_write_stream<
  585. AsyncWriteStream>::value,
  586. "AsyncWriteStream type requirements not met");
  587. static_assert(is_body<Body>::value,
  588. "Body type requirements not met");
  589. static_assert(is_body_writer<Body>::value,
  590. "BodyWriter type requirements not met");
  591. sr.split(true);
  592. return net::async_initiate<
  593. WriteHandler,
  594. void(error_code, std::size_t)>(
  595. detail::run_write_op{},
  596. handler,
  597. &stream,
  598. detail::serializer_is_header_done{},
  599. &sr);
  600. }
  601. //------------------------------------------------------------------------------
  602. template<
  603. class SyncWriteStream,
  604. bool isRequest, class Body, class Fields>
  605. std::size_t
  606. write(
  607. SyncWriteStream& stream,
  608. serializer<isRequest, Body, Fields>& sr)
  609. {
  610. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  611. "SyncWriteStream type requirements not met");
  612. error_code ec;
  613. auto const bytes_transferred =
  614. write(stream, sr, ec);
  615. if(ec)
  616. BOOST_THROW_EXCEPTION(system_error{ec});
  617. return bytes_transferred;
  618. }
  619. template<
  620. class SyncWriteStream,
  621. bool isRequest, class Body, class Fields>
  622. std::size_t
  623. write(
  624. SyncWriteStream& stream,
  625. serializer<isRequest, Body, Fields>& sr,
  626. error_code& ec)
  627. {
  628. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  629. "SyncWriteStream type requirements not met");
  630. std::size_t bytes_transferred = 0;
  631. sr.split(false);
  632. for(;;)
  633. {
  634. bytes_transferred +=
  635. write_some(stream, sr, ec);
  636. if(ec)
  637. return bytes_transferred;
  638. if(sr.is_done())
  639. break;
  640. }
  641. return bytes_transferred;
  642. }
  643. template<
  644. class AsyncWriteStream,
  645. bool isRequest, class Body, class Fields,
  646. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
  647. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  648. async_write(
  649. AsyncWriteStream& stream,
  650. serializer<isRequest, Body, Fields>& sr,
  651. WriteHandler&& handler)
  652. {
  653. static_assert(is_async_write_stream<
  654. AsyncWriteStream>::value,
  655. "AsyncWriteStream type requirements not met");
  656. static_assert(is_body<Body>::value,
  657. "Body type requirements not met");
  658. static_assert(is_body_writer<Body>::value,
  659. "BodyWriter type requirements not met");
  660. sr.split(false);
  661. return net::async_initiate<
  662. WriteHandler,
  663. void(error_code, std::size_t)>(
  664. detail::run_write_op{},
  665. handler,
  666. &stream,
  667. detail::serializer_is_done{},
  668. &sr);
  669. }
  670. //------------------------------------------------------------------------------
  671. template<
  672. class SyncWriteStream,
  673. bool isRequest, class Body, class Fields>
  674. typename std::enable_if<
  675. is_mutable_body_writer<Body>::value,
  676. std::size_t>::type
  677. write(
  678. SyncWriteStream& stream,
  679. message<isRequest, Body, Fields>& msg)
  680. {
  681. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  682. "SyncWriteStream type requirements not met");
  683. static_assert(is_body<Body>::value,
  684. "Body type requirements not met");
  685. static_assert(is_body_writer<Body>::value,
  686. "BodyWriter type requirements not met");
  687. error_code ec;
  688. auto const bytes_transferred =
  689. write(stream, msg, ec);
  690. if(ec)
  691. BOOST_THROW_EXCEPTION(system_error{ec});
  692. return bytes_transferred;
  693. }
  694. template<
  695. class SyncWriteStream,
  696. bool isRequest, class Body, class Fields>
  697. typename std::enable_if<
  698. ! is_mutable_body_writer<Body>::value,
  699. std::size_t>::type
  700. write(
  701. SyncWriteStream& stream,
  702. message<isRequest, Body, Fields> const& msg)
  703. {
  704. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  705. "SyncWriteStream type requirements not met");
  706. static_assert(is_body<Body>::value,
  707. "Body type requirements not met");
  708. static_assert(is_body_writer<Body>::value,
  709. "BodyWriter type requirements not met");
  710. error_code ec;
  711. auto const bytes_transferred =
  712. write(stream, msg, ec);
  713. if(ec)
  714. BOOST_THROW_EXCEPTION(system_error{ec});
  715. return bytes_transferred;
  716. }
  717. template<
  718. class SyncWriteStream,
  719. bool isRequest, class Body, class Fields>
  720. typename std::enable_if<
  721. is_mutable_body_writer<Body>::value,
  722. std::size_t>::type
  723. write(
  724. SyncWriteStream& stream,
  725. message<isRequest, Body, Fields>& msg,
  726. error_code& ec)
  727. {
  728. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  729. "SyncWriteStream type requirements not met");
  730. static_assert(is_body<Body>::value,
  731. "Body type requirements not met");
  732. static_assert(is_body_writer<Body>::value,
  733. "BodyWriter type requirements not met");
  734. serializer<isRequest, Body, Fields> sr{msg};
  735. return write(stream, sr, ec);
  736. }
  737. template<
  738. class SyncWriteStream,
  739. bool isRequest, class Body, class Fields>
  740. typename std::enable_if<
  741. ! is_mutable_body_writer<Body>::value,
  742. std::size_t>::type
  743. write(
  744. SyncWriteStream& stream,
  745. message<isRequest, Body, Fields> const& msg,
  746. error_code& ec)
  747. {
  748. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  749. "SyncWriteStream type requirements not met");
  750. static_assert(is_body<Body>::value,
  751. "Body type requirements not met");
  752. static_assert(is_body_writer<Body>::value,
  753. "BodyWriter type requirements not met");
  754. serializer<isRequest, Body, Fields> sr{msg};
  755. return write(stream, sr, ec);
  756. }
  757. template<
  758. class AsyncWriteStream,
  759. bool isRequest, class Body, class Fields,
  760. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
  761. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  762. async_write(
  763. AsyncWriteStream& stream,
  764. message<isRequest, Body, Fields>& msg,
  765. WriteHandler&& handler,
  766. typename std::enable_if<
  767. is_mutable_body_writer<Body>::value>::type*)
  768. {
  769. static_assert(
  770. is_async_write_stream<AsyncWriteStream>::value,
  771. "AsyncWriteStream type requirements not met");
  772. static_assert(is_body<Body>::value,
  773. "Body type requirements not met");
  774. static_assert(is_body_writer<Body>::value,
  775. "BodyWriter type requirements not met");
  776. return net::async_initiate<
  777. WriteHandler,
  778. void(error_code, std::size_t)>(
  779. detail::run_write_msg_op{},
  780. handler,
  781. &stream,
  782. &msg,
  783. std::false_type{});
  784. }
  785. template<
  786. class AsyncWriteStream,
  787. bool isRequest, class Body, class Fields,
  788. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
  789. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  790. async_write(
  791. AsyncWriteStream& stream,
  792. message<isRequest, Body, Fields> const& msg,
  793. WriteHandler&& handler,
  794. typename std::enable_if<
  795. ! is_mutable_body_writer<Body>::value>::type*)
  796. {
  797. static_assert(
  798. is_async_write_stream<AsyncWriteStream>::value,
  799. "AsyncWriteStream type requirements not met");
  800. static_assert(is_body<Body>::value,
  801. "Body type requirements not met");
  802. static_assert(is_body_writer<Body>::value,
  803. "BodyWriter type requirements not met");
  804. return net::async_initiate<
  805. WriteHandler,
  806. void(error_code, std::size_t)>(
  807. detail::run_write_msg_op{},
  808. handler,
  809. &stream,
  810. &msg,
  811. std::true_type{});
  812. }
  813. //------------------------------------------------------------------------------
  814. namespace detail {
  815. template<class Serializer>
  816. class write_ostream_lambda
  817. {
  818. std::ostream& os_;
  819. Serializer& sr_;
  820. public:
  821. write_ostream_lambda(std::ostream& os,
  822. Serializer& sr)
  823. : os_(os)
  824. , sr_(sr)
  825. {
  826. }
  827. template<class ConstBufferSequence>
  828. void
  829. operator()(error_code& ec,
  830. ConstBufferSequence const& buffers) const
  831. {
  832. ec = {};
  833. if(os_.fail())
  834. return;
  835. std::size_t bytes_transferred = 0;
  836. for(auto b : beast::buffers_range_ref(buffers))
  837. {
  838. os_.write(static_cast<char const*>(
  839. b.data()), b.size());
  840. if(os_.fail())
  841. return;
  842. bytes_transferred += b.size();
  843. }
  844. sr_.consume(bytes_transferred);
  845. }
  846. };
  847. } // detail
  848. template<class Fields>
  849. std::ostream&
  850. operator<<(std::ostream& os,
  851. header<true, Fields> const& h)
  852. {
  853. typename Fields::writer fr{
  854. h, h.version(), h.method()};
  855. return os << beast::make_printable(fr.get());
  856. }
  857. template<class Fields>
  858. std::ostream&
  859. operator<<(std::ostream& os,
  860. header<false, Fields> const& h)
  861. {
  862. typename Fields::writer fr{
  863. h, h.version(), h.result_int()};
  864. return os << beast::make_printable(fr.get());
  865. }
  866. template<bool isRequest, class Body, class Fields>
  867. std::ostream&
  868. operator<<(std::ostream& os,
  869. message<isRequest, Body, Fields> const& msg)
  870. {
  871. static_assert(is_body<Body>::value,
  872. "Body type requirements not met");
  873. static_assert(is_body_writer<Body>::value,
  874. "BodyWriter type requirements not met");
  875. serializer<isRequest, Body, Fields> sr{msg};
  876. error_code ec;
  877. detail::write_ostream_lambda<decltype(sr)> f{os, sr};
  878. do
  879. {
  880. sr.next(ec, f);
  881. if(os.fail())
  882. break;
  883. if(ec)
  884. {
  885. os.setstate(std::ios::failbit);
  886. break;
  887. }
  888. }
  889. while(! sr.is_done());
  890. return os;
  891. }
  892. } // http
  893. } // beast
  894. } // boost
  895. #endif