Files
boost_beast/include/beast/websocket/impl/response_op.ipp
T

140 lines
3.4 KiB
C++
Raw Normal View History

2016-04-19 20:29:16 -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)
//
2016-02-25 16:17:19 -05:00
2016-04-19 20:29:16 -04:00
#ifndef BEAST_WEBSOCKET_IMPL_RESPONSE_OP_HPP
#define BEAST_WEBSOCKET_IMPL_RESPONSE_OP_HPP
2016-02-25 16:17:19 -05:00
2016-10-09 06:34:35 -04:00
#include <beast/http/message.hpp>
2016-04-19 20:29:16 -04:00
#include <beast/http/string_body.hpp>
#include <beast/http/write.hpp>
2016-05-07 14:57:15 -04:00
#include <beast/core/handler_alloc.hpp>
2016-02-25 16:17:19 -05:00
#include <memory>
namespace beast {
2016-04-19 20:29:16 -04:00
namespace websocket {
2016-02-25 16:17:19 -05:00
// Respond to an upgrade HTTP request
2016-04-19 20:29:16 -04:00
template<class NextLayer>
2016-02-25 16:17:19 -05:00
template<class Handler>
2016-04-19 20:29:16 -04:00
class stream<NextLayer>::response_op
2016-02-25 16:17:19 -05:00
{
using alloc_type =
handler_alloc<char, Handler>;
struct data
{
2016-04-19 20:29:16 -04:00
stream<NextLayer>& ws;
2016-10-09 06:34:35 -04:00
http::response<http::string_body> resp;
2016-02-25 16:17:19 -05:00
Handler h;
error_code final_ec;
bool cont;
int state = 0;
template<class DeducedHandler,
class Body, class Headers>
2016-04-19 20:29:16 -04:00
data(DeducedHandler&& h_, stream<NextLayer>& ws_,
2016-10-09 06:34:35 -04:00
http::request<Body, Headers> const& req,
2016-02-25 16:17:19 -05:00
bool cont_)
: ws(ws_)
, resp(ws_.build_response(req))
, h(std::forward<DeducedHandler>(h_))
, cont(cont_)
{
2016-05-15 16:22:25 -04:00
// can't call stream::reset() here
// otherwise accept_op will malfunction
//
2016-02-25 16:17:19 -05:00
if(resp.status != 101)
final_ec = error::handshake_failed;
}
};
std::shared_ptr<data> d_;
public:
response_op(response_op&&) = default;
response_op(response_op const&) = default;
template<class DeducedHandler, class... Args>
response_op(DeducedHandler&& h,
2016-04-19 20:29:16 -04:00
stream<NextLayer>& ws, Args&&... args)
2016-02-25 16:17:19 -05:00
: d_(std::allocate_shared<data>(alloc_type{h},
std::forward<DeducedHandler>(h), ws,
std::forward<Args>(args)...))
{
(*this)(error_code{}, false);
}
void operator()(
error_code ec, bool again = true);
friend
2016-04-22 07:34:44 -04:00
void* asio_handler_allocate(
2016-02-25 16:17:19 -05:00
std::size_t size, response_op* op)
{
return boost_asio_handler_alloc_helpers::
allocate(size, op->d_->h);
}
friend
2016-04-22 07:34:44 -04:00
void asio_handler_deallocate(
2016-02-25 16:17:19 -05:00
void* p, std::size_t size, response_op* op)
{
return boost_asio_handler_alloc_helpers::
deallocate(p, size, op->d_->h);
}
friend
2016-04-22 07:34:44 -04:00
bool asio_handler_is_continuation(response_op* op)
2016-02-25 16:17:19 -05:00
{
return op->d_->cont;
}
2016-08-26 08:01:44 -04:00
template<class Function>
2016-02-25 16:17:19 -05:00
friend
2016-04-22 07:34:44 -04:00
void asio_handler_invoke(Function&& f, response_op* op)
2016-02-25 16:17:19 -05:00
{
return boost_asio_handler_invoke_helpers::
invoke(f, op->d_->h);
}
};
2016-04-19 20:29:16 -04:00
template<class NextLayer>
2016-02-25 16:17:19 -05:00
template<class Handler>
void
2016-04-19 20:29:16 -04:00
stream<NextLayer>::response_op<Handler>::
2016-02-25 16:17:19 -05:00
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;
2016-04-30 13:00:33 -04:00
http::async_write(d.ws.next_layer(),
2016-02-25 16:17:19 -05:00
d.resp, std::move(*this));
return;
// sent response
case 1:
d.state = 99;
ec = d.final_ec;
if(! ec)
2016-05-15 16:22:25 -04:00
d.ws.open(detail::role_type::server);
2016-02-25 16:17:19 -05:00
break;
}
}
d.h(ec);
}
2016-04-19 20:29:16 -04:00
} // websocket
2016-02-25 16:17:19 -05:00
} // beast
#endif