handshake.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 BHO_BEAST_WEBSOCKET_IMPL_HANDSHAKE_HPP
  10. #define BHO_BEAST_WEBSOCKET_IMPL_HANDSHAKE_HPP
  11. #include <asio2/bho/beast/websocket/impl/stream_impl.hpp>
  12. #include <asio2/bho/beast/websocket/detail/type_traits.hpp>
  13. #include <asio2/bho/beast/http/empty_body.hpp>
  14. #include <asio2/bho/beast/http/message.hpp>
  15. #include <asio2/bho/beast/http/read.hpp>
  16. #include <asio2/bho/beast/http/write.hpp>
  17. #include <asio2/bho/beast/core/async_base.hpp>
  18. #include <asio2/bho/beast/core/flat_buffer.hpp>
  19. #include <asio2/bho/beast/core/stream_traits.hpp>
  20. #include <asio/coroutine.hpp>
  21. #include <asio2/bho/assert.hpp>
  22. #include <asio2/bho/throw_exception.hpp>
  23. #include <memory>
  24. namespace bho {
  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. std::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. std::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. bho::ignore_unused(bytes_used);
  84. auto sp = wp_.lock();
  85. if(! sp)
  86. {
  87. BHO_BEAST_ASSIGN_EC(ec, net::error::operation_aborted);
  88. return this->complete(cont, ec);
  89. }
  90. auto& impl = *sp;
  91. 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. ASIO_CORO_YIELD
  98. {
  99. 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. ASIO_CORO_YIELD
  109. {
  110. 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. ASIO_CORO_YIELD
  127. {
  128. 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. BHO_BEAST_ASSIGN_EC(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. std::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. if(res_p)
  203. res_p->result(http::status::internal_server_error);
  204. auto& impl = *impl_;
  205. impl.change_status(status::handshake);
  206. impl.reset();
  207. detail::sec_ws_key_type key;
  208. {
  209. auto const req = impl.build_request(
  210. key, host, target, decorator);
  211. impl.do_pmd_config(req);
  212. http::write(impl.stream(), req, ec);
  213. }
  214. if(impl.check_stop_now(ec))
  215. return;
  216. http::response_parser<
  217. typename response_type::body_type> p;
  218. http::read(next_layer(), impl.rd_buf, p, ec);
  219. if(ec == http::error::buffer_overflow)
  220. {
  221. // If the response overflows the internal
  222. // read buffer, switch to a dynamically
  223. // allocated flat buffer.
  224. flat_buffer fb;
  225. fb.commit(net::buffer_copy(
  226. fb.prepare(impl.rd_buf.size()),
  227. impl.rd_buf.data()));
  228. impl.rd_buf.clear();
  229. http::read(next_layer(), fb, p, ec);;
  230. if(! ec)
  231. {
  232. // Copy any leftovers back into the read
  233. // buffer, since this represents websocket
  234. // frame data.
  235. if(fb.size() <= impl.rd_buf.capacity())
  236. {
  237. impl.rd_buf.commit(net::buffer_copy(
  238. impl.rd_buf.prepare(fb.size()),
  239. fb.data()));
  240. }
  241. else
  242. {
  243. BHO_BEAST_ASSIGN_EC(ec, http::error::buffer_overflow);
  244. }
  245. }
  246. }
  247. if(impl.check_stop_now(ec))
  248. return;
  249. if (res_p)
  250. {
  251. // If res_p is not null, move parser's response into it.
  252. *res_p = p.release();
  253. }
  254. else
  255. {
  256. // Otherwise point res_p at the response in the parser.
  257. res_p = &p.get();
  258. }
  259. impl.on_response(*res_p, key, ec);
  260. }
  261. //------------------------------------------------------------------------------
  262. template<class NextLayer, bool deflateSupported>
  263. template<BHO_BEAST_ASYNC_TPARAM1 HandshakeHandler>
  264. BHO_BEAST_ASYNC_RESULT1(HandshakeHandler)
  265. stream<NextLayer, deflateSupported>::
  266. async_handshake(
  267. string_view host,
  268. string_view target,
  269. HandshakeHandler&& handler)
  270. {
  271. static_assert(is_async_stream<next_layer_type>::value,
  272. "AsyncStream type requirements not met");
  273. detail::sec_ws_key_type key;
  274. auto req = impl_->build_request(
  275. key, host, target, &default_decorate_req);
  276. return net::async_initiate<
  277. HandshakeHandler,
  278. void(error_code)>(
  279. run_handshake_op{},
  280. handler,
  281. impl_,
  282. std::move(req),
  283. key,
  284. nullptr);
  285. }
  286. template<class NextLayer, bool deflateSupported>
  287. template<BHO_BEAST_ASYNC_TPARAM1 HandshakeHandler>
  288. BHO_BEAST_ASYNC_RESULT1(HandshakeHandler)
  289. stream<NextLayer, deflateSupported>::
  290. async_handshake(
  291. response_type& res,
  292. string_view host,
  293. string_view target,
  294. HandshakeHandler&& handler)
  295. {
  296. static_assert(is_async_stream<next_layer_type>::value,
  297. "AsyncStream type requirements not met");
  298. detail::sec_ws_key_type key;
  299. auto req = impl_->build_request(
  300. key, host, target, &default_decorate_req);
  301. res.result(http::status::internal_server_error);
  302. return net::async_initiate<
  303. HandshakeHandler,
  304. void(error_code)>(
  305. run_handshake_op{},
  306. handler,
  307. impl_,
  308. std::move(req),
  309. key,
  310. &res);
  311. }
  312. template<class NextLayer, bool deflateSupported>
  313. void
  314. stream<NextLayer, deflateSupported>::
  315. handshake(string_view host,
  316. string_view target)
  317. {
  318. static_assert(is_sync_stream<next_layer_type>::value,
  319. "SyncStream type requirements not met");
  320. error_code ec;
  321. handshake(
  322. host, target, ec);
  323. if(ec)
  324. BHO_THROW_EXCEPTION(system_error{ec});
  325. }
  326. template<class NextLayer, bool deflateSupported>
  327. void
  328. stream<NextLayer, deflateSupported>::
  329. handshake(response_type& res,
  330. string_view host,
  331. string_view target)
  332. {
  333. static_assert(is_sync_stream<next_layer_type>::value,
  334. "SyncStream type requirements not met");
  335. error_code ec;
  336. handshake(res, host, target, ec);
  337. if(ec)
  338. BHO_THROW_EXCEPTION(system_error{ec});
  339. }
  340. template<class NextLayer, bool deflateSupported>
  341. void
  342. stream<NextLayer, deflateSupported>::
  343. handshake(string_view host,
  344. string_view target, error_code& ec)
  345. {
  346. static_assert(is_sync_stream<next_layer_type>::value,
  347. "SyncStream type requirements not met");
  348. do_handshake(nullptr,
  349. host, target, &default_decorate_req, ec);
  350. }
  351. template<class NextLayer, bool deflateSupported>
  352. void
  353. stream<NextLayer, deflateSupported>::
  354. handshake(response_type& res,
  355. string_view host,
  356. string_view target,
  357. error_code& ec)
  358. {
  359. static_assert(is_sync_stream<next_layer_type>::value,
  360. "SyncStream type requirements not met");
  361. do_handshake(&res,
  362. host, target, &default_decorate_req, ec);
  363. }
  364. } // websocket
  365. } // beast
  366. } // bho
  367. #endif