handshake.ipp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //
  2. // Copyright (c) 2016-2017 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_IPP
  10. #define BOOST_BEAST_WEBSOCKET_IMPL_HANDSHAKE_IPP
  11. #include <boost/beast/websocket/detail/type_traits.hpp>
  12. #include <boost/beast/http/empty_body.hpp>
  13. #include <boost/beast/http/message.hpp>
  14. #include <boost/beast/http/read.hpp>
  15. #include <boost/beast/http/write.hpp>
  16. #include <boost/beast/core/handler_ptr.hpp>
  17. #include <boost/beast/core/type_traits.hpp>
  18. #include <boost/asio/associated_allocator.hpp>
  19. #include <boost/asio/associated_executor.hpp>
  20. #include <boost/asio/coroutine.hpp>
  21. #include <boost/asio/executor_work_guard.hpp>
  22. #include <boost/asio/handler_continuation_hook.hpp>
  23. #include <boost/asio/handler_invoke_hook.hpp>
  24. #include <boost/assert.hpp>
  25. #include <boost/throw_exception.hpp>
  26. #include <memory>
  27. namespace boost {
  28. namespace beast {
  29. namespace websocket {
  30. //------------------------------------------------------------------------------
  31. // send the upgrade request and process the response
  32. //
  33. template<class NextLayer, bool deflateSupported>
  34. template<class Handler>
  35. class stream<NextLayer, deflateSupported>::handshake_op
  36. : public boost::asio::coroutine
  37. {
  38. struct data
  39. {
  40. stream<NextLayer, deflateSupported>& ws;
  41. boost::asio::executor_work_guard<decltype(std::declval<
  42. stream<NextLayer, deflateSupported>&>().get_executor())> wg;
  43. response_type* res_p;
  44. detail::sec_ws_key_type key;
  45. http::request<http::empty_body> req;
  46. response_type res;
  47. template<class Decorator>
  48. data(
  49. Handler const&,
  50. stream<NextLayer, deflateSupported>& ws_,
  51. response_type* res_p_,
  52. string_view host,
  53. string_view target,
  54. Decorator const& decorator)
  55. : ws(ws_)
  56. , wg(ws.get_executor())
  57. , res_p(res_p_)
  58. , req(ws.build_request(key,
  59. host, target, decorator))
  60. {
  61. ws.reset();
  62. }
  63. };
  64. handler_ptr<data, Handler> d_;
  65. public:
  66. handshake_op(handshake_op&&) = default;
  67. handshake_op(handshake_op const&) = delete;
  68. template<class DeducedHandler, class... Args>
  69. handshake_op(DeducedHandler&& h,
  70. stream<NextLayer, deflateSupported>& ws, Args&&... args)
  71. : d_(std::forward<DeducedHandler>(h),
  72. ws, std::forward<Args>(args)...)
  73. {
  74. }
  75. using allocator_type =
  76. boost::asio::associated_allocator_t<Handler>;
  77. allocator_type
  78. get_allocator() const noexcept
  79. {
  80. return (boost::asio::get_associated_allocator)(d_.handler());
  81. }
  82. using executor_type = boost::asio::associated_executor_t<
  83. Handler, decltype(std::declval<stream<NextLayer, deflateSupported>&>().get_executor())>;
  84. executor_type
  85. get_executor() const noexcept
  86. {
  87. return (boost::asio::get_associated_executor)(
  88. d_.handler(), d_->ws.get_executor());
  89. }
  90. void
  91. operator()(
  92. error_code ec = {},
  93. std::size_t bytes_used = 0);
  94. friend
  95. bool asio_handler_is_continuation(handshake_op* op)
  96. {
  97. using boost::asio::asio_handler_is_continuation;
  98. return asio_handler_is_continuation(
  99. std::addressof(op->d_.handler()));
  100. }
  101. template<class Function>
  102. friend
  103. void asio_handler_invoke(Function&& f, handshake_op* op)
  104. {
  105. using boost::asio::asio_handler_invoke;
  106. asio_handler_invoke(f,
  107. std::addressof(op->d_.handler()));
  108. }
  109. };
  110. template<class NextLayer, bool deflateSupported>
  111. template<class Handler>
  112. void
  113. stream<NextLayer, deflateSupported>::
  114. handshake_op<Handler>::
  115. operator()(error_code ec, std::size_t)
  116. {
  117. auto& d = *d_;
  118. BOOST_ASIO_CORO_REENTER(*this)
  119. {
  120. // Send HTTP Upgrade
  121. d.ws.do_pmd_config(d.req, is_deflate_supported{});
  122. BOOST_ASIO_CORO_YIELD
  123. http::async_write(d.ws.stream_,
  124. d.req, std::move(*this));
  125. if(ec)
  126. goto upcall;
  127. // VFALCO We could pre-serialize the request to
  128. // a single buffer, send that instead,
  129. // and delete the buffer here. The buffer
  130. // could be a variable block at the end
  131. // of handler_ptr's allocation.
  132. // Read HTTP response
  133. BOOST_ASIO_CORO_YIELD
  134. http::async_read(d.ws.next_layer(),
  135. d.ws.rd_buf_, d.res,
  136. std::move(*this));
  137. if(ec)
  138. goto upcall;
  139. d.ws.on_response(d.res, d.key, ec);
  140. if(d.res_p)
  141. swap(d.res, *d.res_p);
  142. upcall:
  143. {
  144. auto wg = std::move(d.wg);
  145. d_.invoke(ec);
  146. }
  147. }
  148. }
  149. template<class NextLayer, bool deflateSupported>
  150. template<class HandshakeHandler>
  151. BOOST_ASIO_INITFN_RESULT_TYPE(
  152. HandshakeHandler, void(error_code))
  153. stream<NextLayer, deflateSupported>::
  154. async_handshake(string_view host,
  155. string_view target,
  156. HandshakeHandler&& handler)
  157. {
  158. static_assert(is_async_stream<next_layer_type>::value,
  159. "AsyncStream requirements not met");
  160. BOOST_BEAST_HANDLER_INIT(
  161. HandshakeHandler, void(error_code));
  162. handshake_op<BOOST_ASIO_HANDLER_TYPE(
  163. HandshakeHandler, void(error_code))>{
  164. std::move(init.completion_handler), *this, nullptr, host,
  165. target, &default_decorate_req}();
  166. return init.result.get();
  167. }
  168. template<class NextLayer, bool deflateSupported>
  169. template<class HandshakeHandler>
  170. BOOST_ASIO_INITFN_RESULT_TYPE(
  171. HandshakeHandler, void(error_code))
  172. stream<NextLayer, deflateSupported>::
  173. async_handshake(response_type& res,
  174. string_view host,
  175. string_view target,
  176. HandshakeHandler&& handler)
  177. {
  178. static_assert(is_async_stream<next_layer_type>::value,
  179. "AsyncStream requirements not met");
  180. BOOST_BEAST_HANDLER_INIT(
  181. HandshakeHandler, void(error_code));
  182. handshake_op<BOOST_ASIO_HANDLER_TYPE(
  183. HandshakeHandler, void(error_code))>{
  184. std::move(init.completion_handler), *this, &res, host,
  185. target, &default_decorate_req}();
  186. return init.result.get();
  187. }
  188. template<class NextLayer, bool deflateSupported>
  189. template<class RequestDecorator, class HandshakeHandler>
  190. BOOST_ASIO_INITFN_RESULT_TYPE(
  191. HandshakeHandler, void(error_code))
  192. stream<NextLayer, deflateSupported>::
  193. async_handshake_ex(string_view host,
  194. string_view target,
  195. RequestDecorator const& decorator,
  196. HandshakeHandler&& handler)
  197. {
  198. static_assert(is_async_stream<next_layer_type>::value,
  199. "AsyncStream requirements not met");
  200. static_assert(detail::is_request_decorator<
  201. RequestDecorator>::value,
  202. "RequestDecorator requirements not met");
  203. BOOST_BEAST_HANDLER_INIT(
  204. HandshakeHandler, void(error_code));
  205. handshake_op<BOOST_ASIO_HANDLER_TYPE(
  206. HandshakeHandler, void(error_code))>{
  207. std::move(init.completion_handler), *this, nullptr, host,
  208. target, decorator}();
  209. return init.result.get();
  210. }
  211. template<class NextLayer, bool deflateSupported>
  212. template<class RequestDecorator, class HandshakeHandler>
  213. BOOST_ASIO_INITFN_RESULT_TYPE(
  214. HandshakeHandler, void(error_code))
  215. stream<NextLayer, deflateSupported>::
  216. async_handshake_ex(response_type& res,
  217. string_view host,
  218. string_view target,
  219. RequestDecorator const& decorator,
  220. HandshakeHandler&& handler)
  221. {
  222. static_assert(is_async_stream<next_layer_type>::value,
  223. "AsyncStream requirements not met");
  224. static_assert(detail::is_request_decorator<
  225. RequestDecorator>::value,
  226. "RequestDecorator requirements not met");
  227. BOOST_BEAST_HANDLER_INIT(
  228. HandshakeHandler, void(error_code));
  229. handshake_op<BOOST_ASIO_HANDLER_TYPE(
  230. HandshakeHandler, void(error_code))>{
  231. std::move(init.completion_handler), *this, &res, host,
  232. target, decorator}();
  233. return init.result.get();
  234. }
  235. template<class NextLayer, bool deflateSupported>
  236. void
  237. stream<NextLayer, deflateSupported>::
  238. handshake(string_view host,
  239. string_view target)
  240. {
  241. static_assert(is_sync_stream<next_layer_type>::value,
  242. "SyncStream requirements not met");
  243. error_code ec;
  244. handshake(
  245. host, target, ec);
  246. if(ec)
  247. BOOST_THROW_EXCEPTION(system_error{ec});
  248. }
  249. template<class NextLayer, bool deflateSupported>
  250. void
  251. stream<NextLayer, deflateSupported>::
  252. handshake(response_type& res,
  253. string_view host,
  254. string_view target)
  255. {
  256. static_assert(is_sync_stream<next_layer_type>::value,
  257. "SyncStream requirements not met");
  258. error_code ec;
  259. handshake(res, host, target, ec);
  260. if(ec)
  261. BOOST_THROW_EXCEPTION(system_error{ec});
  262. }
  263. template<class NextLayer, bool deflateSupported>
  264. template<class RequestDecorator>
  265. void
  266. stream<NextLayer, deflateSupported>::
  267. handshake_ex(string_view host,
  268. string_view target,
  269. RequestDecorator const& decorator)
  270. {
  271. static_assert(is_sync_stream<next_layer_type>::value,
  272. "SyncStream requirements not met");
  273. static_assert(detail::is_request_decorator<
  274. RequestDecorator>::value,
  275. "RequestDecorator requirements not met");
  276. error_code ec;
  277. handshake_ex(host, target, decorator, ec);
  278. if(ec)
  279. BOOST_THROW_EXCEPTION(system_error{ec});
  280. }
  281. template<class NextLayer, bool deflateSupported>
  282. template<class RequestDecorator>
  283. void
  284. stream<NextLayer, deflateSupported>::
  285. handshake_ex(response_type& res,
  286. string_view host,
  287. string_view target,
  288. RequestDecorator const& decorator)
  289. {
  290. static_assert(is_sync_stream<next_layer_type>::value,
  291. "SyncStream requirements not met");
  292. static_assert(detail::is_request_decorator<
  293. RequestDecorator>::value,
  294. "RequestDecorator requirements not met");
  295. error_code ec;
  296. handshake_ex(res, host, target, decorator, ec);
  297. if(ec)
  298. BOOST_THROW_EXCEPTION(system_error{ec});
  299. }
  300. template<class NextLayer, bool deflateSupported>
  301. void
  302. stream<NextLayer, deflateSupported>::
  303. handshake(string_view host,
  304. string_view target, error_code& ec)
  305. {
  306. static_assert(is_sync_stream<next_layer_type>::value,
  307. "SyncStream requirements not met");
  308. do_handshake(nullptr,
  309. host, target, &default_decorate_req, ec);
  310. }
  311. template<class NextLayer, bool deflateSupported>
  312. void
  313. stream<NextLayer, deflateSupported>::
  314. handshake(response_type& res,
  315. string_view host,
  316. string_view target,
  317. error_code& ec)
  318. {
  319. static_assert(is_sync_stream<next_layer_type>::value,
  320. "SyncStream requirements not met");
  321. do_handshake(&res,
  322. host, target, &default_decorate_req, ec);
  323. }
  324. template<class NextLayer, bool deflateSupported>
  325. template<class RequestDecorator>
  326. void
  327. stream<NextLayer, deflateSupported>::
  328. handshake_ex(string_view host,
  329. string_view target,
  330. RequestDecorator const& decorator,
  331. error_code& ec)
  332. {
  333. static_assert(is_sync_stream<next_layer_type>::value,
  334. "SyncStream requirements not met");
  335. static_assert(detail::is_request_decorator<
  336. RequestDecorator>::value,
  337. "RequestDecorator requirements not met");
  338. do_handshake(nullptr,
  339. host, target, decorator, ec);
  340. }
  341. template<class NextLayer, bool deflateSupported>
  342. template<class RequestDecorator>
  343. void
  344. stream<NextLayer, deflateSupported>::
  345. handshake_ex(response_type& res,
  346. string_view host,
  347. string_view target,
  348. RequestDecorator const& decorator,
  349. error_code& ec)
  350. {
  351. static_assert(is_sync_stream<next_layer_type>::value,
  352. "SyncStream requirements not met");
  353. static_assert(detail::is_request_decorator<
  354. RequestDecorator>::value,
  355. "RequestDecorator requirements not met");
  356. do_handshake(&res,
  357. host, target, decorator, ec);
  358. }
  359. //------------------------------------------------------------------------------
  360. template<class NextLayer, bool deflateSupported>
  361. template<class RequestDecorator>
  362. void
  363. stream<NextLayer, deflateSupported>::
  364. do_handshake(
  365. response_type* res_p,
  366. string_view host,
  367. string_view target,
  368. RequestDecorator const& decorator,
  369. error_code& ec)
  370. {
  371. response_type res;
  372. reset();
  373. detail::sec_ws_key_type key;
  374. {
  375. auto const req = build_request(
  376. key, host, target, decorator);
  377. do_pmd_config(req, is_deflate_supported{});
  378. http::write(stream_, req, ec);
  379. }
  380. if(ec)
  381. return;
  382. http::read(next_layer(), rd_buf_, res, ec);
  383. if(ec)
  384. return;
  385. on_response(res, key, ec);
  386. if(res_p)
  387. *res_p = std::move(res);
  388. }
  389. } // websocket
  390. } // beast
  391. } // boost
  392. #endif