stream.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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_STREAM_HPP
  10. #define BOOST_BEAST_WEBSOCKET_IMPL_STREAM_HPP
  11. #include <boost/beast/core/buffer_traits.hpp>
  12. #include <boost/beast/websocket/rfc6455.hpp>
  13. #include <boost/beast/websocket/teardown.hpp>
  14. #include <boost/beast/websocket/detail/hybi13.hpp>
  15. #include <boost/beast/websocket/detail/mask.hpp>
  16. #include <boost/beast/websocket/impl/stream_impl.hpp>
  17. #include <boost/beast/version.hpp>
  18. #include <boost/beast/http/read.hpp>
  19. #include <boost/beast/http/write.hpp>
  20. #include <boost/beast/http/rfc7230.hpp>
  21. #include <boost/beast/core/buffers_cat.hpp>
  22. #include <boost/beast/core/buffers_prefix.hpp>
  23. #include <boost/beast/core/buffers_suffix.hpp>
  24. #include <boost/beast/core/flat_static_buffer.hpp>
  25. #include <boost/beast/core/detail/clamp.hpp>
  26. #include <boost/asio/steady_timer.hpp>
  27. #include <boost/assert.hpp>
  28. #include <boost/make_shared.hpp>
  29. #include <boost/throw_exception.hpp>
  30. #include <algorithm>
  31. #include <chrono>
  32. #include <memory>
  33. #include <stdexcept>
  34. #include <utility>
  35. namespace boost {
  36. namespace beast {
  37. namespace websocket {
  38. template<class NextLayer, bool deflateSupported>
  39. stream<NextLayer, deflateSupported>::
  40. ~stream()
  41. {
  42. if(impl_)
  43. impl_->remove();
  44. }
  45. template<class NextLayer, bool deflateSupported>
  46. template<class... Args>
  47. stream<NextLayer, deflateSupported>::
  48. stream(Args&&... args)
  49. : impl_(boost::make_shared<impl_type>(
  50. std::forward<Args>(args)...))
  51. {
  52. BOOST_ASSERT(impl_->rd_buf.max_size() >=
  53. max_control_frame_size);
  54. }
  55. template<class NextLayer, bool deflateSupported>
  56. template<class Other>
  57. stream<NextLayer, deflateSupported>::
  58. stream(stream<Other> && other)
  59. : impl_(boost::make_shared<impl_type>(std::move(other.next_layer())))
  60. {
  61. }
  62. template<class NextLayer, bool deflateSupported>
  63. auto
  64. stream<NextLayer, deflateSupported>::
  65. get_executor() noexcept ->
  66. executor_type
  67. {
  68. return impl_->stream().get_executor();
  69. }
  70. template<class NextLayer, bool deflateSupported>
  71. auto
  72. stream<NextLayer, deflateSupported>::
  73. next_layer() noexcept ->
  74. next_layer_type&
  75. {
  76. return impl_->stream();
  77. }
  78. template<class NextLayer, bool deflateSupported>
  79. auto
  80. stream<NextLayer, deflateSupported>::
  81. next_layer() const noexcept ->
  82. next_layer_type const&
  83. {
  84. return impl_->stream();
  85. }
  86. template<class NextLayer, bool deflateSupported>
  87. bool
  88. stream<NextLayer, deflateSupported>::
  89. is_open() const noexcept
  90. {
  91. return impl_->status_ == status::open;
  92. }
  93. template<class NextLayer, bool deflateSupported>
  94. bool
  95. stream<NextLayer, deflateSupported>::
  96. got_binary() const noexcept
  97. {
  98. return impl_->rd_op == detail::opcode::binary;
  99. }
  100. template<class NextLayer, bool deflateSupported>
  101. bool
  102. stream<NextLayer, deflateSupported>::
  103. is_message_done() const noexcept
  104. {
  105. return impl_->rd_done;
  106. }
  107. template<class NextLayer, bool deflateSupported>
  108. close_reason const&
  109. stream<NextLayer, deflateSupported>::
  110. reason() const noexcept
  111. {
  112. return impl_->cr;
  113. }
  114. template<class NextLayer, bool deflateSupported>
  115. std::size_t
  116. stream<NextLayer, deflateSupported>::
  117. read_size_hint(
  118. std::size_t initial_size) const
  119. {
  120. return impl_->read_size_hint_pmd(
  121. initial_size, impl_->rd_done, impl_->rd_msg_max,
  122. impl_->rd_remain, impl_->rd_fh);
  123. }
  124. template<class NextLayer, bool deflateSupported>
  125. template<class DynamicBuffer, class>
  126. std::size_t
  127. stream<NextLayer, deflateSupported>::
  128. read_size_hint(DynamicBuffer& buffer) const
  129. {
  130. static_assert(
  131. net::is_dynamic_buffer<DynamicBuffer>::value,
  132. "DynamicBuffer type requirements not met");
  133. return impl_->read_size_hint_db(buffer);
  134. }
  135. //------------------------------------------------------------------------------
  136. //
  137. // Settings
  138. //
  139. //------------------------------------------------------------------------------
  140. // decorator
  141. template<class NextLayer, bool deflateSupported>
  142. void
  143. stream<NextLayer, deflateSupported>::
  144. set_option(decorator opt)
  145. {
  146. impl_->decorator_opt = std::move(opt.d_);
  147. }
  148. // timeout
  149. template<class NextLayer, bool deflateSupported>
  150. void
  151. stream<NextLayer, deflateSupported>::
  152. get_option(timeout& opt)
  153. {
  154. opt = impl_->timeout_opt;
  155. }
  156. template <class NextLayer, bool deflateSupported>
  157. void
  158. stream<NextLayer, deflateSupported>::
  159. get_status(permessage_deflate_status &status) const noexcept
  160. {
  161. detail::pmd_offer pmd;
  162. impl_->get_config_pmd(pmd);
  163. status.active = pmd.accept;
  164. status.client_window_bits = pmd.client_max_window_bits;
  165. status.server_window_bits = pmd.server_max_window_bits;
  166. }
  167. template<class NextLayer, bool deflateSupported>
  168. void
  169. stream<NextLayer, deflateSupported>::
  170. set_option(timeout const& opt)
  171. {
  172. impl_->set_option(opt);
  173. }
  174. //
  175. template<class NextLayer, bool deflateSupported>
  176. void
  177. stream<NextLayer, deflateSupported>::
  178. set_option(permessage_deflate const& o)
  179. {
  180. impl_->set_option_pmd(o);
  181. }
  182. template<class NextLayer, bool deflateSupported>
  183. void
  184. stream<NextLayer, deflateSupported>::
  185. get_option(permessage_deflate& o)
  186. {
  187. impl_->get_option_pmd(o);
  188. }
  189. template<class NextLayer, bool deflateSupported>
  190. void
  191. stream<NextLayer, deflateSupported>::
  192. auto_fragment(bool value)
  193. {
  194. impl_->wr_frag_opt = value;
  195. }
  196. template<class NextLayer, bool deflateSupported>
  197. bool
  198. stream<NextLayer, deflateSupported>::
  199. auto_fragment() const
  200. {
  201. return impl_->wr_frag_opt;
  202. }
  203. template<class NextLayer, bool deflateSupported>
  204. void
  205. stream<NextLayer, deflateSupported>::
  206. binary(bool value)
  207. {
  208. impl_->wr_opcode = value ?
  209. detail::opcode::binary :
  210. detail::opcode::text;
  211. }
  212. template<class NextLayer, bool deflateSupported>
  213. bool
  214. stream<NextLayer, deflateSupported>::
  215. binary() const
  216. {
  217. return impl_->wr_opcode == detail::opcode::binary;
  218. }
  219. template<class NextLayer, bool deflateSupported>
  220. void
  221. stream<NextLayer, deflateSupported>::
  222. control_callback(std::function<
  223. void(frame_type, string_view)> cb)
  224. {
  225. impl_->ctrl_cb = std::move(cb);
  226. }
  227. template<class NextLayer, bool deflateSupported>
  228. void
  229. stream<NextLayer, deflateSupported>::
  230. control_callback()
  231. {
  232. impl_->ctrl_cb = {};
  233. }
  234. template<class NextLayer, bool deflateSupported>
  235. void
  236. stream<NextLayer, deflateSupported>::
  237. read_message_max(std::size_t amount)
  238. {
  239. impl_->rd_msg_max = amount;
  240. }
  241. template<class NextLayer, bool deflateSupported>
  242. std::size_t
  243. stream<NextLayer, deflateSupported>::
  244. read_message_max() const
  245. {
  246. return impl_->rd_msg_max;
  247. }
  248. template<class NextLayer, bool deflateSupported>
  249. void
  250. stream<NextLayer, deflateSupported>::
  251. secure_prng(bool value)
  252. {
  253. this->impl_->secure_prng_ = value;
  254. }
  255. template<class NextLayer, bool deflateSupported>
  256. void
  257. stream<NextLayer, deflateSupported>::
  258. write_buffer_bytes(std::size_t amount)
  259. {
  260. if(amount < 8)
  261. BOOST_THROW_EXCEPTION(std::invalid_argument{
  262. "write buffer size underflow"});
  263. impl_->wr_buf_opt = amount;
  264. }
  265. template<class NextLayer, bool deflateSupported>
  266. std::size_t
  267. stream<NextLayer, deflateSupported>::
  268. write_buffer_bytes() const
  269. {
  270. return impl_->wr_buf_opt;
  271. }
  272. template<class NextLayer, bool deflateSupported>
  273. void
  274. stream<NextLayer, deflateSupported>::
  275. text(bool value)
  276. {
  277. impl_->wr_opcode = value ?
  278. detail::opcode::text :
  279. detail::opcode::binary;
  280. }
  281. template<class NextLayer, bool deflateSupported>
  282. bool
  283. stream<NextLayer, deflateSupported>::
  284. text() const
  285. {
  286. return impl_->wr_opcode == detail::opcode::text;
  287. }
  288. template<class NextLayer, bool deflateSupported>
  289. void
  290. stream<NextLayer, deflateSupported>::
  291. compress(bool value)
  292. {
  293. impl_->wr_compress_opt = value;
  294. }
  295. template<class NextLayer, bool deflateSupported>
  296. bool
  297. stream<NextLayer, deflateSupported>::
  298. compress() const
  299. {
  300. return impl_->wr_compress_opt;
  301. }
  302. //------------------------------------------------------------------------------
  303. // _Fail the WebSocket Connection_
  304. template<class NextLayer, bool deflateSupported>
  305. void
  306. stream<NextLayer, deflateSupported>::
  307. do_fail(
  308. std::uint16_t code, // if set, send a close frame first
  309. error_code ev, // error code to use upon success
  310. error_code& ec) // set to the error, else set to ev
  311. {
  312. BOOST_ASSERT(ev);
  313. impl_->change_status(status::closing);
  314. if(code != close_code::none && ! impl_->wr_close)
  315. {
  316. impl_->wr_close = true;
  317. detail::frame_buffer fb;
  318. impl_->template write_close<
  319. flat_static_buffer_base>(fb, code);
  320. net::write(impl_->stream(), fb.data(), ec);
  321. if(impl_->check_stop_now(ec))
  322. return;
  323. }
  324. using beast::websocket::teardown;
  325. teardown(impl_->role, impl_->stream(), ec);
  326. if(ec == net::error::eof)
  327. {
  328. // Rationale:
  329. // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
  330. ec = {};
  331. }
  332. if(! ec)
  333. {
  334. BOOST_BEAST_ASSIGN_EC(ec, ev);
  335. }
  336. if(ec && ec != error::closed)
  337. impl_->change_status(status::failed);
  338. else
  339. impl_->change_status(status::closed);
  340. impl_->close();
  341. }
  342. } // websocket
  343. } // beast
  344. } // boost
  345. #endif