Refactor accept, handshake ops

This commit is contained in:
Vinnie Falco
2017-07-27 14:14:47 -07:00
parent 07535d6289
commit 70c1d361fd
4 changed files with 350 additions and 285 deletions

View File

@@ -10,6 +10,7 @@ Version 91:
WebSocket: WebSocket:
* Tidy up websocket javadocs * Tidy up websocket javadocs
* Refactor accept, handshake ops
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@@ -38,38 +38,18 @@ class stream<NextLayer>::response_op
{ {
struct data struct data
{ {
bool cont;
stream<NextLayer>& ws; stream<NextLayer>& ws;
response_type res; response_type res;
int state = 0; int step = 0;
template<class Body, class Allocator, class Decorator> template<class Body, class Allocator, class Decorator>
data(Handler&, stream<NextLayer>& ws_, http::request< data(Handler&, stream<NextLayer>& ws_, http::request<
Body, http::basic_fields<Allocator>> const& req, Body, http::basic_fields<Allocator>> const& req,
Decorator const& decorator, bool cont_) Decorator const& decorator)
: cont(cont_) : ws(ws_)
, ws(ws_)
, res(ws_.build_response(req, decorator)) , res(ws_.build_response(req, decorator))
{ {
} }
template<class Body, class Allocator,
class Buffers, class Decorator>
data(Handler&, stream<NextLayer>& ws_, http::request<
Body, http::basic_fields<Allocator>> const& req,
Buffers const& buffers, Decorator const& decorator,
bool cont_)
: cont(cont_)
, ws(ws_)
, res(ws_.build_response(req, decorator))
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
// VFALCO What about catch(std::length_error const&)?
ws.stream_.buffer().commit(buffer_copy(
ws.stream_.buffer().prepare(
buffer_size(buffers)), buffers));
}
}; };
handler_ptr<data, Handler> d_; handler_ptr<data, Handler> d_;
@@ -84,11 +64,12 @@ public:
: d_(std::forward<DeducedHandler>(h), : d_(std::forward<DeducedHandler>(h),
ws, std::forward<Args>(args)...) ws, std::forward<Args>(args)...)
{ {
(*this)(error_code{}, false);
} }
void operator()( template<class Buffers>
error_code ec, bool again = true); void operator()(Buffers const& buffers);
void operator()(error_code ec);
friend friend
void* asio_handler_allocate( void* asio_handler_allocate(
@@ -111,7 +92,8 @@ public:
friend friend
bool asio_handler_is_continuation(response_op* op) bool asio_handler_is_continuation(response_op* op)
{ {
return op->d_->cont; // VFALCO This will go away in Net-TS
return false;
} }
template<class Function> template<class Function>
@@ -126,26 +108,54 @@ public:
template<class NextLayer> template<class NextLayer>
template<class Handler> template<class Handler>
template<class Buffers>
void void
stream<NextLayer>::response_op<Handler>:: stream<NextLayer>::
operator()(error_code ec, bool again) response_op<Handler>::
operator()(Buffers const& buffers)
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
auto& d = *d_;
error_code ec;
boost::optional<typename
flat_buffer::mutable_buffers_type> mb;
auto const len = buffer_size(buffers);
try
{
mb.emplace(d.ws.stream_.buffer().prepare(len));
}
catch(std::length_error const&)
{
d.step = 2;
ec = error::buffer_overflow;
return d.ws.get_io_service().post(
bind_handler(std::move(*this), ec));
}
d.ws.stream_.buffer().commit(
buffer_copy(*mb, buffers));
(*this)(ec);
}
template<class NextLayer>
template<class Handler>
void
stream<NextLayer>::
response_op<Handler>::
operator()(error_code ec)
{ {
auto& d = *d_; auto& d = *d_;
d.cont = d.cont || again; switch(d.step)
while(! ec && d.state != 99)
{
switch(d.state)
{ {
case 0: case 0:
// send response // send response
d.state = 1; d.step = 1;
http::async_write(d.ws.next_layer(), http::async_write(d.ws.next_layer(),
d.res, std::move(*this)); d.res, std::move(*this));
return; return;
// sent response // sent response
case 1: case 1:
d.state = 99;
if(d.res.result() != if(d.res.result() !=
http::status::switching_protocols) http::status::switching_protocols)
ec = error::handshake_failed; ec = error::handshake_failed;
@@ -155,7 +165,10 @@ operator()(error_code ec, bool again)
d.ws.open(role_type::server); d.ws.open(role_type::server);
} }
break; break;
}
// call handler
case 2:
break;
} }
d_.invoke(ec); d_.invoke(ec);
} }
@@ -173,28 +186,13 @@ class stream<NextLayer>::accept_op
stream<NextLayer>& ws; stream<NextLayer>& ws;
Decorator decorator; Decorator decorator;
http::request_parser<http::empty_body> p; http::request_parser<http::empty_body> p;
int step = 0;
data(Handler&, stream<NextLayer>& ws_, data(Handler&, stream<NextLayer>& ws_,
Decorator const& decorator_) Decorator const& decorator_)
: ws(ws_) : ws(ws_)
, decorator(decorator_) , decorator(decorator_)
{ {
} }
template<class Buffers>
data(Handler&, stream<NextLayer>& ws_,
Buffers const& buffers,
Decorator const& decorator_)
: ws(ws_)
, decorator(decorator_)
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
// VFALCO What about catch(std::length_error const&)?
ws.stream_.buffer().commit(buffer_copy(
ws.stream_.buffer().prepare(
buffer_size(buffers)), buffers));
}
}; };
handler_ptr<data, Handler> d_; handler_ptr<data, Handler> d_;
@@ -211,7 +209,8 @@ public:
{ {
} }
void operator()(); template<class Buffers>
void operator()(Buffers const& buffers);
void operator()(error_code ec); void operator()(error_code ec);
@@ -253,44 +252,73 @@ public:
template<class NextLayer> template<class NextLayer>
template<class Decorator, class Handler> template<class Decorator, class Handler>
template<class Buffers>
void void
stream<NextLayer>::accept_op<Decorator, Handler>:: stream<NextLayer>::
operator()() accept_op<Decorator, Handler>::
operator()(Buffers const& buffers)
{ {
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
auto& d = *d_; auto& d = *d_;
http::async_read_header(d.ws.next_layer(), error_code ec;
d.ws.stream_.buffer(), d.p, boost::optional<typename
std::move(*this)); flat_buffer::mutable_buffers_type> mb;
auto const len = buffer_size(buffers);
try
{
mb.emplace(d.ws.stream_.buffer().prepare(len));
}
catch(std::length_error const&)
{
d.step = 2;
ec = error::buffer_overflow;
return d.ws.get_io_service().post(
bind_handler(std::move(*this), ec));
}
d.ws.stream_.buffer().commit(
buffer_copy(*mb, buffers));
(*this)(ec);
} }
template<class NextLayer> template<class NextLayer>
template<class Decorator, class Handler> template<class Decorator, class Handler>
void void
stream<NextLayer>::accept_op<Decorator, Handler>:: stream<NextLayer>::
accept_op<Decorator, Handler>::
operator()(error_code ec) operator()(error_code ec)
{ {
auto& d = *d_; auto& d = *d_;
if(! ec) switch(d.step)
{ {
BOOST_ASSERT(d.p.is_header_done()); case 0:
// Arguments from our state must be d.step = 1;
return http::async_read(
d.ws.next_layer(), d.ws.stream_.buffer(),
d.p, std::move(*this));
case 1:
{
if(ec)
break;
// Arguments from our step must be
// moved to the stack before releasing // moved to the stack before releasing
// the handler. // the handler.
auto& ws = d.ws; auto& ws = d.ws;
auto const req = d.p.release(); auto const req = d.p.release();
auto const decorator = d.decorator; auto const decorator = d.decorator;
#if 1 #if 1
response_op<Handler>{ return response_op<Handler>{
d_.release_handler(), d_.release_handler(),
ws, req, decorator, true}; ws, req, decorator}(ec);
#else #else
// VFALCO This *should* work but breaks // VFALCO This *should* work but breaks
// coroutine invariants in the unit test. // coroutine invariants in the unit test.
// Also it calls reset() when it shouldn't. // Also it calls reset() when it shouldn't.
ws.async_accept_ex( return ws.async_accept_ex(
req, decorator, d_.release_handler()); req, decorator, d_.release_handler());
#endif #endif
return; }
} }
d_.invoke(ec); d_.invoke(ec);
} }
@@ -606,29 +634,35 @@ accept_ex(http::request<Body,
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template<class NextLayer> template<class NextLayer>
template<class AcceptHandler> template<
async_return_type< class AcceptHandler>
AcceptHandler, void(error_code)> async_return_type<AcceptHandler, void(error_code)>
stream<NextLayer>:: stream<NextLayer>::
async_accept(AcceptHandler&& handler) async_accept(
AcceptHandler&& handler)
{ {
static_assert(is_async_stream<next_layer_type>::value, static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream requirements requirements not met"); "AsyncStream requirements requirements not met");
async_completion<AcceptHandler, async_completion<AcceptHandler,
void(error_code)> init{handler}; void(error_code)> init{handler};
reset(); reset();
accept_op<decltype(&default_decorate_res), accept_op<
decltype(&default_decorate_res),
handler_type<AcceptHandler, void(error_code)>>{ handler_type<AcceptHandler, void(error_code)>>{
init.completion_handler, *this, &default_decorate_res}(); init.completion_handler,
*this,
&default_decorate_res}({});
return init.result.get(); return init.result.get();
} }
template<class NextLayer> template<class NextLayer>
template<class ResponseDecorator, class AcceptHandler> template<
async_return_type< class ResponseDecorator,
AcceptHandler, void(error_code)> class AcceptHandler>
async_return_type<AcceptHandler, void(error_code)>
stream<NextLayer>:: stream<NextLayer>::
async_accept_ex(ResponseDecorator const& decorator, async_accept_ex(
ResponseDecorator const& decorator,
AcceptHandler&& handler) AcceptHandler&& handler)
{ {
static_assert(is_async_stream<next_layer_type>::value, static_assert(is_async_stream<next_layer_type>::value,
@@ -639,19 +673,25 @@ async_accept_ex(ResponseDecorator const& decorator,
async_completion<AcceptHandler, async_completion<AcceptHandler,
void(error_code)> init{handler}; void(error_code)> init{handler};
reset(); reset();
accept_op<ResponseDecorator, handler_type< accept_op<
AcceptHandler, void(error_code)>>{ ResponseDecorator,
init.completion_handler, *this, decorator}(); handler_type<AcceptHandler, void(error_code)>>{
init.completion_handler,
*this,
decorator}({});
return init.result.get(); return init.result.get();
} }
template<class NextLayer> template<class NextLayer>
template<class ConstBufferSequence, class AcceptHandler> template<
class ConstBufferSequence,
class AcceptHandler>
typename std::enable_if< typename std::enable_if<
! http::detail::is_header<ConstBufferSequence>::value, ! http::detail::is_header<ConstBufferSequence>::value,
async_return_type<AcceptHandler, void(error_code)>>::type async_return_type<AcceptHandler, void(error_code)>>::type
stream<NextLayer>:: stream<NextLayer>::
async_accept(ConstBufferSequence const& buffers, async_accept(
ConstBufferSequence const& buffers,
AcceptHandler&& handler) AcceptHandler&& handler)
{ {
static_assert(is_async_stream<next_layer_type>::value, static_assert(is_async_stream<next_layer_type>::value,
@@ -662,21 +702,26 @@ async_accept(ConstBufferSequence const& buffers,
async_completion<AcceptHandler, async_completion<AcceptHandler,
void(error_code)> init{handler}; void(error_code)> init{handler};
reset(); reset();
accept_op<decltype(&default_decorate_res), accept_op<
decltype(&default_decorate_res),
handler_type<AcceptHandler, void(error_code)>>{ handler_type<AcceptHandler, void(error_code)>>{
init.completion_handler, *this, buffers, init.completion_handler,
&default_decorate_res}(); *this,
&default_decorate_res}(buffers);
return init.result.get(); return init.result.get();
} }
template<class NextLayer> template<class NextLayer>
template<class ConstBufferSequence, template<
class ResponseDecorator, class AcceptHandler> class ConstBufferSequence,
class ResponseDecorator,
class AcceptHandler>
typename std::enable_if< typename std::enable_if<
! http::detail::is_header<ConstBufferSequence>::value, ! http::detail::is_header<ConstBufferSequence>::value,
async_return_type<AcceptHandler, void(error_code)>>::type async_return_type<AcceptHandler, void(error_code)>>::type
stream<NextLayer>:: stream<NextLayer>::
async_accept_ex(ConstBufferSequence const& buffers, async_accept_ex(
ConstBufferSequence const& buffers,
ResponseDecorator const& decorator, ResponseDecorator const& decorator,
AcceptHandler&& handler) AcceptHandler&& handler)
{ {
@@ -691,21 +736,23 @@ async_accept_ex(ConstBufferSequence const& buffers,
async_completion<AcceptHandler, async_completion<AcceptHandler,
void(error_code)> init{handler}; void(error_code)> init{handler};
reset(); reset();
accept_op<ResponseDecorator, handler_type< accept_op<
AcceptHandler, void(error_code)>>{ ResponseDecorator,
init.completion_handler, *this, buffers, handler_type<AcceptHandler, void(error_code)>>{
decorator}(); init.completion_handler,
*this,
decorator}(buffers);
return init.result.get(); return init.result.get();
} }
template<class NextLayer> template<class NextLayer>
template<class Body, class Allocator, template<
class Body, class Allocator,
class AcceptHandler> class AcceptHandler>
async_return_type< async_return_type<AcceptHandler, void(error_code)>
AcceptHandler, void(error_code)>
stream<NextLayer>:: stream<NextLayer>::
async_accept(http::request<Body, async_accept(
http::basic_fields<Allocator>> const& req, http::request<Body, http::basic_fields<Allocator>> const& req,
AcceptHandler&& handler) AcceptHandler&& handler)
{ {
static_assert(is_async_stream<next_layer_type>::value, static_assert(is_async_stream<next_layer_type>::value,
@@ -714,23 +761,26 @@ async_accept(http::request<Body,
void(error_code)> init{handler}; void(error_code)> init{handler};
reset(); reset();
using boost::asio::asio_handler_is_continuation; using boost::asio::asio_handler_is_continuation;
response_op<handler_type< response_op<
AcceptHandler, void(error_code)>>{init.completion_handler, handler_type<AcceptHandler, void(error_code)>>{
*this, req, &default_decorate_res, init.completion_handler,
asio_handler_is_continuation( *this,
std::addressof(init.completion_handler))}; req,
&default_decorate_res}({});
return init.result.get(); return init.result.get();
} }
template<class NextLayer> template<class NextLayer>
template<class Body, class Allocator, template<
class ResponseDecorator, class AcceptHandler> class Body, class Allocator,
async_return_type< class ResponseDecorator,
AcceptHandler, void(error_code)> class AcceptHandler>
async_return_type<AcceptHandler, void(error_code)>
stream<NextLayer>:: stream<NextLayer>::
async_accept_ex(http::request<Body, async_accept_ex(
http::basic_fields<Allocator>> const& req, http::request<Body, http::basic_fields<Allocator>> const& req,
ResponseDecorator const& decorator, AcceptHandler&& handler) ResponseDecorator const& decorator,
AcceptHandler&& handler)
{ {
static_assert(is_async_stream<next_layer_type>::value, static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream requirements requirements not met"); "AsyncStream requirements requirements not met");
@@ -741,22 +791,24 @@ async_accept_ex(http::request<Body,
void(error_code)> init{handler}; void(error_code)> init{handler};
reset(); reset();
using boost::asio::asio_handler_is_continuation; using boost::asio::asio_handler_is_continuation;
response_op<handler_type< response_op<
AcceptHandler, void(error_code)>>{ handler_type<AcceptHandler, void(error_code)>>{
init.completion_handler, *this, req, decorator, init.completion_handler,
asio_handler_is_continuation( *this,
std::addressof(init.completion_handler))}; req,
decorator}({});
return init.result.get(); return init.result.get();
} }
template<class NextLayer> template<class NextLayer>
template<class Body, class Allocator, template<
class ConstBufferSequence, class AcceptHandler> class Body, class Allocator,
async_return_type< class ConstBufferSequence,
AcceptHandler, void(error_code)> class AcceptHandler>
async_return_type<AcceptHandler, void(error_code)>
stream<NextLayer>:: stream<NextLayer>::
async_accept(http::request<Body, async_accept(
http::basic_fields<Allocator>> const& req, http::request<Body, http::basic_fields<Allocator>> const& req,
ConstBufferSequence const& buffers, ConstBufferSequence const& buffers,
AcceptHandler&& handler) AcceptHandler&& handler)
{ {
@@ -769,17 +821,20 @@ async_accept(http::request<Body,
void(error_code)> init{handler}; void(error_code)> init{handler};
reset(); reset();
using boost::asio::asio_handler_is_continuation; using boost::asio::asio_handler_is_continuation;
response_op<handler_type< response_op<
AcceptHandler, void(error_code)>>{ handler_type<AcceptHandler, void(error_code)>>{
init.completion_handler, *this, req, buffers, init.completion_handler,
&default_decorate_res, asio_handler_is_continuation( *this,
std::addressof(init.completion_handler))}; req,
&default_decorate_res}(buffers);
return init.result.get(); return init.result.get();
} }
template<class NextLayer> template<class NextLayer>
template<class Body, class Allocator, template<
class ConstBufferSequence, class ResponseDecorator, class Body, class Allocator,
class ConstBufferSequence,
class ResponseDecorator,
class AcceptHandler> class AcceptHandler>
async_return_type< async_return_type<
AcceptHandler, void(error_code)> AcceptHandler, void(error_code)>
@@ -802,13 +857,58 @@ async_accept_ex(http::request<Body,
void(error_code)> init{handler}; void(error_code)> init{handler};
reset(); reset();
using boost::asio::asio_handler_is_continuation; using boost::asio::asio_handler_is_continuation;
response_op<handler_type< response_op<
AcceptHandler, void(error_code)>>{init.completion_handler, handler_type<AcceptHandler, void(error_code)>>{
*this, req, buffers, decorator, asio_handler_is_continuation( init.completion_handler,
std::addressof(init.completion_handler))}; *this,
req,
decorator}(buffers);
return init.result.get(); return init.result.get();
} }
//------------------------------------------------------------------------------
template<class NextLayer>
template<class Decorator>
void
stream<NextLayer>::
do_accept(
Decorator const& decorator,
error_code& ec)
{
http::request_parser<http::empty_body> p;
http::read(next_layer(),
stream_.buffer(), p, ec);
if(ec)
return;
do_accept(p.get(), decorator, ec);
}
template<class NextLayer>
template<class Body, class Allocator,
class Decorator>
void
stream<NextLayer>::
do_accept(
http::request<Body,http::basic_fields<Allocator>> const& req,
Decorator const& decorator,
error_code& ec)
{
auto const res = build_response(req, decorator);
http::write(stream_, res, ec);
if(ec)
return;
if(res.result() != http::status::switching_protocols)
{
ec = error::handshake_failed;
// VFALCO TODO Respect keep alive setting, perform
// teardown if Connection: close.
return;
}
pmd_read(pmd_config_, req);
open(role_type::server);
}
} // websocket } // websocket
} // beast } // beast
} // boost } // boost

View File

@@ -391,6 +391,38 @@ handshake_ex(response_type& res,
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template<class NextLayer>
template<class RequestDecorator>
void
stream<NextLayer>::
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);
pmd_read(pmd_config_, req);
http::write(stream_, req, ec);
}
if(ec)
return;
http::read(next_layer(), stream_.buffer(), res, ec);
if(ec)
return;
do_response(res, key, ec);
if(res_p)
*res_p = std::move(res);
}
//------------------------------------------------------------------------------
} // websocket } // websocket
} // beast } // beast
} // boost } // boost

View File

@@ -818,74 +818,6 @@ build_response(http::request<Body,
return res; return res;
} }
template<class NextLayer>
template<class Decorator>
void
stream<NextLayer>::
do_accept(
Decorator const& decorator, error_code& ec)
{
http::request_parser<http::empty_body> p;
http::read_header(next_layer(),
stream_.buffer(), p, ec);
if(ec)
return;
do_accept(p.get(), decorator, ec);
}
template<class NextLayer>
template<class Body, class Allocator,
class Decorator>
void
stream<NextLayer>::
do_accept(http::request<Body,
http::basic_fields<Allocator>> const& req,
Decorator const& decorator, error_code& ec)
{
auto const res = build_response(req, decorator);
http::write(stream_, res, ec);
if(ec)
return;
if(res.result() != http::status::switching_protocols)
{
ec = error::handshake_failed;
// VFALCO TODO Respect keep alive setting, perform
// teardown if Connection: close.
return;
}
pmd_read(pmd_config_, req);
open(role_type::server);
}
template<class NextLayer>
template<class RequestDecorator>
void
stream<NextLayer>::
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);
pmd_read(pmd_config_, req);
http::write(stream_, req, ec);
}
if(ec)
return;
http::read(next_layer(), stream_.buffer(), res, ec);
if(ec)
return;
do_response(res, key, ec);
if(res_p)
*res_p = std::move(res);
}
template<class NextLayer> template<class NextLayer>
void void
stream<NextLayer>:: stream<NextLayer>::