handshake.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. BOOST_BEAST_ASSIGN_EC(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. BOOST_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. boost::shared_ptr<impl_type> const& self;
  170. using executor_type = typename stream::executor_type;
  171. executor_type
  172. get_executor() const noexcept
  173. {
  174. return self->stream().get_executor();
  175. }
  176. template<class HandshakeHandler>
  177. void operator()(
  178. HandshakeHandler&& h,
  179. request_type&& req,
  180. detail::sec_ws_key_type key,
  181. response_type* res_p)
  182. {
  183. // If you get an error on the following line it means
  184. // that your handler does not meet the documented type
  185. // requirements for the handler.
  186. static_assert(
  187. beast::detail::is_invocable<HandshakeHandler,
  188. void(error_code)>::value,
  189. "HandshakeHandler type requirements not met");
  190. handshake_op<
  191. typename std::decay<HandshakeHandler>::type>(
  192. std::forward<HandshakeHandler>(h),
  193. self, std::move(req), key, res_p);
  194. }
  195. };
  196. //------------------------------------------------------------------------------
  197. template<class NextLayer, bool deflateSupported>
  198. template<class RequestDecorator>
  199. void
  200. stream<NextLayer, deflateSupported>::
  201. do_handshake(
  202. response_type* res_p,
  203. string_view host,
  204. string_view target,
  205. RequestDecorator const& decorator,
  206. error_code& ec)
  207. {
  208. if(res_p)
  209. res_p->result(http::status::internal_server_error);
  210. auto& impl = *impl_;
  211. impl.change_status(status::handshake);
  212. impl.reset();
  213. detail::sec_ws_key_type key;
  214. {
  215. auto const req = impl.build_request(
  216. key, host, target, decorator);
  217. impl.do_pmd_config(req);
  218. http::write(impl.stream(), req, ec);
  219. }
  220. if(impl.check_stop_now(ec))
  221. return;
  222. http::response_parser<
  223. typename response_type::body_type> p;
  224. http::read(next_layer(), impl.rd_buf, p, ec);
  225. if(ec == http::error::buffer_overflow)
  226. {
  227. // If the response overflows the internal
  228. // read buffer, switch to a dynamically
  229. // allocated flat buffer.
  230. flat_buffer fb;
  231. fb.commit(net::buffer_copy(
  232. fb.prepare(impl.rd_buf.size()),
  233. impl.rd_buf.data()));
  234. impl.rd_buf.clear();
  235. http::read(next_layer(), fb, p, ec);;
  236. if(! ec)
  237. {
  238. // Copy any leftovers back into the read
  239. // buffer, since this represents websocket
  240. // frame data.
  241. if(fb.size() <= impl.rd_buf.capacity())
  242. {
  243. impl.rd_buf.commit(net::buffer_copy(
  244. impl.rd_buf.prepare(fb.size()),
  245. fb.data()));
  246. }
  247. else
  248. {
  249. BOOST_BEAST_ASSIGN_EC(ec, http::error::buffer_overflow);
  250. }
  251. }
  252. }
  253. if(impl.check_stop_now(ec))
  254. return;
  255. if (res_p)
  256. {
  257. // If res_p is not null, move parser's response into it.
  258. *res_p = p.release();
  259. }
  260. else
  261. {
  262. // Otherwise point res_p at the response in the parser.
  263. res_p = &p.get();
  264. }
  265. impl.on_response(*res_p, key, ec);
  266. }
  267. //------------------------------------------------------------------------------
  268. template<class NextLayer, bool deflateSupported>
  269. template<BOOST_BEAST_ASYNC_TPARAM1 HandshakeHandler>
  270. BOOST_BEAST_ASYNC_RESULT1(HandshakeHandler)
  271. stream<NextLayer, deflateSupported>::
  272. async_handshake(
  273. string_view host,
  274. string_view target,
  275. HandshakeHandler&& handler)
  276. {
  277. static_assert(is_async_stream<next_layer_type>::value,
  278. "AsyncStream type requirements not met");
  279. detail::sec_ws_key_type key;
  280. auto req = impl_->build_request(
  281. key, host, target, &default_decorate_req);
  282. return net::async_initiate<
  283. HandshakeHandler,
  284. void(error_code)>(
  285. run_handshake_op{impl_},
  286. handler,
  287. std::move(req),
  288. key,
  289. nullptr);
  290. }
  291. template<class NextLayer, bool deflateSupported>
  292. template<BOOST_BEAST_ASYNC_TPARAM1 HandshakeHandler>
  293. BOOST_BEAST_ASYNC_RESULT1(HandshakeHandler)
  294. stream<NextLayer, deflateSupported>::
  295. async_handshake(
  296. response_type& res,
  297. string_view host,
  298. string_view target,
  299. HandshakeHandler&& handler)
  300. {
  301. static_assert(is_async_stream<next_layer_type>::value,
  302. "AsyncStream type requirements not met");
  303. detail::sec_ws_key_type key;
  304. auto req = impl_->build_request(
  305. key, host, target, &default_decorate_req);
  306. res.result(http::status::internal_server_error);
  307. return net::async_initiate<
  308. HandshakeHandler,
  309. void(error_code)>(
  310. run_handshake_op{impl_},
  311. handler,
  312. std::move(req),
  313. key,
  314. &res);
  315. }
  316. template<class NextLayer, bool deflateSupported>
  317. void
  318. stream<NextLayer, deflateSupported>::
  319. handshake(string_view host,
  320. string_view target)
  321. {
  322. static_assert(is_sync_stream<next_layer_type>::value,
  323. "SyncStream type requirements not met");
  324. error_code ec;
  325. handshake(
  326. host, target, ec);
  327. if(ec)
  328. BOOST_THROW_EXCEPTION(system_error{ec});
  329. }
  330. template<class NextLayer, bool deflateSupported>
  331. void
  332. stream<NextLayer, deflateSupported>::
  333. handshake(response_type& res,
  334. string_view host,
  335. string_view target)
  336. {
  337. static_assert(is_sync_stream<next_layer_type>::value,
  338. "SyncStream type requirements not met");
  339. error_code ec;
  340. handshake(res, host, target, ec);
  341. if(ec)
  342. BOOST_THROW_EXCEPTION(system_error{ec});
  343. }
  344. template<class NextLayer, bool deflateSupported>
  345. void
  346. stream<NextLayer, deflateSupported>::
  347. handshake(string_view host,
  348. string_view target, error_code& ec)
  349. {
  350. static_assert(is_sync_stream<next_layer_type>::value,
  351. "SyncStream type requirements not met");
  352. do_handshake(nullptr,
  353. host, target, &default_decorate_req, ec);
  354. }
  355. template<class NextLayer, bool deflateSupported>
  356. void
  357. stream<NextLayer, deflateSupported>::
  358. handshake(response_type& res,
  359. string_view host,
  360. string_view target,
  361. error_code& ec)
  362. {
  363. static_assert(is_sync_stream<next_layer_type>::value,
  364. "SyncStream type requirements not met");
  365. do_handshake(&res,
  366. host, target, &default_decorate_req, ec);
  367. }
  368. } // websocket
  369. } // beast
  370. } // boost
  371. #endif