Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:36:08

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
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 } // detail
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 /** Respond to an HTTP request
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_; // must come before res_
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")) // do
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             // Send response
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 // read and respond to an upgrade request
0267 //
0268 // Cancellation: the async_accept cancellation can be terminal
0269 // because it will just interrupt the reading of the header.
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         impl.reset();
0300         error_code ec;
0301         auto const mb =
0302             beast::detail::dynamic_buffer_prepare(
0303             impl.rd_buf, buffer_bytes(buffers),
0304                 ec, error::buffer_overflow);
0305         if(! ec)
0306             impl.rd_buf.commit(
0307                 net::buffer_copy(*mb, buffers));
0308         (*this)(ec);
0309     }
0310 
0311     void operator()(
0312         error_code ec = {},
0313         std::size_t bytes_transferred = 0,
0314         bool cont = true)
0315     {
0316         boost::ignore_unused(bytes_transferred);
0317         auto sp = wp_.lock();
0318         if(! sp)
0319         {
0320             BOOST_BEAST_ASSIGN_EC(ec, net::error::operation_aborted);
0321             return this->complete(cont, ec);
0322         }
0323         auto& impl = *sp;
0324         BOOST_ASIO_CORO_REENTER(*this)
0325         {
0326             impl.change_status(status::handshake);
0327             impl.update_timer(this->get_executor());
0328 
0329             // The constructor could have set ec
0330             if(ec)
0331                 goto upcall;
0332 
0333             BOOST_ASIO_CORO_YIELD
0334             {
0335                 BOOST_ASIO_HANDLER_LOCATION((
0336                     __FILE__, __LINE__,
0337                     "websocket::async_accept"));
0338 
0339                 http::async_read(impl.stream(),
0340                     impl.rd_buf, p_, std::move(*this));
0341             }
0342             if(ec == http::error::end_of_stream)
0343             {
0344                 BOOST_BEAST_ASSIGN_EC(ec, error::closed);
0345             }
0346             if(impl.check_stop_now(ec))
0347                 goto upcall;
0348 
0349             {
0350                 // Arguments from our state must be
0351                 // moved to the stack before releasing
0352                 // the handler.
0353                 auto const req = p_.release();
0354                 auto const decorator = d_;
0355 
0356                 response_op<Handler>(
0357                     this->release_handler(),
0358                         sp, req, decorator, true);
0359                 return;
0360             }
0361 
0362         upcall:
0363             this->complete(cont, ec);
0364         }
0365     }
0366 };
0367 
0368 template<class NextLayer, bool deflateSupported>
0369 struct stream<NextLayer, deflateSupported>::
0370     run_response_op
0371 {
0372     boost::shared_ptr<impl_type> const& self;
0373 
0374     using executor_type = typename stream::executor_type;
0375 
0376     executor_type
0377     get_executor() const noexcept
0378     {
0379         return self->stream().get_executor();
0380     }
0381 
0382     template<
0383         class AcceptHandler,
0384         class Body, class Allocator,
0385         class Decorator>
0386     void
0387     operator()(
0388         AcceptHandler&& h,
0389         http::request<Body,
0390             http::basic_fields<Allocator>> const* m,
0391         Decorator const& d)
0392     {
0393         // If you get an error on the following line it means
0394         // that your handler does not meet the documented type
0395         // requirements for the handler.
0396 
0397         static_assert(
0398             beast::detail::is_invocable<AcceptHandler,
0399                 void(error_code)>::value,
0400             "AcceptHandler type requirements not met");
0401 
0402         response_op<
0403             typename std::decay<AcceptHandler>::type>(
0404                 std::forward<AcceptHandler>(h), self, *m, d);
0405     }
0406 };
0407 
0408 template<class NextLayer, bool deflateSupported>
0409 struct stream<NextLayer, deflateSupported>::
0410     run_accept_op
0411 {
0412     boost::shared_ptr<impl_type> const& self;
0413 
0414     using executor_type = typename stream::executor_type;
0415 
0416     executor_type
0417     get_executor() const noexcept
0418     {
0419         return self->stream().get_executor();
0420     }
0421 
0422     template<
0423         class AcceptHandler,
0424         class Decorator,
0425         class Buffers>
0426     void
0427     operator()(
0428         AcceptHandler&& h,
0429         Decorator const& d,
0430         Buffers const& b)
0431     {
0432         // If you get an error on the following line it means
0433         // that your handler does not meet the documented type
0434         // requirements for the handler.
0435 
0436         static_assert(
0437             beast::detail::is_invocable<AcceptHandler,
0438                 void(error_code)>::value,
0439             "AcceptHandler type requirements not met");
0440 
0441         accept_op<
0442             typename std::decay<AcceptHandler>::type,
0443             Decorator>(
0444                 std::forward<AcceptHandler>(h),
0445                 self,
0446                 d,
0447                 b);
0448     }
0449 };
0450 
0451 //------------------------------------------------------------------------------
0452 
0453 template<class NextLayer, bool deflateSupported>
0454 template<class Body, class Allocator,
0455     class Decorator>
0456 void
0457 stream<NextLayer, deflateSupported>::
0458 do_accept(
0459     http::request<Body,
0460         http::basic_fields<Allocator>> const& req,
0461     Decorator const& decorator,
0462     error_code& ec)
0463 {
0464     impl_->change_status(status::handshake);
0465 
0466     error_code result;
0467     auto const res = impl_->build_response(req, decorator, result);
0468 
0469     auto itr = req.find(http::field::expect);
0470     if (itr != req.end() && iequals(itr->value(), "100-continue")) // do
0471     {
0472         http::response<http::empty_body> res_100;
0473         res_100.version(res.version());
0474         res_100.set(http::field::server, res[http::field::server]);
0475         res_100.result(http::status::continue_);
0476         res_100.prepare_payload();
0477         http::write(impl_->stream(), res_100, ec);
0478         if (ec)
0479             return;
0480     }
0481 
0482     http::write(impl_->stream(), res, ec);
0483     if(ec)
0484         return;
0485     BOOST_BEAST_ASSIGN_EC(ec, result);
0486     if(ec)
0487     {
0488         // VFALCO TODO Respect keep alive setting, perform
0489         //             teardown if Connection: close.
0490         return;
0491     }
0492     impl_->do_pmd_config(res);
0493     impl_->open(role_type::server);
0494 }
0495 
0496 template<class NextLayer, bool deflateSupported>
0497 template<class Buffers, class Decorator>
0498 void
0499 stream<NextLayer, deflateSupported>::
0500 do_accept(
0501     Buffers const& buffers,
0502     Decorator const& decorator,
0503     error_code& ec)
0504 {
0505     impl_->reset();
0506     auto const mb =
0507         beast::detail::dynamic_buffer_prepare(
0508         impl_->rd_buf, buffer_bytes(buffers), ec,
0509             error::buffer_overflow);
0510     if(ec)
0511         return;
0512     impl_->rd_buf.commit(net::buffer_copy(*mb, buffers));
0513 
0514     http::request_parser<http::empty_body> p;
0515     http::read(next_layer(), impl_->rd_buf, p, ec);
0516     if(ec == http::error::end_of_stream)
0517     {
0518         BOOST_BEAST_ASSIGN_EC(ec, error::closed);
0519     }
0520     if(ec)
0521         return;
0522     do_accept(p.get(), decorator, ec);
0523 }
0524 
0525 //------------------------------------------------------------------------------
0526 
0527 template<class NextLayer, bool deflateSupported>
0528 void
0529 stream<NextLayer, deflateSupported>::
0530 accept()
0531 {
0532     static_assert(is_sync_stream<next_layer_type>::value,
0533         "SyncStream type requirements not met");
0534     error_code ec;
0535     accept(ec);
0536     if(ec)
0537         BOOST_THROW_EXCEPTION(system_error{ec});
0538 }
0539 
0540 template<class NextLayer, bool deflateSupported>
0541 void
0542 stream<NextLayer, deflateSupported>::
0543 accept(error_code& ec)
0544 {
0545     static_assert(is_sync_stream<next_layer_type>::value,
0546         "SyncStream type requirements not met");
0547     do_accept(
0548         net::const_buffer{},
0549         &default_decorate_res, ec);
0550 }
0551 
0552 template<class NextLayer, bool deflateSupported>
0553 template<class ConstBufferSequence>
0554 typename std::enable_if<! http::detail::is_header<
0555     ConstBufferSequence>::value>::type
0556 stream<NextLayer, deflateSupported>::
0557 accept(ConstBufferSequence const& buffers)
0558 {
0559     static_assert(is_sync_stream<next_layer_type>::value,
0560         "SyncStream type requirements not met");
0561     static_assert(net::is_const_buffer_sequence<
0562         ConstBufferSequence>::value,
0563             "ConstBufferSequence type requirements not met");
0564     error_code ec;
0565     accept(buffers, ec);
0566     if(ec)
0567         BOOST_THROW_EXCEPTION(system_error{ec});
0568 }
0569 template<class NextLayer, bool deflateSupported>
0570 template<class ConstBufferSequence>
0571 typename std::enable_if<! http::detail::is_header<
0572     ConstBufferSequence>::value>::type
0573 stream<NextLayer, deflateSupported>::
0574 accept(
0575     ConstBufferSequence const& buffers, error_code& ec)
0576 {
0577     static_assert(is_sync_stream<next_layer_type>::value,
0578         "SyncStream type requirements not met");
0579     static_assert(net::is_const_buffer_sequence<
0580         ConstBufferSequence>::value,
0581             "ConstBufferSequence type requirements not met");
0582     do_accept(buffers, &default_decorate_res, ec);
0583 }
0584 
0585 
0586 template<class NextLayer, bool deflateSupported>
0587 template<class Body, class Allocator>
0588 void
0589 stream<NextLayer, deflateSupported>::
0590 accept(
0591     http::request<Body,
0592         http::basic_fields<Allocator>> const& req)
0593 {
0594     static_assert(is_sync_stream<next_layer_type>::value,
0595         "SyncStream type requirements not met");
0596     error_code ec;
0597     accept(req, ec);
0598     if(ec)
0599         BOOST_THROW_EXCEPTION(system_error{ec});
0600 }
0601 
0602 template<class NextLayer, bool deflateSupported>
0603 template<class Body, class Allocator>
0604 void
0605 stream<NextLayer, deflateSupported>::
0606 accept(
0607     http::request<Body,
0608         http::basic_fields<Allocator>> const& req,
0609     error_code& ec)
0610 {
0611     static_assert(is_sync_stream<next_layer_type>::value,
0612         "SyncStream type requirements not met");
0613     impl_->reset();
0614     do_accept(req, &default_decorate_res, ec);
0615 }
0616 
0617 //------------------------------------------------------------------------------
0618 
0619 template<class NextLayer, bool deflateSupported>
0620 template<
0621     BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler>
0622 BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
0623 stream<NextLayer, deflateSupported>::
0624 async_accept(
0625     AcceptHandler&& handler,
0626     typename std::enable_if<
0627         ! net::is_const_buffer_sequence<
0628         AcceptHandler>::value>::type*
0629 )
0630 {
0631     static_assert(is_async_stream<next_layer_type>::value,
0632         "AsyncStream type requirements not met");
0633     return net::async_initiate<
0634         AcceptHandler,
0635         void(error_code)>(
0636             run_accept_op{impl_},
0637             handler,
0638             &default_decorate_res,
0639             net::const_buffer{});
0640 }
0641 
0642 template<class NextLayer, bool deflateSupported>
0643 template<
0644     class ConstBufferSequence,
0645     BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler>
0646 BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
0647 stream<NextLayer, deflateSupported>::
0648 async_accept(
0649     ConstBufferSequence const& buffers,
0650     AcceptHandler&& handler,
0651     typename std::enable_if<
0652         net::is_const_buffer_sequence<
0653         ConstBufferSequence>::value>::type*,
0654     typename std::enable_if<
0655         ! http::detail::is_header<
0656         ConstBufferSequence>::value>::type*
0657 )
0658 {
0659     static_assert(is_async_stream<next_layer_type>::value,
0660         "AsyncStream type requirements not met");
0661     static_assert(net::is_const_buffer_sequence<
0662         ConstBufferSequence>::value,
0663             "ConstBufferSequence type requirements not met");
0664     return net::async_initiate<
0665         AcceptHandler,
0666         void(error_code)>(
0667             run_accept_op{impl_},
0668             handler,
0669             &default_decorate_res,
0670             buffers);
0671 }
0672 
0673 template<class NextLayer, bool deflateSupported>
0674 template<
0675     class Body, class Allocator,
0676     BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler>
0677 BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
0678 stream<NextLayer, deflateSupported>::
0679 async_accept(
0680     http::request<Body, http::basic_fields<Allocator>> const& req,
0681     AcceptHandler&& handler)
0682 {
0683     static_assert(is_async_stream<next_layer_type>::value,
0684         "AsyncStream type requirements not met");
0685     return net::async_initiate<
0686         AcceptHandler,
0687         void(error_code)>(
0688             run_response_op{impl_},
0689             handler,
0690             &req,
0691             &default_decorate_res);
0692 }
0693 
0694 } // websocket
0695 } // beast
0696 } // boost
0697 
0698 #endif