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

411 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
2017-07-20 13:40:34 -07:00
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_HANDSHAKE_IPP
#define BOOST_BEAST_WEBSOCKET_IMPL_HANDSHAKE_IPP
2017-07-20 08:01:46 -07:00
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>
#include <boost/beast/core/handler_ptr.hpp>
#include <boost/beast/core/type_traits.hpp>
2017-09-07 07:39:52 -07:00
#include <boost/asio/associated_allocator.hpp>
#include <boost/asio/associated_executor.hpp>
2017-08-19 19:27:08 -07:00
#include <boost/asio/coroutine.hpp>
#include <boost/asio/handler_continuation_hook.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
2017-08-19 19:27:08 -07:00
: public boost::asio::coroutine
2017-07-20 08:01:46 -07:00
{
struct data
{
stream<NextLayer, deflateSupported>& ws;
2017-04-25 09:35:22 -07:00
response_type* res_p;
2017-04-29 15:50:05 -07:00
detail::sec_ws_key_type key;
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
2017-04-25 09:35:22 -07:00
template<class Decorator>
data(
Handler const&,
stream<NextLayer, deflateSupported>& ws_,
2017-04-25 09:35:22 -07:00
response_type* res_p_,
string_view host,
string_view target,
Decorator const& decorator)
: ws(ws_)
2017-04-25 09:35:22 -07:00
, res_p(res_p_)
, req(ws.build_request(key,
host, target, decorator))
2017-07-20 08:01:46 -07:00
{
2016-05-15 16:22:25 -04:00
ws.reset();
2017-07-20 08:01:46 -07:00
}
};
handler_ptr<data, Handler> d_;
2017-07-20 08:01:46 -07:00
public:
handshake_op(handshake_op&&) = default;
2017-12-02 12:59:30 +01:00
handshake_op(handshake_op const&) = delete;
2017-07-20 08:01:46 -07:00
template<class DeducedHandler, class... Args>
handshake_op(DeducedHandler&& h,
stream<NextLayer, deflateSupported>& ws, Args&&... args)
2017-01-29 19:46:17 -05:00
: d_(std::forward<DeducedHandler>(h),
ws, std::forward<Args>(args)...)
2017-07-20 08:01:46 -07:00
{
}
2017-09-07 07:39:52 -07:00
using allocator_type =
boost::asio::associated_allocator_t<Handler>;
allocator_type
get_allocator() const noexcept
{
2017-12-02 05:57:06 -08:00
return (boost::asio::get_associated_allocator)(d_.handler());
2017-09-07 07:39:52 -07:00
}
using executor_type = boost::asio::associated_executor_t<
Handler, decltype(std::declval<stream<NextLayer, deflateSupported>&>().get_executor())>;
2017-09-07 07:39:52 -07:00
2017-10-24 06:40:22 -07:00
executor_type
get_executor() const noexcept
2017-09-07 07:39:52 -07:00
{
2017-12-02 05:57:06 -08:00
return (boost::asio::get_associated_executor)(
2017-09-07 07:39:52 -07:00
d_.handler(), d_->ws.get_executor());
}
void
operator()(
error_code ec = {},
std::size_t bytes_used = 0);
2017-07-20 08:01:46 -07:00
friend
bool asio_handler_is_continuation(handshake_op* op)
{
2017-08-19 19:27:08 -07:00
using boost::asio::asio_handler_is_continuation;
return asio_handler_is_continuation(
std::addressof(op->d_.handler()));
2017-07-20 08:01:46 -07:00
}
};
template<class NextLayer, bool deflateSupported>
2017-07-20 08:01:46 -07:00
template<class Handler>
void
stream<NextLayer, deflateSupported>::
handshake_op<Handler>::
operator()(error_code ec, std::size_t)
2017-07-20 08:01:46 -07:00
{
auto& d = *d_;
2017-08-19 19:27:08 -07:00
BOOST_ASIO_CORO_REENTER(*this)
2017-07-20 08:01:46 -07:00
{
2017-08-19 19:27:08 -07:00
// Send HTTP Upgrade
d.ws.do_pmd_config(d.req, is_deflate_supported{});
2017-08-19 19:27:08 -07:00
BOOST_ASIO_CORO_YIELD
http::async_write(d.ws.stream_,
d.req, std::move(*this));
if(ec)
goto upcall;
2017-07-20 08:01:46 -07:00
2017-08-19 19:27:08 -07:00
// 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.
2017-07-20 08:01:46 -07:00
2017-08-19 19:27:08 -07:00
// Read HTTP response
BOOST_ASIO_CORO_YIELD
http::async_read(d.ws.next_layer(),
2017-08-26 15:18:02 -07:00
d.ws.rd_buf_, d.res,
2017-08-19 19:27:08 -07:00
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:
d_.invoke(ec);
2017-07-20 08:01:46 -07:00
}
}
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(
HandshakeHandler, void(error_code))>{
2017-12-02 12:59:30 +01:00
std::move(init.completion_handler), *this, nullptr, host,
2017-08-19 19:27:08 -07:00
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(
HandshakeHandler, void(error_code))>{
2017-12-02 12:59:30 +01:00
std::move(init.completion_handler), *this, &res, host,
2017-08-19 19:27:08 -07:00
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(
HandshakeHandler, void(error_code))>{
2017-12-02 12:59:30 +01:00
std::move(init.completion_handler), *this, nullptr, host,
2017-08-19 19:27:08 -07:00
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(
HandshakeHandler, void(error_code))>{
2017-12-02 12:59:30 +01:00
std::move(init.completion_handler), *this, &res, host,
2017-08-19 19:27:08 -07:00
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
}
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
2017-07-27 14:14:47 -07:00
template<class RequestDecorator>
void
stream<NextLayer, deflateSupported>::
2017-07-27 14:14:47 -07:00
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{});
2017-07-27 14:14:47 -07:00
http::write(stream_, req, ec);
}
if(ec)
return;
2017-08-26 15:18:02 -07:00
http::read(next_layer(), rd_buf_, res, ec);
2017-07-27 14:14:47 -07:00
if(ec)
return;
2017-08-02 13:25:53 -07:00
on_response(res, key, ec);
2017-07-27 14:14:47 -07:00
if(res_p)
*res_p = std::move(res);
}
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