2016-10-24 08:12:09 -04:00
|
|
|
//
|
|
|
|
// Copyright (c) 2013-2016 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)
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef BEAST_WEBSOCKET_IMPL_ACCEPT_IPP
|
|
|
|
#define BEAST_WEBSOCKET_IMPL_ACCEPT_IPP
|
|
|
|
|
|
|
|
#include <beast/http/message.hpp>
|
|
|
|
#include <beast/http/parser_v1.hpp>
|
|
|
|
#include <beast/http/read.hpp>
|
|
|
|
#include <beast/http/string_body.hpp>
|
|
|
|
#include <beast/http/write.hpp>
|
2017-01-05 09:07:18 -05:00
|
|
|
#include <beast/core/handler_helpers.hpp>
|
2017-01-02 13:29:48 -05:00
|
|
|
#include <beast/core/handler_ptr.hpp>
|
2016-10-24 08:12:09 -04:00
|
|
|
#include <beast/core/prepare_buffers.hpp>
|
|
|
|
#include <beast/core/detail/type_traits.hpp>
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <memory>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace beast {
|
|
|
|
namespace websocket {
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Respond to an upgrade HTTP request
|
|
|
|
template<class NextLayer>
|
|
|
|
template<class Handler>
|
|
|
|
class stream<NextLayer>::response_op
|
|
|
|
{
|
|
|
|
struct data
|
|
|
|
{
|
2017-01-02 13:29:48 -05:00
|
|
|
bool cont;
|
2016-10-24 08:12:09 -04:00
|
|
|
stream<NextLayer>& ws;
|
2016-10-24 18:41:25 -04:00
|
|
|
http::response<http::string_body> res;
|
2016-10-24 08:12:09 -04:00
|
|
|
error_code final_ec;
|
|
|
|
int state = 0;
|
|
|
|
|
2017-01-02 13:29:48 -05:00
|
|
|
template<class Body, class Fields>
|
|
|
|
data(Handler&, stream<NextLayer>& ws_,
|
2016-11-10 05:34:49 -05:00
|
|
|
http::request<Body, Fields> const& req,
|
2016-10-24 08:12:09 -04:00
|
|
|
bool cont_)
|
2017-01-02 13:29:48 -05:00
|
|
|
: cont(cont_)
|
|
|
|
, ws(ws_)
|
2016-10-24 18:41:25 -04:00
|
|
|
, res(ws_.build_response(req))
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
|
|
|
// can't call stream::reset() here
|
|
|
|
// otherwise accept_op will malfunction
|
|
|
|
//
|
2016-10-24 18:41:25 -04:00
|
|
|
if(res.status != 101)
|
2016-10-24 08:12:09 -04:00
|
|
|
final_ec = error::handshake_failed;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-02 13:29:48 -05:00
|
|
|
handler_ptr<data, Handler> d_;
|
2016-10-24 08:12:09 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
response_op(response_op&&) = default;
|
|
|
|
response_op(response_op const&) = default;
|
|
|
|
|
|
|
|
template<class DeducedHandler, class... Args>
|
|
|
|
response_op(DeducedHandler&& h,
|
|
|
|
stream<NextLayer>& ws, Args&&... args)
|
2017-01-29 19:46:17 -05:00
|
|
|
: d_(std::forward<DeducedHandler>(h),
|
|
|
|
ws, std::forward<Args>(args)...)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
|
|
|
(*this)(error_code{}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(
|
|
|
|
error_code ec, bool again = true);
|
|
|
|
|
|
|
|
friend
|
|
|
|
void* asio_handler_allocate(
|
|
|
|
std::size_t size, response_op* op)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
allocate(size, op->d_.handler());
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
void asio_handler_deallocate(
|
|
|
|
void* p, std::size_t size, response_op* op)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
deallocate(p, size, op->d_.handler());
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
bool asio_handler_is_continuation(response_op* op)
|
|
|
|
{
|
|
|
|
return op->d_->cont;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Function>
|
|
|
|
friend
|
|
|
|
void asio_handler_invoke(Function&& f, response_op* op)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
invoke(f, op->d_.handler());
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
template<class Handler>
|
|
|
|
void
|
|
|
|
stream<NextLayer>::response_op<Handler>::
|
|
|
|
operator()(error_code ec, bool again)
|
|
|
|
{
|
|
|
|
auto& d = *d_;
|
|
|
|
d.cont = d.cont || again;
|
|
|
|
while(! ec && d.state != 99)
|
|
|
|
{
|
|
|
|
switch(d.state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// send response
|
|
|
|
d.state = 1;
|
|
|
|
http::async_write(d.ws.next_layer(),
|
2016-10-24 18:41:25 -04:00
|
|
|
d.res, std::move(*this));
|
2016-10-24 08:12:09 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
// sent response
|
|
|
|
case 1:
|
|
|
|
d.state = 99;
|
|
|
|
ec = d.final_ec;
|
|
|
|
if(! ec)
|
2016-10-24 18:41:25 -04:00
|
|
|
{
|
|
|
|
pmd_read(
|
|
|
|
d.ws.pmd_config_, d.res.fields);
|
2016-10-24 08:12:09 -04:00
|
|
|
d.ws.open(detail::role_type::server);
|
2016-10-24 18:41:25 -04:00
|
|
|
}
|
2016-10-24 08:12:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-02 13:29:48 -05:00
|
|
|
d_.invoke(ec);
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// read and respond to an upgrade request
|
|
|
|
//
|
|
|
|
template<class NextLayer>
|
|
|
|
template<class Handler>
|
|
|
|
class stream<NextLayer>::accept_op
|
|
|
|
{
|
|
|
|
struct data
|
|
|
|
{
|
2017-01-02 13:29:48 -05:00
|
|
|
bool cont;
|
2016-10-24 08:12:09 -04:00
|
|
|
stream<NextLayer>& ws;
|
|
|
|
http::request<http::string_body> req;
|
|
|
|
int state = 0;
|
|
|
|
|
2017-01-02 13:29:48 -05:00
|
|
|
template<class Buffers>
|
|
|
|
data(Handler& handler, stream<NextLayer>& ws_,
|
2016-10-24 08:12:09 -04:00
|
|
|
Buffers const& buffers)
|
2017-01-05 09:07:18 -05:00
|
|
|
: cont(beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
is_continuation(handler))
|
|
|
|
, ws(ws_)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
|
|
|
using boost::asio::buffer_copy;
|
|
|
|
using boost::asio::buffer_size;
|
|
|
|
ws.reset();
|
|
|
|
ws.stream_.buffer().commit(buffer_copy(
|
|
|
|
ws.stream_.buffer().prepare(
|
|
|
|
buffer_size(buffers)), buffers));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-02 13:29:48 -05:00
|
|
|
handler_ptr<data, Handler> d_;
|
2016-10-24 08:12:09 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
accept_op(accept_op&&) = default;
|
|
|
|
accept_op(accept_op const&) = default;
|
|
|
|
|
|
|
|
template<class DeducedHandler, class... Args>
|
|
|
|
accept_op(DeducedHandler&& h,
|
|
|
|
stream<NextLayer>& ws, Args&&... args)
|
2017-01-29 19:46:17 -05:00
|
|
|
: d_(std::forward<DeducedHandler>(h),
|
|
|
|
ws, std::forward<Args>(args)...)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
|
|
|
(*this)(error_code{}, 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(error_code const& ec)
|
|
|
|
{
|
|
|
|
(*this)(ec, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(error_code const& ec,
|
|
|
|
std::size_t bytes_transferred, bool again = true);
|
|
|
|
|
|
|
|
friend
|
|
|
|
void* asio_handler_allocate(
|
|
|
|
std::size_t size, accept_op* op)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
allocate(size, op->d_.handler());
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
void asio_handler_deallocate(
|
|
|
|
void* p, std::size_t size, accept_op* op)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
deallocate(p, size, op->d_.handler());
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
bool asio_handler_is_continuation(accept_op* op)
|
|
|
|
{
|
|
|
|
return op->d_->cont;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Function>
|
|
|
|
friend
|
|
|
|
void asio_handler_invoke(Function&& f, accept_op* op)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
invoke(f, op->d_.handler());
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
template<class Handler>
|
|
|
|
void
|
|
|
|
stream<NextLayer>::accept_op<Handler>::
|
|
|
|
operator()(error_code const& ec,
|
|
|
|
std::size_t bytes_transferred, bool again)
|
|
|
|
{
|
|
|
|
beast::detail::ignore_unused(bytes_transferred);
|
|
|
|
auto& d = *d_;
|
|
|
|
d.cont = d.cont || again;
|
|
|
|
while(! ec && d.state != 99)
|
|
|
|
{
|
|
|
|
switch(d.state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// read message
|
|
|
|
d.state = 1;
|
|
|
|
http::async_read(d.ws.next_layer(),
|
|
|
|
d.ws.stream_.buffer(), d.req,
|
|
|
|
std::move(*this));
|
|
|
|
return;
|
|
|
|
|
|
|
|
// got message
|
|
|
|
case 1:
|
2017-01-02 13:29:48 -05:00
|
|
|
{
|
2016-10-24 08:12:09 -04:00
|
|
|
// respond to request
|
2017-01-02 13:29:48 -05:00
|
|
|
auto& ws = d.ws;
|
|
|
|
auto req = std::move(d.req);
|
2016-10-24 08:12:09 -04:00
|
|
|
response_op<Handler>{
|
2017-01-02 13:29:48 -05:00
|
|
|
d_.release_handler(), ws, req, true};
|
2016-10-24 08:12:09 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-01-02 13:29:48 -05:00
|
|
|
}
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
2017-01-02 13:29:48 -05:00
|
|
|
d_.invoke(ec);
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
template<class AcceptHandler>
|
|
|
|
typename async_completion<
|
|
|
|
AcceptHandler, void(error_code)>::result_type
|
|
|
|
stream<NextLayer>::
|
|
|
|
async_accept(AcceptHandler&& handler)
|
|
|
|
{
|
|
|
|
static_assert(is_AsyncStream<next_layer_type>::value,
|
|
|
|
"AsyncStream requirements requirements not met");
|
|
|
|
return async_accept(boost::asio::null_buffers{},
|
|
|
|
std::forward<AcceptHandler>(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
template<class ConstBufferSequence, class AcceptHandler>
|
|
|
|
typename async_completion<
|
|
|
|
AcceptHandler, void(error_code)>::result_type
|
|
|
|
stream<NextLayer>::
|
|
|
|
async_accept(ConstBufferSequence const& bs, AcceptHandler&& handler)
|
|
|
|
{
|
|
|
|
static_assert(is_AsyncStream<next_layer_type>::value,
|
|
|
|
"AsyncStream requirements requirements not met");
|
|
|
|
static_assert(beast::is_ConstBufferSequence<
|
|
|
|
ConstBufferSequence>::value,
|
|
|
|
"ConstBufferSequence requirements not met");
|
|
|
|
beast::async_completion<
|
|
|
|
AcceptHandler, void(error_code)
|
|
|
|
> completion(handler);
|
|
|
|
accept_op<decltype(completion.handler)>{
|
|
|
|
completion.handler, *this, bs};
|
|
|
|
return completion.result.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NextLayer>
|
2016-11-10 05:34:49 -05:00
|
|
|
template<class Body, class Fields, class AcceptHandler>
|
2016-10-24 08:12:09 -04:00
|
|
|
typename async_completion<
|
|
|
|
AcceptHandler, void(error_code)>::result_type
|
|
|
|
stream<NextLayer>::
|
2016-11-10 05:34:49 -05:00
|
|
|
async_accept(http::request<Body, Fields> const& req,
|
2016-10-24 08:12:09 -04:00
|
|
|
AcceptHandler&& handler)
|
|
|
|
{
|
|
|
|
static_assert(is_AsyncStream<next_layer_type>::value,
|
|
|
|
"AsyncStream requirements requirements not met");
|
|
|
|
beast::async_completion<
|
|
|
|
AcceptHandler, void(error_code)
|
|
|
|
> completion(handler);
|
|
|
|
reset();
|
|
|
|
response_op<decltype(completion.handler)>{
|
|
|
|
completion.handler, *this, req,
|
2017-01-05 09:07:18 -05:00
|
|
|
beast_asio_helpers::
|
2016-10-24 08:12:09 -04:00
|
|
|
is_continuation(completion.handler)};
|
|
|
|
return completion.result.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
void
|
|
|
|
stream<NextLayer>::
|
|
|
|
accept()
|
|
|
|
{
|
|
|
|
static_assert(is_SyncStream<next_layer_type>::value,
|
|
|
|
"SyncStream requirements not met");
|
|
|
|
error_code ec;
|
|
|
|
accept(boost::asio::null_buffers{}, ec);
|
|
|
|
if(ec)
|
|
|
|
throw system_error{ec};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
void
|
|
|
|
stream<NextLayer>::
|
|
|
|
accept(error_code& ec)
|
|
|
|
{
|
|
|
|
static_assert(is_SyncStream<next_layer_type>::value,
|
|
|
|
"SyncStream requirements not met");
|
|
|
|
accept(boost::asio::null_buffers{}, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
template<class ConstBufferSequence>
|
|
|
|
void
|
|
|
|
stream<NextLayer>::
|
|
|
|
accept(ConstBufferSequence const& buffers)
|
|
|
|
{
|
|
|
|
static_assert(is_SyncStream<next_layer_type>::value,
|
|
|
|
"SyncStream requirements not met");
|
|
|
|
static_assert(is_ConstBufferSequence<
|
|
|
|
ConstBufferSequence>::value,
|
|
|
|
"ConstBufferSequence requirements not met");
|
|
|
|
error_code ec;
|
|
|
|
accept(buffers, ec);
|
|
|
|
if(ec)
|
|
|
|
throw system_error{ec};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
template<class ConstBufferSequence>
|
|
|
|
void
|
|
|
|
stream<NextLayer>::
|
|
|
|
accept(ConstBufferSequence const& buffers, error_code& ec)
|
|
|
|
{
|
|
|
|
static_assert(is_SyncStream<next_layer_type>::value,
|
|
|
|
"SyncStream requirements not met");
|
|
|
|
static_assert(beast::is_ConstBufferSequence<
|
|
|
|
ConstBufferSequence>::value,
|
|
|
|
"ConstBufferSequence requirements not met");
|
|
|
|
using boost::asio::buffer_copy;
|
|
|
|
using boost::asio::buffer_size;
|
|
|
|
reset();
|
|
|
|
stream_.buffer().commit(buffer_copy(
|
|
|
|
stream_.buffer().prepare(
|
|
|
|
buffer_size(buffers)), buffers));
|
|
|
|
http::request<http::string_body> m;
|
|
|
|
http::read(next_layer(), stream_.buffer(), m, ec);
|
|
|
|
if(ec)
|
|
|
|
return;
|
|
|
|
accept(m, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NextLayer>
|
2016-11-10 05:34:49 -05:00
|
|
|
template<class Body, class Fields>
|
2016-10-24 08:12:09 -04:00
|
|
|
void
|
|
|
|
stream<NextLayer>::
|
2016-11-10 05:34:49 -05:00
|
|
|
accept(http::request<Body, Fields> const& request)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
|
|
|
static_assert(is_SyncStream<next_layer_type>::value,
|
|
|
|
"SyncStream requirements not met");
|
|
|
|
error_code ec;
|
|
|
|
accept(request, ec);
|
|
|
|
if(ec)
|
|
|
|
throw system_error{ec};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NextLayer>
|
2016-11-10 05:34:49 -05:00
|
|
|
template<class Body, class Fields>
|
2016-10-24 08:12:09 -04:00
|
|
|
void
|
|
|
|
stream<NextLayer>::
|
2016-11-10 05:34:49 -05:00
|
|
|
accept(http::request<Body, Fields> const& req,
|
2016-10-24 08:12:09 -04:00
|
|
|
error_code& ec)
|
|
|
|
{
|
|
|
|
static_assert(is_SyncStream<next_layer_type>::value,
|
|
|
|
"SyncStream requirements not met");
|
|
|
|
reset();
|
|
|
|
auto const res = build_response(req);
|
|
|
|
http::write(stream_, res, ec);
|
|
|
|
if(ec)
|
|
|
|
return;
|
|
|
|
if(res.status != 101)
|
|
|
|
{
|
|
|
|
ec = error::handshake_failed;
|
|
|
|
// VFALCO TODO Respect keep alive setting, perform
|
|
|
|
// teardown if Connection: close.
|
|
|
|
return;
|
|
|
|
}
|
2016-10-24 18:41:25 -04:00
|
|
|
pmd_read(pmd_config_, req.fields);
|
2016-10-24 08:12:09 -04:00
|
|
|
open(detail::role_type::server);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
} // websocket
|
|
|
|
} // beast
|
|
|
|
|
|
|
|
#endif
|