handshake.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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_HANDSHAKE_HPP
  10. #define BOOST_BEAST_WEBSOCKET_IMPL_HANDSHAKE_HPP
  11. #include <boost/beast/websocket/impl/stream_impl.hpp>
  12. #include <boost/beast/websocket/detail/type_traits.hpp>
  13. #include <boost/beast/http/empty_body.hpp>
  14. #include <boost/beast/http/message.hpp>
  15. #include <boost/beast/http/read.hpp>
  16. #include <boost/beast/http/write.hpp>
  17. #include <boost/beast/core/async_base.hpp>
  18. #include <boost/beast/core/flat_buffer.hpp>
  19. #include <boost/beast/core/stream_traits.hpp>
  20. #include <boost/asio/coroutine.hpp>
  21. #include <boost/assert.hpp>
  22. #include <boost/throw_exception.hpp>
  23. #include <memory>
  24. namespace boost {
  25. namespace beast {
  26. namespace websocket {
  27. //------------------------------------------------------------------------------
  28. // send the upgrade request and process the response
  29. //
  30. template<class NextLayer, bool deflateSupported>
  31. template<class Handler>
  32. class stream<NextLayer, deflateSupported>::handshake_op
  33. : public beast::stable_async_base<Handler,
  34. beast::executor_type<stream>>
  35. , public asio::coroutine
  36. {
  37. struct data
  38. {
  39. // VFALCO This really should be two separate
  40. // composed operations, to save on memory
  41. request_type req;
  42. http::response_parser<
  43. typename response_type::body_type> p;
  44. flat_buffer fb;
  45. bool overflow = false; // could be a member of the op
  46. explicit
  47. data(request_type&& req_)
  48. : req(std::move(req_))
  49. {
  50. }
  51. };
  52. boost::weak_ptr<impl_type> wp_;
  53. detail::sec_ws_key_type key_;
  54. response_type* res_p_;
  55. data& d_;
  56. public:
  57. template<class Handler_>
  58. handshake_op(
  59. Handler_&& h,
  60. boost::shared_ptr<impl_type> const& sp,
  61. request_type&& req,
  62. detail::sec_ws_key_type key,
  63. response_type* res_p)
  64. : stable_async_base<Handler,
  65. beast::executor_type<stream>>(
  66. std::forward<Handler_>(h),
  67. sp->stream().get_executor())
  68. , wp_(sp)
  69. , key_(key)
  70. , res_p_(res_p)
  71. , d_(beast::allocate_stable<data>(
  72. *this, std::move(req)))
  73. {
  74. sp->reset(); // VFALCO I don't like this
  75. (*this)({}, 0, false);
  76. }
  77. void
  78. operator()(
  79. error_code ec = {},
  80. std::size_t bytes_used = 0,
  81. bool cont = true)
  82. {
  83. boost::ignore_unused(bytes_used);
  84. auto sp = wp_.lock();
  85. if(! sp)
  86. {
  87. ec = net::error::operation_aborted;
  88. return this->complete(cont, ec);
  89. }
  90. auto& impl = *sp;
  91. BOOST_ASIO_CORO_REENTER(*this)
  92. {
  93. impl.change_status(status::handshake);
  94. impl.update_timer(this->get_executor());
  95. // write HTTP request
  96. impl.do_pmd_config(d_.req);
  97. BOOST_ASIO_CORO_YIELD
  98. {
  99. BOOST_ASIO_HANDLER_LOCATION((
  100. __FILE__, __LINE__,
  101. "websocket::async_handshake"));
  102. http::async_write(impl.stream(),
  103. d_.req, std::move(*this));
  104. }
  105. if(impl.check_stop_now(ec))
  106. goto upcall;
  107. // read HTTP response
  108. BOOST_ASIO_CORO_YIELD
  109. {
  110. BOOST_ASIO_HANDLER_LOCATION((
  111. __FILE__, __LINE__,
  112. "websocket::async_handshake"));
  113. http::async_read(impl.stream(),
  114. impl.rd_buf, d_.p,
  115. std::move(*this));
  116. }
  117. if(ec == http::error::buffer_overflow)
  118. {
  119. // If the response overflows the internal
  120. // read buffer, switch to a dynamically
  121. // allocated flat buffer.
  122. d_.fb.commit(net::buffer_copy(
  123. d_.fb.prepare(impl.rd_buf.size()),
  124. impl.rd_buf.data()));
  125. impl.rd_buf.clear();
  126. BOOST_ASIO_CORO_YIELD
  127. {
  128. BOOST_ASIO_HANDLER_LOCATION((
  129. __FILE__, __LINE__,
  130. "websocket::async_handshake"));
  131. http::async_read(impl.stream(),
  132. d_.fb, d_.p, std::move(*this));
  133. }
  134. if(! ec)
  135. {
  136. // Copy any leftovers back into the read
  137. // buffer, since this represents websocket
  138. // frame data.
  139. if(d_.fb.size() <= impl.rd_buf.capacity())
  140. {
  141. impl.rd_buf.commit(net::buffer_copy(
  142. impl.rd_buf.prepare(d_.fb.size()),
  143. d_.fb.data()));
  144. }
  145. else
  146. {
  147. ec = http::error::buffer_overflow;
  148. }
  149. }
  150. // Do this before the upcall
  151. d_.fb.clear();
  152. }
  153. if(impl.check_stop_now(ec))
  154. goto upcall;
  155. // success
  156. impl.reset_idle();
  157. impl.on_response(d_.p.get(), key_, ec);
  158. if(res_p_)
  159. swap(d_.p.get(), *res_p_);
  160. upcall:
  161. this->complete(cont ,ec);
  162. }
  163. }
  164. };
  165. template<class NextLayer, bool deflateSupported>
  166. struct stream<NextLayer, deflateSupported>::
  167. run_handshake_op
  168. {
  169. template<class HandshakeHandler>
  170. void operator()(
  171. HandshakeHandler&& h,
  172. boost::shared_ptr<impl_type> const& sp,
  173. request_type&& req,
  174. detail::sec_ws_key_type key,
  175. response_type* res_p)
  176. {
  177. // If you get an error on the following line it means
  178. // that your handler does not meet the documented type
  179. // requirements for the handler.
  180. static_assert(
  181. beast::detail::is_invocable<HandshakeHandler,
  182. void(error_code)>::value,
  183. "HandshakeHandler type requirements not met");
  184. handshake_op<
  185. typename std::decay<HandshakeHandler>::type>(
  186. std::forward<HandshakeHandler>(h),
  187. sp, std::move(req), key, res_p);
  188. }
  189. };
  190. //------------------------------------------------------------------------------
  191. template<class NextLayer, bool deflateSupported>
  192. template<class RequestDecorator>
  193. void
  194. stream<NextLayer, deflateSupported>::
  195. do_handshake(
  196. response_type* res_p,
  197. string_view host,
  198. string_view target,
  199. RequestDecorator const& decorator,
  200. error_code& ec)
  201. {
  202. auto& impl = *impl_;
  203. impl.change_status(status::handshake);
  204. impl.reset();
  205. detail::sec_ws_key_type key;
  206. {
  207. auto const req = impl.build_request(
  208. key, host, target, decorator);
  209. impl.do_pmd_config(req);
  210. http::write(impl.stream(), req, ec);
  211. }
  212. if(impl.check_stop_now(ec))
  213. return;
  214. http::response_parser<
  215. typename response_type::body_type> p;
  216. http::read(next_layer(), impl.rd_buf, p, ec);
  217. if(ec == http::error::buffer_overflow)
  218. {
  219. // If the response overflows the internal
  220. // read buffer, switch to a dynamically
  221. // allocated flat buffer.
  222. flat_buffer fb;
  223. fb.commit(net::buffer_copy(
  224. fb.prepare(impl.rd_buf.size()),
  225. impl.rd_buf.data()));
  226. impl.rd_buf.clear();
  227. http::read(next_layer(), fb, p, ec);;
  228. if(! ec)
  229. {
  230. // Copy any leftovers back into the read
  231. // buffer, since this represents websocket
  232. // frame data.
  233. if(fb.size() <= impl.rd_buf.capacity())
  234. {
  235. impl.rd_buf.commit(net::buffer_copy(
  236. impl.rd_buf.prepare(fb.size()),
  237. fb.data()));
  238. }
  239. else
  240. {
  241. ec = http::error::buffer_overflow;
  242. }
  243. }
  244. }
  245. if(impl.check_stop_now(ec))
  246. return;
  247. impl.on_response(p.get(), key, ec);
  248. if(impl.check_stop_now(ec))
  249. return;
  250. if(res_p)
  251. *res_p = p.release();
  252. }
  253. //------------------------------------------------------------------------------
  254. template<class NextLayer, bool deflateSupported>
  255. template<BOOST_BEAST_ASYNC_TPARAM1 HandshakeHandler>
  256. BOOST_BEAST_ASYNC_RESULT1(HandshakeHandler)
  257. stream<NextLayer, deflateSupported>::
  258. async_handshake(
  259. string_view host,
  260. string_view target,
  261. HandshakeHandler&& handler)
  262. {
  263. static_assert(is_async_stream<next_layer_type>::value,
  264. "AsyncStream type requirements not met");
  265. detail::sec_ws_key_type key;
  266. auto req = impl_->build_request(
  267. key, host, target, &default_decorate_req);
  268. return net::async_initiate<
  269. HandshakeHandler,
  270. void(error_code)>(
  271. run_handshake_op{},
  272. handler,
  273. impl_,
  274. std::move(req),
  275. key,
  276. nullptr);
  277. }
  278. template<class NextLayer, bool deflateSupported>
  279. template<BOOST_BEAST_ASYNC_TPARAM1 HandshakeHandler>
  280. BOOST_BEAST_ASYNC_RESULT1(HandshakeHandler)
  281. stream<NextLayer, deflateSupported>::
  282. async_handshake(
  283. response_type& res,
  284. string_view host,
  285. string_view target,
  286. HandshakeHandler&& handler)
  287. {
  288. static_assert(is_async_stream<next_layer_type>::value,
  289. "AsyncStream type requirements not met");
  290. detail::sec_ws_key_type key;
  291. auto req = impl_->build_request(
  292. key, host, target, &default_decorate_req);
  293. return net::async_initiate<
  294. HandshakeHandler,
  295. void(error_code)>(
  296. run_handshake_op{},
  297. handler,
  298. impl_,
  299. std::move(req),
  300. key,
  301. &res);
  302. }
  303. template<class NextLayer, bool deflateSupported>
  304. void
  305. stream<NextLayer, deflateSupported>::
  306. handshake(string_view host,
  307. string_view target)
  308. {
  309. static_assert(is_sync_stream<next_layer_type>::value,
  310. "SyncStream type requirements not met");
  311. error_code ec;
  312. handshake(
  313. host, target, ec);
  314. if(ec)
  315. BOOST_THROW_EXCEPTION(system_error{ec});
  316. }
  317. template<class NextLayer, bool deflateSupported>
  318. void
  319. stream<NextLayer, deflateSupported>::
  320. handshake(response_type& res,
  321. string_view host,
  322. string_view target)
  323. {
  324. static_assert(is_sync_stream<next_layer_type>::value,
  325. "SyncStream type requirements not met");
  326. error_code ec;
  327. handshake(res, host, target, ec);
  328. if(ec)
  329. BOOST_THROW_EXCEPTION(system_error{ec});
  330. }
  331. template<class NextLayer, bool deflateSupported>
  332. void
  333. stream<NextLayer, deflateSupported>::
  334. handshake(string_view host,
  335. string_view target, error_code& ec)
  336. {
  337. static_assert(is_sync_stream<next_layer_type>::value,
  338. "SyncStream type requirements not met");
  339. do_handshake(nullptr,
  340. host, target, &default_decorate_req, ec);
  341. }
  342. template<class NextLayer, bool deflateSupported>
  343. void
  344. stream<NextLayer, deflateSupported>::
  345. handshake(response_type& res,
  346. string_view host,
  347. string_view target,
  348. error_code& ec)
  349. {
  350. static_assert(is_sync_stream<next_layer_type>::value,
  351. "SyncStream type requirements not met");
  352. do_handshake(&res,
  353. host, target, &default_decorate_req, ec);
  354. }
  355. } // websocket
  356. } // beast
  357. } // boost
  358. #endif