stream.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. set_option(timeout const& opt)
  160. {
  161. impl_->set_option(opt);
  162. }
  163. //
  164. template<class NextLayer, bool deflateSupported>
  165. void
  166. stream<NextLayer, deflateSupported>::
  167. set_option(permessage_deflate const& o)
  168. {
  169. impl_->set_option_pmd(o);
  170. }
  171. template<class NextLayer, bool deflateSupported>
  172. void
  173. stream<NextLayer, deflateSupported>::
  174. get_option(permessage_deflate& o)
  175. {
  176. impl_->get_option_pmd(o);
  177. }
  178. template<class NextLayer, bool deflateSupported>
  179. void
  180. stream<NextLayer, deflateSupported>::
  181. auto_fragment(bool value)
  182. {
  183. impl_->wr_frag_opt = value;
  184. }
  185. template<class NextLayer, bool deflateSupported>
  186. bool
  187. stream<NextLayer, deflateSupported>::
  188. auto_fragment() const
  189. {
  190. return impl_->wr_frag_opt;
  191. }
  192. template<class NextLayer, bool deflateSupported>
  193. void
  194. stream<NextLayer, deflateSupported>::
  195. binary(bool value)
  196. {
  197. impl_->wr_opcode = value ?
  198. detail::opcode::binary :
  199. detail::opcode::text;
  200. }
  201. template<class NextLayer, bool deflateSupported>
  202. bool
  203. stream<NextLayer, deflateSupported>::
  204. binary() const
  205. {
  206. return impl_->wr_opcode == detail::opcode::binary;
  207. }
  208. template<class NextLayer, bool deflateSupported>
  209. void
  210. stream<NextLayer, deflateSupported>::
  211. control_callback(std::function<
  212. void(frame_type, string_view)> cb)
  213. {
  214. impl_->ctrl_cb = std::move(cb);
  215. }
  216. template<class NextLayer, bool deflateSupported>
  217. void
  218. stream<NextLayer, deflateSupported>::
  219. control_callback()
  220. {
  221. impl_->ctrl_cb = {};
  222. }
  223. template<class NextLayer, bool deflateSupported>
  224. void
  225. stream<NextLayer, deflateSupported>::
  226. read_message_max(std::size_t amount)
  227. {
  228. impl_->rd_msg_max = amount;
  229. }
  230. template<class NextLayer, bool deflateSupported>
  231. std::size_t
  232. stream<NextLayer, deflateSupported>::
  233. read_message_max() const
  234. {
  235. return impl_->rd_msg_max;
  236. }
  237. template<class NextLayer, bool deflateSupported>
  238. void
  239. stream<NextLayer, deflateSupported>::
  240. secure_prng(bool value)
  241. {
  242. this->impl_->secure_prng_ = value;
  243. }
  244. template<class NextLayer, bool deflateSupported>
  245. void
  246. stream<NextLayer, deflateSupported>::
  247. write_buffer_bytes(std::size_t amount)
  248. {
  249. if(amount < 8)
  250. BOOST_THROW_EXCEPTION(std::invalid_argument{
  251. "write buffer size underflow"});
  252. impl_->wr_buf_opt = amount;
  253. }
  254. template<class NextLayer, bool deflateSupported>
  255. std::size_t
  256. stream<NextLayer, deflateSupported>::
  257. write_buffer_bytes() const
  258. {
  259. return impl_->wr_buf_opt;
  260. }
  261. template<class NextLayer, bool deflateSupported>
  262. void
  263. stream<NextLayer, deflateSupported>::
  264. text(bool value)
  265. {
  266. impl_->wr_opcode = value ?
  267. detail::opcode::text :
  268. detail::opcode::binary;
  269. }
  270. template<class NextLayer, bool deflateSupported>
  271. bool
  272. stream<NextLayer, deflateSupported>::
  273. text() const
  274. {
  275. return impl_->wr_opcode == detail::opcode::text;
  276. }
  277. template<class NextLayer, bool deflateSupported>
  278. void
  279. stream<NextLayer, deflateSupported>::
  280. compress(bool value)
  281. {
  282. impl_->wr_compress_opt = value;
  283. }
  284. template<class NextLayer, bool deflateSupported>
  285. bool
  286. stream<NextLayer, deflateSupported>::
  287. compress() const
  288. {
  289. return impl_->wr_compress_opt;
  290. }
  291. //------------------------------------------------------------------------------
  292. // _Fail the WebSocket Connection_
  293. template<class NextLayer, bool deflateSupported>
  294. void
  295. stream<NextLayer, deflateSupported>::
  296. do_fail(
  297. std::uint16_t code, // if set, send a close frame first
  298. error_code ev, // error code to use upon success
  299. error_code& ec) // set to the error, else set to ev
  300. {
  301. BOOST_ASSERT(ev);
  302. impl_->change_status(status::closing);
  303. if(code != close_code::none && ! impl_->wr_close)
  304. {
  305. impl_->wr_close = true;
  306. detail::frame_buffer fb;
  307. impl_->template write_close<
  308. flat_static_buffer_base>(fb, code);
  309. net::write(impl_->stream(), fb.data(), ec);
  310. if(impl_->check_stop_now(ec))
  311. return;
  312. }
  313. using beast::websocket::teardown;
  314. teardown(impl_->role, impl_->stream(), ec);
  315. if(ec == net::error::eof)
  316. {
  317. // Rationale:
  318. // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
  319. ec = {};
  320. }
  321. if(! ec)
  322. {
  323. BOOST_BEAST_ASSIGN_EC(ec, ev);
  324. }
  325. if(ec && ec != error::closed)
  326. impl_->change_status(status::failed);
  327. else
  328. impl_->change_status(status::closed);
  329. impl_->close();
  330. }
  331. } // websocket
  332. } // beast
  333. } // boost
  334. #endif