read.hpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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_WEBSOCKET_IMPL_READ_HPP
  10. #define BOOST_BEAST_WEBSOCKET_IMPL_READ_HPP
  11. #include <boost/beast/core/buffer_traits.hpp>
  12. #include <boost/beast/websocket/teardown.hpp>
  13. #include <boost/beast/websocket/detail/mask.hpp>
  14. #include <boost/beast/websocket/impl/stream_impl.hpp>
  15. #include <boost/beast/core/async_base.hpp>
  16. #include <boost/beast/core/bind_handler.hpp>
  17. #include <boost/beast/core/buffers_prefix.hpp>
  18. #include <boost/beast/core/buffers_suffix.hpp>
  19. #include <boost/beast/core/flat_static_buffer.hpp>
  20. #include <boost/beast/core/read_size.hpp>
  21. #include <boost/beast/core/stream_traits.hpp>
  22. #include <boost/beast/core/detail/bind_continuation.hpp>
  23. #include <boost/beast/core/detail/buffer.hpp>
  24. #include <boost/beast/core/detail/clamp.hpp>
  25. #include <boost/beast/core/detail/config.hpp>
  26. #include <boost/asio/coroutine.hpp>
  27. #include <boost/asio/post.hpp>
  28. #include <boost/assert.hpp>
  29. #include <boost/config.hpp>
  30. #include <boost/optional.hpp>
  31. #include <boost/throw_exception.hpp>
  32. #include <algorithm>
  33. #include <limits>
  34. #include <memory>
  35. namespace boost {
  36. namespace beast {
  37. namespace websocket {
  38. /* Read some message data into a buffer sequence.
  39. Also reads and handles control frames.
  40. */
  41. template<class NextLayer, bool deflateSupported>
  42. template<class Handler, class MutableBufferSequence>
  43. class stream<NextLayer, deflateSupported>::read_some_op
  44. : public beast::async_base<
  45. Handler, beast::executor_type<stream>>
  46. , public asio::coroutine
  47. {
  48. boost::weak_ptr<impl_type> wp_;
  49. MutableBufferSequence bs_;
  50. buffers_suffix<MutableBufferSequence> cb_;
  51. std::size_t bytes_written_ = 0;
  52. error_code result_;
  53. close_code code_;
  54. bool did_read_ = false;
  55. public:
  56. static constexpr int id = 1; // for soft_mutex
  57. template<class Handler_>
  58. read_some_op(
  59. Handler_&& h,
  60. boost::shared_ptr<impl_type> const& sp,
  61. MutableBufferSequence const& bs)
  62. : async_base<
  63. Handler, beast::executor_type<stream>>(
  64. std::forward<Handler_>(h),
  65. sp->stream().get_executor())
  66. , wp_(sp)
  67. , bs_(bs)
  68. , cb_(bs)
  69. , code_(close_code::none)
  70. {
  71. (*this)({}, 0, false);
  72. }
  73. void operator()(
  74. error_code ec = {},
  75. std::size_t bytes_transferred = 0,
  76. bool cont = true)
  77. {
  78. using beast::detail::clamp;
  79. auto sp = wp_.lock();
  80. if(! sp)
  81. {
  82. ec = net::error::operation_aborted;
  83. bytes_written_ = 0;
  84. return this->complete(cont, ec, bytes_written_);
  85. }
  86. auto& impl = *sp;
  87. BOOST_ASIO_CORO_REENTER(*this)
  88. {
  89. impl.update_timer(this->get_executor());
  90. acquire_read_lock:
  91. // Acquire the read lock
  92. if(! impl.rd_block.try_lock(this))
  93. {
  94. do_suspend:
  95. BOOST_ASIO_CORO_YIELD
  96. {
  97. BOOST_ASIO_HANDLER_LOCATION((
  98. __FILE__, __LINE__,
  99. "websocket::async_read_some"));
  100. impl.op_r_rd.emplace(std::move(*this));
  101. }
  102. impl.rd_block.lock(this);
  103. BOOST_ASIO_CORO_YIELD
  104. {
  105. BOOST_ASIO_HANDLER_LOCATION((
  106. __FILE__, __LINE__,
  107. "websocket::async_read_some"));
  108. net::post(std::move(*this));
  109. }
  110. BOOST_ASSERT(impl.rd_block.is_locked(this));
  111. BOOST_ASSERT(!ec);
  112. if(impl.check_stop_now(ec))
  113. {
  114. BOOST_ASSERT(ec == net::error::operation_aborted);
  115. goto upcall;
  116. }
  117. // VFALCO Should never get here
  118. // The only way to get read blocked is if
  119. // a `close_op` wrote a close frame
  120. BOOST_ASSERT(impl.wr_close);
  121. BOOST_ASSERT(impl.status_ != status::open);
  122. ec = net::error::operation_aborted;
  123. goto upcall;
  124. }
  125. else
  126. {
  127. // Make sure the stream is not closed
  128. if( impl.status_ == status::closed ||
  129. impl.status_ == status::failed)
  130. {
  131. ec = net::error::operation_aborted;
  132. goto upcall;
  133. }
  134. }
  135. // if status_ == status::closing, we want to suspend
  136. // the read operation until the close completes,
  137. // then finish the read with operation_aborted.
  138. loop:
  139. BOOST_ASSERT(impl.rd_block.is_locked(this));
  140. // See if we need to read a frame header. This
  141. // condition is structured to give the decompressor
  142. // a chance to emit the final empty deflate block
  143. //
  144. if(impl.rd_remain == 0 &&
  145. (! impl.rd_fh.fin || impl.rd_done))
  146. {
  147. // Read frame header
  148. while(! impl.parse_fh(
  149. impl.rd_fh, impl.rd_buf, result_))
  150. {
  151. if(result_)
  152. {
  153. // _Fail the WebSocket Connection_
  154. if(result_ == error::message_too_big)
  155. code_ = close_code::too_big;
  156. else
  157. code_ = close_code::protocol_error;
  158. goto close;
  159. }
  160. BOOST_ASSERT(impl.rd_block.is_locked(this));
  161. BOOST_ASIO_CORO_YIELD
  162. {
  163. BOOST_ASIO_HANDLER_LOCATION((
  164. __FILE__, __LINE__,
  165. "websocket::async_read_some"));
  166. impl.stream().async_read_some(
  167. impl.rd_buf.prepare(read_size(
  168. impl.rd_buf, impl.rd_buf.max_size())),
  169. std::move(*this));
  170. }
  171. BOOST_ASSERT(impl.rd_block.is_locked(this));
  172. impl.rd_buf.commit(bytes_transferred);
  173. if(impl.check_stop_now(ec))
  174. goto upcall;
  175. impl.reset_idle();
  176. // Allow a close operation
  177. // to acquire the read block
  178. impl.rd_block.unlock(this);
  179. if( impl.op_r_close.maybe_invoke())
  180. {
  181. // Suspend
  182. BOOST_ASSERT(impl.rd_block.is_locked());
  183. goto do_suspend;
  184. }
  185. // Acquire read block
  186. impl.rd_block.lock(this);
  187. }
  188. // Immediately apply the mask to the portion
  189. // of the buffer holding payload data.
  190. if(impl.rd_fh.len > 0 && impl.rd_fh.mask)
  191. detail::mask_inplace(buffers_prefix(
  192. clamp(impl.rd_fh.len),
  193. impl.rd_buf.data()),
  194. impl.rd_key);
  195. if(detail::is_control(impl.rd_fh.op))
  196. {
  197. // Clear this otherwise the next
  198. // frame will be considered final.
  199. impl.rd_fh.fin = false;
  200. // Handle ping frame
  201. if(impl.rd_fh.op == detail::opcode::ping)
  202. {
  203. if(impl.ctrl_cb)
  204. {
  205. if(! cont)
  206. {
  207. BOOST_ASIO_CORO_YIELD
  208. {
  209. BOOST_ASIO_HANDLER_LOCATION((
  210. __FILE__, __LINE__,
  211. "websocket::async_read_some"));
  212. net::post(std::move(*this));
  213. }
  214. BOOST_ASSERT(cont);
  215. // VFALCO call check_stop_now() here?
  216. }
  217. }
  218. {
  219. auto const b = buffers_prefix(
  220. clamp(impl.rd_fh.len),
  221. impl.rd_buf.data());
  222. auto const len = buffer_bytes(b);
  223. BOOST_ASSERT(len == impl.rd_fh.len);
  224. ping_data payload;
  225. detail::read_ping(payload, b);
  226. impl.rd_buf.consume(len);
  227. // Ignore ping when closing
  228. if(impl.status_ == status::closing)
  229. goto loop;
  230. if(impl.ctrl_cb)
  231. impl.ctrl_cb(
  232. frame_type::ping, payload);
  233. impl.rd_fb.clear();
  234. impl.template write_ping<
  235. flat_static_buffer_base>(impl.rd_fb,
  236. detail::opcode::pong, payload);
  237. }
  238. // Allow a close operation
  239. // to acquire the read block
  240. impl.rd_block.unlock(this);
  241. impl.op_r_close.maybe_invoke();
  242. // Acquire the write lock
  243. if(! impl.wr_block.try_lock(this))
  244. {
  245. BOOST_ASIO_CORO_YIELD
  246. {
  247. BOOST_ASIO_HANDLER_LOCATION((
  248. __FILE__, __LINE__,
  249. "websocket::async_read_some"));
  250. impl.op_rd.emplace(std::move(*this));
  251. }
  252. impl.wr_block.lock(this);
  253. BOOST_ASIO_CORO_YIELD
  254. {
  255. BOOST_ASIO_HANDLER_LOCATION((
  256. __FILE__, __LINE__,
  257. "websocket::async_read_some"));
  258. net::post(std::move(*this));
  259. }
  260. BOOST_ASSERT(impl.wr_block.is_locked(this));
  261. if(impl.check_stop_now(ec))
  262. goto upcall;
  263. }
  264. // Send pong
  265. BOOST_ASSERT(impl.wr_block.is_locked(this));
  266. BOOST_ASIO_CORO_YIELD
  267. {
  268. BOOST_ASIO_HANDLER_LOCATION((
  269. __FILE__, __LINE__,
  270. "websocket::async_read_some"));
  271. net::async_write(
  272. impl.stream(), net::const_buffer(impl.rd_fb.data()),
  273. beast::detail::bind_continuation(std::move(*this)));
  274. }
  275. BOOST_ASSERT(impl.wr_block.is_locked(this));
  276. if(impl.check_stop_now(ec))
  277. goto upcall;
  278. impl.wr_block.unlock(this);
  279. impl.op_close.maybe_invoke()
  280. || impl.op_idle_ping.maybe_invoke()
  281. || impl.op_ping.maybe_invoke()
  282. || impl.op_wr.maybe_invoke();
  283. goto acquire_read_lock;
  284. }
  285. // Handle pong frame
  286. if(impl.rd_fh.op == detail::opcode::pong)
  287. {
  288. // Ignore pong when closing
  289. if(! impl.wr_close && impl.ctrl_cb)
  290. {
  291. if(! cont)
  292. {
  293. BOOST_ASIO_CORO_YIELD
  294. {
  295. BOOST_ASIO_HANDLER_LOCATION((
  296. __FILE__, __LINE__,
  297. "websocket::async_read_some"));
  298. net::post(std::move(*this));
  299. }
  300. BOOST_ASSERT(cont);
  301. }
  302. }
  303. auto const cb = buffers_prefix(clamp(
  304. impl.rd_fh.len), impl.rd_buf.data());
  305. auto const len = buffer_bytes(cb);
  306. BOOST_ASSERT(len == impl.rd_fh.len);
  307. ping_data payload;
  308. detail::read_ping(payload, cb);
  309. impl.rd_buf.consume(len);
  310. // Ignore pong when closing
  311. if(! impl.wr_close && impl.ctrl_cb)
  312. impl.ctrl_cb(frame_type::pong, payload);
  313. goto loop;
  314. }
  315. // Handle close frame
  316. BOOST_ASSERT(impl.rd_fh.op == detail::opcode::close);
  317. {
  318. if(impl.ctrl_cb)
  319. {
  320. if(! cont)
  321. {
  322. BOOST_ASIO_CORO_YIELD
  323. {
  324. BOOST_ASIO_HANDLER_LOCATION((
  325. __FILE__, __LINE__,
  326. "websocket::async_read_some"));
  327. net::post(std::move(*this));
  328. }
  329. BOOST_ASSERT(cont);
  330. }
  331. }
  332. auto const cb = buffers_prefix(clamp(
  333. impl.rd_fh.len), impl.rd_buf.data());
  334. auto const len = buffer_bytes(cb);
  335. BOOST_ASSERT(len == impl.rd_fh.len);
  336. BOOST_ASSERT(! impl.rd_close);
  337. impl.rd_close = true;
  338. close_reason cr;
  339. detail::read_close(cr, cb, result_);
  340. if(result_)
  341. {
  342. // _Fail the WebSocket Connection_
  343. code_ = close_code::protocol_error;
  344. goto close;
  345. }
  346. impl.cr = cr;
  347. impl.rd_buf.consume(len);
  348. if(impl.ctrl_cb)
  349. impl.ctrl_cb(frame_type::close,
  350. impl.cr.reason);
  351. // See if we are already closing
  352. if(impl.status_ == status::closing)
  353. {
  354. // _Close the WebSocket Connection_
  355. BOOST_ASSERT(impl.wr_close);
  356. code_ = close_code::none;
  357. result_ = error::closed;
  358. goto close;
  359. }
  360. // _Start the WebSocket Closing Handshake_
  361. code_ = cr.code == close_code::none ?
  362. close_code::normal :
  363. static_cast<close_code>(cr.code);
  364. result_ = error::closed;
  365. goto close;
  366. }
  367. }
  368. if(impl.rd_fh.len == 0 && ! impl.rd_fh.fin)
  369. {
  370. // Empty non-final frame
  371. goto loop;
  372. }
  373. impl.rd_done = false;
  374. }
  375. if(! impl.rd_deflated())
  376. {
  377. if(impl.rd_remain > 0)
  378. {
  379. if(impl.rd_buf.size() == 0 && impl.rd_buf.max_size() >
  380. (std::min)(clamp(impl.rd_remain),
  381. buffer_bytes(cb_)))
  382. {
  383. // Fill the read buffer first, otherwise we
  384. // get fewer bytes at the cost of one I/O.
  385. BOOST_ASIO_CORO_YIELD
  386. {
  387. BOOST_ASIO_HANDLER_LOCATION((
  388. __FILE__, __LINE__,
  389. "websocket::async_read_some"));
  390. impl.stream().async_read_some(
  391. impl.rd_buf.prepare(read_size(
  392. impl.rd_buf, impl.rd_buf.max_size())),
  393. std::move(*this));
  394. }
  395. impl.rd_buf.commit(bytes_transferred);
  396. if(impl.check_stop_now(ec))
  397. goto upcall;
  398. impl.reset_idle();
  399. if(impl.rd_fh.mask)
  400. detail::mask_inplace(buffers_prefix(clamp(
  401. impl.rd_remain), impl.rd_buf.data()),
  402. impl.rd_key);
  403. }
  404. if(impl.rd_buf.size() > 0)
  405. {
  406. // Copy from the read buffer.
  407. // The mask was already applied.
  408. bytes_transferred = net::buffer_copy(cb_,
  409. impl.rd_buf.data(), clamp(impl.rd_remain));
  410. auto const mb = buffers_prefix(
  411. bytes_transferred, cb_);
  412. impl.rd_remain -= bytes_transferred;
  413. if(impl.rd_op == detail::opcode::text)
  414. {
  415. if(! impl.rd_utf8.write(mb) ||
  416. (impl.rd_remain == 0 && impl.rd_fh.fin &&
  417. ! impl.rd_utf8.finish()))
  418. {
  419. // _Fail the WebSocket Connection_
  420. code_ = close_code::bad_payload;
  421. result_ = error::bad_frame_payload;
  422. goto close;
  423. }
  424. }
  425. bytes_written_ += bytes_transferred;
  426. impl.rd_size += bytes_transferred;
  427. impl.rd_buf.consume(bytes_transferred);
  428. }
  429. else
  430. {
  431. // Read into caller's buffer
  432. BOOST_ASSERT(impl.rd_remain > 0);
  433. BOOST_ASSERT(buffer_bytes(cb_) > 0);
  434. BOOST_ASSERT(buffer_bytes(buffers_prefix(
  435. clamp(impl.rd_remain), cb_)) > 0);
  436. BOOST_ASIO_CORO_YIELD
  437. {
  438. BOOST_ASIO_HANDLER_LOCATION((
  439. __FILE__, __LINE__,
  440. "websocket::async_read_some"));
  441. impl.stream().async_read_some(buffers_prefix(
  442. clamp(impl.rd_remain), cb_), std::move(*this));
  443. }
  444. if(impl.check_stop_now(ec))
  445. goto upcall;
  446. impl.reset_idle();
  447. BOOST_ASSERT(bytes_transferred > 0);
  448. auto const mb = buffers_prefix(
  449. bytes_transferred, cb_);
  450. impl.rd_remain -= bytes_transferred;
  451. if(impl.rd_fh.mask)
  452. detail::mask_inplace(mb, impl.rd_key);
  453. if(impl.rd_op == detail::opcode::text)
  454. {
  455. if(! impl.rd_utf8.write(mb) ||
  456. (impl.rd_remain == 0 && impl.rd_fh.fin &&
  457. ! impl.rd_utf8.finish()))
  458. {
  459. // _Fail the WebSocket Connection_
  460. code_ = close_code::bad_payload;
  461. result_ = error::bad_frame_payload;
  462. goto close;
  463. }
  464. }
  465. bytes_written_ += bytes_transferred;
  466. impl.rd_size += bytes_transferred;
  467. }
  468. }
  469. BOOST_ASSERT( ! impl.rd_done );
  470. if( impl.rd_remain == 0 && impl.rd_fh.fin )
  471. impl.rd_done = true;
  472. }
  473. else
  474. {
  475. // Read compressed message frame payload:
  476. // inflate even if rd_fh_.len == 0, otherwise we
  477. // never emit the end-of-stream deflate block.
  478. while(buffer_bytes(cb_) > 0)
  479. {
  480. if( impl.rd_remain > 0 &&
  481. impl.rd_buf.size() == 0 &&
  482. ! did_read_)
  483. {
  484. // read new
  485. BOOST_ASIO_CORO_YIELD
  486. {
  487. BOOST_ASIO_HANDLER_LOCATION((
  488. __FILE__, __LINE__,
  489. "websocket::async_read_some"));
  490. impl.stream().async_read_some(
  491. impl.rd_buf.prepare(read_size(
  492. impl.rd_buf, impl.rd_buf.max_size())),
  493. std::move(*this));
  494. }
  495. if(impl.check_stop_now(ec))
  496. goto upcall;
  497. impl.reset_idle();
  498. BOOST_ASSERT(bytes_transferred > 0);
  499. impl.rd_buf.commit(bytes_transferred);
  500. if(impl.rd_fh.mask)
  501. detail::mask_inplace(
  502. buffers_prefix(clamp(impl.rd_remain),
  503. impl.rd_buf.data()), impl.rd_key);
  504. did_read_ = true;
  505. }
  506. zlib::z_params zs;
  507. {
  508. auto const out = buffers_front(cb_);
  509. zs.next_out = out.data();
  510. zs.avail_out = out.size();
  511. BOOST_ASSERT(zs.avail_out > 0);
  512. }
  513. // boolean to track the end of the message.
  514. bool fin = false;
  515. if(impl.rd_remain > 0)
  516. {
  517. if(impl.rd_buf.size() > 0)
  518. {
  519. // use what's there
  520. auto const in = buffers_prefix(
  521. clamp(impl.rd_remain), buffers_front(
  522. impl.rd_buf.data()));
  523. zs.avail_in = in.size();
  524. zs.next_in = in.data();
  525. }
  526. else
  527. {
  528. break;
  529. }
  530. }
  531. else if(impl.rd_fh.fin)
  532. {
  533. // append the empty block codes
  534. static std::uint8_t constexpr
  535. empty_block[4] = { 0x00, 0x00, 0xff, 0xff };
  536. zs.next_in = empty_block;
  537. zs.avail_in = sizeof(empty_block);
  538. fin = true;
  539. }
  540. else
  541. {
  542. break;
  543. }
  544. impl.inflate(zs, zlib::Flush::sync, ec);
  545. if(impl.check_stop_now(ec))
  546. goto upcall;
  547. if(fin && zs.total_out == 0) {
  548. impl.do_context_takeover_read(impl.role);
  549. impl.rd_done = true;
  550. break;
  551. }
  552. if(impl.rd_msg_max && beast::detail::sum_exceeds(
  553. impl.rd_size, zs.total_out, impl.rd_msg_max))
  554. {
  555. // _Fail the WebSocket Connection_
  556. code_ = close_code::too_big;
  557. result_ = error::message_too_big;
  558. goto close;
  559. }
  560. cb_.consume(zs.total_out);
  561. impl.rd_size += zs.total_out;
  562. if (! fin) {
  563. impl.rd_remain -= zs.total_in;
  564. impl.rd_buf.consume(zs.total_in);
  565. }
  566. bytes_written_ += zs.total_out;
  567. }
  568. if(impl.rd_op == detail::opcode::text)
  569. {
  570. // check utf8
  571. if(! impl.rd_utf8.write(
  572. buffers_prefix(bytes_written_, bs_)) || (
  573. impl.rd_done && ! impl.rd_utf8.finish()))
  574. {
  575. // _Fail the WebSocket Connection_
  576. code_ = close_code::bad_payload;
  577. result_ = error::bad_frame_payload;
  578. goto close;
  579. }
  580. }
  581. }
  582. goto upcall;
  583. close:
  584. // Acquire the write lock
  585. if(! impl.wr_block.try_lock(this))
  586. {
  587. BOOST_ASIO_CORO_YIELD
  588. {
  589. BOOST_ASIO_HANDLER_LOCATION((
  590. __FILE__, __LINE__,
  591. "websocket::async_read_some"));
  592. impl.op_rd.emplace(std::move(*this));
  593. }
  594. impl.wr_block.lock(this);
  595. BOOST_ASIO_CORO_YIELD
  596. {
  597. BOOST_ASIO_HANDLER_LOCATION((
  598. __FILE__, __LINE__,
  599. "websocket::async_read_some"));
  600. net::post(std::move(*this));
  601. }
  602. BOOST_ASSERT(impl.wr_block.is_locked(this));
  603. if(impl.check_stop_now(ec))
  604. goto upcall;
  605. }
  606. impl.change_status(status::closing);
  607. if(! impl.wr_close)
  608. {
  609. impl.wr_close = true;
  610. // Serialize close frame
  611. impl.rd_fb.clear();
  612. impl.template write_close<
  613. flat_static_buffer_base>(
  614. impl.rd_fb, code_);
  615. // Send close frame
  616. BOOST_ASSERT(impl.wr_block.is_locked(this));
  617. BOOST_ASIO_CORO_YIELD
  618. {
  619. BOOST_ASIO_HANDLER_LOCATION((
  620. __FILE__, __LINE__,
  621. "websocket::async_read_some"));
  622. net::async_write(impl.stream(), net::const_buffer(impl.rd_fb.data()),
  623. beast::detail::bind_continuation(std::move(*this)));
  624. }
  625. BOOST_ASSERT(impl.wr_block.is_locked(this));
  626. if(impl.check_stop_now(ec))
  627. goto upcall;
  628. }
  629. // Teardown
  630. using beast::websocket::async_teardown;
  631. BOOST_ASSERT(impl.wr_block.is_locked(this));
  632. BOOST_ASIO_CORO_YIELD
  633. {
  634. BOOST_ASIO_HANDLER_LOCATION((
  635. __FILE__, __LINE__,
  636. "websocket::async_read_some"));
  637. async_teardown(impl.role, impl.stream(),
  638. beast::detail::bind_continuation(std::move(*this)));
  639. }
  640. BOOST_ASSERT(impl.wr_block.is_locked(this));
  641. if(ec == net::error::eof)
  642. {
  643. // Rationale:
  644. // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
  645. ec = {};
  646. }
  647. if(! ec)
  648. ec = result_;
  649. if(ec && ec != error::closed)
  650. impl.change_status(status::failed);
  651. else
  652. impl.change_status(status::closed);
  653. impl.close();
  654. upcall:
  655. impl.rd_block.try_unlock(this);
  656. impl.op_r_close.maybe_invoke();
  657. if(impl.wr_block.try_unlock(this))
  658. impl.op_close.maybe_invoke()
  659. || impl.op_idle_ping.maybe_invoke()
  660. || impl.op_ping.maybe_invoke()
  661. || impl.op_wr.maybe_invoke();
  662. this->complete(cont, ec, bytes_written_);
  663. }
  664. }
  665. };
  666. //------------------------------------------------------------------------------
  667. template<class NextLayer, bool deflateSupported>
  668. template<class Handler, class DynamicBuffer>
  669. class stream<NextLayer, deflateSupported>::read_op
  670. : public beast::async_base<
  671. Handler, beast::executor_type<stream>>
  672. , public asio::coroutine
  673. {
  674. boost::weak_ptr<impl_type> wp_;
  675. DynamicBuffer& b_;
  676. std::size_t limit_;
  677. std::size_t bytes_written_ = 0;
  678. bool some_;
  679. public:
  680. template<class Handler_>
  681. read_op(
  682. Handler_&& h,
  683. boost::shared_ptr<impl_type> const& sp,
  684. DynamicBuffer& b,
  685. std::size_t limit,
  686. bool some)
  687. : async_base<Handler,
  688. beast::executor_type<stream>>(
  689. std::forward<Handler_>(h),
  690. sp->stream().get_executor())
  691. , wp_(sp)
  692. , b_(b)
  693. , limit_(limit ? limit : (
  694. std::numeric_limits<std::size_t>::max)())
  695. , some_(some)
  696. {
  697. (*this)({}, 0, false);
  698. }
  699. void operator()(
  700. error_code ec = {},
  701. std::size_t bytes_transferred = 0,
  702. bool cont = true)
  703. {
  704. using beast::detail::clamp;
  705. auto sp = wp_.lock();
  706. if(! sp)
  707. {
  708. ec = net::error::operation_aborted;
  709. bytes_written_ = 0;
  710. return this->complete(cont, ec, bytes_written_);
  711. }
  712. auto& impl = *sp;
  713. using mutable_buffers_type = typename
  714. DynamicBuffer::mutable_buffers_type;
  715. BOOST_ASIO_CORO_REENTER(*this)
  716. {
  717. do
  718. {
  719. // VFALCO TODO use boost::beast::bind_continuation
  720. BOOST_ASIO_CORO_YIELD
  721. {
  722. auto mb = beast::detail::dynamic_buffer_prepare(b_,
  723. clamp(impl.read_size_hint_db(b_), limit_),
  724. ec, error::buffer_overflow);
  725. if(impl.check_stop_now(ec))
  726. goto upcall;
  727. BOOST_ASIO_HANDLER_LOCATION((
  728. __FILE__, __LINE__,
  729. "websocket::async_read"));
  730. read_some_op<read_op, mutable_buffers_type>(
  731. std::move(*this), sp, *mb);
  732. }
  733. b_.commit(bytes_transferred);
  734. bytes_written_ += bytes_transferred;
  735. if(ec)
  736. goto upcall;
  737. }
  738. while(! some_ && ! impl.rd_done);
  739. upcall:
  740. this->complete(cont, ec, bytes_written_);
  741. }
  742. }
  743. };
  744. template<class NextLayer, bool deflateSupported>
  745. struct stream<NextLayer, deflateSupported>::
  746. run_read_some_op
  747. {
  748. template<
  749. class ReadHandler,
  750. class MutableBufferSequence>
  751. void
  752. operator()(
  753. ReadHandler&& h,
  754. boost::shared_ptr<impl_type> const& sp,
  755. MutableBufferSequence const& b)
  756. {
  757. // If you get an error on the following line it means
  758. // that your handler does not meet the documented type
  759. // requirements for the handler.
  760. static_assert(
  761. beast::detail::is_invocable<ReadHandler,
  762. void(error_code, std::size_t)>::value,
  763. "ReadHandler type requirements not met");
  764. read_some_op<
  765. typename std::decay<ReadHandler>::type,
  766. MutableBufferSequence>(
  767. std::forward<ReadHandler>(h),
  768. sp,
  769. b);
  770. }
  771. };
  772. template<class NextLayer, bool deflateSupported>
  773. struct stream<NextLayer, deflateSupported>::
  774. run_read_op
  775. {
  776. template<
  777. class ReadHandler,
  778. class DynamicBuffer>
  779. void
  780. operator()(
  781. ReadHandler&& h,
  782. boost::shared_ptr<impl_type> const& sp,
  783. DynamicBuffer* b,
  784. std::size_t limit,
  785. bool some)
  786. {
  787. // If you get an error on the following line it means
  788. // that your handler does not meet the documented type
  789. // requirements for the handler.
  790. static_assert(
  791. beast::detail::is_invocable<ReadHandler,
  792. void(error_code, std::size_t)>::value,
  793. "ReadHandler type requirements not met");
  794. read_op<
  795. typename std::decay<ReadHandler>::type,
  796. DynamicBuffer>(
  797. std::forward<ReadHandler>(h),
  798. sp,
  799. *b,
  800. limit,
  801. some);
  802. }
  803. };
  804. //------------------------------------------------------------------------------
  805. template<class NextLayer, bool deflateSupported>
  806. template<class DynamicBuffer>
  807. std::size_t
  808. stream<NextLayer, deflateSupported>::
  809. read(DynamicBuffer& buffer)
  810. {
  811. static_assert(is_sync_stream<next_layer_type>::value,
  812. "SyncStream type requirements not met");
  813. static_assert(
  814. net::is_dynamic_buffer<DynamicBuffer>::value,
  815. "DynamicBuffer type requirements not met");
  816. error_code ec;
  817. auto const bytes_written = read(buffer, ec);
  818. if(ec)
  819. BOOST_THROW_EXCEPTION(system_error{ec});
  820. return bytes_written;
  821. }
  822. template<class NextLayer, bool deflateSupported>
  823. template<class DynamicBuffer>
  824. std::size_t
  825. stream<NextLayer, deflateSupported>::
  826. read(DynamicBuffer& buffer, error_code& ec)
  827. {
  828. static_assert(is_sync_stream<next_layer_type>::value,
  829. "SyncStream type requirements not met");
  830. static_assert(
  831. net::is_dynamic_buffer<DynamicBuffer>::value,
  832. "DynamicBuffer type requirements not met");
  833. std::size_t bytes_written = 0;
  834. do
  835. {
  836. bytes_written += read_some(buffer, 0, ec);
  837. if(ec)
  838. return bytes_written;
  839. }
  840. while(! is_message_done());
  841. return bytes_written;
  842. }
  843. template<class NextLayer, bool deflateSupported>
  844. template<class DynamicBuffer, BOOST_BEAST_ASYNC_TPARAM2 ReadHandler>
  845. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  846. stream<NextLayer, deflateSupported>::
  847. async_read(DynamicBuffer& buffer, ReadHandler&& handler)
  848. {
  849. static_assert(is_async_stream<next_layer_type>::value,
  850. "AsyncStream type requirements not met");
  851. static_assert(
  852. net::is_dynamic_buffer<DynamicBuffer>::value,
  853. "DynamicBuffer type requirements not met");
  854. return net::async_initiate<
  855. ReadHandler,
  856. void(error_code, std::size_t)>(
  857. run_read_op{},
  858. handler,
  859. impl_,
  860. &buffer,
  861. 0,
  862. false);
  863. }
  864. //------------------------------------------------------------------------------
  865. template<class NextLayer, bool deflateSupported>
  866. template<class DynamicBuffer>
  867. std::size_t
  868. stream<NextLayer, deflateSupported>::
  869. read_some(
  870. DynamicBuffer& buffer,
  871. std::size_t limit)
  872. {
  873. static_assert(is_sync_stream<next_layer_type>::value,
  874. "SyncStream type requirements not met");
  875. static_assert(
  876. net::is_dynamic_buffer<DynamicBuffer>::value,
  877. "DynamicBuffer type requirements not met");
  878. error_code ec;
  879. auto const bytes_written =
  880. read_some(buffer, limit, ec);
  881. if(ec)
  882. BOOST_THROW_EXCEPTION(system_error{ec});
  883. return bytes_written;
  884. }
  885. template<class NextLayer, bool deflateSupported>
  886. template<class DynamicBuffer>
  887. std::size_t
  888. stream<NextLayer, deflateSupported>::
  889. read_some(
  890. DynamicBuffer& buffer,
  891. std::size_t limit,
  892. error_code& ec)
  893. {
  894. static_assert(is_sync_stream<next_layer_type>::value,
  895. "SyncStream type requirements not met");
  896. static_assert(
  897. net::is_dynamic_buffer<DynamicBuffer>::value,
  898. "DynamicBuffer type requirements not met");
  899. using beast::detail::clamp;
  900. if(! limit)
  901. limit = (std::numeric_limits<std::size_t>::max)();
  902. auto const size =
  903. clamp(read_size_hint(buffer), limit);
  904. BOOST_ASSERT(size > 0);
  905. auto mb = beast::detail::dynamic_buffer_prepare(
  906. buffer, size, ec, error::buffer_overflow);
  907. if(impl_->check_stop_now(ec))
  908. return 0;
  909. auto const bytes_written = read_some(*mb, ec);
  910. buffer.commit(bytes_written);
  911. return bytes_written;
  912. }
  913. template<class NextLayer, bool deflateSupported>
  914. template<class DynamicBuffer, BOOST_BEAST_ASYNC_TPARAM2 ReadHandler>
  915. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  916. stream<NextLayer, deflateSupported>::
  917. async_read_some(
  918. DynamicBuffer& buffer,
  919. std::size_t limit,
  920. ReadHandler&& handler)
  921. {
  922. static_assert(is_async_stream<next_layer_type>::value,
  923. "AsyncStream type requirements not met");
  924. static_assert(
  925. net::is_dynamic_buffer<DynamicBuffer>::value,
  926. "DynamicBuffer type requirements not met");
  927. return net::async_initiate<
  928. ReadHandler,
  929. void(error_code, std::size_t)>(
  930. run_read_op{},
  931. handler,
  932. impl_,
  933. &buffer,
  934. limit,
  935. true);
  936. }
  937. //------------------------------------------------------------------------------
  938. template<class NextLayer, bool deflateSupported>
  939. template<class MutableBufferSequence>
  940. std::size_t
  941. stream<NextLayer, deflateSupported>::
  942. read_some(
  943. MutableBufferSequence const& buffers)
  944. {
  945. static_assert(is_sync_stream<next_layer_type>::value,
  946. "SyncStream type requirements not met");
  947. static_assert(net::is_mutable_buffer_sequence<
  948. MutableBufferSequence>::value,
  949. "MutableBufferSequence type requirements not met");
  950. error_code ec;
  951. auto const bytes_written = read_some(buffers, ec);
  952. if(ec)
  953. BOOST_THROW_EXCEPTION(system_error{ec});
  954. return bytes_written;
  955. }
  956. template<class NextLayer, bool deflateSupported>
  957. template<class MutableBufferSequence>
  958. std::size_t
  959. stream<NextLayer, deflateSupported>::
  960. read_some(
  961. MutableBufferSequence const& buffers,
  962. error_code& ec)
  963. {
  964. static_assert(is_sync_stream<next_layer_type>::value,
  965. "SyncStream type requirements not met");
  966. static_assert(net::is_mutable_buffer_sequence<
  967. MutableBufferSequence>::value,
  968. "MutableBufferSequence type requirements not met");
  969. using beast::detail::clamp;
  970. auto& impl = *impl_;
  971. close_code code{};
  972. std::size_t bytes_written = 0;
  973. ec = {};
  974. // Make sure the stream is open
  975. if(impl.check_stop_now(ec))
  976. return bytes_written;
  977. loop:
  978. // See if we need to read a frame header. This
  979. // condition is structured to give the decompressor
  980. // a chance to emit the final empty deflate block
  981. //
  982. if(impl.rd_remain == 0 && (
  983. ! impl.rd_fh.fin || impl.rd_done))
  984. {
  985. // Read frame header
  986. error_code result;
  987. while(! impl.parse_fh(impl.rd_fh, impl.rd_buf, result))
  988. {
  989. if(result)
  990. {
  991. // _Fail the WebSocket Connection_
  992. if(result == error::message_too_big)
  993. code = close_code::too_big;
  994. else
  995. code = close_code::protocol_error;
  996. do_fail(code, result, ec);
  997. return bytes_written;
  998. }
  999. auto const bytes_transferred =
  1000. impl.stream().read_some(
  1001. impl.rd_buf.prepare(read_size(
  1002. impl.rd_buf, impl.rd_buf.max_size())),
  1003. ec);
  1004. impl.rd_buf.commit(bytes_transferred);
  1005. if(impl.check_stop_now(ec))
  1006. return bytes_written;
  1007. }
  1008. // Immediately apply the mask to the portion
  1009. // of the buffer holding payload data.
  1010. if(impl.rd_fh.len > 0 && impl.rd_fh.mask)
  1011. detail::mask_inplace(buffers_prefix(
  1012. clamp(impl.rd_fh.len), impl.rd_buf.data()),
  1013. impl.rd_key);
  1014. if(detail::is_control(impl.rd_fh.op))
  1015. {
  1016. // Get control frame payload
  1017. auto const b = buffers_prefix(
  1018. clamp(impl.rd_fh.len), impl.rd_buf.data());
  1019. auto const len = buffer_bytes(b);
  1020. BOOST_ASSERT(len == impl.rd_fh.len);
  1021. // Clear this otherwise the next
  1022. // frame will be considered final.
  1023. impl.rd_fh.fin = false;
  1024. // Handle ping frame
  1025. if(impl.rd_fh.op == detail::opcode::ping)
  1026. {
  1027. ping_data payload;
  1028. detail::read_ping(payload, b);
  1029. impl.rd_buf.consume(len);
  1030. if(impl.wr_close)
  1031. {
  1032. // Ignore ping when closing
  1033. goto loop;
  1034. }
  1035. if(impl.ctrl_cb)
  1036. impl.ctrl_cb(frame_type::ping, payload);
  1037. detail::frame_buffer fb;
  1038. impl.template write_ping<flat_static_buffer_base>(fb,
  1039. detail::opcode::pong, payload);
  1040. net::write(impl.stream(), fb.data(), ec);
  1041. if(impl.check_stop_now(ec))
  1042. return bytes_written;
  1043. goto loop;
  1044. }
  1045. // Handle pong frame
  1046. if(impl.rd_fh.op == detail::opcode::pong)
  1047. {
  1048. ping_data payload;
  1049. detail::read_ping(payload, b);
  1050. impl.rd_buf.consume(len);
  1051. if(impl.ctrl_cb)
  1052. impl.ctrl_cb(frame_type::pong, payload);
  1053. goto loop;
  1054. }
  1055. // Handle close frame
  1056. BOOST_ASSERT(impl.rd_fh.op == detail::opcode::close);
  1057. {
  1058. BOOST_ASSERT(! impl.rd_close);
  1059. impl.rd_close = true;
  1060. close_reason cr;
  1061. detail::read_close(cr, b, result);
  1062. if(result)
  1063. {
  1064. // _Fail the WebSocket Connection_
  1065. do_fail(close_code::protocol_error,
  1066. result, ec);
  1067. return bytes_written;
  1068. }
  1069. impl.cr = cr;
  1070. impl.rd_buf.consume(len);
  1071. if(impl.ctrl_cb)
  1072. impl.ctrl_cb(frame_type::close, impl.cr.reason);
  1073. BOOST_ASSERT(! impl.wr_close);
  1074. // _Start the WebSocket Closing Handshake_
  1075. do_fail(
  1076. cr.code == close_code::none ?
  1077. close_code::normal :
  1078. static_cast<close_code>(cr.code),
  1079. error::closed, ec);
  1080. return bytes_written;
  1081. }
  1082. }
  1083. if(impl.rd_fh.len == 0 && ! impl.rd_fh.fin)
  1084. {
  1085. // Empty non-final frame
  1086. goto loop;
  1087. }
  1088. impl.rd_done = false;
  1089. }
  1090. else
  1091. {
  1092. ec = {};
  1093. }
  1094. if(! impl.rd_deflated())
  1095. {
  1096. if(impl.rd_remain > 0)
  1097. {
  1098. if(impl.rd_buf.size() == 0 && impl.rd_buf.max_size() >
  1099. (std::min)(clamp(impl.rd_remain),
  1100. buffer_bytes(buffers)))
  1101. {
  1102. // Fill the read buffer first, otherwise we
  1103. // get fewer bytes at the cost of one I/O.
  1104. impl.rd_buf.commit(impl.stream().read_some(
  1105. impl.rd_buf.prepare(read_size(impl.rd_buf,
  1106. impl.rd_buf.max_size())), ec));
  1107. if(impl.check_stop_now(ec))
  1108. return bytes_written;
  1109. if(impl.rd_fh.mask)
  1110. detail::mask_inplace(
  1111. buffers_prefix(clamp(impl.rd_remain),
  1112. impl.rd_buf.data()), impl.rd_key);
  1113. }
  1114. if(impl.rd_buf.size() > 0)
  1115. {
  1116. // Copy from the read buffer.
  1117. // The mask was already applied.
  1118. auto const bytes_transferred = net::buffer_copy(
  1119. buffers, impl.rd_buf.data(),
  1120. clamp(impl.rd_remain));
  1121. auto const mb = buffers_prefix(
  1122. bytes_transferred, buffers);
  1123. impl.rd_remain -= bytes_transferred;
  1124. if(impl.rd_op == detail::opcode::text)
  1125. {
  1126. if(! impl.rd_utf8.write(mb) ||
  1127. (impl.rd_remain == 0 && impl.rd_fh.fin &&
  1128. ! impl.rd_utf8.finish()))
  1129. {
  1130. // _Fail the WebSocket Connection_
  1131. do_fail(close_code::bad_payload,
  1132. error::bad_frame_payload, ec);
  1133. return bytes_written;
  1134. }
  1135. }
  1136. bytes_written += bytes_transferred;
  1137. impl.rd_size += bytes_transferred;
  1138. impl.rd_buf.consume(bytes_transferred);
  1139. }
  1140. else
  1141. {
  1142. // Read into caller's buffer
  1143. BOOST_ASSERT(impl.rd_remain > 0);
  1144. BOOST_ASSERT(buffer_bytes(buffers) > 0);
  1145. BOOST_ASSERT(buffer_bytes(buffers_prefix(
  1146. clamp(impl.rd_remain), buffers)) > 0);
  1147. auto const bytes_transferred =
  1148. impl.stream().read_some(buffers_prefix(
  1149. clamp(impl.rd_remain), buffers), ec);
  1150. // VFALCO What if some bytes were written?
  1151. if(impl.check_stop_now(ec))
  1152. return bytes_written;
  1153. BOOST_ASSERT(bytes_transferred > 0);
  1154. auto const mb = buffers_prefix(
  1155. bytes_transferred, buffers);
  1156. impl.rd_remain -= bytes_transferred;
  1157. if(impl.rd_fh.mask)
  1158. detail::mask_inplace(mb, impl.rd_key);
  1159. if(impl.rd_op == detail::opcode::text)
  1160. {
  1161. if(! impl.rd_utf8.write(mb) ||
  1162. (impl.rd_remain == 0 && impl.rd_fh.fin &&
  1163. ! impl.rd_utf8.finish()))
  1164. {
  1165. // _Fail the WebSocket Connection_
  1166. do_fail(close_code::bad_payload,
  1167. error::bad_frame_payload, ec);
  1168. return bytes_written;
  1169. }
  1170. }
  1171. bytes_written += bytes_transferred;
  1172. impl.rd_size += bytes_transferred;
  1173. }
  1174. }
  1175. BOOST_ASSERT( ! impl.rd_done );
  1176. if( impl.rd_remain == 0 && impl.rd_fh.fin )
  1177. impl.rd_done = true;
  1178. }
  1179. else
  1180. {
  1181. // Read compressed message frame payload:
  1182. // inflate even if rd_fh_.len == 0, otherwise we
  1183. // never emit the end-of-stream deflate block.
  1184. //
  1185. bool did_read = false;
  1186. buffers_suffix<MutableBufferSequence> cb(buffers);
  1187. while(buffer_bytes(cb) > 0)
  1188. {
  1189. zlib::z_params zs;
  1190. {
  1191. auto const out = beast::buffers_front(cb);
  1192. zs.next_out = out.data();
  1193. zs.avail_out = out.size();
  1194. BOOST_ASSERT(zs.avail_out > 0);
  1195. }
  1196. // boolean to track the end of the message.
  1197. bool fin = false;
  1198. if(impl.rd_remain > 0)
  1199. {
  1200. if(impl.rd_buf.size() > 0)
  1201. {
  1202. // use what's there
  1203. auto const in = buffers_prefix(
  1204. clamp(impl.rd_remain), beast::buffers_front(
  1205. impl.rd_buf.data()));
  1206. zs.avail_in = in.size();
  1207. zs.next_in = in.data();
  1208. }
  1209. else if(! did_read)
  1210. {
  1211. // read new
  1212. auto const bytes_transferred =
  1213. impl.stream().read_some(
  1214. impl.rd_buf.prepare(read_size(
  1215. impl.rd_buf, impl.rd_buf.max_size())),
  1216. ec);
  1217. if(impl.check_stop_now(ec))
  1218. return bytes_written;
  1219. BOOST_ASSERT(bytes_transferred > 0);
  1220. impl.rd_buf.commit(bytes_transferred);
  1221. if(impl.rd_fh.mask)
  1222. detail::mask_inplace(
  1223. buffers_prefix(clamp(impl.rd_remain),
  1224. impl.rd_buf.data()), impl.rd_key);
  1225. auto const in = buffers_prefix(
  1226. clamp(impl.rd_remain), buffers_front(
  1227. impl.rd_buf.data()));
  1228. zs.avail_in = in.size();
  1229. zs.next_in = in.data();
  1230. did_read = true;
  1231. }
  1232. else
  1233. {
  1234. break;
  1235. }
  1236. }
  1237. else if(impl.rd_fh.fin)
  1238. {
  1239. // append the empty block codes
  1240. static std::uint8_t constexpr
  1241. empty_block[4] = { 0x00, 0x00, 0xff, 0xff };
  1242. zs.next_in = empty_block;
  1243. zs.avail_in = sizeof(empty_block);
  1244. fin = true;
  1245. }
  1246. else
  1247. {
  1248. break;
  1249. }
  1250. impl.inflate(zs, zlib::Flush::sync, ec);
  1251. if(impl.check_stop_now(ec))
  1252. return bytes_written;
  1253. if (fin && zs.total_out == 0) {
  1254. impl.do_context_takeover_read(impl.role);
  1255. impl.rd_done = true;
  1256. break;
  1257. }
  1258. if(impl.rd_msg_max && beast::detail::sum_exceeds(
  1259. impl.rd_size, zs.total_out, impl.rd_msg_max))
  1260. {
  1261. do_fail(close_code::too_big,
  1262. error::message_too_big, ec);
  1263. return bytes_written;
  1264. }
  1265. cb.consume(zs.total_out);
  1266. impl.rd_size += zs.total_out;
  1267. if (! fin) {
  1268. impl.rd_remain -= zs.total_in;
  1269. impl.rd_buf.consume(zs.total_in);
  1270. }
  1271. bytes_written += zs.total_out;
  1272. }
  1273. if(impl.rd_op == detail::opcode::text)
  1274. {
  1275. // check utf8
  1276. if(! impl.rd_utf8.write(beast::buffers_prefix(
  1277. bytes_written, buffers)) || (
  1278. impl.rd_done && ! impl.rd_utf8.finish()))
  1279. {
  1280. // _Fail the WebSocket Connection_
  1281. do_fail(close_code::bad_payload,
  1282. error::bad_frame_payload, ec);
  1283. return bytes_written;
  1284. }
  1285. }
  1286. }
  1287. return bytes_written;
  1288. }
  1289. template<class NextLayer, bool deflateSupported>
  1290. template<class MutableBufferSequence, BOOST_BEAST_ASYNC_TPARAM2 ReadHandler>
  1291. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  1292. stream<NextLayer, deflateSupported>::
  1293. async_read_some(
  1294. MutableBufferSequence const& buffers,
  1295. ReadHandler&& handler)
  1296. {
  1297. static_assert(is_async_stream<next_layer_type>::value,
  1298. "AsyncStream type requirements not met");
  1299. static_assert(net::is_mutable_buffer_sequence<
  1300. MutableBufferSequence>::value,
  1301. "MutableBufferSequence type requirements not met");
  1302. return net::async_initiate<
  1303. ReadHandler,
  1304. void(error_code, std::size_t)>(
  1305. run_read_some_op{},
  1306. handler,
  1307. impl_,
  1308. buffers);
  1309. }
  1310. } // websocket
  1311. } // beast
  1312. } // boost
  1313. #endif