File indexing completed on 2025-01-18 09:29:33
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_BEAST_WEBSOCKET_IMPL_ACCEPT_IPP
0011 #define BOOST_BEAST_WEBSOCKET_IMPL_ACCEPT_IPP
0012
0013 #include <boost/beast/websocket/impl/stream_impl.hpp>
0014 #include <boost/beast/websocket/detail/type_traits.hpp>
0015 #include <boost/beast/http/empty_body.hpp>
0016 #include <boost/beast/http/parser.hpp>
0017 #include <boost/beast/http/read.hpp>
0018 #include <boost/beast/http/string_body.hpp>
0019 #include <boost/beast/http/write.hpp>
0020 #include <boost/beast/core/async_base.hpp>
0021 #include <boost/beast/core/buffer_traits.hpp>
0022 #include <boost/beast/core/stream_traits.hpp>
0023 #include <boost/beast/core/detail/buffer.hpp>
0024 #include <boost/beast/version.hpp>
0025 #include <boost/asio/coroutine.hpp>
0026 #include <boost/assert.hpp>
0027 #include <boost/throw_exception.hpp>
0028 #include <memory>
0029 #include <type_traits>
0030
0031 namespace boost {
0032 namespace beast {
0033 namespace websocket {
0034
0035
0036
0037 namespace detail {
0038
0039 template<class Body, class Allocator>
0040 void
0041 impl_base<true>::
0042 build_response_pmd(
0043 http::response<http::string_body>& res,
0044 http::request<Body,
0045 http::basic_fields<Allocator>> const& req)
0046 {
0047 pmd_offer offer;
0048 pmd_offer unused;
0049 pmd_read(offer, req);
0050 pmd_negotiate(res, unused, offer, pmd_opts_);
0051 }
0052
0053 template<class Body, class Allocator>
0054 void
0055 impl_base<false>::
0056 build_response_pmd(
0057 http::response<http::string_body>&,
0058 http::request<Body,
0059 http::basic_fields<Allocator>> const&)
0060 {
0061 }
0062
0063 }
0064
0065 template<class NextLayer, bool deflateSupported>
0066 template<class Body, class Allocator, class Decorator>
0067 response_type
0068 stream<NextLayer, deflateSupported>::impl_type::
0069 build_response(
0070 http::request<Body,
0071 http::basic_fields<Allocator>> const& req,
0072 Decorator const& decorator,
0073 error_code& result)
0074 {
0075 auto const decorate =
0076 [this, &decorator](response_type& res)
0077 {
0078 decorator_opt(res);
0079 decorator(res);
0080 if(! res.count(http::field::server))
0081 res.set(http::field::server,
0082 string_view(BOOST_BEAST_VERSION_STRING));
0083 };
0084 auto err =
0085 [&](error e)
0086 {
0087 result = e;
0088 response_type res;
0089 res.version(req.version());
0090 res.result(http::status::bad_request);
0091 res.body() = result.message();
0092 res.prepare_payload();
0093 decorate(res);
0094 return res;
0095 };
0096 if(req.version() != 11)
0097 return err(error::bad_http_version);
0098 if(req.method() != http::verb::get)
0099 return err(error::bad_method);
0100 if(! req.count(http::field::host))
0101 return err(error::no_host);
0102 {
0103 auto const it = req.find(http::field::connection);
0104 if(it == req.end())
0105 return err(error::no_connection);
0106 if(! http::token_list{it->value()}.exists("upgrade"))
0107 return err(error::no_connection_upgrade);
0108 }
0109 {
0110 auto const it = req.find(http::field::upgrade);
0111 if(it == req.end())
0112 return err(error::no_upgrade);
0113 if(! http::token_list{it->value()}.exists("websocket"))
0114 return err(error::no_upgrade_websocket);
0115 }
0116 string_view key;
0117 {
0118 auto const it = req.find(http::field::sec_websocket_key);
0119 if(it == req.end())
0120 return err(error::no_sec_key);
0121 key = it->value();
0122 if(key.size() > detail::sec_ws_key_type::static_capacity)
0123 return err(error::bad_sec_key);
0124 }
0125 {
0126 auto const it = req.find(http::field::sec_websocket_version);
0127 if(it == req.end())
0128 return err(error::no_sec_version);
0129 if(it->value() != "13")
0130 {
0131 response_type res;
0132 res.result(http::status::upgrade_required);
0133 res.version(req.version());
0134 res.set(http::field::sec_websocket_version, "13");
0135 result = error::bad_sec_version;
0136 res.body() = result.message();
0137 res.prepare_payload();
0138 decorate(res);
0139 return res;
0140 }
0141 }
0142
0143 response_type res;
0144 res.result(http::status::switching_protocols);
0145 res.version(req.version());
0146 res.set(http::field::upgrade, "websocket");
0147 res.set(http::field::connection, "Upgrade");
0148 {
0149 detail::sec_ws_accept_type acc;
0150 detail::make_sec_ws_accept(acc, key);
0151 res.set(http::field::sec_websocket_accept, to_string_view(acc));
0152 }
0153 this->build_response_pmd(res, req);
0154 decorate(res);
0155 result = {};
0156 return res;
0157 }
0158
0159
0160
0161
0162
0163 template<class NextLayer, bool deflateSupported>
0164 template<class Handler>
0165 class stream<NextLayer, deflateSupported>::response_op
0166 : public beast::stable_async_base<
0167 Handler, beast::executor_type<stream>>
0168 , public asio::coroutine
0169 {
0170 boost::weak_ptr<impl_type> wp_;
0171 error_code result_;
0172 response_type& res_;
0173 http::response<http::empty_body> res_100_;
0174 bool needs_res_100_{false};
0175
0176 public:
0177 template<
0178 class Handler_,
0179 class Body, class Allocator,
0180 class Decorator>
0181 response_op(
0182 Handler_&& h,
0183 boost::shared_ptr<impl_type> const& sp,
0184 http::request<Body,
0185 http::basic_fields<Allocator>> const& req,
0186 Decorator const& decorator,
0187 bool cont = false)
0188 : stable_async_base<Handler,
0189 beast::executor_type<stream>>(
0190 std::forward<Handler_>(h),
0191 sp->stream().get_executor())
0192 , wp_(sp)
0193 , res_(beast::allocate_stable<response_type>(*this,
0194 sp->build_response(req, decorator, result_)))
0195 {
0196 auto itr = req.find(http::field::expect);
0197 if (itr != req.end() && iequals(itr->value(), "100-continue"))
0198 {
0199 res_100_.version(res_.version());
0200 res_100_.set(http::field::server, res_[http::field::server]);
0201 res_100_.result(http::status::continue_);
0202 res_100_.prepare_payload();
0203 needs_res_100_ = true;
0204 }
0205 (*this)({}, 0, cont);
0206 }
0207
0208 void operator()(
0209 error_code ec = {},
0210 std::size_t bytes_transferred = 0,
0211 bool cont = true)
0212 {
0213 boost::ignore_unused(bytes_transferred);
0214 auto sp = wp_.lock();
0215 if(! sp)
0216 {
0217 BOOST_BEAST_ASSIGN_EC(ec, net::error::operation_aborted);
0218 return this->complete(cont, ec);
0219 }
0220 auto& impl = *sp;
0221 BOOST_ASIO_CORO_REENTER(*this)
0222 {
0223 impl.change_status(status::handshake);
0224 impl.update_timer(this->get_executor());
0225
0226 if (needs_res_100_)
0227 {
0228 BOOST_ASIO_CORO_YIELD
0229 {
0230 BOOST_ASIO_HANDLER_LOCATION((__FILE__, __LINE__, "websocket::async_accept"));
0231 http::async_write(
0232 impl.stream(), res_100_, std::move(*this));
0233 }
0234 }
0235
0236
0237 BOOST_ASIO_CORO_YIELD
0238 {
0239 BOOST_ASIO_HANDLER_LOCATION((
0240 __FILE__, __LINE__,
0241 "websocket::async_accept"));
0242
0243 http::async_write(
0244 impl.stream(), res_, std::move(*this));
0245 }
0246 if(impl.check_stop_now(ec))
0247 goto upcall;
0248 if(! ec)
0249 {
0250 BOOST_BEAST_ASSIGN_EC(ec, result_);
0251 BOOST_BEAST_ASSIGN_EC(ec, result_);
0252 }
0253 if(! ec)
0254 {
0255 impl.do_pmd_config(res_);
0256 impl.open(role_type::server);
0257 }
0258 upcall:
0259 this->complete(cont, ec);
0260 }
0261 }
0262 };
0263
0264
0265
0266
0267
0268
0269
0270
0271 template<class NextLayer, bool deflateSupported>
0272 template<class Handler, class Decorator>
0273 class stream<NextLayer, deflateSupported>::accept_op
0274 : public beast::stable_async_base<
0275 Handler, beast::executor_type<stream>>
0276 , public asio::coroutine
0277 {
0278 boost::weak_ptr<impl_type> wp_;
0279 http::request_parser<http::empty_body>& p_;
0280 Decorator d_;
0281
0282 public:
0283 template<class Handler_, class Buffers>
0284 accept_op(
0285 Handler_&& h,
0286 boost::shared_ptr<impl_type> const& sp,
0287 Decorator const& decorator,
0288 Buffers const& buffers)
0289 : stable_async_base<Handler,
0290 beast::executor_type<stream>>(
0291 std::forward<Handler_>(h),
0292 sp->stream().get_executor())
0293 , wp_(sp)
0294 , p_(beast::allocate_stable<
0295 http::request_parser<http::empty_body>>(*this))
0296 , d_(decorator)
0297 {
0298 auto& impl = *sp;
0299 error_code ec;
0300 auto const mb =
0301 beast::detail::dynamic_buffer_prepare(
0302 impl.rd_buf, buffer_bytes(buffers),
0303 ec, error::buffer_overflow);
0304 if(! ec)
0305 impl.rd_buf.commit(
0306 net::buffer_copy(*mb, buffers));
0307 (*this)(ec);
0308 }
0309
0310 void operator()(
0311 error_code ec = {},
0312 std::size_t bytes_transferred = 0,
0313 bool cont = true)
0314 {
0315 boost::ignore_unused(bytes_transferred);
0316 auto sp = wp_.lock();
0317 if(! sp)
0318 {
0319 BOOST_BEAST_ASSIGN_EC(ec, net::error::operation_aborted);
0320 return this->complete(cont, ec);
0321 }
0322 auto& impl = *sp;
0323 BOOST_ASIO_CORO_REENTER(*this)
0324 {
0325 impl.change_status(status::handshake);
0326 impl.update_timer(this->get_executor());
0327
0328
0329 if(ec)
0330 goto upcall;
0331
0332 BOOST_ASIO_CORO_YIELD
0333 {
0334 BOOST_ASIO_HANDLER_LOCATION((
0335 __FILE__, __LINE__,
0336 "websocket::async_accept"));
0337
0338 http::async_read(impl.stream(),
0339 impl.rd_buf, p_, std::move(*this));
0340 }
0341 if(ec == http::error::end_of_stream)
0342 {
0343 BOOST_BEAST_ASSIGN_EC(ec, error::closed);
0344 }
0345 if(impl.check_stop_now(ec))
0346 goto upcall;
0347
0348 {
0349
0350
0351
0352 auto const req = p_.release();
0353 auto const decorator = d_;
0354
0355 response_op<Handler>(
0356 this->release_handler(),
0357 sp, req, decorator, true);
0358 return;
0359 }
0360
0361 upcall:
0362 this->complete(cont, ec);
0363 }
0364 }
0365 };
0366
0367 template<class NextLayer, bool deflateSupported>
0368 struct stream<NextLayer, deflateSupported>::
0369 run_response_op
0370 {
0371 template<
0372 class AcceptHandler,
0373 class Body, class Allocator,
0374 class Decorator>
0375 void
0376 operator()(
0377 AcceptHandler&& h,
0378 boost::shared_ptr<impl_type> const& sp,
0379 http::request<Body,
0380 http::basic_fields<Allocator>> const* m,
0381 Decorator const& d)
0382 {
0383
0384
0385
0386
0387 static_assert(
0388 beast::detail::is_invocable<AcceptHandler,
0389 void(error_code)>::value,
0390 "AcceptHandler type requirements not met");
0391
0392 response_op<
0393 typename std::decay<AcceptHandler>::type>(
0394 std::forward<AcceptHandler>(h), sp, *m, d);
0395 }
0396 };
0397
0398 template<class NextLayer, bool deflateSupported>
0399 struct stream<NextLayer, deflateSupported>::
0400 run_accept_op
0401 {
0402 template<
0403 class AcceptHandler,
0404 class Decorator,
0405 class Buffers>
0406 void
0407 operator()(
0408 AcceptHandler&& h,
0409 boost::shared_ptr<impl_type> const& sp,
0410 Decorator const& d,
0411 Buffers const& b)
0412 {
0413
0414
0415
0416
0417 static_assert(
0418 beast::detail::is_invocable<AcceptHandler,
0419 void(error_code)>::value,
0420 "AcceptHandler type requirements not met");
0421
0422 accept_op<
0423 typename std::decay<AcceptHandler>::type,
0424 Decorator>(
0425 std::forward<AcceptHandler>(h),
0426 sp,
0427 d,
0428 b);
0429 }
0430 };
0431
0432
0433
0434 template<class NextLayer, bool deflateSupported>
0435 template<class Body, class Allocator,
0436 class Decorator>
0437 void
0438 stream<NextLayer, deflateSupported>::
0439 do_accept(
0440 http::request<Body,
0441 http::basic_fields<Allocator>> const& req,
0442 Decorator const& decorator,
0443 error_code& ec)
0444 {
0445 impl_->change_status(status::handshake);
0446
0447 error_code result;
0448 auto const res = impl_->build_response(req, decorator, result);
0449
0450 auto itr = req.find(http::field::expect);
0451 if (itr != req.end() && iequals(itr->value(), "100-continue"))
0452 {
0453 http::response<http::empty_body> res_100;
0454 res_100.version(res.version());
0455 res_100.set(http::field::server, res[http::field::server]);
0456 res_100.result(http::status::continue_);
0457 res_100.prepare_payload();
0458 http::write(impl_->stream(), res_100, ec);
0459 if (ec)
0460 return;
0461 }
0462
0463 http::write(impl_->stream(), res, ec);
0464 if(ec)
0465 return;
0466 BOOST_BEAST_ASSIGN_EC(ec, result);
0467 if(ec)
0468 {
0469
0470
0471 return;
0472 }
0473 impl_->do_pmd_config(res);
0474 impl_->open(role_type::server);
0475 }
0476
0477 template<class NextLayer, bool deflateSupported>
0478 template<class Buffers, class Decorator>
0479 void
0480 stream<NextLayer, deflateSupported>::
0481 do_accept(
0482 Buffers const& buffers,
0483 Decorator const& decorator,
0484 error_code& ec)
0485 {
0486 impl_->reset();
0487 auto const mb =
0488 beast::detail::dynamic_buffer_prepare(
0489 impl_->rd_buf, buffer_bytes(buffers), ec,
0490 error::buffer_overflow);
0491 if(ec)
0492 return;
0493 impl_->rd_buf.commit(net::buffer_copy(*mb, buffers));
0494
0495 http::request_parser<http::empty_body> p;
0496 http::read(next_layer(), impl_->rd_buf, p, ec);
0497 if(ec == http::error::end_of_stream)
0498 {
0499 BOOST_BEAST_ASSIGN_EC(ec, error::closed);
0500 }
0501 if(ec)
0502 return;
0503 do_accept(p.get(), decorator, ec);
0504 }
0505
0506
0507
0508 template<class NextLayer, bool deflateSupported>
0509 void
0510 stream<NextLayer, deflateSupported>::
0511 accept()
0512 {
0513 static_assert(is_sync_stream<next_layer_type>::value,
0514 "SyncStream type requirements not met");
0515 error_code ec;
0516 accept(ec);
0517 if(ec)
0518 BOOST_THROW_EXCEPTION(system_error{ec});
0519 }
0520
0521 template<class NextLayer, bool deflateSupported>
0522 void
0523 stream<NextLayer, deflateSupported>::
0524 accept(error_code& ec)
0525 {
0526 static_assert(is_sync_stream<next_layer_type>::value,
0527 "SyncStream type requirements not met");
0528 do_accept(
0529 net::const_buffer{},
0530 &default_decorate_res, ec);
0531 }
0532
0533 template<class NextLayer, bool deflateSupported>
0534 template<class ConstBufferSequence>
0535 typename std::enable_if<! http::detail::is_header<
0536 ConstBufferSequence>::value>::type
0537 stream<NextLayer, deflateSupported>::
0538 accept(ConstBufferSequence const& buffers)
0539 {
0540 static_assert(is_sync_stream<next_layer_type>::value,
0541 "SyncStream type requirements not met");
0542 static_assert(net::is_const_buffer_sequence<
0543 ConstBufferSequence>::value,
0544 "ConstBufferSequence type requirements not met");
0545 error_code ec;
0546 accept(buffers, ec);
0547 if(ec)
0548 BOOST_THROW_EXCEPTION(system_error{ec});
0549 }
0550 template<class NextLayer, bool deflateSupported>
0551 template<class ConstBufferSequence>
0552 typename std::enable_if<! http::detail::is_header<
0553 ConstBufferSequence>::value>::type
0554 stream<NextLayer, deflateSupported>::
0555 accept(
0556 ConstBufferSequence const& buffers, error_code& ec)
0557 {
0558 static_assert(is_sync_stream<next_layer_type>::value,
0559 "SyncStream type requirements not met");
0560 static_assert(net::is_const_buffer_sequence<
0561 ConstBufferSequence>::value,
0562 "ConstBufferSequence type requirements not met");
0563 do_accept(buffers, &default_decorate_res, ec);
0564 }
0565
0566
0567 template<class NextLayer, bool deflateSupported>
0568 template<class Body, class Allocator>
0569 void
0570 stream<NextLayer, deflateSupported>::
0571 accept(
0572 http::request<Body,
0573 http::basic_fields<Allocator>> const& req)
0574 {
0575 static_assert(is_sync_stream<next_layer_type>::value,
0576 "SyncStream type requirements not met");
0577 error_code ec;
0578 accept(req, ec);
0579 if(ec)
0580 BOOST_THROW_EXCEPTION(system_error{ec});
0581 }
0582
0583 template<class NextLayer, bool deflateSupported>
0584 template<class Body, class Allocator>
0585 void
0586 stream<NextLayer, deflateSupported>::
0587 accept(
0588 http::request<Body,
0589 http::basic_fields<Allocator>> const& req,
0590 error_code& ec)
0591 {
0592 static_assert(is_sync_stream<next_layer_type>::value,
0593 "SyncStream type requirements not met");
0594 impl_->reset();
0595 do_accept(req, &default_decorate_res, ec);
0596 }
0597
0598
0599
0600 template<class NextLayer, bool deflateSupported>
0601 template<
0602 BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler>
0603 BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
0604 stream<NextLayer, deflateSupported>::
0605 async_accept(
0606 AcceptHandler&& handler)
0607 {
0608 static_assert(is_async_stream<next_layer_type>::value,
0609 "AsyncStream type requirements not met");
0610 impl_->reset();
0611 return net::async_initiate<
0612 AcceptHandler,
0613 void(error_code)>(
0614 run_accept_op{},
0615 handler,
0616 impl_,
0617 &default_decorate_res,
0618 net::const_buffer{});
0619 }
0620
0621 template<class NextLayer, bool deflateSupported>
0622 template<
0623 class ConstBufferSequence,
0624 BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler>
0625 BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
0626 stream<NextLayer, deflateSupported>::
0627 async_accept(
0628 ConstBufferSequence const& buffers,
0629 AcceptHandler&& handler,
0630 typename std::enable_if<
0631 ! http::detail::is_header<
0632 ConstBufferSequence>::value>::type*
0633 )
0634 {
0635 static_assert(is_async_stream<next_layer_type>::value,
0636 "AsyncStream type requirements not met");
0637 static_assert(net::is_const_buffer_sequence<
0638 ConstBufferSequence>::value,
0639 "ConstBufferSequence type requirements not met");
0640 impl_->reset();
0641 return net::async_initiate<
0642 AcceptHandler,
0643 void(error_code)>(
0644 run_accept_op{},
0645 handler,
0646 impl_,
0647 &default_decorate_res,
0648 buffers);
0649 }
0650
0651 template<class NextLayer, bool deflateSupported>
0652 template<
0653 class Body, class Allocator,
0654 BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler>
0655 BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
0656 stream<NextLayer, deflateSupported>::
0657 async_accept(
0658 http::request<Body, http::basic_fields<Allocator>> const& req,
0659 AcceptHandler&& handler)
0660 {
0661 static_assert(is_async_stream<next_layer_type>::value,
0662 "AsyncStream type requirements not met");
0663 impl_->reset();
0664 return net::async_initiate<
0665 AcceptHandler,
0666 void(error_code)>(
0667 run_response_op{},
0668 handler,
0669 impl_,
0670 &req,
0671 &default_decorate_res);
0672 }
0673
0674 }
0675 }
0676 }
0677
0678 #endif