Files
boost_beast/include/boost/beast/websocket/impl/handshake.hpp
T

386 lines
12 KiB
C++
Raw Normal View History

2017-07-20 08:01:46 -07:00
//
2017-07-24 09:42:36 -07:00
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-07-20 08:01:46 -07:00
//
// 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)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
2017-07-20 08:01:46 -07:00
2019-01-19 07:24:00 -08:00
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_HANDSHAKE_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_HANDSHAKE_HPP
2017-07-20 08:01:46 -07:00
2019-02-13 08:00:07 -08:00
#include <boost/beast/websocket/impl/stream_impl.hpp>
2017-07-20 13:40:34 -07:00
#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>
2019-01-07 13:35:19 -08:00
#include <boost/beast/core/async_op_base.hpp>
2019-02-04 21:52:54 -08:00
#include <boost/beast/core/stream_traits.hpp>
2017-08-19 19:27:08 -07:00
#include <boost/asio/coroutine.hpp>
2016-09-25 12:17:32 -04:00
#include <boost/assert.hpp>
2017-05-22 15:30:12 -07:00
#include <boost/throw_exception.hpp>
2017-07-20 08:01:46 -07:00
#include <memory>
2017-07-20 13:40:34 -07:00
namespace boost {
2017-07-20 08:01:46 -07:00
namespace beast {
namespace websocket {
2016-10-24 08:12:09 -04:00
//------------------------------------------------------------------------------
2017-07-20 08:01:46 -07:00
// send the upgrade request and process the response
//
template<class NextLayer, bool deflateSupported>
2017-07-20 08:01:46 -07:00
template<class Handler>
class stream<NextLayer, deflateSupported>::handshake_op
2019-01-07 13:35:19 -08:00
: public beast::stable_async_op_base<Handler,
2019-02-04 21:52:54 -08:00
beast::executor_type<stream>>
2019-01-05 19:40:46 -08:00
, public net::coroutine
2017-07-20 08:01:46 -07:00
{
struct data
{
2019-01-20 12:25:30 -08:00
// VFALCO This really should be two separate
// composed operations, to save on memory
2017-06-07 16:30:49 -07:00
http::request<http::empty_body> req;
2017-04-25 09:35:22 -07:00
response_type res;
2017-07-20 08:01:46 -07:00
};
2019-02-13 08:00:07 -08:00
boost::weak_ptr<impl_type> wp_;
2019-01-20 12:25:30 -08:00
detail::sec_ws_key_type key_;
response_type* res_p_;
2019-01-05 19:40:46 -08:00
data& d_;
2017-07-20 08:01:46 -07:00
public:
2019-01-20 12:25:30 -08:00
template<class Handler_, class Decorator>
2019-01-05 19:40:46 -08:00
handshake_op(
Handler_&& h,
2019-02-13 08:00:07 -08:00
boost::shared_ptr<impl_type> const& sp,
2019-01-20 12:25:30 -08:00
response_type* res_p,
string_view host, string_view target,
Decorator const& decorator)
2019-01-09 16:11:15 -08:00
: stable_async_op_base<Handler,
2019-02-04 21:52:54 -08:00
beast::executor_type<stream>>(
2019-02-13 08:00:07 -08:00
std::forward<Handler_>(h),
sp->stream.get_executor())
, wp_(sp)
2019-01-20 12:25:30 -08:00
, res_p_(res_p)
, d_(beast::allocate_stable<data>(*this))
2017-07-20 08:01:46 -07:00
{
2019-02-13 08:00:07 -08:00
d_.req = sp->build_request(
2019-01-20 12:25:30 -08:00
key_, host, target, decorator);
2019-02-13 08:00:07 -08:00
sp->reset(); // VFALCO I don't like this
2017-07-20 08:01:46 -07:00
}
void
operator()(
error_code ec = {},
2019-02-13 08:00:07 -08:00
std::size_t bytes_used = 0,
bool cont = true)
2017-07-20 08:01:46 -07:00
{
2019-01-05 19:40:46 -08:00
boost::ignore_unused(bytes_used);
2019-02-13 08:00:07 -08:00
auto sp = wp_.lock();
if(! sp)
return this->invoke(cont,
net::error::operation_aborted);
auto& impl = *sp;
2019-01-05 19:40:46 -08:00
BOOST_ASIO_CORO_REENTER(*this)
{
2019-02-13 08:00:07 -08:00
impl.change_status(status::handshake);
impl.update_timer(this->get_executor());
// write HTTP request
impl.do_pmd_config(d_.req);
2019-01-05 19:40:46 -08:00
BOOST_ASIO_CORO_YIELD
2019-02-13 08:00:07 -08:00
http::async_write(impl.stream,
2019-01-05 19:40:46 -08:00
d_.req, std::move(*this));
2019-02-13 08:00:07 -08:00
if(impl.check_stop_now(ec))
2019-01-05 19:40:46 -08:00
goto upcall;
2019-02-13 08:00:07 -08:00
// read HTTP response
2019-01-05 19:40:46 -08:00
BOOST_ASIO_CORO_YIELD
2019-02-13 08:00:07 -08:00
http::async_read(impl.stream,
impl.rd_buf, d_.res,
2019-01-05 19:40:46 -08:00
std::move(*this));
2019-02-13 08:00:07 -08:00
if(impl.check_stop_now(ec))
2019-01-05 19:40:46 -08:00
goto upcall;
2019-02-13 08:00:07 -08:00
// success
impl.reset_idle();
impl.on_response(d_.res, key_, ec);
2019-01-20 12:25:30 -08:00
if(res_p_)
swap(d_.res, *res_p_);
2019-02-13 08:00:07 -08:00
2019-01-05 19:40:46 -08:00
upcall:
2019-02-13 08:00:07 -08:00
this->invoke(cont ,ec);
}
2017-07-20 08:01:46 -07:00
}
2019-01-05 19:40:46 -08:00
};
2017-07-20 08:01:46 -07:00
2019-02-13 08:00:07 -08:00
//------------------------------------------------------------------------------
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;
impl_->change_status(status::handshake);
impl_->reset();
detail::sec_ws_key_type key;
{
auto const req = impl_->build_request(
key, host, target, decorator);
this->impl_->do_pmd_config(req);
http::write(impl_->stream, req, ec);
}
if(ec)
return;
http::read(next_layer(), impl_->rd_buf, res, ec);
if(ec)
return;
impl_->on_response(res, key, ec);
if(res_p)
*res_p = std::move(res);
}
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
2016-10-24 08:12:09 -04:00
template<class HandshakeHandler>
2017-09-07 07:39:52 -07:00
BOOST_ASIO_INITFN_RESULT_TYPE(
HandshakeHandler, void(error_code))
stream<NextLayer, deflateSupported>::
2017-06-04 10:52:28 -07:00
async_handshake(string_view host,
string_view target,
2017-04-25 09:35:22 -07:00
HandshakeHandler&& handler)
2016-10-24 08:12:09 -04:00
{
static_assert(is_async_stream<next_layer_type>::value,
2016-10-24 08:12:09 -04:00
"AsyncStream requirements not met");
2018-01-26 08:58:19 -08:00
BOOST_BEAST_HANDLER_INIT(
HandshakeHandler, void(error_code));
2017-09-07 07:39:52 -07:00
handshake_op<BOOST_ASIO_HANDLER_TYPE(
2019-02-13 08:00:07 -08:00
HandshakeHandler, void(error_code))>(
std::move(init.completion_handler),
impl_, nullptr, host, target,
&default_decorate_req)();
return init.result.get();
2017-04-25 09:35:22 -07:00
}
template<class NextLayer, bool deflateSupported>
2017-04-25 09:35:22 -07:00
template<class HandshakeHandler>
2017-09-07 07:39:52 -07:00
BOOST_ASIO_INITFN_RESULT_TYPE(
HandshakeHandler, void(error_code))
stream<NextLayer, deflateSupported>::
2017-04-25 09:35:22 -07:00
async_handshake(response_type& res,
2017-06-04 10:52:28 -07:00
string_view host,
string_view target,
2017-04-25 09:35:22 -07:00
HandshakeHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
2017-04-25 09:35:22 -07:00
"AsyncStream requirements not met");
2018-01-26 08:58:19 -08:00
BOOST_BEAST_HANDLER_INIT(
HandshakeHandler, void(error_code));
2017-09-07 07:39:52 -07:00
handshake_op<BOOST_ASIO_HANDLER_TYPE(
2019-02-13 08:00:07 -08:00
HandshakeHandler, void(error_code))>(
std::move(init.completion_handler),
impl_, &res, host, target,
&default_decorate_req)();
return init.result.get();
2017-04-25 09:35:22 -07:00
}
template<class NextLayer, bool deflateSupported>
2017-04-25 09:35:22 -07:00
template<class RequestDecorator, class HandshakeHandler>
2017-09-07 07:39:52 -07:00
BOOST_ASIO_INITFN_RESULT_TYPE(
HandshakeHandler, void(error_code))
stream<NextLayer, deflateSupported>::
2017-06-04 10:52:28 -07:00
async_handshake_ex(string_view host,
string_view target,
2017-04-25 09:35:22 -07:00
RequestDecorator const& decorator,
HandshakeHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
2017-04-25 09:35:22 -07:00
"AsyncStream requirements not met");
static_assert(detail::is_request_decorator<
2017-04-25 09:35:22 -07:00
RequestDecorator>::value,
"RequestDecorator requirements not met");
2018-01-26 08:58:19 -08:00
BOOST_BEAST_HANDLER_INIT(
HandshakeHandler, void(error_code));
2017-09-07 07:39:52 -07:00
handshake_op<BOOST_ASIO_HANDLER_TYPE(
2019-02-13 08:00:07 -08:00
HandshakeHandler, void(error_code))>(
std::move(init.completion_handler),
impl_, nullptr, host, target,
decorator)();
return init.result.get();
2017-04-25 09:35:22 -07:00
}
template<class NextLayer, bool deflateSupported>
2017-04-25 09:35:22 -07:00
template<class RequestDecorator, class HandshakeHandler>
2017-09-07 07:39:52 -07:00
BOOST_ASIO_INITFN_RESULT_TYPE(
HandshakeHandler, void(error_code))
stream<NextLayer, deflateSupported>::
2017-04-25 09:35:22 -07:00
async_handshake_ex(response_type& res,
2017-06-04 10:52:28 -07:00
string_view host,
string_view target,
2017-04-25 09:35:22 -07:00
RequestDecorator const& decorator,
HandshakeHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
2017-04-25 09:35:22 -07:00
"AsyncStream requirements not met");
static_assert(detail::is_request_decorator<
2017-04-25 09:35:22 -07:00
RequestDecorator>::value,
"RequestDecorator requirements not met");
2018-01-26 08:58:19 -08:00
BOOST_BEAST_HANDLER_INIT(
HandshakeHandler, void(error_code));
2017-09-07 07:39:52 -07:00
handshake_op<BOOST_ASIO_HANDLER_TYPE(
2019-02-13 08:00:07 -08:00
HandshakeHandler, void(error_code))>(
std::move(init.completion_handler),
impl_, &res, host, target,
decorator)();
return init.result.get();
2016-10-24 08:12:09 -04:00
}
template<class NextLayer, bool deflateSupported>
2016-10-24 08:12:09 -04:00
void
stream<NextLayer, deflateSupported>::
2017-06-04 10:52:28 -07:00
handshake(string_view host,
string_view target)
2016-10-24 08:12:09 -04:00
{
static_assert(is_sync_stream<next_layer_type>::value,
2016-10-24 08:12:09 -04:00
"SyncStream requirements not met");
error_code ec;
2017-04-25 09:35:22 -07:00
handshake(
host, target, ec);
2017-04-25 09:35:22 -07:00
if(ec)
2017-05-22 15:30:12 -07:00
BOOST_THROW_EXCEPTION(system_error{ec});
2017-04-25 09:35:22 -07:00
}
template<class NextLayer, bool deflateSupported>
2017-04-25 09:35:22 -07:00
void
stream<NextLayer, deflateSupported>::
2017-04-25 09:35:22 -07:00
handshake(response_type& res,
2017-06-04 10:52:28 -07:00
string_view host,
string_view target)
2017-04-25 09:35:22 -07:00
{
static_assert(is_sync_stream<next_layer_type>::value,
2017-04-25 09:35:22 -07:00
"SyncStream requirements not met");
error_code ec;
handshake(res, host, target, ec);
2017-04-25 09:35:22 -07:00
if(ec)
2017-05-22 15:30:12 -07:00
BOOST_THROW_EXCEPTION(system_error{ec});
2017-04-25 09:35:22 -07:00
}
template<class NextLayer, bool deflateSupported>
2017-04-25 09:35:22 -07:00
template<class RequestDecorator>
void
stream<NextLayer, deflateSupported>::
2017-06-04 10:52:28 -07:00
handshake_ex(string_view host,
string_view target,
2017-04-25 09:35:22 -07:00
RequestDecorator const& decorator)
{
static_assert(is_sync_stream<next_layer_type>::value,
2017-04-25 09:35:22 -07:00
"SyncStream requirements not met");
static_assert(detail::is_request_decorator<
2017-04-25 09:35:22 -07:00
RequestDecorator>::value,
"RequestDecorator requirements not met");
error_code ec;
handshake_ex(host, target, decorator, ec);
2017-04-25 09:35:22 -07:00
if(ec)
2017-05-22 15:30:12 -07:00
BOOST_THROW_EXCEPTION(system_error{ec});
2017-04-25 09:35:22 -07:00
}
template<class NextLayer, bool deflateSupported>
2017-04-25 09:35:22 -07:00
template<class RequestDecorator>
void
stream<NextLayer, deflateSupported>::
2017-04-25 09:35:22 -07:00
handshake_ex(response_type& res,
2017-06-04 10:52:28 -07:00
string_view host,
string_view target,
2017-04-25 09:35:22 -07:00
RequestDecorator const& decorator)
{
static_assert(is_sync_stream<next_layer_type>::value,
2017-04-25 09:35:22 -07:00
"SyncStream requirements not met");
static_assert(detail::is_request_decorator<
2017-04-25 09:35:22 -07:00
RequestDecorator>::value,
"RequestDecorator requirements not met");
error_code ec;
handshake_ex(res, host, target, decorator, ec);
2016-10-24 08:12:09 -04:00
if(ec)
2017-05-22 15:30:12 -07:00
BOOST_THROW_EXCEPTION(system_error{ec});
2016-10-24 08:12:09 -04:00
}
template<class NextLayer, bool deflateSupported>
2016-10-24 08:12:09 -04:00
void
stream<NextLayer, deflateSupported>::
2017-06-04 10:52:28 -07:00
handshake(string_view host,
string_view target, error_code& ec)
2016-10-24 08:12:09 -04:00
{
static_assert(is_sync_stream<next_layer_type>::value,
2016-10-24 08:12:09 -04:00
"SyncStream requirements not met");
2017-04-25 09:35:22 -07:00
do_handshake(nullptr,
host, target, &default_decorate_req, ec);
2017-04-25 09:35:22 -07:00
}
template<class NextLayer, bool deflateSupported>
2017-04-25 09:35:22 -07:00
void
stream<NextLayer, deflateSupported>::
2017-04-25 09:35:22 -07:00
handshake(response_type& res,
2017-06-04 10:52:28 -07:00
string_view host,
string_view target,
2017-04-25 09:35:22 -07:00
error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
2017-04-25 09:35:22 -07:00
"SyncStream requirements not met");
do_handshake(&res,
host, target, &default_decorate_req, ec);
2017-04-25 09:35:22 -07:00
}
template<class NextLayer, bool deflateSupported>
2017-04-25 09:35:22 -07:00
template<class RequestDecorator>
void
stream<NextLayer, deflateSupported>::
2017-06-04 10:52:28 -07:00
handshake_ex(string_view host,
string_view target,
2017-04-25 09:35:22 -07:00
RequestDecorator const& decorator,
error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
2017-04-25 09:35:22 -07:00
"SyncStream requirements not met");
static_assert(detail::is_request_decorator<
2017-04-25 09:35:22 -07:00
RequestDecorator>::value,
"RequestDecorator requirements not met");
do_handshake(nullptr,
host, target, decorator, ec);
2017-04-25 09:35:22 -07:00
}
template<class NextLayer, bool deflateSupported>
2017-04-25 09:35:22 -07:00
template<class RequestDecorator>
void
stream<NextLayer, deflateSupported>::
2017-04-25 09:35:22 -07:00
handshake_ex(response_type& res,
2017-06-04 10:52:28 -07:00
string_view host,
string_view target,
2017-04-25 09:35:22 -07:00
RequestDecorator const& decorator,
error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
2017-04-25 09:35:22 -07:00
"SyncStream requirements not met");
static_assert(detail::is_request_decorator<
2017-04-25 09:35:22 -07:00
RequestDecorator>::value,
"RequestDecorator requirements not met");
do_handshake(&res,
host, target, decorator, ec);
2016-10-24 08:12:09 -04:00
}
2017-07-20 08:01:46 -07:00
} // websocket
} // beast
2017-07-20 13:40:34 -07:00
} // boost
2017-07-20 08:01:46 -07:00
#endif