add boost on mac

This commit is contained in:
Bassem Girgis
2019-08-10 16:38:17 -05:00
parent 861b918727
commit be945cb63b
14105 changed files with 2714968 additions and 0 deletions

View File

@@ -0,0 +1,862 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_ACCEPT_IPP
#define BOOST_BEAST_WEBSOCKET_IMPL_ACCEPT_IPP
#include <boost/beast/websocket/impl/stream_impl.hpp>
#include <boost/beast/websocket/detail/type_traits.hpp>
#include <boost/beast/http/empty_body.hpp>
#include <boost/beast/http/parser.hpp>
#include <boost/beast/http/read.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/beast/http/write.hpp>
#include <boost/beast/core/async_base.hpp>
#include <boost/beast/core/buffer_traits.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/beast/core/detail/buffer.hpp>
#include <boost/beast/core/detail/type_traits.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/post.hpp>
#include <boost/assert.hpp>
#include <boost/throw_exception.hpp>
#include <memory>
#include <type_traits>
namespace boost {
namespace beast {
namespace websocket {
//------------------------------------------------------------------------------
namespace detail {
template<class Body, class Allocator>
void
impl_base<true>::
build_response_pmd(
http::response<http::string_body>& res,
http::request<Body,
http::basic_fields<Allocator>> const& req)
{
pmd_offer offer;
pmd_offer unused;
pmd_read(offer, req);
pmd_negotiate(res, unused, offer, pmd_opts_);
}
template<class Body, class Allocator>
void
impl_base<false>::
build_response_pmd(
http::response<http::string_body>&,
http::request<Body,
http::basic_fields<Allocator>> const&)
{
}
} // detail
template<class NextLayer, bool deflateSupported>
template<class Body, class Allocator, class Decorator>
response_type
stream<NextLayer, deflateSupported>::impl_type::
build_response(
http::request<Body,
http::basic_fields<Allocator>> const& req,
Decorator const& decorator,
error_code& result)
{
auto const decorate =
[this, &decorator](response_type& res)
{
decorator_opt(res);
decorator(res);
if(! res.count(http::field::server))
{
// VFALCO this is weird..
BOOST_STATIC_ASSERT(sizeof(
BOOST_BEAST_VERSION_STRING) < 20);
static_string<20> s(BOOST_BEAST_VERSION_STRING);
res.set(http::field::server, s);
}
};
auto err =
[&](error e)
{
result = e;
response_type res;
res.version(req.version());
res.result(http::status::bad_request);
res.body() = result.message();
res.prepare_payload();
decorate(res);
return res;
};
if(req.version() != 11)
return err(error::bad_http_version);
if(req.method() != http::verb::get)
return err(error::bad_method);
if(! req.count(http::field::host))
return err(error::no_host);
{
auto const it = req.find(http::field::connection);
if(it == req.end())
return err(error::no_connection);
if(! http::token_list{it->value()}.exists("upgrade"))
return err(error::no_connection_upgrade);
}
{
auto const it = req.find(http::field::upgrade);
if(it == req.end())
return err(error::no_upgrade);
if(! http::token_list{it->value()}.exists("websocket"))
return err(error::no_upgrade_websocket);
}
string_view key;
{
auto const it = req.find(http::field::sec_websocket_key);
if(it == req.end())
return err(error::no_sec_key);
key = it->value();
if(key.size() > detail::sec_ws_key_type::max_size_n)
return err(error::bad_sec_key);
}
{
auto const it = req.find(http::field::sec_websocket_version);
if(it == req.end())
return err(error::no_sec_version);
if(it->value() != "13")
{
response_type res;
res.result(http::status::upgrade_required);
res.version(req.version());
res.set(http::field::sec_websocket_version, "13");
result = error::bad_sec_version;
res.body() = result.message();
res.prepare_payload();
decorate(res);
return res;
}
}
response_type res;
res.result(http::status::switching_protocols);
res.version(req.version());
res.set(http::field::upgrade, "websocket");
res.set(http::field::connection, "upgrade");
{
detail::sec_ws_accept_type acc;
detail::make_sec_ws_accept(acc, key);
res.set(http::field::sec_websocket_accept, acc);
}
this->build_response_pmd(res, req);
decorate(res);
result = {};
return res;
}
//------------------------------------------------------------------------------
/** Respond to an HTTP request
*/
template<class NextLayer, bool deflateSupported>
template<class Handler>
class stream<NextLayer, deflateSupported>::response_op
: public beast::stable_async_base<
Handler, beast::executor_type<stream>>
, public net::coroutine
{
boost::weak_ptr<impl_type> wp_;
error_code result_; // must come before res_
response_type& res_;
public:
template<
class Handler_,
class Body, class Allocator,
class Decorator>
response_op(
Handler_&& h,
boost::shared_ptr<impl_type> const& sp,
http::request<Body,
http::basic_fields<Allocator>> const& req,
Decorator const& decorator,
bool cont = false)
: stable_async_base<Handler,
beast::executor_type<stream>>(
std::forward<Handler_>(h),
sp->stream().get_executor())
, wp_(sp)
, res_(beast::allocate_stable<response_type>(*this,
sp->build_response(req, decorator, result_)))
{
(*this)({}, 0, cont);
}
void operator()(
error_code ec = {},
std::size_t bytes_transferred = 0,
bool cont = true)
{
boost::ignore_unused(bytes_transferred);
auto sp = wp_.lock();
if(! sp)
{
ec = net::error::operation_aborted;
return this->complete(cont, ec);
}
auto& impl = *sp;
BOOST_ASIO_CORO_REENTER(*this)
{
impl.change_status(status::handshake);
impl.update_timer(this->get_executor());
// Send response
BOOST_ASIO_CORO_YIELD
http::async_write(
impl.stream(), res_, std::move(*this));
if(impl.check_stop_now(ec))
goto upcall;
if(! ec)
ec = result_;
if(! ec)
{
impl.do_pmd_config(res_);
impl.open(role_type::server);
}
upcall:
this->complete(cont, ec);
}
}
};
//------------------------------------------------------------------------------
// read and respond to an upgrade request
//
template<class NextLayer, bool deflateSupported>
template<class Handler, class Decorator>
class stream<NextLayer, deflateSupported>::accept_op
: public beast::stable_async_base<
Handler, beast::executor_type<stream>>
, public net::coroutine
{
boost::weak_ptr<impl_type> wp_;
http::request_parser<http::empty_body>& p_;
Decorator d_;
public:
template<class Handler_, class Buffers>
accept_op(
Handler_&& h,
boost::shared_ptr<impl_type> const& sp,
Decorator const& decorator,
Buffers const& buffers)
: stable_async_base<Handler,
beast::executor_type<stream>>(
std::forward<Handler_>(h),
sp->stream().get_executor())
, wp_(sp)
, p_(beast::allocate_stable<
http::request_parser<http::empty_body>>(*this))
, d_(decorator)
{
auto& impl = *sp;
error_code ec;
auto const mb =
beast::detail::dynamic_buffer_prepare(
impl.rd_buf, buffer_bytes(buffers),
ec, error::buffer_overflow);
if(! ec)
impl.rd_buf.commit(
net::buffer_copy(*mb, buffers));
(*this)(ec);
}
void operator()(
error_code ec = {},
std::size_t bytes_transferred = 0,
bool cont = true)
{
boost::ignore_unused(bytes_transferred);
auto sp = wp_.lock();
if(! sp)
{
ec = net::error::operation_aborted;
return this->complete(cont, ec);
}
auto& impl = *sp;
BOOST_ASIO_CORO_REENTER(*this)
{
impl.change_status(status::handshake);
impl.update_timer(this->get_executor());
// The constructor could have set ec
if(ec)
goto upcall;
BOOST_ASIO_CORO_YIELD
http::async_read(impl.stream(),
impl.rd_buf, p_, std::move(*this));
if(ec == http::error::end_of_stream)
ec = error::closed;
if(impl.check_stop_now(ec))
goto upcall;
{
// Arguments from our state must be
// moved to the stack before releasing
// the handler.
auto const req = p_.release();
auto const decorator = d_;
response_op<Handler>(
this->release_handler(),
sp, req, decorator, true);
return;
}
upcall:
this->complete(cont, ec);
}
}
};
template<class NextLayer, bool deflateSupported>
struct stream<NextLayer, deflateSupported>::
run_response_op
{
template<
class AcceptHandler,
class Body, class Allocator,
class Decorator>
void
operator()(
AcceptHandler&& h,
boost::shared_ptr<impl_type> const& sp,
http::request<Body,
http::basic_fields<Allocator>> const* m,
Decorator const& d)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
static_assert(
beast::detail::is_invocable<AcceptHandler,
void(error_code)>::value,
"AcceptHandler type requirements not met");
response_op<
typename std::decay<AcceptHandler>::type>(
std::forward<AcceptHandler>(h), sp, *m, d);
}
};
template<class NextLayer, bool deflateSupported>
struct stream<NextLayer, deflateSupported>::
run_accept_op
{
template<
class AcceptHandler,
class Decorator,
class Buffers>
void
operator()(
AcceptHandler&& h,
boost::shared_ptr<impl_type> const& sp,
Decorator const& d,
Buffers const& b)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
static_assert(
beast::detail::is_invocable<AcceptHandler,
void(error_code)>::value,
"AcceptHandler type requirements not met");
accept_op<
typename std::decay<AcceptHandler>::type,
Decorator>(
std::forward<AcceptHandler>(h),
sp,
d,
b);
}
};
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
template<class Body, class Allocator,
class Decorator>
void
stream<NextLayer, deflateSupported>::
do_accept(
http::request<Body,
http::basic_fields<Allocator>> const& req,
Decorator const& decorator,
error_code& ec)
{
impl_->change_status(status::handshake);
error_code result;
auto const res = impl_->build_response(req, decorator, result);
http::write(impl_->stream(), res, ec);
if(ec)
return;
ec = result;
if(ec)
{
// VFALCO TODO Respect keep alive setting, perform
// teardown if Connection: close.
return;
}
impl_->do_pmd_config(res);
impl_->open(role_type::server);
}
template<class NextLayer, bool deflateSupported>
template<class Buffers, class Decorator>
void
stream<NextLayer, deflateSupported>::
do_accept(
Buffers const& buffers,
Decorator const& decorator,
error_code& ec)
{
impl_->reset();
auto const mb =
beast::detail::dynamic_buffer_prepare(
impl_->rd_buf, buffer_bytes(buffers), ec,
error::buffer_overflow);
if(ec)
return;
impl_->rd_buf.commit(net::buffer_copy(*mb, buffers));
http::request_parser<http::empty_body> p;
http::read(next_layer(), impl_->rd_buf, p, ec);
if(ec == http::error::end_of_stream)
ec = error::closed;
if(ec)
return;
do_accept(p.get(), decorator, ec);
}
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
accept()
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
error_code ec;
accept(ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
accept(error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
do_accept(
net::const_buffer{},
&default_decorate_res, ec);
}
template<class NextLayer, bool deflateSupported>
template<class ConstBufferSequence>
typename std::enable_if<! http::detail::is_header<
ConstBufferSequence>::value>::type
stream<NextLayer, deflateSupported>::
accept(ConstBufferSequence const& buffers)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
error_code ec;
accept(buffers, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
template<class ConstBufferSequence>
typename std::enable_if<! http::detail::is_header<
ConstBufferSequence>::value>::type
stream<NextLayer, deflateSupported>::
accept(
ConstBufferSequence const& buffers, error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
do_accept(buffers, &default_decorate_res, ec);
}
template<class NextLayer, bool deflateSupported>
template<class Body, class Allocator>
void
stream<NextLayer, deflateSupported>::
accept(
http::request<Body,
http::basic_fields<Allocator>> const& req)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
error_code ec;
accept(req, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
template<class Body, class Allocator>
void
stream<NextLayer, deflateSupported>::
accept(
http::request<Body,
http::basic_fields<Allocator>> const& req,
error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
impl_->reset();
do_accept(req, &default_decorate_res, ec);
}
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
template<
class AcceptHandler>
BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
stream<NextLayer, deflateSupported>::
async_accept(
AcceptHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
impl_->reset();
return net::async_initiate<
AcceptHandler,
void(error_code)>(
run_accept_op{},
handler,
impl_,
&default_decorate_res,
net::const_buffer{});
}
template<class NextLayer, bool deflateSupported>
template<
class ResponseDecorator,
class AcceptHandler>
BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
stream<NextLayer, deflateSupported>::
async_accept_ex(
ResponseDecorator const& decorator,
AcceptHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
static_assert(detail::is_response_decorator<
ResponseDecorator>::value,
"ResponseDecorator requirements not met");
impl_->reset();
return net::async_initiate<
AcceptHandler,
void(error_code)>(
run_accept_op{},
handler,
impl_,
decorator,
net::const_buffer{});
}
template<class NextLayer, bool deflateSupported>
template<
class ConstBufferSequence,
class AcceptHandler>
typename std::enable_if<
! http::detail::is_header<ConstBufferSequence>::value,
BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)>::type
stream<NextLayer, deflateSupported>::
async_accept(
ConstBufferSequence const& buffers,
AcceptHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
impl_->reset();
return net::async_initiate<
AcceptHandler,
void(error_code)>(
run_accept_op{},
handler,
impl_,
&default_decorate_res,
buffers);
}
template<class NextLayer, bool deflateSupported>
template<
class ConstBufferSequence,
class ResponseDecorator,
class AcceptHandler>
typename std::enable_if<
! http::detail::is_header<ConstBufferSequence>::value,
BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)>::type
stream<NextLayer, deflateSupported>::
async_accept_ex(
ConstBufferSequence const& buffers,
ResponseDecorator const& decorator,
AcceptHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
static_assert(detail::is_response_decorator<
ResponseDecorator>::value,
"ResponseDecorator requirements not met");
impl_->reset();
return net::async_initiate<
AcceptHandler,
void(error_code)>(
run_accept_op{},
handler,
impl_,
decorator,
buffers);
}
template<class NextLayer, bool deflateSupported>
template<
class Body, class Allocator,
class AcceptHandler>
BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
stream<NextLayer, deflateSupported>::
async_accept(
http::request<Body, http::basic_fields<Allocator>> const& req,
AcceptHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
impl_->reset();
return net::async_initiate<
AcceptHandler,
void(error_code)>(
run_response_op{},
handler,
impl_,
&req,
&default_decorate_res);
}
template<class NextLayer, bool deflateSupported>
template<
class Body, class Allocator,
class ResponseDecorator,
class AcceptHandler>
BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
stream<NextLayer, deflateSupported>::
async_accept_ex(
http::request<Body, http::basic_fields<Allocator>> const& req,
ResponseDecorator const& decorator,
AcceptHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
static_assert(detail::is_response_decorator<
ResponseDecorator>::value,
"ResponseDecorator requirements not met");
impl_->reset();
return net::async_initiate<
AcceptHandler,
void(error_code)>(
run_response_op{},
handler,
impl_,
&req,
decorator);
}
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
template<class ResponseDecorator>
void
stream<NextLayer, deflateSupported>::
accept_ex(ResponseDecorator const& decorator)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(ResponseDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(detail::is_response_decorator<
ResponseDecorator>::value,
"ResponseDecorator requirements not met");
error_code ec;
accept_ex(decorator, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
template<class ResponseDecorator>
void
stream<NextLayer, deflateSupported>::
accept_ex(ResponseDecorator const& decorator, error_code& ec)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(ResponseDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(detail::is_response_decorator<
ResponseDecorator>::value,
"ResponseDecorator requirements not met");
do_accept(
net::const_buffer{},
decorator, ec);
}
template<class NextLayer, bool deflateSupported>
template<
class ConstBufferSequence,
class ResponseDecorator>
typename std::enable_if<! http::detail::is_header<
ConstBufferSequence>::value>::type
stream<NextLayer, deflateSupported>::
accept_ex(
ConstBufferSequence const& buffers,
ResponseDecorator const &decorator)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(ResponseDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
static_assert(detail::is_response_decorator<
ResponseDecorator>::value,
"ResponseDecorator requirements not met");
error_code ec;
accept_ex(buffers, decorator, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
template<
class ConstBufferSequence,
class ResponseDecorator>
typename std::enable_if<! http::detail::is_header<
ConstBufferSequence>::value>::type
stream<NextLayer, deflateSupported>::
accept_ex(
ConstBufferSequence const& buffers,
ResponseDecorator const& decorator,
error_code& ec)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(ResponseDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
do_accept(buffers, decorator, ec);
}
template<class NextLayer, bool deflateSupported>
template<
class Body, class Allocator,
class ResponseDecorator>
void
stream<NextLayer, deflateSupported>::
accept_ex(
http::request<Body,
http::basic_fields<Allocator>> const& req,
ResponseDecorator const& decorator)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(ResponseDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(detail::is_response_decorator<
ResponseDecorator>::value,
"ResponseDecorator requirements not met");
error_code ec;
accept_ex(req, decorator, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
template<
class Body, class Allocator,
class ResponseDecorator>
void
stream<NextLayer, deflateSupported>::
accept_ex(
http::request<Body,
http::basic_fields<Allocator>> const& req,
ResponseDecorator const& decorator,
error_code& ec)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(ResponseDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(detail::is_response_decorator<
ResponseDecorator>::value,
"ResponseDecorator requirements not met");
impl_->reset();
do_accept(req, decorator, ec);
}
} // websocket
} // beast
} // boost
#endif

View File

@@ -0,0 +1,405 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_CLOSE_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_CLOSE_HPP
#include <boost/beast/websocket/teardown.hpp>
#include <boost/beast/websocket/detail/mask.hpp>
#include <boost/beast/websocket/impl/stream_impl.hpp>
#include <boost/beast/core/async_base.hpp>
#include <boost/beast/core/flat_static_buffer.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/beast/core/detail/bind_continuation.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/post.hpp>
#include <boost/throw_exception.hpp>
#include <memory>
namespace boost {
namespace beast {
namespace websocket {
/* Close the WebSocket Connection
This composed operation sends the close frame if it hasn't already
been sent, then reads and discards frames until receiving a close
frame. Finally it invokes the teardown operation to shut down the
underlying connection.
*/
template<class NextLayer, bool deflateSupported>
template<class Handler>
class stream<NextLayer, deflateSupported>::close_op
: public beast::stable_async_base<
Handler, beast::executor_type<stream>>
, public net::coroutine
{
boost::weak_ptr<impl_type> wp_;
error_code ev_;
detail::frame_buffer& fb_;
public:
static constexpr int id = 5; // for soft_mutex
template<class Handler_>
close_op(
Handler_&& h,
boost::shared_ptr<impl_type> const& sp,
close_reason const& cr)
: stable_async_base<Handler,
beast::executor_type<stream>>(
std::forward<Handler_>(h),
sp->stream().get_executor())
, wp_(sp)
, fb_(beast::allocate_stable<
detail::frame_buffer>(*this))
{
// Serialize the close frame
sp->template write_close<
flat_static_buffer_base>(fb_, cr);
(*this)({}, 0, false);
}
void
operator()(
error_code ec = {},
std::size_t bytes_transferred = 0,
bool cont = true)
{
using beast::detail::clamp;
auto sp = wp_.lock();
if(! sp)
{
ec = net::error::operation_aborted;
return this->complete(cont, ec);
}
auto& impl = *sp;
BOOST_ASIO_CORO_REENTER(*this)
{
// Acquire the write lock
if(! impl.wr_block.try_lock(this))
{
BOOST_ASIO_CORO_YIELD
impl.op_close.emplace(std::move(*this));
impl.wr_block.lock(this);
BOOST_ASIO_CORO_YIELD
net::post(std::move(*this));
BOOST_ASSERT(impl.wr_block.is_locked(this));
}
if(impl.check_stop_now(ec))
goto upcall;
// Can't call close twice
// TODO return a custom error code
BOOST_ASSERT(! impl.wr_close);
// Send close frame
impl.wr_close = true;
impl.change_status(status::closing);
impl.update_timer(this->get_executor());
BOOST_ASIO_CORO_YIELD
net::async_write(impl.stream(), fb_.data(),
beast::detail::bind_continuation(std::move(*this)));
if(impl.check_stop_now(ec))
goto upcall;
if(impl.rd_close)
{
// This happens when the read_op gets a close frame
// at the same time close_op is sending the close frame.
// The read_op will be suspended on the write block.
goto teardown;
}
// Acquire the read lock
if(! impl.rd_block.try_lock(this))
{
BOOST_ASIO_CORO_YIELD
impl.op_r_close.emplace(std::move(*this));
impl.rd_block.lock(this);
BOOST_ASIO_CORO_YIELD
net::post(std::move(*this));
BOOST_ASSERT(impl.rd_block.is_locked(this));
if(impl.check_stop_now(ec))
goto upcall;
BOOST_ASSERT(! impl.rd_close);
}
// Read until a receiving a close frame
// TODO There should be a timeout on this
if(impl.rd_remain > 0)
goto read_payload;
for(;;)
{
// Read frame header
while(! impl.parse_fh(
impl.rd_fh, impl.rd_buf, ev_))
{
if(ev_)
goto teardown;
BOOST_ASIO_CORO_YIELD
impl.stream().async_read_some(
impl.rd_buf.prepare(read_size(
impl.rd_buf, impl.rd_buf.max_size())),
beast::detail::bind_continuation(std::move(*this)));
impl.rd_buf.commit(bytes_transferred);
if(impl.check_stop_now(ec))
goto upcall;
}
if(detail::is_control(impl.rd_fh.op))
{
// Discard ping or pong frame
if(impl.rd_fh.op != detail::opcode::close)
{
impl.rd_buf.consume(clamp(impl.rd_fh.len));
continue;
}
// Process close frame
// TODO Should we invoke the control callback?
BOOST_ASSERT(! impl.rd_close);
impl.rd_close = true;
auto const mb = buffers_prefix(
clamp(impl.rd_fh.len),
impl.rd_buf.data());
if(impl.rd_fh.len > 0 && impl.rd_fh.mask)
detail::mask_inplace(mb, impl.rd_key);
detail::read_close(impl.cr, mb, ev_);
if(ev_)
goto teardown;
impl.rd_buf.consume(clamp(impl.rd_fh.len));
goto teardown;
}
read_payload:
// Discard message frame
while(impl.rd_buf.size() < impl.rd_remain)
{
impl.rd_remain -= impl.rd_buf.size();
impl.rd_buf.consume(impl.rd_buf.size());
BOOST_ASIO_CORO_YIELD
impl.stream().async_read_some(
impl.rd_buf.prepare(read_size(
impl.rd_buf, impl.rd_buf.max_size())),
beast::detail::bind_continuation(std::move(*this)));
impl.rd_buf.commit(bytes_transferred);
if(impl.check_stop_now(ec))
goto upcall;
}
BOOST_ASSERT(impl.rd_buf.size() >= impl.rd_remain);
impl.rd_buf.consume(clamp(impl.rd_remain));
impl.rd_remain = 0;
}
teardown:
// Teardown
BOOST_ASSERT(impl.wr_block.is_locked(this));
using beast::websocket::async_teardown;
BOOST_ASIO_CORO_YIELD
async_teardown(impl.role, impl.stream(),
beast::detail::bind_continuation(std::move(*this)));
BOOST_ASSERT(impl.wr_block.is_locked(this));
if(ec == net::error::eof)
{
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
ec = {};
}
if(! ec)
ec = ev_;
if(ec)
impl.change_status(status::failed);
else
impl.change_status(status::closed);
impl.close();
upcall:
impl.wr_block.unlock(this);
impl.rd_block.try_unlock(this)
&& impl.op_r_rd.maybe_invoke();
impl.op_rd.maybe_invoke()
|| impl.op_idle_ping.maybe_invoke()
|| impl.op_ping.maybe_invoke()
|| impl.op_wr.maybe_invoke();
this->complete(cont, ec);
}
}
};
template<class NextLayer, bool deflateSupported>
struct stream<NextLayer, deflateSupported>::
run_close_op
{
template<class CloseHandler>
void
operator()(
CloseHandler&& h,
boost::shared_ptr<impl_type> const& sp,
close_reason const& cr)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
static_assert(
beast::detail::is_invocable<CloseHandler,
void(error_code)>::value,
"CloseHandler type requirements not met");
close_op<
typename std::decay<CloseHandler>::type>(
std::forward<CloseHandler>(h),
sp,
cr);
}
};
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
close(close_reason const& cr)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
error_code ec;
close(cr, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
close(close_reason const& cr, error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
using beast::detail::clamp;
auto& impl = *impl_;
ec = {};
if(impl.check_stop_now(ec))
return;
BOOST_ASSERT(! impl.rd_close);
// Can't call close twice
// TODO return a custom error code
BOOST_ASSERT(! impl.wr_close);
// Send close frame
{
impl.wr_close = true;
impl.change_status(status::closing);
detail::frame_buffer fb;
impl.template write_close<flat_static_buffer_base>(fb, cr);
net::write(impl.stream(), fb.data(), ec);
if(impl.check_stop_now(ec))
return;
}
// Read until a receiving a close frame
error_code ev;
if(impl.rd_remain > 0)
goto read_payload;
for(;;)
{
// Read frame header
while(! impl.parse_fh(
impl.rd_fh, impl.rd_buf, ev))
{
if(ev)
{
// Protocol violation
return do_fail(close_code::none, ev, ec);
}
impl.rd_buf.commit(impl.stream().read_some(
impl.rd_buf.prepare(read_size(
impl.rd_buf, impl.rd_buf.max_size())), ec));
if(impl.check_stop_now(ec))
return;
}
if(detail::is_control(impl.rd_fh.op))
{
// Discard ping/pong frame
if(impl.rd_fh.op != detail::opcode::close)
{
impl.rd_buf.consume(clamp(impl.rd_fh.len));
continue;
}
// Handle close frame
// TODO Should we invoke the control callback?
BOOST_ASSERT(! impl.rd_close);
impl.rd_close = true;
auto const mb = buffers_prefix(
clamp(impl.rd_fh.len),
impl.rd_buf.data());
if(impl.rd_fh.len > 0 && impl.rd_fh.mask)
detail::mask_inplace(mb, impl.rd_key);
detail::read_close(impl.cr, mb, ev);
if(ev)
{
// Protocol violation
return do_fail(close_code::none, ev, ec);
}
impl.rd_buf.consume(clamp(impl.rd_fh.len));
break;
}
read_payload:
// Discard message frame
while(impl.rd_buf.size() < impl.rd_remain)
{
impl.rd_remain -= impl.rd_buf.size();
impl.rd_buf.consume(impl.rd_buf.size());
impl.rd_buf.commit(
impl.stream().read_some(
impl.rd_buf.prepare(
read_size(
impl.rd_buf,
impl.rd_buf.max_size())),
ec));
if(impl.check_stop_now(ec))
return;
}
BOOST_ASSERT(
impl.rd_buf.size() >= impl.rd_remain);
impl.rd_buf.consume(clamp(impl.rd_remain));
impl.rd_remain = 0;
}
// _Close the WebSocket Connection_
do_fail(close_code::none, error::closed, ec);
if(ec == error::closed)
ec = {};
}
template<class NextLayer, bool deflateSupported>
template<class CloseHandler>
BOOST_BEAST_ASYNC_RESULT1(CloseHandler)
stream<NextLayer, deflateSupported>::
async_close(close_reason const& cr, CloseHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
return net::async_initiate<
CloseHandler,
void(error_code)>(
run_close_op{},
handler,
impl_,
cr);
}
} // websocket
} // beast
} // boost
#endif

View File

@@ -0,0 +1,44 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_ERROR_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_ERROR_HPP
namespace boost {
namespace system {
template<>
struct is_error_code_enum<::boost::beast::websocket::error>
{
static bool const value = true;
};
template<>
struct is_error_condition_enum<::boost::beast::websocket::condition>
{
static bool const value = true;
};
} // system
} // boost
namespace boost {
namespace beast {
namespace websocket {
BOOST_BEAST_DECL
error_code
make_error_code(error e);
BOOST_BEAST_DECL
error_condition
make_error_condition(condition c);
} // websocket
} // beast
} // boost
#endif

View File

@@ -0,0 +1,160 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_ERROR_IPP
#define BOOST_BEAST_WEBSOCKET_IMPL_ERROR_IPP
#include <boost/beast/websocket/error.hpp>
namespace boost {
namespace beast {
namespace websocket {
namespace detail {
class error_codes : public error_category
{
public:
const char*
name() const noexcept override
{
return "boost.beast.websocket";
}
std::string
message(int ev) const override
{
switch(static_cast<error>(ev))
{
default:
case error::closed: return "The WebSocket stream was gracefully closed at both endpoints";
case error::buffer_overflow: return "The WebSocket operation caused a dynamic buffer overflow";
case error::partial_deflate_block: return "The WebSocket stream produced an incomplete deflate block";
case error::message_too_big: return "The WebSocket message exceeded the locally configured limit";
case error::bad_http_version: return "The WebSocket handshake was not HTTP/1.1";
case error::bad_method: return "The WebSocket handshake method was not GET";
case error::no_host: return "The WebSocket handshake Host field is missing";
case error::no_connection: return "The WebSocket handshake Connection field is missing";
case error::no_connection_upgrade: return "The WebSocket handshake Connection field is missing the upgrade token";
case error::no_upgrade: return "The WebSocket handshake Upgrade field is missing";
case error::no_upgrade_websocket: return "The WebSocket handshake Upgrade field is missing the websocket token";
case error::no_sec_key: return "The WebSocket handshake Sec-WebSocket-Key field is missing";
case error::bad_sec_key: return "The WebSocket handshake Sec-WebSocket-Key field is invalid";
case error::no_sec_version: return "The WebSocket handshake Sec-WebSocket-Version field is missing";
case error::bad_sec_version: return "The WebSocket handshake Sec-WebSocket-Version field is invalid";
case error::no_sec_accept: return "The WebSocket handshake Sec-WebSocket-Accept field is missing";
case error::bad_sec_accept: return "The WebSocket handshake Sec-WebSocket-Accept field is invalid";
case error::upgrade_declined: return "The WebSocket handshake was declined by the remote peer";
case error::bad_opcode: return "The WebSocket frame contained an illegal opcode";
case error::bad_data_frame: return "The WebSocket data frame was unexpected";
case error::bad_continuation: return "The WebSocket continuation frame was unexpected";
case error::bad_reserved_bits: return "The WebSocket frame contained illegal reserved bits";
case error::bad_control_fragment: return "The WebSocket control frame was fragmented";
case error::bad_control_size: return "The WebSocket control frame size was invalid";
case error::bad_unmasked_frame: return "The WebSocket frame was unmasked";
case error::bad_masked_frame: return "The WebSocket frame was masked";
case error::bad_size: return "The WebSocket frame size was not canonical";
case error::bad_frame_payload: return "The WebSocket frame payload was not valid utf8";
case error::bad_close_code: return "The WebSocket close frame reason code was invalid";
case error::bad_close_size: return "The WebSocket close frame payload size was invalid";
case error::bad_close_payload: return "The WebSocket close frame payload was not valid utf8";
}
}
error_condition
default_error_condition(int ev) const noexcept override
{
switch(static_cast<error>(ev))
{
default:
case error::closed:
case error::buffer_overflow:
case error::partial_deflate_block:
case error::message_too_big:
return {ev, *this};
case error::bad_http_version:
case error::bad_method:
case error::no_host:
case error::no_connection:
case error::no_connection_upgrade:
case error::no_upgrade:
case error::no_upgrade_websocket:
case error::no_sec_key:
case error::bad_sec_key:
case error::no_sec_version:
case error::bad_sec_version:
case error::no_sec_accept:
case error::bad_sec_accept:
case error::upgrade_declined:
return condition::handshake_failed;
case error::bad_opcode:
case error::bad_data_frame:
case error::bad_continuation:
case error::bad_reserved_bits:
case error::bad_control_fragment:
case error::bad_control_size:
case error::bad_unmasked_frame:
case error::bad_masked_frame:
case error::bad_size:
case error::bad_frame_payload:
case error::bad_close_code:
case error::bad_close_size:
case error::bad_close_payload:
return condition::protocol_violation;
}
}
};
class error_conditions : public error_category
{
public:
const char*
name() const noexcept override
{
return "boost.beast.websocket";
}
std::string
message(int cv) const override
{
switch(static_cast<condition>(cv))
{
default:
case condition::handshake_failed: return "The WebSocket handshake failed";
case condition::protocol_violation: return "A WebSocket protocol violation occurred";
}
}
};
} // detail
error_code
make_error_code(error e)
{
static detail::error_codes const cat{};
return error_code{static_cast<
std::underlying_type<error>::type>(e), cat};
}
error_condition
make_error_condition(condition c)
{
static detail::error_conditions const cat{};
return error_condition{static_cast<
std::underlying_type<condition>::type>(c), cat};
}
} // websocket
} // beast
} // boost
#endif

View File

@@ -0,0 +1,540 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_HANDSHAKE_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_HANDSHAKE_HPP
#include <boost/beast/websocket/impl/stream_impl.hpp>
#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/async_base.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/assert.hpp>
#include <boost/throw_exception.hpp>
#include <memory>
namespace boost {
namespace beast {
namespace websocket {
//------------------------------------------------------------------------------
// send the upgrade request and process the response
//
template<class NextLayer, bool deflateSupported>
template<class Handler>
class stream<NextLayer, deflateSupported>::handshake_op
: public beast::stable_async_base<Handler,
beast::executor_type<stream>>
, public net::coroutine
{
struct data
{
// VFALCO This really should be two separate
// composed operations, to save on memory
request_type req;
http::response_parser<
typename response_type::body_type> p;
flat_buffer fb;
bool overflow = false; // could be a member of the op
explicit
data(request_type&& req_)
: req(std::move(req_))
{
}
};
boost::weak_ptr<impl_type> wp_;
detail::sec_ws_key_type key_;
response_type* res_p_;
data& d_;
public:
template<class Handler_>
handshake_op(
Handler_&& h,
boost::shared_ptr<impl_type> const& sp,
request_type&& req,
detail::sec_ws_key_type key,
response_type* res_p)
: stable_async_base<Handler,
beast::executor_type<stream>>(
std::forward<Handler_>(h),
sp->stream().get_executor())
, wp_(sp)
, key_(key)
, res_p_(res_p)
, d_(beast::allocate_stable<data>(
*this, std::move(req)))
{
sp->reset(); // VFALCO I don't like this
(*this)({}, 0, false);
}
void
operator()(
error_code ec = {},
std::size_t bytes_used = 0,
bool cont = true)
{
boost::ignore_unused(bytes_used);
auto sp = wp_.lock();
if(! sp)
{
ec = net::error::operation_aborted;
return this->complete(cont, ec);
}
auto& impl = *sp;
BOOST_ASIO_CORO_REENTER(*this)
{
impl.change_status(status::handshake);
impl.update_timer(this->get_executor());
// write HTTP request
impl.do_pmd_config(d_.req);
BOOST_ASIO_CORO_YIELD
http::async_write(impl.stream(),
d_.req, std::move(*this));
if(impl.check_stop_now(ec))
goto upcall;
// read HTTP response
BOOST_ASIO_CORO_YIELD
http::async_read(impl.stream(),
impl.rd_buf, d_.p,
std::move(*this));
if(ec == http::error::buffer_overflow)
{
// If the response overflows the internal
// read buffer, switch to a dynamically
// allocated flat buffer.
d_.fb.commit(net::buffer_copy(
d_.fb.prepare(impl.rd_buf.size()),
impl.rd_buf.data()));
impl.rd_buf.clear();
BOOST_ASIO_CORO_YIELD
http::async_read(impl.stream(),
d_.fb, d_.p, std::move(*this));
if(! ec)
{
// Copy any leftovers back into the read
// buffer, since this represents websocket
// frame data.
if(d_.fb.size() <= impl.rd_buf.capacity())
{
impl.rd_buf.commit(net::buffer_copy(
impl.rd_buf.prepare(d_.fb.size()),
d_.fb.data()));
}
else
{
ec = http::error::buffer_overflow;
}
}
// Do this before the upcall
d_.fb.clear();
}
if(impl.check_stop_now(ec))
goto upcall;
// success
impl.reset_idle();
impl.on_response(d_.p.get(), key_, ec);
if(res_p_)
swap(d_.p.get(), *res_p_);
upcall:
this->complete(cont ,ec);
}
}
};
template<class NextLayer, bool deflateSupported>
struct stream<NextLayer, deflateSupported>::
run_handshake_op
{
template<class HandshakeHandler>
void operator()(
HandshakeHandler&& h,
boost::shared_ptr<impl_type> const& sp,
request_type&& req,
detail::sec_ws_key_type key,
response_type* res_p)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
static_assert(
beast::detail::is_invocable<HandshakeHandler,
void(error_code)>::value,
"HandshakeHandler type requirements not met");
handshake_op<
typename std::decay<HandshakeHandler>::type>(
std::forward<HandshakeHandler>(h),
sp, std::move(req), key, res_p);
}
};
//------------------------------------------------------------------------------
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)
{
auto& impl = *impl_;
impl.change_status(status::handshake);
impl.reset();
detail::sec_ws_key_type key;
{
auto const req = impl.build_request(
key, host, target, decorator);
impl.do_pmd_config(req);
http::write(impl.stream(), req, ec);
}
if(impl.check_stop_now(ec))
return;
http::response_parser<
typename response_type::body_type> p;
http::read(next_layer(), impl.rd_buf, p, ec);
if(ec == http::error::buffer_overflow)
{
// If the response overflows the internal
// read buffer, switch to a dynamically
// allocated flat buffer.
flat_buffer fb;
fb.commit(net::buffer_copy(
fb.prepare(impl.rd_buf.size()),
impl.rd_buf.data()));
impl.rd_buf.clear();
http::read(next_layer(), fb, p, ec);;
if(! ec)
{
// Copy any leftovers back into the read
// buffer, since this represents websocket
// frame data.
if(fb.size() <= impl.rd_buf.capacity())
{
impl.rd_buf.commit(net::buffer_copy(
impl.rd_buf.prepare(fb.size()),
fb.data()));
}
else
{
ec = http::error::buffer_overflow;
}
}
}
if(impl.check_stop_now(ec))
return;
impl.on_response(p.get(), key, ec);
if(impl.check_stop_now(ec))
return;
if(res_p)
*res_p = p.release();
}
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
template<class HandshakeHandler>
BOOST_BEAST_ASYNC_RESULT1(HandshakeHandler)
stream<NextLayer, deflateSupported>::
async_handshake(
string_view host,
string_view target,
HandshakeHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
detail::sec_ws_key_type key;
auto req = impl_->build_request(
key, host, target, &default_decorate_req);
return net::async_initiate<
HandshakeHandler,
void(error_code)>(
run_handshake_op{},
handler,
impl_,
std::move(req),
key,
nullptr);
}
template<class NextLayer, bool deflateSupported>
template<class HandshakeHandler>
BOOST_BEAST_ASYNC_RESULT1(HandshakeHandler)
stream<NextLayer, deflateSupported>::
async_handshake(
response_type& res,
string_view host,
string_view target,
HandshakeHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
detail::sec_ws_key_type key;
auto req = impl_->build_request(
key, host, target, &default_decorate_req);
return net::async_initiate<
HandshakeHandler,
void(error_code)>(
run_handshake_op{},
handler,
impl_,
std::move(req),
key,
&res);
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
handshake(string_view host,
string_view target)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
error_code ec;
handshake(
host, target, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
handshake(response_type& res,
string_view host,
string_view target)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
error_code ec;
handshake(res, host, target, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
handshake(string_view host,
string_view target, error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
do_handshake(nullptr,
host, target, &default_decorate_req, ec);
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
handshake(response_type& res,
string_view host,
string_view target,
error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
do_handshake(&res,
host, target, &default_decorate_req, ec);
}
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
template<class RequestDecorator>
void
stream<NextLayer, deflateSupported>::
handshake_ex(string_view host,
string_view target,
RequestDecorator const& decorator)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(RequestDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(detail::is_request_decorator<
RequestDecorator>::value,
"RequestDecorator requirements not met");
error_code ec;
handshake_ex(host, target, decorator, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
template<class RequestDecorator>
void
stream<NextLayer, deflateSupported>::
handshake_ex(response_type& res,
string_view host,
string_view target,
RequestDecorator const& decorator)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(RequestDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(detail::is_request_decorator<
RequestDecorator>::value,
"RequestDecorator requirements not met");
error_code ec;
handshake_ex(res, host, target, decorator, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
template<class RequestDecorator>
void
stream<NextLayer, deflateSupported>::
handshake_ex(string_view host,
string_view target,
RequestDecorator const& decorator,
error_code& ec)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(RequestDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(detail::is_request_decorator<
RequestDecorator>::value,
"RequestDecorator requirements not met");
do_handshake(nullptr,
host, target, decorator, ec);
}
template<class NextLayer, bool deflateSupported>
template<class RequestDecorator>
void
stream<NextLayer, deflateSupported>::
handshake_ex(response_type& res,
string_view host,
string_view target,
RequestDecorator const& decorator,
error_code& ec)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(RequestDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(detail::is_request_decorator<
RequestDecorator>::value,
"RequestDecorator requirements not met");
do_handshake(&res,
host, target, decorator, ec);
}
template<class NextLayer, bool deflateSupported>
template<class RequestDecorator, class HandshakeHandler>
BOOST_BEAST_ASYNC_RESULT1(HandshakeHandler)
stream<NextLayer, deflateSupported>::
async_handshake_ex(string_view host,
string_view target,
RequestDecorator const& decorator,
HandshakeHandler&& handler)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(RequestDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
static_assert(detail::is_request_decorator<
RequestDecorator>::value,
"RequestDecorator requirements not met");
detail::sec_ws_key_type key;
auto req = impl_->build_request(
key, host, target, decorator);
return net::async_initiate<
HandshakeHandler,
void(error_code)>(
run_handshake_op{},
handler,
impl_,
std::move(req),
key,
nullptr);
}
template<class NextLayer, bool deflateSupported>
template<class RequestDecorator, class HandshakeHandler>
BOOST_BEAST_ASYNC_RESULT1(HandshakeHandler)
stream<NextLayer, deflateSupported>::
async_handshake_ex(response_type& res,
string_view host,
string_view target,
RequestDecorator const& decorator,
HandshakeHandler&& handler)
{
#ifndef BOOST_BEAST_ALLOW_DEPRECATED
static_assert(sizeof(RequestDecorator) == 0,
BOOST_BEAST_DEPRECATION_STRING);
#endif
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
static_assert(detail::is_request_decorator<
RequestDecorator>::value,
"RequestDecorator requirements not met");
detail::sec_ws_key_type key;
auto req = impl_->build_request(
key, host, target, decorator);
return net::async_initiate<
HandshakeHandler,
void(error_code)>(
run_handshake_op{},
handler,
impl_,
std::move(req),
key,
&res);
}
} // websocket
} // beast
} // boost
#endif

View File

@@ -0,0 +1,329 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_PING_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_PING_HPP
#include <boost/beast/core/async_base.hpp>
#include <boost/beast/core/bind_handler.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/beast/core/detail/bind_continuation.hpp>
#include <boost/beast/websocket/detail/frame.hpp>
#include <boost/beast/websocket/impl/stream_impl.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/post.hpp>
#include <boost/throw_exception.hpp>
#include <memory>
namespace boost {
namespace beast {
namespace websocket {
/*
This composed operation handles sending ping and pong frames.
It only sends the frames it does not make attempts to read
any frame data.
*/
template<class NextLayer, bool deflateSupported>
template<class Handler>
class stream<NextLayer, deflateSupported>::ping_op
: public beast::stable_async_base<
Handler, beast::executor_type<stream>>
, public net::coroutine
{
boost::weak_ptr<impl_type> wp_;
detail::frame_buffer& fb_;
public:
static constexpr int id = 3; // for soft_mutex
template<class Handler_>
ping_op(
Handler_&& h,
boost::shared_ptr<impl_type> const& sp,
detail::opcode op,
ping_data const& payload)
: stable_async_base<Handler,
beast::executor_type<stream>>(
std::forward<Handler_>(h),
sp->stream().get_executor())
, wp_(sp)
, fb_(beast::allocate_stable<
detail::frame_buffer>(*this))
{
// Serialize the ping or pong frame
sp->template write_ping<
flat_static_buffer_base>(fb_, op, payload);
(*this)({}, 0, false);
}
void operator()(
error_code ec = {},
std::size_t bytes_transferred = 0,
bool cont = true)
{
boost::ignore_unused(bytes_transferred);
auto sp = wp_.lock();
if(! sp)
{
ec = net::error::operation_aborted;
return this->complete(cont, ec);
}
auto& impl = *sp;
BOOST_ASIO_CORO_REENTER(*this)
{
// Acquire the write lock
if(! impl.wr_block.try_lock(this))
{
BOOST_ASIO_CORO_YIELD
impl.op_ping.emplace(std::move(*this));
impl.wr_block.lock(this);
BOOST_ASIO_CORO_YIELD
net::post(std::move(*this));
BOOST_ASSERT(impl.wr_block.is_locked(this));
}
if(impl.check_stop_now(ec))
goto upcall;
// Send ping frame
BOOST_ASIO_CORO_YIELD
net::async_write(impl.stream(), fb_.data(),
beast::detail::bind_continuation(std::move(*this)));
if(impl.check_stop_now(ec))
goto upcall;
upcall:
impl.wr_block.unlock(this);
impl.op_close.maybe_invoke()
|| impl.op_idle_ping.maybe_invoke()
|| impl.op_rd.maybe_invoke()
|| impl.op_wr.maybe_invoke();
this->complete(cont, ec);
}
}
};
//------------------------------------------------------------------------------
// sends the idle ping
template<class NextLayer, bool deflateSupported>
template<class Executor>
class stream<NextLayer, deflateSupported>::idle_ping_op
: public net::coroutine
, public boost::empty_value<Executor>
{
boost::weak_ptr<impl_type> wp_;
std::unique_ptr<detail::frame_buffer> fb_;
public:
static constexpr int id = 4; // for soft_mutex
using executor_type = Executor;
executor_type
get_executor() const noexcept
{
return this->get();
}
idle_ping_op(
boost::shared_ptr<impl_type> const& sp,
Executor const& ex)
: boost::empty_value<Executor>(
boost::empty_init_t{}, ex)
, wp_(sp)
, fb_(new detail::frame_buffer)
{
if(! sp->idle_pinging)
{
// Create the ping frame
ping_data payload; // empty for now
sp->template write_ping<
flat_static_buffer_base>(*fb_,
detail::opcode::ping, payload);
sp->idle_pinging = true;
(*this)({}, 0);
}
else
{
// if we are already in the middle of sending
// an idle ping, don't bother sending another.
}
}
void operator()(
error_code ec = {},
std::size_t bytes_transferred = 0)
{
boost::ignore_unused(bytes_transferred);
auto sp = wp_.lock();
if(! sp)
return;
auto& impl = *sp;
BOOST_ASIO_CORO_REENTER(*this)
{
// Acquire the write lock
if(! impl.wr_block.try_lock(this))
{
BOOST_ASIO_CORO_YIELD
impl.op_idle_ping.emplace(std::move(*this));
impl.wr_block.lock(this);
BOOST_ASIO_CORO_YIELD
net::post(this->get(), std::move(*this));
BOOST_ASSERT(impl.wr_block.is_locked(this));
}
if(impl.check_stop_now(ec))
goto upcall;
// Send ping frame
BOOST_ASIO_CORO_YIELD
net::async_write(impl.stream(), fb_->data(),
//beast::detail::bind_continuation(std::move(*this)));
std::move(*this));
if(impl.check_stop_now(ec))
goto upcall;
upcall:
BOOST_ASSERT(sp->idle_pinging);
sp->idle_pinging = false;
impl.wr_block.unlock(this);
impl.op_close.maybe_invoke()
|| impl.op_ping.maybe_invoke()
|| impl.op_rd.maybe_invoke()
|| impl.op_wr.maybe_invoke();
}
}
};
template<class NextLayer, bool deflateSupported>
struct stream<NextLayer, deflateSupported>::
run_ping_op
{
template<class WriteHandler>
void
operator()(
WriteHandler&& h,
boost::shared_ptr<impl_type> const& sp,
detail::opcode op,
ping_data const& p)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
static_assert(
beast::detail::is_invocable<WriteHandler,
void(error_code)>::value,
"WriteHandler type requirements not met");
ping_op<
typename std::decay<WriteHandler>::type>(
std::forward<WriteHandler>(h),
sp,
op,
p);
}
};
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
ping(ping_data const& payload)
{
error_code ec;
ping(payload, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
ping(ping_data const& payload, error_code& ec)
{
if(impl_->check_stop_now(ec))
return;
detail::frame_buffer fb;
impl_->template write_ping<flat_static_buffer_base>(
fb, detail::opcode::ping, payload);
net::write(impl_->stream(), fb.data(), ec);
if(impl_->check_stop_now(ec))
return;
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
pong(ping_data const& payload)
{
error_code ec;
pong(payload, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
pong(ping_data const& payload, error_code& ec)
{
if(impl_->check_stop_now(ec))
return;
detail::frame_buffer fb;
impl_->template write_ping<flat_static_buffer_base>(
fb, detail::opcode::pong, payload);
net::write(impl_->stream(), fb.data(), ec);
if(impl_->check_stop_now(ec))
return;
}
template<class NextLayer, bool deflateSupported>
template<class WriteHandler>
BOOST_BEAST_ASYNC_RESULT1(WriteHandler)
stream<NextLayer, deflateSupported>::
async_ping(ping_data const& payload, WriteHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
return net::async_initiate<
WriteHandler,
void(error_code)>(
run_ping_op{},
handler,
impl_,
detail::opcode::ping,
payload);
}
template<class NextLayer, bool deflateSupported>
template<class WriteHandler>
BOOST_BEAST_ASYNC_RESULT1(WriteHandler)
stream<NextLayer, deflateSupported>::
async_pong(ping_data const& payload, WriteHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
return net::async_initiate<
WriteHandler,
void(error_code)>(
run_ping_op{},
handler,
impl_,
detail::opcode::pong,
payload);
}
} // websocket
} // beast
} // boost
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,40 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_RFC6455_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_RFC6455_HPP
#include <boost/beast/http/fields.hpp>
#include <boost/beast/http/rfc7230.hpp>
namespace boost {
namespace beast {
namespace websocket {
template<class Allocator>
bool
is_upgrade(http::header<true,
http::basic_fields<Allocator>> const& req)
{
if(req.version() < 11)
return false;
if(req.method() != http::verb::get)
return false;
if(! http::token_list{req[http::field::connection]}.exists("upgrade"))
return false;
if(! http::token_list{req[http::field::upgrade]}.exists("websocket"))
return false;
return true;
}
} // websocket
} // beast
} // boost
#endif

View File

@@ -0,0 +1,59 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_SSL_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_SSL_HPP
#include <utility>
namespace boost {
namespace beast {
/*
See
http://stackoverflow.com/questions/32046034/what-is-the-proper-way-to-securely-disconnect-an-asio-ssl-socket/32054476#32054476
Behavior of ssl::stream regarding close_notify
If the remote host calls async_shutdown then the
local host's async_read will complete with eof.
If both hosts call async_shutdown then the calls
to async_shutdown will complete with eof.
*/
template<class AsyncStream>
void
teardown(
role_type,
net::ssl::stream<AsyncStream>& stream,
error_code& ec)
{
stream.shutdown(ec);
}
template<
class AsyncStream,
class TeardownHandler>
void
async_teardown(
role_type,
net::ssl::stream<AsyncStream>& stream,
TeardownHandler&& handler)
{
stream.async_shutdown(
std::forward<TeardownHandler>(handler));
}
} // beast
} // boost
#endif

View File

@@ -0,0 +1,359 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_STREAM_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_STREAM_HPP
#include <boost/beast/core/buffer_traits.hpp>
#include <boost/beast/websocket/rfc6455.hpp>
#include <boost/beast/websocket/teardown.hpp>
#include <boost/beast/websocket/detail/hybi13.hpp>
#include <boost/beast/websocket/detail/mask.hpp>
#include <boost/beast/websocket/impl/stream_impl.hpp>
#include <boost/beast/version.hpp>
#include <boost/beast/http/read.hpp>
#include <boost/beast/http/write.hpp>
#include <boost/beast/http/rfc7230.hpp>
#include <boost/beast/core/buffers_cat.hpp>
#include <boost/beast/core/buffers_prefix.hpp>
#include <boost/beast/core/buffers_suffix.hpp>
#include <boost/beast/core/flat_static_buffer.hpp>
#include <boost/beast/core/detail/clamp.hpp>
#include <boost/beast/core/detail/type_traits.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/assert.hpp>
#include <boost/endian/buffers.hpp>
#include <boost/make_shared.hpp>
#include <boost/throw_exception.hpp>
#include <algorithm>
#include <chrono>
#include <memory>
#include <stdexcept>
#include <utility>
namespace boost {
namespace beast {
namespace websocket {
template<class NextLayer, bool deflateSupported>
stream<NextLayer, deflateSupported>::
~stream()
{
if(impl_)
impl_->remove();
}
template<class NextLayer, bool deflateSupported>
template<class... Args>
stream<NextLayer, deflateSupported>::
stream(Args&&... args)
: impl_(boost::make_shared<impl_type>(
std::forward<Args>(args)...))
{
BOOST_ASSERT(impl_->rd_buf.max_size() >=
max_control_frame_size);
}
template<class NextLayer, bool deflateSupported>
auto
stream<NextLayer, deflateSupported>::
get_executor() const noexcept ->
executor_type
{
return impl_->stream().get_executor();
}
template<class NextLayer, bool deflateSupported>
auto
stream<NextLayer, deflateSupported>::
next_layer() noexcept ->
next_layer_type&
{
return impl_->stream();
}
template<class NextLayer, bool deflateSupported>
auto
stream<NextLayer, deflateSupported>::
next_layer() const noexcept ->
next_layer_type const&
{
return impl_->stream();
}
template<class NextLayer, bool deflateSupported>
bool
stream<NextLayer, deflateSupported>::
is_open() const noexcept
{
return impl_->status_ == status::open;
}
template<class NextLayer, bool deflateSupported>
bool
stream<NextLayer, deflateSupported>::
got_binary() const noexcept
{
return impl_->rd_op == detail::opcode::binary;
}
template<class NextLayer, bool deflateSupported>
bool
stream<NextLayer, deflateSupported>::
is_message_done() const noexcept
{
return impl_->rd_done;
}
template<class NextLayer, bool deflateSupported>
close_reason const&
stream<NextLayer, deflateSupported>::
reason() const noexcept
{
return impl_->cr;
}
template<class NextLayer, bool deflateSupported>
std::size_t
stream<NextLayer, deflateSupported>::
read_size_hint(
std::size_t initial_size) const
{
return impl_->read_size_hint_pmd(
initial_size, impl_->rd_done,
impl_->rd_remain, impl_->rd_fh);
}
template<class NextLayer, bool deflateSupported>
template<class DynamicBuffer, class>
std::size_t
stream<NextLayer, deflateSupported>::
read_size_hint(DynamicBuffer& buffer) const
{
static_assert(
net::is_dynamic_buffer<DynamicBuffer>::value,
"DynamicBuffer type requirements not met");
auto const initial_size = (std::min)(
+tcp_frame_size,
buffer.max_size() - buffer.size());
if(initial_size == 0)
return 1; // buffer is full
return read_size_hint(initial_size);
}
//------------------------------------------------------------------------------
//
// Settings
//
//------------------------------------------------------------------------------
// decorator
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
set_option(decorator opt)
{
impl_->decorator_opt = std::move(opt.d_);
}
// timeout
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
get_option(timeout& opt)
{
opt = impl_->timeout_opt;
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
set_option(timeout const& opt)
{
impl_->set_option(opt);
}
//
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
set_option(permessage_deflate const& o)
{
impl_->set_option_pmd(o);
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
get_option(permessage_deflate& o)
{
impl_->get_option_pmd(o);
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
auto_fragment(bool value)
{
impl_->wr_frag_opt = value;
}
template<class NextLayer, bool deflateSupported>
bool
stream<NextLayer, deflateSupported>::
auto_fragment() const
{
return impl_->wr_frag_opt;
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
binary(bool value)
{
impl_->wr_opcode = value ?
detail::opcode::binary :
detail::opcode::text;
}
template<class NextLayer, bool deflateSupported>
bool
stream<NextLayer, deflateSupported>::
binary() const
{
return impl_->wr_opcode == detail::opcode::binary;
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
control_callback(std::function<
void(frame_type, string_view)> cb)
{
impl_->ctrl_cb = std::move(cb);
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
control_callback()
{
impl_->ctrl_cb = {};
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
read_message_max(std::size_t amount)
{
impl_->rd_msg_max = amount;
}
template<class NextLayer, bool deflateSupported>
std::size_t
stream<NextLayer, deflateSupported>::
read_message_max() const
{
return impl_->rd_msg_max;
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
secure_prng(bool value)
{
this->impl_->secure_prng_ = value;
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
write_buffer_bytes(std::size_t amount)
{
if(amount < 8)
BOOST_THROW_EXCEPTION(std::invalid_argument{
"write buffer size underflow"});
impl_->wr_buf_opt = amount;
}
template<class NextLayer, bool deflateSupported>
std::size_t
stream<NextLayer, deflateSupported>::
write_buffer_bytes() const
{
return impl_->wr_buf_opt;
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
text(bool value)
{
impl_->wr_opcode = value ?
detail::opcode::text :
detail::opcode::binary;
}
template<class NextLayer, bool deflateSupported>
bool
stream<NextLayer, deflateSupported>::
text() const
{
return impl_->wr_opcode == detail::opcode::text;
}
//------------------------------------------------------------------------------
// _Fail the WebSocket Connection_
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
do_fail(
std::uint16_t code, // if set, send a close frame first
error_code ev, // error code to use upon success
error_code& ec) // set to the error, else set to ev
{
BOOST_ASSERT(ev);
impl_->change_status(status::closing);
if(code != close_code::none && ! impl_->wr_close)
{
impl_->wr_close = true;
detail::frame_buffer fb;
impl_->template write_close<
flat_static_buffer_base>(fb, code);
net::write(impl_->stream(), fb.data(), ec);
if(impl_->check_stop_now(ec))
return;
}
using beast::websocket::teardown;
teardown(impl_->role, impl_->stream(), ec);
if(ec == net::error::eof)
{
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
ec = {};
}
if(! ec)
ec = ev;
if(ec && ec != error::closed)
impl_->change_status(status::failed);
else
impl_->change_status(status::closed);
impl_->close();
}
} // websocket
} // beast
} // boost
#endif

View File

@@ -0,0 +1,939 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_STREAM_IMPL_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_STREAM_IMPL_HPP
#include <boost/beast/websocket/rfc6455.hpp>
#include <boost/beast/websocket/detail/frame.hpp>
#include <boost/beast/websocket/detail/hybi13.hpp>
#include <boost/beast/websocket/detail/mask.hpp>
#include <boost/beast/websocket/detail/pmd_extension.hpp>
#include <boost/beast/websocket/detail/prng.hpp>
#include <boost/beast/websocket/detail/service.hpp>
#include <boost/beast/websocket/detail/soft_mutex.hpp>
#include <boost/beast/websocket/detail/utf8_checker.hpp>
#include <boost/beast/http/read.hpp>
#include <boost/beast/http/write.hpp>
#include <boost/beast/http/rfc7230.hpp>
#include <boost/beast/core/buffers_cat.hpp>
#include <boost/beast/core/buffers_prefix.hpp>
#include <boost/beast/core/buffers_suffix.hpp>
#include <boost/beast/core/flat_static_buffer.hpp>
#include <boost/beast/core/saved_handler.hpp>
#include <boost/beast/core/static_buffer.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/beast/core/detail/clamp.hpp>
#include <boost/beast/core/detail/type_traits.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/core/empty_value.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>
namespace boost {
namespace beast {
namespace websocket {
template<
class NextLayer, bool deflateSupported>
struct stream<NextLayer, deflateSupported>::impl_type
: boost::empty_value<NextLayer>
, detail::service::impl_type
, detail::impl_base<deflateSupported>
{
NextLayer& stream() noexcept
{
return this->boost::empty_value<
NextLayer>::get();
}
boost::weak_ptr<impl_type>
weak_from_this()
{
return boost::static_pointer_cast<
impl_type>(this->detail::service::
impl_type::shared_from_this());
}
boost::shared_ptr<impl_type>
shared_this()
{
return boost::static_pointer_cast<
impl_type>(this->detail::service::
impl_type::shared_from_this());
}
net::steady_timer timer; // used for timeouts
close_reason cr; // set from received close frame
control_cb_type ctrl_cb; // control callback
std::size_t rd_msg_max /* max message size */ = 16 * 1024 * 1024;
std::uint64_t rd_size /* total size of current message so far */ = 0;
std::uint64_t rd_remain /* message frame bytes left in current frame */ = 0;
detail::frame_header rd_fh; // current frame header
detail::prepared_key rd_key; // current stateful mask key
detail::frame_buffer rd_fb; // to write control frames (during reads)
detail::utf8_checker rd_utf8; // to validate utf8
static_buffer<
+tcp_frame_size> rd_buf; // buffer for reads
detail::opcode rd_op /* current message binary or text */ = detail::opcode::text;
bool rd_cont /* `true` if the next frame is a continuation */ = false;
bool rd_done /* set when a message is done */ = true;
bool rd_close /* did we read a close frame? */ = false;
detail::soft_mutex rd_block; // op currently reading
role_type role /* server or client */ = role_type::client;
status status_ /* state of the object */ = status::closed;
detail::soft_mutex wr_block; // op currently writing
bool wr_close /* did we write a close frame? */ = false;
bool wr_cont /* next write is a continuation */ = false;
bool wr_frag /* autofrag the current message */ = false;
bool wr_frag_opt /* autofrag option setting */ = true;
bool wr_compress /* compress current message */ = false;
detail::opcode wr_opcode /* message type */ = detail::opcode::text;
std::unique_ptr<
std::uint8_t[]> wr_buf; // write buffer
std::size_t wr_buf_size /* write buffer size (current message) */ = 0;
std::size_t wr_buf_opt /* write buffer size option setting */ = 4096;
detail::fh_buffer wr_fb; // header buffer used for writes
saved_handler op_rd; // paused read op
saved_handler op_wr; // paused write op
saved_handler op_ping; // paused ping op
saved_handler op_idle_ping; // paused idle ping op
saved_handler op_close; // paused close op
saved_handler op_r_rd; // paused read op (async read)
saved_handler op_r_close; // paused close op (async read)
bool idle_pinging = false;
bool secure_prng_ = true;
bool ec_delivered = false;
bool timed_out = false;
int idle_counter = 0;
detail::decorator decorator_opt; // Decorator for HTTP messages
timeout timeout_opt; // Timeout/idle settings
template<class... Args>
impl_type(Args&&... args)
: boost::empty_value<NextLayer>(
boost::empty_init_t{},
std::forward<Args>(args)...)
, detail::service::impl_type(
this->boost::empty_value<NextLayer>::get().get_executor().context())
, timer(this->boost::empty_value<NextLayer>::get().get_executor())
{
timeout_opt.handshake_timeout = none();
timeout_opt.idle_timeout = none();
timeout_opt.keep_alive_pings = false;
}
void
shutdown() override
{
op_rd.reset();
op_wr.reset();
op_ping.reset();
op_idle_ping.reset();
op_close.reset();
op_r_rd.reset();
op_r_close.reset();
}
void
open(role_type role_)
{
// VFALCO TODO analyze and remove dupe code in reset()
timer.expires_at(never());
timed_out = false;
cr.code = close_code::none;
role = role_;
status_ = status::open;
rd_remain = 0;
rd_cont = false;
rd_done = true;
// Can't clear this because accept uses it
//rd_buf.reset();
rd_fh.fin = false;
rd_close = false;
wr_close = false;
// These should not be necessary, because all completion
// handlers must be allowed to execute otherwise the
// stream exhibits undefined behavior.
wr_block.reset();
rd_block.reset();
wr_cont = false;
wr_buf_size = 0;
this->open_pmd(role);
}
void
close()
{
timer.cancel();
wr_buf.reset();
this->close_pmd();
}
void
reset()
{
BOOST_ASSERT(status_ != status::open);
timer.expires_at(never());
cr.code = close_code::none;
rd_remain = 0;
rd_cont = false;
rd_done = true;
rd_buf.consume(rd_buf.size());
rd_fh.fin = false;
rd_close = false;
wr_close = false;
wr_cont = false;
// These should not be necessary, because all completion
// handlers must be allowed to execute otherwise the
// stream exhibits undefined behavior.
wr_block.reset();
rd_block.reset();
// VFALCO Is this needed?
timer.cancel();
}
// Called before each write frame
void
begin_msg()
{
wr_frag = wr_frag_opt;
// Maintain the write buffer
if( this->pmd_enabled() ||
role == role_type::client)
{
if(! wr_buf ||
wr_buf_size != wr_buf_opt)
{
wr_buf_size = wr_buf_opt;
wr_buf = boost::make_unique_noinit<
std::uint8_t[]>(wr_buf_size);
}
}
else
{
wr_buf_size = wr_buf_opt;
wr_buf.reset();
}
}
//--------------------------------------------------------------------------
template<class Decorator>
request_type
build_request(
detail::sec_ws_key_type& key,
string_view host, string_view target,
Decorator const& decorator);
void
on_response(
response_type const& res,
detail::sec_ws_key_type const& key,
error_code& ec);
template<class Body, class Allocator, class Decorator>
response_type
build_response(
http::request<Body,
http::basic_fields<Allocator>> const& req,
Decorator const& decorator,
error_code& result);
// Attempt to read a complete frame header.
// Returns `false` if more bytes are needed
template<class DynamicBuffer>
bool
parse_fh(detail::frame_header& fh,
DynamicBuffer& b, error_code& ec);
std::uint32_t
create_mask()
{
auto g = detail::make_prng(secure_prng_);
for(;;)
if(auto key = g())
return key;
}
std::size_t
read_size_hint(std::size_t initial_size) const
{
return this->read_size_hint_pmd(
initial_size, rd_done, rd_remain, rd_fh);
}
template<class DynamicBuffer>
std::size_t
read_size_hint_db(DynamicBuffer& buffer) const
{
auto const initial_size = (std::min)(
+tcp_frame_size,
buffer.max_size() - buffer.size());
if(initial_size == 0)
return 1; // buffer is full
return this->read_size_hint(initial_size);
}
template<class DynamicBuffer>
void
write_ping(DynamicBuffer& db,
detail::opcode code, ping_data const& data);
template<class DynamicBuffer>
void
write_close(DynamicBuffer& db, close_reason const& cr);
//--------------------------------------------------------------------------
void
set_option(timeout const& opt)
{
if( opt.handshake_timeout == none() &&
opt.idle_timeout == none())
{
// turn timer off
timer.cancel();
timer.expires_at(never());
}
timeout_opt = opt;
}
// Determine if an operation should stop and
// deliver an error code to the completion handler.
//
// This function must be called at the beginning
// of every composed operation, and every time a
// composed operation receives an intermediate
// completion.
//
bool
check_stop_now(error_code& ec)
{
// Deliver the timeout to the first caller
if(timed_out)
{
timed_out = false;
ec = beast::error::timeout;
return true;
}
// If the stream is closed then abort
if( status_ == status::closed ||
status_ == status::failed)
{
//BOOST_ASSERT(ec_delivered);
ec = net::error::operation_aborted;
return true;
}
// If no error then keep going
if(! ec)
return false;
// Is this the first error seen?
if(ec_delivered)
{
// No, so abort
ec = net::error::operation_aborted;
return true;
}
// Deliver the error to the completion handler
ec_delivered = true;
if(status_ != status::closed)
status_ = status::failed;
return true;
}
// Change the status of the stream
void
change_status(status new_status)
{
switch(new_status)
{
case status::handshake:
break;
case status::open:
break;
case status::closing:
//BOOST_ASSERT(status_ == status::open);
break;
case status::failed:
case status::closed:
// this->close(); // Is this right?
break;
default:
break;
}
status_ = new_status;
}
// Called to disarm the idle timeout counter
void
reset_idle()
{
idle_counter = 0;
}
// Maintain the expiration timer
template<class Executor>
void
update_timer(Executor const& ex)
{
switch(status_)
{
case status::handshake:
BOOST_ASSERT(idle_counter == 0);
if(! is_timer_set() &&
timeout_opt.handshake_timeout != none())
{
timer.expires_after(
timeout_opt.handshake_timeout);
timer.async_wait(
timeout_handler<Executor>(
ex, this->weak_from_this()));
}
break;
case status::open:
if(timeout_opt.idle_timeout != none())
{
idle_counter = 0;
if(timeout_opt.keep_alive_pings)
timer.expires_after(
timeout_opt.idle_timeout / 2);
else
timer.expires_after(
timeout_opt.idle_timeout);
timer.async_wait(
timeout_handler<Executor>(
ex, this->weak_from_this()));
}
else
{
timer.cancel();
timer.expires_at(never());
}
break;
case status::closing:
if(timeout_opt.handshake_timeout != none())
{
idle_counter = 0;
timer.expires_after(
timeout_opt.handshake_timeout);
timer.async_wait(
timeout_handler<Executor>(
ex, this->weak_from_this()));
}
else
{
BOOST_ASSERT(! is_timer_set());
}
break;
case status::failed:
case status::closed:
// this->close(); // Is this right?
timer.cancel();
timer.expires_at(never());
break;
}
}
private:
bool
is_timer_set() const
{
return timer.expiry() != never();
}
template<class Executor>
class timeout_handler
: boost::empty_value<Executor>
{
boost::weak_ptr<impl_type> wp_;
public:
timeout_handler(
Executor const& ex,
boost::weak_ptr<impl_type>&& wp)
: boost::empty_value<Executor>(
boost::empty_init_t{}, ex)
, wp_(std::move(wp))
{
}
using executor_type = Executor;
executor_type
get_executor() const noexcept
{
return this->get();
}
void
operator()(error_code ec)
{
// timer canceled?
if(ec == net::error::operation_aborted)
return;
BOOST_ASSERT(! ec);
// stream destroyed?
auto sp = wp_.lock();
if(! sp)
return;
auto& impl = *sp;
switch(impl.status_)
{
case status::handshake:
impl.timed_out = true;
close_socket(get_lowest_layer(impl.stream()));
return;
case status::open:
// timeout was disabled
if(impl.timeout_opt.idle_timeout == none())
return;
if( impl.timeout_opt.keep_alive_pings &&
impl.idle_counter < 1)
{
idle_ping_op<Executor>(sp, get_executor());
++impl.idle_counter;
impl.timer.expires_after(
impl.timeout_opt.idle_timeout / 2);
impl.timer.async_wait(std::move(*this));
return;
}
// timeout
impl.timed_out = true;
close_socket(get_lowest_layer(impl.stream()));
return;
case status::closing:
impl.timed_out = true;
close_socket(get_lowest_layer(impl.stream()));
return;
case status::closed:
case status::failed:
// nothing to do?
return;
}
}
};
};
//--------------------------------------------------------------------------
//
// client
//
//--------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
template<class Decorator>
request_type
stream<NextLayer, deflateSupported>::impl_type::
build_request(
detail::sec_ws_key_type& key,
string_view host, string_view target,
Decorator const& decorator)
{
request_type req;
req.target(target);
req.version(11);
req.method(http::verb::get);
req.set(http::field::host, host);
req.set(http::field::upgrade, "websocket");
req.set(http::field::connection, "upgrade");
detail::make_sec_ws_key(key);
req.set(http::field::sec_websocket_key, key);
req.set(http::field::sec_websocket_version, "13");
this->build_request_pmd(req);
decorator_opt(req);
decorator(req);
if(! req.count(http::field::user_agent))
req.set(http::field::user_agent,
BOOST_BEAST_VERSION_STRING);
return req;
}
// Called when the WebSocket Upgrade response is received
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::impl_type::
on_response(
response_type const& res,
detail::sec_ws_key_type const& key,
error_code& ec)
{
auto const err =
[&](error e)
{
ec = e;
};
if(res.result() != http::status::switching_protocols)
return err(error::upgrade_declined);
if(res.version() != 11)
return err(error::bad_http_version);
{
auto const it = res.find(http::field::connection);
if(it == res.end())
return err(error::no_connection);
if(! http::token_list{it->value()}.exists("upgrade"))
return err(error::no_connection_upgrade);
}
{
auto const it = res.find(http::field::upgrade);
if(it == res.end())
return err(error::no_upgrade);
if(! http::token_list{it->value()}.exists("websocket"))
return err(error::no_upgrade_websocket);
}
{
auto const it = res.find(
http::field::sec_websocket_accept);
if(it == res.end())
return err(error::no_sec_accept);
detail::sec_ws_accept_type acc;
detail::make_sec_ws_accept(acc, key);
if(acc.compare(it->value()) != 0)
return err(error::bad_sec_accept);
}
ec = {};
this->on_response_pmd(res);
this->open(role_type::client);
}
//------------------------------------------------------------------------------
// Attempt to read a complete frame header.
// Returns `false` if more bytes are needed
template<class NextLayer, bool deflateSupported>
template<class DynamicBuffer>
bool
stream<NextLayer, deflateSupported>::impl_type::
parse_fh(
detail::frame_header& fh,
DynamicBuffer& b,
error_code& ec)
{
if(buffer_bytes(b.data()) < 2)
{
// need more bytes
ec = {};
return false;
}
buffers_suffix<typename
DynamicBuffer::const_buffers_type> cb{
b.data()};
std::size_t need;
{
std::uint8_t tmp[2];
cb.consume(net::buffer_copy(
net::buffer(tmp), cb));
fh.len = tmp[1] & 0x7f;
switch(fh.len)
{
case 126: need = 2; break;
case 127: need = 8; break;
default:
need = 0;
}
fh.mask = (tmp[1] & 0x80) != 0;
if(fh.mask)
need += 4;
if(buffer_bytes(cb) < need)
{
// need more bytes
ec = {};
return false;
}
fh.op = static_cast<
detail::opcode>(tmp[0] & 0x0f);
fh.fin = (tmp[0] & 0x80) != 0;
fh.rsv1 = (tmp[0] & 0x40) != 0;
fh.rsv2 = (tmp[0] & 0x20) != 0;
fh.rsv3 = (tmp[0] & 0x10) != 0;
}
switch(fh.op)
{
case detail::opcode::binary:
case detail::opcode::text:
if(rd_cont)
{
// new data frame when continuation expected
ec = error::bad_data_frame;
return false;
}
if(fh.rsv2 || fh.rsv3 ||
! this->rd_deflated(fh.rsv1))
{
// reserved bits not cleared
ec = error::bad_reserved_bits;
return false;
}
break;
case detail::opcode::cont:
if(! rd_cont)
{
// continuation without an active message
ec = error::bad_continuation;
return false;
}
if(fh.rsv1 || fh.rsv2 || fh.rsv3)
{
// reserved bits not cleared
ec = error::bad_reserved_bits;
return false;
}
break;
default:
if(detail::is_reserved(fh.op))
{
// reserved opcode
ec = error::bad_opcode;
return false;
}
if(! fh.fin)
{
// fragmented control message
ec = error::bad_control_fragment;
return false;
}
if(fh.len > 125)
{
// invalid length for control message
ec = error::bad_control_size;
return false;
}
if(fh.rsv1 || fh.rsv2 || fh.rsv3)
{
// reserved bits not cleared
ec = error::bad_reserved_bits;
return false;
}
break;
}
if(role == role_type::server && ! fh.mask)
{
// unmasked frame from client
ec = error::bad_unmasked_frame;
return false;
}
if(role == role_type::client && fh.mask)
{
// masked frame from server
ec = error::bad_masked_frame;
return false;
}
if(detail::is_control(fh.op) &&
buffer_bytes(cb) < need + fh.len)
{
// Make the entire control frame payload
// get read in before we return `true`
return false;
}
switch(fh.len)
{
case 126:
{
std::uint8_t tmp[2];
BOOST_ASSERT(buffer_bytes(cb) >= sizeof(tmp));
cb.consume(net::buffer_copy(net::buffer(tmp), cb));
fh.len = detail::big_uint16_to_native(&tmp[0]);
if(fh.len < 126)
{
// length not canonical
ec = error::bad_size;
return false;
}
break;
}
case 127:
{
std::uint8_t tmp[8];
BOOST_ASSERT(buffer_bytes(cb) >= sizeof(tmp));
cb.consume(net::buffer_copy(net::buffer(tmp), cb));
fh.len = detail::big_uint64_to_native(&tmp[0]);
if(fh.len < 65536)
{
// length not canonical
ec = error::bad_size;
return false;
}
break;
}
}
if(fh.mask)
{
std::uint8_t tmp[4];
BOOST_ASSERT(buffer_bytes(cb) >= sizeof(tmp));
cb.consume(net::buffer_copy(net::buffer(tmp), cb));
fh.key = detail::little_uint32_to_native(&tmp[0]);
detail::prepare_key(rd_key, fh.key);
}
else
{
// initialize this otherwise operator== breaks
fh.key = 0;
}
if(! detail::is_control(fh.op))
{
if(fh.op != detail::opcode::cont)
{
rd_size = 0;
rd_op = fh.op;
}
else
{
if(rd_size > (std::numeric_limits<
std::uint64_t>::max)() - fh.len)
{
// message size exceeds configured limit
ec = error::message_too_big;
return false;
}
}
if(! this->rd_deflated())
{
if(rd_msg_max && beast::detail::sum_exceeds(
rd_size, fh.len, rd_msg_max))
{
// message size exceeds configured limit
ec = error::message_too_big;
return false;
}
}
rd_cont = ! fh.fin;
rd_remain = fh.len;
}
b.consume(b.size() - buffer_bytes(cb));
ec = {};
return true;
}
template<class NextLayer, bool deflateSupported>
template<class DynamicBuffer>
void
stream<NextLayer, deflateSupported>::impl_type::
write_ping(DynamicBuffer& db,
detail::opcode code, ping_data const& data)
{
detail::frame_header fh;
fh.op = code;
fh.fin = true;
fh.rsv1 = false;
fh.rsv2 = false;
fh.rsv3 = false;
fh.len = data.size();
fh.mask = role == role_type::client;
if(fh.mask)
fh.key = create_mask();
detail::write(db, fh);
if(data.empty())
return;
detail::prepared_key key;
if(fh.mask)
detail::prepare_key(key, fh.key);
auto mb = db.prepare(data.size());
net::buffer_copy(mb,
net::const_buffer(
data.data(), data.size()));
if(fh.mask)
detail::mask_inplace(mb, key);
db.commit(data.size());
}
template<class NextLayer, bool deflateSupported>
template<class DynamicBuffer>
void
stream<NextLayer, deflateSupported>::impl_type::
write_close(DynamicBuffer& db, close_reason const& cr)
{
using namespace boost::endian;
detail::frame_header fh;
fh.op = detail::opcode::close;
fh.fin = true;
fh.rsv1 = false;
fh.rsv2 = false;
fh.rsv3 = false;
fh.len = cr.code == close_code::none ?
0 : 2 + cr.reason.size();
if(role == role_type::client)
{
fh.mask = true;
fh.key = create_mask();
}
else
{
fh.mask = false;
}
detail::write(db, fh);
if(cr.code != close_code::none)
{
detail::prepared_key key;
if(fh.mask)
detail::prepare_key(key, fh.key);
{
std::uint8_t tmp[2];
::new(&tmp[0]) big_uint16_buf_t{
(std::uint16_t)cr.code};
auto mb = db.prepare(2);
net::buffer_copy(mb,
net::buffer(tmp));
if(fh.mask)
detail::mask_inplace(mb, key);
db.commit(2);
}
if(! cr.reason.empty())
{
auto mb = db.prepare(cr.reason.size());
net::buffer_copy(mb,
net::const_buffer(
cr.reason.data(), cr.reason.size()));
if(fh.mask)
detail::mask_inplace(mb, key);
db.commit(cr.reason.size());
}
}
}
} // websocket
} // beast
} // boost
#endif

View File

@@ -0,0 +1,198 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_TEARDOWN_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_TEARDOWN_HPP
#include <boost/beast/core/async_base.hpp>
#include <boost/beast/core/bind_handler.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/beast/core/detail/bind_continuation.hpp>
#include <boost/beast/core/detail/type_traits.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/post.hpp>
#include <memory>
namespace boost {
namespace beast {
namespace websocket {
namespace detail {
template<
class Protocol, class Executor,
class Handler>
class teardown_tcp_op
: public beast::async_base<
Handler, beast::executor_type<
net::basic_stream_socket<
Protocol, Executor>>>
, public net::coroutine
{
using socket_type =
net::basic_stream_socket<Protocol, Executor>;
socket_type& s_;
role_type role_;
bool nb_;
public:
template<class Handler_>
teardown_tcp_op(
Handler_&& h,
socket_type& s,
role_type role)
: async_base<Handler,
beast::executor_type<
net::basic_stream_socket<
Protocol, Executor>>>(
std::forward<Handler_>(h),
s.get_executor())
, s_(s)
, role_(role)
{
(*this)({}, 0, false);
}
void
operator()(
error_code ec = {},
std::size_t bytes_transferred = 0,
bool cont = true)
{
BOOST_ASIO_CORO_REENTER(*this)
{
nb_ = s_.non_blocking();
s_.non_blocking(true, ec);
if(ec)
goto upcall;
if(role_ == role_type::server)
s_.shutdown(net::socket_base::shutdown_send, ec);
if(ec)
goto upcall;
for(;;)
{
{
char buf[2048];
s_.read_some(net::buffer(buf), ec);
}
if(ec == net::error::would_block)
{
BOOST_ASIO_CORO_YIELD
s_.async_wait(
net::socket_base::wait_read,
beast::detail::bind_continuation(std::move(*this)));
continue;
}
if(ec)
{
if(ec != net::error::eof)
goto upcall;
ec = {};
break;
}
if(bytes_transferred == 0)
{
// happens sometimes
// https://github.com/boostorg/beast/issues/1373
break;
}
}
if(role_ == role_type::client)
s_.shutdown(net::socket_base::shutdown_send, ec);
if(ec)
goto upcall;
s_.close(ec);
upcall:
if(! cont)
{
BOOST_ASIO_CORO_YIELD
net::post(bind_front_handler(
std::move(*this), ec));
}
{
error_code ignored;
s_.non_blocking(nb_, ignored);
}
this->complete_now(ec);
}
}
};
} // detail
//------------------------------------------------------------------------------
template<class Protocol, class Executor>
void
teardown(
role_type role,
net::basic_stream_socket<
Protocol, Executor>& socket,
error_code& ec)
{
if(role == role_type::server)
socket.shutdown(
net::socket_base::shutdown_send, ec);
if(ec)
return;
for(;;)
{
char buf[2048];
auto const bytes_transferred =
socket.read_some(net::buffer(buf), ec);
if(ec)
{
if(ec != net::error::eof)
return;
ec = {};
break;
}
if(bytes_transferred == 0)
{
// happens sometimes
// https://github.com/boostorg/beast/issues/1373
break;
}
}
if(role == role_type::client)
socket.shutdown(
net::socket_base::shutdown_send, ec);
if(ec)
return;
socket.close(ec);
}
template<
class Protocol, class Executor,
class TeardownHandler>
void
async_teardown(
role_type role,
net::basic_stream_socket<
Protocol, Executor>& socket,
TeardownHandler&& handler)
{
static_assert(beast::detail::is_invocable<
TeardownHandler, void(error_code)>::value,
"TeardownHandler type requirements not met");
detail::teardown_tcp_op<
Protocol,
Executor,
typename std::decay<TeardownHandler>::type>(
std::forward<TeardownHandler>(handler),
socket,
role);
}
} // websocket
} // beast
} // boost
#endif

View File

@@ -0,0 +1,784 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_WRITE_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_WRITE_HPP
#include <boost/beast/websocket/detail/mask.hpp>
#include <boost/beast/core/async_base.hpp>
#include <boost/beast/core/bind_handler.hpp>
#include <boost/beast/core/buffer_traits.hpp>
#include <boost/beast/core/buffers_cat.hpp>
#include <boost/beast/core/buffers_prefix.hpp>
#include <boost/beast/core/buffers_range.hpp>
#include <boost/beast/core/buffers_suffix.hpp>
#include <boost/beast/core/flat_static_buffer.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/beast/core/detail/bind_continuation.hpp>
#include <boost/beast/core/detail/clamp.hpp>
#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/websocket/detail/frame.hpp>
#include <boost/beast/websocket/impl/stream_impl.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/throw_exception.hpp>
#include <algorithm>
#include <memory>
namespace boost {
namespace beast {
namespace websocket {
template<class NextLayer, bool deflateSupported>
template<class Handler, class Buffers>
class stream<NextLayer, deflateSupported>::write_some_op
: public beast::async_base<
Handler, beast::executor_type<stream>>
, public net::coroutine
{
enum
{
do_nomask_nofrag,
do_nomask_frag,
do_mask_nofrag,
do_mask_frag,
do_deflate
};
boost::weak_ptr<impl_type> wp_;
buffers_suffix<Buffers> cb_;
detail::frame_header fh_;
detail::prepared_key key_;
std::size_t bytes_transferred_ = 0;
std::size_t remain_;
std::size_t in_;
int how_;
bool fin_;
bool more_ = false; // for ubsan
bool cont_ = false;
public:
static constexpr int id = 2; // for soft_mutex
template<class Handler_>
write_some_op(
Handler_&& h,
boost::shared_ptr<impl_type> const& sp,
bool fin,
Buffers const& bs)
: beast::async_base<Handler,
beast::executor_type<stream>>(
std::forward<Handler_>(h),
sp->stream().get_executor())
, wp_(sp)
, cb_(bs)
, fin_(fin)
{
auto& impl = *sp;
// Set up the outgoing frame header
if(! impl.wr_cont)
{
impl.begin_msg();
fh_.rsv1 = impl.wr_compress;
}
else
{
fh_.rsv1 = false;
}
fh_.rsv2 = false;
fh_.rsv3 = false;
fh_.op = impl.wr_cont ?
detail::opcode::cont : impl.wr_opcode;
fh_.mask =
impl.role == role_type::client;
// Choose a write algorithm
if(impl.wr_compress)
{
how_ = do_deflate;
}
else if(! fh_.mask)
{
if(! impl.wr_frag)
{
how_ = do_nomask_nofrag;
}
else
{
BOOST_ASSERT(impl.wr_buf_size != 0);
remain_ = buffer_bytes(cb_);
if(remain_ > impl.wr_buf_size)
how_ = do_nomask_frag;
else
how_ = do_nomask_nofrag;
}
}
else
{
if(! impl.wr_frag)
{
how_ = do_mask_nofrag;
}
else
{
BOOST_ASSERT(impl.wr_buf_size != 0);
remain_ = buffer_bytes(cb_);
if(remain_ > impl.wr_buf_size)
how_ = do_mask_frag;
else
how_ = do_mask_nofrag;
}
}
(*this)({}, 0, false);
}
void operator()(
error_code ec = {},
std::size_t bytes_transferred = 0,
bool cont = true);
};
template<class NextLayer, bool deflateSupported>
template<class Buffers, class Handler>
void
stream<NextLayer, deflateSupported>::
write_some_op<Buffers, Handler>::
operator()(
error_code ec,
std::size_t bytes_transferred,
bool cont)
{
using beast::detail::clamp;
std::size_t n;
net::mutable_buffer b;
auto sp = wp_.lock();
if(! sp)
{
ec = net::error::operation_aborted;
bytes_transferred_ = 0;
return this->complete(cont, ec, bytes_transferred_);
}
auto& impl = *sp;
BOOST_ASIO_CORO_REENTER(*this)
{
// Acquire the write lock
if(! impl.wr_block.try_lock(this))
{
do_suspend:
BOOST_ASIO_CORO_YIELD
impl.op_wr.emplace(std::move(*this));
impl.wr_block.lock(this);
BOOST_ASIO_CORO_YIELD
net::post(std::move(*this));
BOOST_ASSERT(impl.wr_block.is_locked(this));
}
if(impl.check_stop_now(ec))
goto upcall;
//------------------------------------------------------------------
if(how_ == do_nomask_nofrag)
{
// send a single frame
fh_.fin = fin_;
fh_.len = buffer_bytes(cb_);
impl.wr_fb.clear();
detail::write<flat_static_buffer_base>(
impl.wr_fb, fh_);
impl.wr_cont = ! fin_;
BOOST_ASIO_CORO_YIELD
net::async_write(impl.stream(),
buffers_cat(impl.wr_fb.data(), cb_),
beast::detail::bind_continuation(std::move(*this)));
bytes_transferred_ += clamp(fh_.len);
if(impl.check_stop_now(ec))
goto upcall;
goto upcall;
}
//------------------------------------------------------------------
if(how_ == do_nomask_frag)
{
// send multiple frames
for(;;)
{
n = clamp(remain_, impl.wr_buf_size);
fh_.len = n;
remain_ -= n;
fh_.fin = fin_ ? remain_ == 0 : false;
impl.wr_fb.clear();
detail::write<flat_static_buffer_base>(
impl.wr_fb, fh_);
impl.wr_cont = ! fin_;
// Send frame
BOOST_ASIO_CORO_YIELD
net::async_write(impl.stream(), buffers_cat(
impl.wr_fb.data(),
buffers_prefix(clamp(fh_.len), cb_)),
beast::detail::bind_continuation(std::move(*this)));
n = clamp(fh_.len); // restore `n` on yield
bytes_transferred_ += n;
if(impl.check_stop_now(ec))
goto upcall;
if(remain_ == 0)
break;
cb_.consume(n);
fh_.op = detail::opcode::cont;
// Give up the write lock in between each frame
// so that outgoing control frames might be sent.
impl.wr_block.unlock(this);
if( impl.op_close.maybe_invoke()
|| impl.op_idle_ping.maybe_invoke()
|| impl.op_rd.maybe_invoke()
|| impl.op_ping.maybe_invoke())
{
BOOST_ASSERT(impl.wr_block.is_locked());
goto do_suspend;
}
impl.wr_block.lock(this);
}
goto upcall;
}
//------------------------------------------------------------------
if(how_ == do_mask_nofrag)
{
// send a single frame using multiple writes
remain_ = beast::buffer_bytes(cb_);
fh_.fin = fin_;
fh_.len = remain_;
fh_.key = impl.create_mask();
detail::prepare_key(key_, fh_.key);
impl.wr_fb.clear();
detail::write<flat_static_buffer_base>(
impl.wr_fb, fh_);
n = clamp(remain_, impl.wr_buf_size);
net::buffer_copy(net::buffer(
impl.wr_buf.get(), n), cb_);
detail::mask_inplace(net::buffer(
impl.wr_buf.get(), n), key_);
remain_ -= n;
impl.wr_cont = ! fin_;
// write frame header and some payload
BOOST_ASIO_CORO_YIELD
net::async_write(impl.stream(), buffers_cat(
impl.wr_fb.data(),
net::buffer(impl.wr_buf.get(), n)),
beast::detail::bind_continuation(std::move(*this)));
// VFALCO What about consuming the buffer on error?
bytes_transferred_ +=
bytes_transferred - impl.wr_fb.size();
if(impl.check_stop_now(ec))
goto upcall;
while(remain_ > 0)
{
cb_.consume(impl.wr_buf_size);
n = clamp(remain_, impl.wr_buf_size);
net::buffer_copy(net::buffer(
impl.wr_buf.get(), n), cb_);
detail::mask_inplace(net::buffer(
impl.wr_buf.get(), n), key_);
remain_ -= n;
// write more payload
BOOST_ASIO_CORO_YIELD
net::async_write(impl.stream(),
net::buffer(impl.wr_buf.get(), n),
beast::detail::bind_continuation(std::move(*this)));
bytes_transferred_ += bytes_transferred;
if(impl.check_stop_now(ec))
goto upcall;
}
goto upcall;
}
//------------------------------------------------------------------
if(how_ == do_mask_frag)
{
// send multiple frames
for(;;)
{
n = clamp(remain_, impl.wr_buf_size);
remain_ -= n;
fh_.len = n;
fh_.key = impl.create_mask();
fh_.fin = fin_ ? remain_ == 0 : false;
detail::prepare_key(key_, fh_.key);
net::buffer_copy(net::buffer(
impl.wr_buf.get(), n), cb_);
detail::mask_inplace(net::buffer(
impl.wr_buf.get(), n), key_);
impl.wr_fb.clear();
detail::write<flat_static_buffer_base>(
impl.wr_fb, fh_);
impl.wr_cont = ! fin_;
// Send frame
BOOST_ASIO_CORO_YIELD
net::async_write(impl.stream(), buffers_cat(
impl.wr_fb.data(),
net::buffer(impl.wr_buf.get(), n)),
beast::detail::bind_continuation(std::move(*this)));
n = bytes_transferred - impl.wr_fb.size();
bytes_transferred_ += n;
if(impl.check_stop_now(ec))
goto upcall;
if(remain_ == 0)
break;
cb_.consume(n);
fh_.op = detail::opcode::cont;
// Give up the write lock in between each frame
// so that outgoing control frames might be sent.
impl.wr_block.unlock(this);
if( impl.op_close.maybe_invoke()
|| impl.op_idle_ping.maybe_invoke()
|| impl.op_rd.maybe_invoke()
|| impl.op_ping.maybe_invoke())
{
BOOST_ASSERT(impl.wr_block.is_locked());
goto do_suspend;
}
impl.wr_block.lock(this);
}
goto upcall;
}
//------------------------------------------------------------------
if(how_ == do_deflate)
{
// send compressed frames
for(;;)
{
b = net::buffer(impl.wr_buf.get(),
impl.wr_buf_size);
more_ = impl.deflate(b, cb_, fin_, in_, ec);
if(impl.check_stop_now(ec))
goto upcall;
n = buffer_bytes(b);
if(n == 0)
{
// The input was consumed, but there is
// no output due to compression latency.
BOOST_ASSERT(! fin_);
BOOST_ASSERT(buffer_bytes(cb_) == 0);
goto upcall;
}
if(fh_.mask)
{
fh_.key = impl.create_mask();
detail::prepared_key key;
detail::prepare_key(key, fh_.key);
detail::mask_inplace(b, key);
}
fh_.fin = ! more_;
fh_.len = n;
impl.wr_fb.clear();
detail::write<
flat_static_buffer_base>(impl.wr_fb, fh_);
impl.wr_cont = ! fin_;
// Send frame
BOOST_ASIO_CORO_YIELD
net::async_write(impl.stream(), buffers_cat(
impl.wr_fb.data(), b),
beast::detail::bind_continuation(std::move(*this)));
bytes_transferred_ += in_;
if(impl.check_stop_now(ec))
goto upcall;
if(more_)
{
fh_.op = detail::opcode::cont;
fh_.rsv1 = false;
// Give up the write lock in between each frame
// so that outgoing control frames might be sent.
impl.wr_block.unlock(this);
if( impl.op_close.maybe_invoke()
|| impl.op_idle_ping.maybe_invoke()
|| impl.op_rd.maybe_invoke()
|| impl.op_ping.maybe_invoke())
{
BOOST_ASSERT(impl.wr_block.is_locked());
goto do_suspend;
}
impl.wr_block.lock(this);
}
else
{
if(fh_.fin)
impl.do_context_takeover_write(impl.role);
goto upcall;
}
}
}
//--------------------------------------------------------------------------
upcall:
impl.wr_block.unlock(this);
impl.op_close.maybe_invoke()
|| impl.op_idle_ping.maybe_invoke()
|| impl.op_rd.maybe_invoke()
|| impl.op_ping.maybe_invoke();
this->complete(cont, ec, bytes_transferred_);
}
}
template<class NextLayer, bool deflateSupported>
struct stream<NextLayer, deflateSupported>::
run_write_some_op
{
template<
class WriteHandler,
class ConstBufferSequence>
void
operator()(
WriteHandler&& h,
boost::shared_ptr<impl_type> const& sp,
bool fin,
ConstBufferSequence const& b)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
static_assert(
beast::detail::is_invocable<WriteHandler,
void(error_code, std::size_t)>::value,
"WriteHandler type requirements not met");
write_some_op<
typename std::decay<WriteHandler>::type,
ConstBufferSequence>(
std::forward<WriteHandler>(h),
sp,
fin,
b);
}
};
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
template<class ConstBufferSequence>
std::size_t
stream<NextLayer, deflateSupported>::
write_some(bool fin, ConstBufferSequence const& buffers)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
error_code ec;
auto const bytes_transferred =
write_some(fin, buffers, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
return bytes_transferred;
}
template<class NextLayer, bool deflateSupported>
template<class ConstBufferSequence>
std::size_t
stream<NextLayer, deflateSupported>::
write_some(bool fin,
ConstBufferSequence const& buffers, error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
using beast::detail::clamp;
auto& impl = *impl_;
std::size_t bytes_transferred = 0;
ec = {};
if(impl.check_stop_now(ec))
return bytes_transferred;
detail::frame_header fh;
if(! impl.wr_cont)
{
impl.begin_msg();
fh.rsv1 = impl.wr_compress;
}
else
{
fh.rsv1 = false;
}
fh.rsv2 = false;
fh.rsv3 = false;
fh.op = impl.wr_cont ?
detail::opcode::cont : impl.wr_opcode;
fh.mask = impl.role == role_type::client;
auto remain = buffer_bytes(buffers);
if(impl.wr_compress)
{
buffers_suffix<
ConstBufferSequence> cb(buffers);
for(;;)
{
auto b = net::buffer(
impl.wr_buf.get(), impl.wr_buf_size);
auto const more = impl.deflate(
b, cb, fin, bytes_transferred, ec);
if(impl.check_stop_now(ec))
return bytes_transferred;
auto const n = buffer_bytes(b);
if(n == 0)
{
// The input was consumed, but there
// is no output due to compression
// latency.
BOOST_ASSERT(! fin);
BOOST_ASSERT(buffer_bytes(cb) == 0);
fh.fin = false;
break;
}
if(fh.mask)
{
fh.key = this->impl_->create_mask();
detail::prepared_key key;
detail::prepare_key(key, fh.key);
detail::mask_inplace(b, key);
}
fh.fin = ! more;
fh.len = n;
detail::fh_buffer fh_buf;
detail::write<
flat_static_buffer_base>(fh_buf, fh);
impl.wr_cont = ! fin;
net::write(impl.stream(),
buffers_cat(fh_buf.data(), b), ec);
if(impl.check_stop_now(ec))
return bytes_transferred;
if(! more)
break;
fh.op = detail::opcode::cont;
fh.rsv1 = false;
}
if(fh.fin)
impl.do_context_takeover_write(impl.role);
}
else if(! fh.mask)
{
if(! impl.wr_frag)
{
// no mask, no autofrag
fh.fin = fin;
fh.len = remain;
detail::fh_buffer fh_buf;
detail::write<
flat_static_buffer_base>(fh_buf, fh);
impl.wr_cont = ! fin;
net::write(impl.stream(),
buffers_cat(fh_buf.data(), buffers), ec);
if(impl.check_stop_now(ec))
return bytes_transferred;
bytes_transferred += remain;
}
else
{
// no mask, autofrag
BOOST_ASSERT(impl.wr_buf_size != 0);
buffers_suffix<
ConstBufferSequence> cb{buffers};
for(;;)
{
auto const n = clamp(remain, impl.wr_buf_size);
remain -= n;
fh.len = n;
fh.fin = fin ? remain == 0 : false;
detail::fh_buffer fh_buf;
detail::write<
flat_static_buffer_base>(fh_buf, fh);
impl.wr_cont = ! fin;
net::write(impl.stream(),
beast::buffers_cat(fh_buf.data(),
beast::buffers_prefix(n, cb)), ec);
bytes_transferred += n;
if(impl.check_stop_now(ec))
return bytes_transferred;
if(remain == 0)
break;
fh.op = detail::opcode::cont;
cb.consume(n);
}
}
}
else if(! impl.wr_frag)
{
// mask, no autofrag
fh.fin = fin;
fh.len = remain;
fh.key = this->impl_->create_mask();
detail::prepared_key key;
detail::prepare_key(key, fh.key);
detail::fh_buffer fh_buf;
detail::write<
flat_static_buffer_base>(fh_buf, fh);
buffers_suffix<
ConstBufferSequence> cb{buffers};
{
auto const n =
clamp(remain, impl.wr_buf_size);
auto const b =
net::buffer(impl.wr_buf.get(), n);
net::buffer_copy(b, cb);
cb.consume(n);
remain -= n;
detail::mask_inplace(b, key);
impl.wr_cont = ! fin;
net::write(impl.stream(),
buffers_cat(fh_buf.data(), b), ec);
bytes_transferred += n;
if(impl.check_stop_now(ec))
return bytes_transferred;
}
while(remain > 0)
{
auto const n =
clamp(remain, impl.wr_buf_size);
auto const b =
net::buffer(impl.wr_buf.get(), n);
net::buffer_copy(b, cb);
cb.consume(n);
remain -= n;
detail::mask_inplace(b, key);
net::write(impl.stream(), b, ec);
bytes_transferred += n;
if(impl.check_stop_now(ec))
return bytes_transferred;
}
}
else
{
// mask, autofrag
BOOST_ASSERT(impl.wr_buf_size != 0);
buffers_suffix<
ConstBufferSequence> cb(buffers);
for(;;)
{
fh.key = this->impl_->create_mask();
detail::prepared_key key;
detail::prepare_key(key, fh.key);
auto const n =
clamp(remain, impl.wr_buf_size);
auto const b =
net::buffer(impl.wr_buf.get(), n);
net::buffer_copy(b, cb);
detail::mask_inplace(b, key);
fh.len = n;
remain -= n;
fh.fin = fin ? remain == 0 : false;
impl.wr_cont = ! fh.fin;
detail::fh_buffer fh_buf;
detail::write<
flat_static_buffer_base>(fh_buf, fh);
net::write(impl.stream(),
buffers_cat(fh_buf.data(), b), ec);
bytes_transferred += n;
if(impl.check_stop_now(ec))
return bytes_transferred;
if(remain == 0)
break;
fh.op = detail::opcode::cont;
cb.consume(n);
}
}
return bytes_transferred;
}
template<class NextLayer, bool deflateSupported>
template<class ConstBufferSequence, class WriteHandler>
BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
stream<NextLayer, deflateSupported>::
async_write_some(bool fin,
ConstBufferSequence const& bs, WriteHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
return net::async_initiate<
WriteHandler,
void(error_code, std::size_t)>(
run_write_some_op{},
handler,
impl_,
fin,
bs);
}
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
template<class ConstBufferSequence>
std::size_t
stream<NextLayer, deflateSupported>::
write(ConstBufferSequence const& buffers)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
error_code ec;
auto const bytes_transferred = write(buffers, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
return bytes_transferred;
}
template<class NextLayer, bool deflateSupported>
template<class ConstBufferSequence>
std::size_t
stream<NextLayer, deflateSupported>::
write(ConstBufferSequence const& buffers, error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
"SyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
return write_some(true, buffers, ec);
}
template<class NextLayer, bool deflateSupported>
template<class ConstBufferSequence, class WriteHandler>
BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
stream<NextLayer, deflateSupported>::
async_write(
ConstBufferSequence const& bs, WriteHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
return net::async_initiate<
WriteHandler,
void(error_code, std::size_t)>(
run_write_some_op{},
handler,
impl_,
true,
bs);
}
} // websocket
} // beast
} // boost
#endif