| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- //
- // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- // Official repository: https://github.com/boostorg/beast
- //
- #ifndef BOOST_BEAST_WEBSOCKET_IMPL_HANDSHAKE_IPP
- #define BOOST_BEAST_WEBSOCKET_IMPL_HANDSHAKE_IPP
- #include <boost/beast/websocket/detail/type_traits.hpp>
- #include <boost/beast/http/empty_body.hpp>
- #include <boost/beast/http/message.hpp>
- #include <boost/beast/http/read.hpp>
- #include <boost/beast/http/write.hpp>
- #include <boost/beast/core/handler_ptr.hpp>
- #include <boost/beast/core/type_traits.hpp>
- #include <boost/asio/associated_allocator.hpp>
- #include <boost/asio/associated_executor.hpp>
- #include <boost/asio/coroutine.hpp>
- #include <boost/asio/executor_work_guard.hpp>
- #include <boost/asio/handler_continuation_hook.hpp>
- #include <boost/asio/handler_invoke_hook.hpp>
- #include <boost/assert.hpp>
- #include <boost/throw_exception.hpp>
- #include <memory>
- namespace boost {
- namespace beast {
- namespace websocket {
- //------------------------------------------------------------------------------
- // send the upgrade request and process the response
- //
- template<class NextLayer, bool deflateSupported>
- template<class Handler>
- class stream<NextLayer, deflateSupported>::handshake_op
- : public boost::asio::coroutine
- {
- struct data
- {
- stream<NextLayer, deflateSupported>& ws;
- boost::asio::executor_work_guard<decltype(std::declval<
- stream<NextLayer, deflateSupported>&>().get_executor())> wg;
- response_type* res_p;
- detail::sec_ws_key_type key;
- http::request<http::empty_body> req;
- response_type res;
- template<class Decorator>
- data(
- Handler const&,
- stream<NextLayer, deflateSupported>& ws_,
- response_type* res_p_,
- string_view host,
- string_view target,
- Decorator const& decorator)
- : ws(ws_)
- , wg(ws.get_executor())
- , res_p(res_p_)
- , req(ws.build_request(key,
- host, target, decorator))
- {
- ws.reset();
- }
- };
- handler_ptr<data, Handler> d_;
- public:
- handshake_op(handshake_op&&) = default;
- handshake_op(handshake_op const&) = delete;
- template<class DeducedHandler, class... Args>
- handshake_op(DeducedHandler&& h,
- stream<NextLayer, deflateSupported>& ws, Args&&... args)
- : d_(std::forward<DeducedHandler>(h),
- ws, std::forward<Args>(args)...)
- {
- }
- using allocator_type =
- boost::asio::associated_allocator_t<Handler>;
- allocator_type
- get_allocator() const noexcept
- {
- return (boost::asio::get_associated_allocator)(d_.handler());
- }
- using executor_type = boost::asio::associated_executor_t<
- Handler, decltype(std::declval<stream<NextLayer, deflateSupported>&>().get_executor())>;
- executor_type
- get_executor() const noexcept
- {
- return (boost::asio::get_associated_executor)(
- d_.handler(), d_->ws.get_executor());
- }
- void
- operator()(
- error_code ec = {},
- std::size_t bytes_used = 0);
- friend
- bool asio_handler_is_continuation(handshake_op* op)
- {
- using boost::asio::asio_handler_is_continuation;
- return asio_handler_is_continuation(
- std::addressof(op->d_.handler()));
- }
- template<class Function>
- friend
- void asio_handler_invoke(Function&& f, handshake_op* op)
- {
- using boost::asio::asio_handler_invoke;
- asio_handler_invoke(f,
- std::addressof(op->d_.handler()));
- }
- };
- template<class NextLayer, bool deflateSupported>
- template<class Handler>
- void
- stream<NextLayer, deflateSupported>::
- handshake_op<Handler>::
- operator()(error_code ec, std::size_t)
- {
- auto& d = *d_;
- BOOST_ASIO_CORO_REENTER(*this)
- {
- // Send HTTP Upgrade
- d.ws.do_pmd_config(d.req, is_deflate_supported{});
- BOOST_ASIO_CORO_YIELD
- http::async_write(d.ws.stream_,
- d.req, std::move(*this));
- if(ec)
- goto upcall;
- // VFALCO We could pre-serialize the request to
- // a single buffer, send that instead,
- // and delete the buffer here. The buffer
- // could be a variable block at the end
- // of handler_ptr's allocation.
- // Read HTTP response
- BOOST_ASIO_CORO_YIELD
- http::async_read(d.ws.next_layer(),
- d.ws.rd_buf_, d.res,
- std::move(*this));
- if(ec)
- goto upcall;
- d.ws.on_response(d.res, d.key, ec);
- if(d.res_p)
- swap(d.res, *d.res_p);
- upcall:
- {
- auto wg = std::move(d.wg);
- d_.invoke(ec);
- }
- }
- }
- template<class NextLayer, bool deflateSupported>
- template<class HandshakeHandler>
- BOOST_ASIO_INITFN_RESULT_TYPE(
- HandshakeHandler, void(error_code))
- stream<NextLayer, deflateSupported>::
- async_handshake(string_view host,
- string_view target,
- HandshakeHandler&& handler)
- {
- static_assert(is_async_stream<next_layer_type>::value,
- "AsyncStream requirements not met");
- BOOST_BEAST_HANDLER_INIT(
- HandshakeHandler, void(error_code));
- handshake_op<BOOST_ASIO_HANDLER_TYPE(
- HandshakeHandler, void(error_code))>{
- std::move(init.completion_handler), *this, nullptr, host,
- target, &default_decorate_req}();
- return init.result.get();
- }
- template<class NextLayer, bool deflateSupported>
- template<class HandshakeHandler>
- BOOST_ASIO_INITFN_RESULT_TYPE(
- HandshakeHandler, void(error_code))
- stream<NextLayer, deflateSupported>::
- async_handshake(response_type& res,
- string_view host,
- string_view target,
- HandshakeHandler&& handler)
- {
- static_assert(is_async_stream<next_layer_type>::value,
- "AsyncStream requirements not met");
- BOOST_BEAST_HANDLER_INIT(
- HandshakeHandler, void(error_code));
- handshake_op<BOOST_ASIO_HANDLER_TYPE(
- HandshakeHandler, void(error_code))>{
- std::move(init.completion_handler), *this, &res, host,
- target, &default_decorate_req}();
- return init.result.get();
- }
- template<class NextLayer, bool deflateSupported>
- template<class RequestDecorator, class HandshakeHandler>
- BOOST_ASIO_INITFN_RESULT_TYPE(
- HandshakeHandler, void(error_code))
- stream<NextLayer, deflateSupported>::
- async_handshake_ex(string_view host,
- string_view target,
- RequestDecorator const& decorator,
- HandshakeHandler&& handler)
- {
- static_assert(is_async_stream<next_layer_type>::value,
- "AsyncStream requirements not met");
- static_assert(detail::is_request_decorator<
- RequestDecorator>::value,
- "RequestDecorator requirements not met");
- BOOST_BEAST_HANDLER_INIT(
- HandshakeHandler, void(error_code));
- handshake_op<BOOST_ASIO_HANDLER_TYPE(
- HandshakeHandler, void(error_code))>{
- std::move(init.completion_handler), *this, nullptr, host,
- target, decorator}();
- return init.result.get();
- }
- template<class NextLayer, bool deflateSupported>
- template<class RequestDecorator, class HandshakeHandler>
- BOOST_ASIO_INITFN_RESULT_TYPE(
- HandshakeHandler, void(error_code))
- stream<NextLayer, deflateSupported>::
- async_handshake_ex(response_type& res,
- string_view host,
- string_view target,
- RequestDecorator const& decorator,
- HandshakeHandler&& handler)
- {
- static_assert(is_async_stream<next_layer_type>::value,
- "AsyncStream requirements not met");
- static_assert(detail::is_request_decorator<
- RequestDecorator>::value,
- "RequestDecorator requirements not met");
- BOOST_BEAST_HANDLER_INIT(
- HandshakeHandler, void(error_code));
- handshake_op<BOOST_ASIO_HANDLER_TYPE(
- HandshakeHandler, void(error_code))>{
- std::move(init.completion_handler), *this, &res, host,
- target, decorator}();
- return init.result.get();
- }
- template<class NextLayer, bool deflateSupported>
- void
- stream<NextLayer, deflateSupported>::
- handshake(string_view host,
- string_view target)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream requirements not met");
- error_code ec;
- handshake(
- host, target, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- }
- template<class NextLayer, bool deflateSupported>
- void
- stream<NextLayer, deflateSupported>::
- handshake(response_type& res,
- string_view host,
- string_view target)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream requirements not met");
- error_code ec;
- handshake(res, host, target, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- }
- template<class NextLayer, bool deflateSupported>
- template<class RequestDecorator>
- void
- stream<NextLayer, deflateSupported>::
- handshake_ex(string_view host,
- string_view target,
- RequestDecorator const& decorator)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream requirements not met");
- static_assert(detail::is_request_decorator<
- RequestDecorator>::value,
- "RequestDecorator requirements not met");
- error_code ec;
- handshake_ex(host, target, decorator, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- }
- template<class NextLayer, bool deflateSupported>
- template<class RequestDecorator>
- void
- stream<NextLayer, deflateSupported>::
- handshake_ex(response_type& res,
- string_view host,
- string_view target,
- RequestDecorator const& decorator)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream requirements not met");
- static_assert(detail::is_request_decorator<
- RequestDecorator>::value,
- "RequestDecorator requirements not met");
- error_code ec;
- handshake_ex(res, host, target, decorator, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- }
- template<class NextLayer, bool deflateSupported>
- void
- stream<NextLayer, deflateSupported>::
- handshake(string_view host,
- string_view target, error_code& ec)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream requirements not met");
- do_handshake(nullptr,
- host, target, &default_decorate_req, ec);
- }
- template<class NextLayer, bool deflateSupported>
- void
- stream<NextLayer, deflateSupported>::
- handshake(response_type& res,
- string_view host,
- string_view target,
- error_code& ec)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream requirements not met");
- do_handshake(&res,
- host, target, &default_decorate_req, ec);
- }
- template<class NextLayer, bool deflateSupported>
- template<class RequestDecorator>
- void
- stream<NextLayer, deflateSupported>::
- handshake_ex(string_view host,
- string_view target,
- RequestDecorator const& decorator,
- error_code& ec)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream requirements not met");
- static_assert(detail::is_request_decorator<
- RequestDecorator>::value,
- "RequestDecorator requirements not met");
- do_handshake(nullptr,
- host, target, decorator, ec);
- }
- template<class NextLayer, bool deflateSupported>
- template<class RequestDecorator>
- void
- stream<NextLayer, deflateSupported>::
- handshake_ex(response_type& res,
- string_view host,
- string_view target,
- RequestDecorator const& decorator,
- error_code& ec)
- {
- static_assert(is_sync_stream<next_layer_type>::value,
- "SyncStream requirements not met");
- static_assert(detail::is_request_decorator<
- RequestDecorator>::value,
- "RequestDecorator requirements not met");
- do_handshake(&res,
- host, target, decorator, ec);
- }
- //------------------------------------------------------------------------------
- template<class NextLayer, bool deflateSupported>
- template<class RequestDecorator>
- void
- stream<NextLayer, deflateSupported>::
- do_handshake(
- response_type* res_p,
- string_view host,
- string_view target,
- RequestDecorator const& decorator,
- error_code& ec)
- {
- response_type res;
- reset();
- detail::sec_ws_key_type key;
- {
- auto const req = build_request(
- key, host, target, decorator);
- do_pmd_config(req, is_deflate_supported{});
- http::write(stream_, req, ec);
- }
- if(ec)
- return;
- http::read(next_layer(), rd_buf_, res, ec);
- if(ec)
- return;
- on_response(res, key, ec);
- if(res_p)
- *res_p = std::move(res);
- }
- } // websocket
- } // beast
- } // boost
- #endif
|