accept.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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_ACCEPT_IPP
  10. #define BOOST_BEAST_WEBSOCKET_IMPL_ACCEPT_IPP
  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/parser.hpp>
  15. #include <boost/beast/http/read.hpp>
  16. #include <boost/beast/http/string_body.hpp>
  17. #include <boost/beast/http/write.hpp>
  18. #include <boost/beast/core/async_base.hpp>
  19. #include <boost/beast/core/buffer_traits.hpp>
  20. #include <boost/beast/core/stream_traits.hpp>
  21. #include <boost/beast/core/detail/buffer.hpp>
  22. #include <boost/beast/version.hpp>
  23. #include <boost/asio/coroutine.hpp>
  24. #include <boost/asio/post.hpp>
  25. #include <boost/assert.hpp>
  26. #include <boost/throw_exception.hpp>
  27. #include <memory>
  28. #include <type_traits>
  29. namespace boost {
  30. namespace beast {
  31. namespace websocket {
  32. //------------------------------------------------------------------------------
  33. namespace detail {
  34. template<class Body, class Allocator>
  35. void
  36. impl_base<true>::
  37. build_response_pmd(
  38. http::response<http::string_body>& res,
  39. http::request<Body,
  40. http::basic_fields<Allocator>> const& req)
  41. {
  42. pmd_offer offer;
  43. pmd_offer unused;
  44. pmd_read(offer, req);
  45. pmd_negotiate(res, unused, offer, pmd_opts_);
  46. }
  47. template<class Body, class Allocator>
  48. void
  49. impl_base<false>::
  50. build_response_pmd(
  51. http::response<http::string_body>&,
  52. http::request<Body,
  53. http::basic_fields<Allocator>> const&)
  54. {
  55. }
  56. } // detail
  57. template<class NextLayer, bool deflateSupported>
  58. template<class Body, class Allocator, class Decorator>
  59. response_type
  60. stream<NextLayer, deflateSupported>::impl_type::
  61. build_response(
  62. http::request<Body,
  63. http::basic_fields<Allocator>> const& req,
  64. Decorator const& decorator,
  65. error_code& result)
  66. {
  67. auto const decorate =
  68. [this, &decorator](response_type& res)
  69. {
  70. decorator_opt(res);
  71. decorator(res);
  72. if(! res.count(http::field::server))
  73. res.set(http::field::server,
  74. string_view(BOOST_BEAST_VERSION_STRING));
  75. };
  76. auto err =
  77. [&](error e)
  78. {
  79. result = e;
  80. response_type res;
  81. res.version(req.version());
  82. res.result(http::status::bad_request);
  83. res.body() = result.message();
  84. res.prepare_payload();
  85. decorate(res);
  86. return res;
  87. };
  88. if(req.version() != 11)
  89. return err(error::bad_http_version);
  90. if(req.method() != http::verb::get)
  91. return err(error::bad_method);
  92. if(! req.count(http::field::host))
  93. return err(error::no_host);
  94. {
  95. auto const it = req.find(http::field::connection);
  96. if(it == req.end())
  97. return err(error::no_connection);
  98. if(! http::token_list{it->value()}.exists("upgrade"))
  99. return err(error::no_connection_upgrade);
  100. }
  101. {
  102. auto const it = req.find(http::field::upgrade);
  103. if(it == req.end())
  104. return err(error::no_upgrade);
  105. if(! http::token_list{it->value()}.exists("websocket"))
  106. return err(error::no_upgrade_websocket);
  107. }
  108. string_view key;
  109. {
  110. auto const it = req.find(http::field::sec_websocket_key);
  111. if(it == req.end())
  112. return err(error::no_sec_key);
  113. key = it->value();
  114. if(key.size() > detail::sec_ws_key_type::static_capacity)
  115. return err(error::bad_sec_key);
  116. }
  117. {
  118. auto const it = req.find(http::field::sec_websocket_version);
  119. if(it == req.end())
  120. return err(error::no_sec_version);
  121. if(it->value() != "13")
  122. {
  123. response_type res;
  124. res.result(http::status::upgrade_required);
  125. res.version(req.version());
  126. res.set(http::field::sec_websocket_version, "13");
  127. result = error::bad_sec_version;
  128. res.body() = result.message();
  129. res.prepare_payload();
  130. decorate(res);
  131. return res;
  132. }
  133. }
  134. response_type res;
  135. res.result(http::status::switching_protocols);
  136. res.version(req.version());
  137. res.set(http::field::upgrade, "websocket");
  138. res.set(http::field::connection, "upgrade");
  139. {
  140. detail::sec_ws_accept_type acc;
  141. detail::make_sec_ws_accept(acc, key);
  142. res.set(http::field::sec_websocket_accept, to_string_view(acc));
  143. }
  144. this->build_response_pmd(res, req);
  145. decorate(res);
  146. result = {};
  147. return res;
  148. }
  149. //------------------------------------------------------------------------------
  150. /** Respond to an HTTP request
  151. */
  152. template<class NextLayer, bool deflateSupported>
  153. template<class Handler>
  154. class stream<NextLayer, deflateSupported>::response_op
  155. : public beast::stable_async_base<
  156. Handler, beast::executor_type<stream>>
  157. , public asio::coroutine
  158. {
  159. boost::weak_ptr<impl_type> wp_;
  160. error_code result_; // must come before res_
  161. response_type& res_;
  162. http::response<http::empty_body> res_100_;
  163. bool needs_res_100_{false};
  164. public:
  165. template<
  166. class Handler_,
  167. class Body, class Allocator,
  168. class Decorator>
  169. response_op(
  170. Handler_&& h,
  171. boost::shared_ptr<impl_type> const& sp,
  172. http::request<Body,
  173. http::basic_fields<Allocator>> const& req,
  174. Decorator const& decorator,
  175. bool cont = false)
  176. : stable_async_base<Handler,
  177. beast::executor_type<stream>>(
  178. std::forward<Handler_>(h),
  179. sp->stream().get_executor())
  180. , wp_(sp)
  181. , res_(beast::allocate_stable<response_type>(*this,
  182. sp->build_response(req, decorator, result_)))
  183. {
  184. auto itr = req.find(http::field::expect);
  185. if (itr != req.end() && iequals(itr->value(), "100-continue")) // do
  186. {
  187. res_100_.version(res_.version());
  188. res_100_.set(http::field::server, res_[http::field::server]);
  189. res_100_.result(http::status::continue_);
  190. res_100_.prepare_payload();
  191. needs_res_100_ = true;
  192. }
  193. (*this)({}, 0, cont);
  194. }
  195. void operator()(
  196. error_code ec = {},
  197. std::size_t bytes_transferred = 0,
  198. bool cont = true)
  199. {
  200. boost::ignore_unused(bytes_transferred);
  201. auto sp = wp_.lock();
  202. if(! sp)
  203. {
  204. BOOST_BEAST_ASSIGN_EC(ec, net::error::operation_aborted);
  205. return this->complete(cont, ec);
  206. }
  207. auto& impl = *sp;
  208. BOOST_ASIO_CORO_REENTER(*this)
  209. {
  210. impl.change_status(status::handshake);
  211. impl.update_timer(this->get_executor());
  212. if (needs_res_100_)
  213. {
  214. BOOST_ASIO_CORO_YIELD
  215. {
  216. BOOST_ASIO_HANDLER_LOCATION((__FILE__, __LINE__, "websocket::async_accept"));
  217. http::async_write(
  218. impl.stream(), res_100_, std::move(*this));
  219. }
  220. }
  221. // Send response
  222. BOOST_ASIO_CORO_YIELD
  223. {
  224. BOOST_ASIO_HANDLER_LOCATION((
  225. __FILE__, __LINE__,
  226. "websocket::async_accept"));
  227. http::async_write(
  228. impl.stream(), res_, std::move(*this));
  229. }
  230. if(impl.check_stop_now(ec))
  231. goto upcall;
  232. if(! ec)
  233. {
  234. BOOST_BEAST_ASSIGN_EC(ec, result_);
  235. BOOST_BEAST_ASSIGN_EC(ec, result_);
  236. }
  237. if(! ec)
  238. {
  239. impl.do_pmd_config(res_);
  240. impl.open(role_type::server);
  241. }
  242. upcall:
  243. this->complete(cont, ec);
  244. }
  245. }
  246. };
  247. //------------------------------------------------------------------------------
  248. // read and respond to an upgrade request
  249. //
  250. // Cancellation: the async_accept cancellation can be terminal
  251. // because it will just interrupt the reading of the header.
  252. //
  253. template<class NextLayer, bool deflateSupported>
  254. template<class Handler, class Decorator>
  255. class stream<NextLayer, deflateSupported>::accept_op
  256. : public beast::stable_async_base<
  257. Handler, beast::executor_type<stream>>
  258. , public asio::coroutine
  259. {
  260. boost::weak_ptr<impl_type> wp_;
  261. http::request_parser<http::empty_body>& p_;
  262. Decorator d_;
  263. public:
  264. template<class Handler_, class Buffers>
  265. accept_op(
  266. Handler_&& h,
  267. boost::shared_ptr<impl_type> const& sp,
  268. Decorator const& decorator,
  269. Buffers const& buffers)
  270. : stable_async_base<Handler,
  271. beast::executor_type<stream>>(
  272. std::forward<Handler_>(h),
  273. sp->stream().get_executor())
  274. , wp_(sp)
  275. , p_(beast::allocate_stable<
  276. http::request_parser<http::empty_body>>(*this))
  277. , d_(decorator)
  278. {
  279. auto& impl = *sp;
  280. error_code ec;
  281. auto const mb =
  282. beast::detail::dynamic_buffer_prepare(
  283. impl.rd_buf, buffer_bytes(buffers),
  284. ec, error::buffer_overflow);
  285. if(! ec)
  286. impl.rd_buf.commit(
  287. net::buffer_copy(*mb, buffers));
  288. (*this)(ec);
  289. }
  290. void operator()(
  291. error_code ec = {},
  292. std::size_t bytes_transferred = 0,
  293. bool cont = true)
  294. {
  295. boost::ignore_unused(bytes_transferred);
  296. auto sp = wp_.lock();
  297. if(! sp)
  298. {
  299. BOOST_BEAST_ASSIGN_EC(ec, net::error::operation_aborted);
  300. return this->complete(cont, ec);
  301. }
  302. auto& impl = *sp;
  303. BOOST_ASIO_CORO_REENTER(*this)
  304. {
  305. impl.change_status(status::handshake);
  306. impl.update_timer(this->get_executor());
  307. // The constructor could have set ec
  308. if(ec)
  309. goto upcall;
  310. BOOST_ASIO_CORO_YIELD
  311. {
  312. BOOST_ASIO_HANDLER_LOCATION((
  313. __FILE__, __LINE__,
  314. "websocket::async_accept"));
  315. http::async_read(impl.stream(),
  316. impl.rd_buf, p_, std::move(*this));
  317. }
  318. if(ec == http::error::end_of_stream)
  319. {
  320. BOOST_BEAST_ASSIGN_EC(ec, error::closed);
  321. }
  322. if(impl.check_stop_now(ec))
  323. goto upcall;
  324. {
  325. // Arguments from our state must be
  326. // moved to the stack before releasing
  327. // the handler.
  328. auto const req = p_.release();
  329. auto const decorator = d_;
  330. response_op<Handler>(
  331. this->release_handler(),
  332. sp, req, decorator, true);
  333. return;
  334. }
  335. upcall:
  336. this->complete(cont, ec);
  337. }
  338. }
  339. };
  340. template<class NextLayer, bool deflateSupported>
  341. struct stream<NextLayer, deflateSupported>::
  342. run_response_op
  343. {
  344. template<
  345. class AcceptHandler,
  346. class Body, class Allocator,
  347. class Decorator>
  348. void
  349. operator()(
  350. AcceptHandler&& h,
  351. boost::shared_ptr<impl_type> const& sp,
  352. http::request<Body,
  353. http::basic_fields<Allocator>> const* m,
  354. Decorator const& d)
  355. {
  356. // If you get an error on the following line it means
  357. // that your handler does not meet the documented type
  358. // requirements for the handler.
  359. static_assert(
  360. beast::detail::is_invocable<AcceptHandler,
  361. void(error_code)>::value,
  362. "AcceptHandler type requirements not met");
  363. response_op<
  364. typename std::decay<AcceptHandler>::type>(
  365. std::forward<AcceptHandler>(h), sp, *m, d);
  366. }
  367. };
  368. template<class NextLayer, bool deflateSupported>
  369. struct stream<NextLayer, deflateSupported>::
  370. run_accept_op
  371. {
  372. template<
  373. class AcceptHandler,
  374. class Decorator,
  375. class Buffers>
  376. void
  377. operator()(
  378. AcceptHandler&& h,
  379. boost::shared_ptr<impl_type> const& sp,
  380. Decorator const& d,
  381. Buffers const& b)
  382. {
  383. // If you get an error on the following line it means
  384. // that your handler does not meet the documented type
  385. // requirements for the handler.
  386. static_assert(
  387. beast::detail::is_invocable<AcceptHandler,
  388. void(error_code)>::value,
  389. "AcceptHandler type requirements not met");
  390. accept_op<
  391. typename std::decay<AcceptHandler>::type,
  392. Decorator>(
  393. std::forward<AcceptHandler>(h),
  394. sp,
  395. d,
  396. b);
  397. }
  398. };
  399. //------------------------------------------------------------------------------
  400. template<class NextLayer, bool deflateSupported>
  401. template<class Body, class Allocator,
  402. class Decorator>
  403. void
  404. stream<NextLayer, deflateSupported>::
  405. do_accept(
  406. http::request<Body,
  407. http::basic_fields<Allocator>> const& req,
  408. Decorator const& decorator,
  409. error_code& ec)
  410. {
  411. impl_->change_status(status::handshake);
  412. error_code result;
  413. auto const res = impl_->build_response(req, decorator, result);
  414. auto itr = req.find(http::field::expect);
  415. if (itr != req.end() && iequals(itr->value(), "100-continue")) // do
  416. {
  417. http::response<http::empty_body> res_100;
  418. res_100.version(res.version());
  419. res_100.set(http::field::server, res[http::field::server]);
  420. res_100.result(http::status::continue_);
  421. res_100.prepare_payload();
  422. http::write(impl_->stream(), res_100, ec);
  423. if (ec)
  424. return;
  425. }
  426. http::write(impl_->stream(), res, ec);
  427. if(ec)
  428. return;
  429. BOOST_BEAST_ASSIGN_EC(ec, result);
  430. if(ec)
  431. {
  432. // VFALCO TODO Respect keep alive setting, perform
  433. // teardown if Connection: close.
  434. return;
  435. }
  436. impl_->do_pmd_config(res);
  437. impl_->open(role_type::server);
  438. }
  439. template<class NextLayer, bool deflateSupported>
  440. template<class Buffers, class Decorator>
  441. void
  442. stream<NextLayer, deflateSupported>::
  443. do_accept(
  444. Buffers const& buffers,
  445. Decorator const& decorator,
  446. error_code& ec)
  447. {
  448. impl_->reset();
  449. auto const mb =
  450. beast::detail::dynamic_buffer_prepare(
  451. impl_->rd_buf, buffer_bytes(buffers), ec,
  452. error::buffer_overflow);
  453. if(ec)
  454. return;
  455. impl_->rd_buf.commit(net::buffer_copy(*mb, buffers));
  456. http::request_parser<http::empty_body> p;
  457. http::read(next_layer(), impl_->rd_buf, p, ec);
  458. if(ec == http::error::end_of_stream)
  459. {
  460. BOOST_BEAST_ASSIGN_EC(ec, error::closed);
  461. }
  462. if(ec)
  463. return;
  464. do_accept(p.get(), decorator, ec);
  465. }
  466. //------------------------------------------------------------------------------
  467. template<class NextLayer, bool deflateSupported>
  468. void
  469. stream<NextLayer, deflateSupported>::
  470. accept()
  471. {
  472. static_assert(is_sync_stream<next_layer_type>::value,
  473. "SyncStream type requirements not met");
  474. error_code ec;
  475. accept(ec);
  476. if(ec)
  477. BOOST_THROW_EXCEPTION(system_error{ec});
  478. }
  479. template<class NextLayer, bool deflateSupported>
  480. void
  481. stream<NextLayer, deflateSupported>::
  482. accept(error_code& ec)
  483. {
  484. static_assert(is_sync_stream<next_layer_type>::value,
  485. "SyncStream type requirements not met");
  486. do_accept(
  487. net::const_buffer{},
  488. &default_decorate_res, ec);
  489. }
  490. template<class NextLayer, bool deflateSupported>
  491. template<class ConstBufferSequence>
  492. typename std::enable_if<! http::detail::is_header<
  493. ConstBufferSequence>::value>::type
  494. stream<NextLayer, deflateSupported>::
  495. accept(ConstBufferSequence const& buffers)
  496. {
  497. static_assert(is_sync_stream<next_layer_type>::value,
  498. "SyncStream type requirements not met");
  499. static_assert(net::is_const_buffer_sequence<
  500. ConstBufferSequence>::value,
  501. "ConstBufferSequence type requirements not met");
  502. error_code ec;
  503. accept(buffers, ec);
  504. if(ec)
  505. BOOST_THROW_EXCEPTION(system_error{ec});
  506. }
  507. template<class NextLayer, bool deflateSupported>
  508. template<class ConstBufferSequence>
  509. typename std::enable_if<! http::detail::is_header<
  510. ConstBufferSequence>::value>::type
  511. stream<NextLayer, deflateSupported>::
  512. accept(
  513. ConstBufferSequence const& buffers, error_code& ec)
  514. {
  515. static_assert(is_sync_stream<next_layer_type>::value,
  516. "SyncStream type requirements not met");
  517. static_assert(net::is_const_buffer_sequence<
  518. ConstBufferSequence>::value,
  519. "ConstBufferSequence type requirements not met");
  520. do_accept(buffers, &default_decorate_res, ec);
  521. }
  522. template<class NextLayer, bool deflateSupported>
  523. template<class Body, class Allocator>
  524. void
  525. stream<NextLayer, deflateSupported>::
  526. accept(
  527. http::request<Body,
  528. http::basic_fields<Allocator>> const& req)
  529. {
  530. static_assert(is_sync_stream<next_layer_type>::value,
  531. "SyncStream type requirements not met");
  532. error_code ec;
  533. accept(req, ec);
  534. if(ec)
  535. BOOST_THROW_EXCEPTION(system_error{ec});
  536. }
  537. template<class NextLayer, bool deflateSupported>
  538. template<class Body, class Allocator>
  539. void
  540. stream<NextLayer, deflateSupported>::
  541. accept(
  542. http::request<Body,
  543. http::basic_fields<Allocator>> const& req,
  544. error_code& ec)
  545. {
  546. static_assert(is_sync_stream<next_layer_type>::value,
  547. "SyncStream type requirements not met");
  548. impl_->reset();
  549. do_accept(req, &default_decorate_res, ec);
  550. }
  551. //------------------------------------------------------------------------------
  552. template<class NextLayer, bool deflateSupported>
  553. template<
  554. BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler>
  555. BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
  556. stream<NextLayer, deflateSupported>::
  557. async_accept(
  558. AcceptHandler&& handler)
  559. {
  560. static_assert(is_async_stream<next_layer_type>::value,
  561. "AsyncStream type requirements not met");
  562. impl_->reset();
  563. return net::async_initiate<
  564. AcceptHandler,
  565. void(error_code)>(
  566. run_accept_op{},
  567. handler,
  568. impl_,
  569. &default_decorate_res,
  570. net::const_buffer{});
  571. }
  572. template<class NextLayer, bool deflateSupported>
  573. template<
  574. class ConstBufferSequence,
  575. BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler>
  576. BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
  577. stream<NextLayer, deflateSupported>::
  578. async_accept(
  579. ConstBufferSequence const& buffers,
  580. AcceptHandler&& handler,
  581. typename std::enable_if<
  582. ! http::detail::is_header<
  583. ConstBufferSequence>::value>::type*
  584. )
  585. {
  586. static_assert(is_async_stream<next_layer_type>::value,
  587. "AsyncStream type requirements not met");
  588. static_assert(net::is_const_buffer_sequence<
  589. ConstBufferSequence>::value,
  590. "ConstBufferSequence type requirements not met");
  591. impl_->reset();
  592. return net::async_initiate<
  593. AcceptHandler,
  594. void(error_code)>(
  595. run_accept_op{},
  596. handler,
  597. impl_,
  598. &default_decorate_res,
  599. buffers);
  600. }
  601. template<class NextLayer, bool deflateSupported>
  602. template<
  603. class Body, class Allocator,
  604. BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler>
  605. BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
  606. stream<NextLayer, deflateSupported>::
  607. async_accept(
  608. http::request<Body, http::basic_fields<Allocator>> const& req,
  609. AcceptHandler&& handler)
  610. {
  611. static_assert(is_async_stream<next_layer_type>::value,
  612. "AsyncStream type requirements not met");
  613. impl_->reset();
  614. return net::async_initiate<
  615. AcceptHandler,
  616. void(error_code)>(
  617. run_response_op{},
  618. handler,
  619. impl_,
  620. &req,
  621. &default_decorate_res);
  622. }
  623. } // websocket
  624. } // beast
  625. } // boost
  626. #endif