stream.hpp 8.1 KB

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