stream.hpp 8.4 KB

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