accept.hpp 20 KB

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