forked from boostorg/beast
Add websocket::stream timeouts
This commit is contained in:
1 parent
dfd08bf6ae
commit
f21358186e
38 files changed
+3305
-2393
No files matched your search
@@ -10,16 +10,32 @@
|
||||
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_STREAM_IMPL_HPP
|
||||
#define 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/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/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/soft_mutex.hpp>
|
||||
#include <boost/beast/websocket/detail/utf8_checker.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/optional.hpp>
|
||||
|
||||
namespace boost {
|
||||
@@ -29,7 +45,7 @@ namespace websocket {
|
||||
template<
|
||||
class NextLayer, bool deflateSupported>
|
||||
struct stream<NextLayer, deflateSupported>::impl_type
|
||||
: std::enable_shared_from_this<impl_type>
|
||||
: boost::enable_shared_from_this<impl_type>
|
||||
, detail::impl_base<deflateSupported>
|
||||
{
|
||||
NextLayer stream; // The underlying stream
|
||||
@@ -68,17 +84,25 @@ struct stream<NextLayer, deflateSupported>::impl_type
|
||||
std::size_t wr_buf_opt /* write buffer size option setting */ = 4096;
|
||||
detail::fh_buffer wr_fb; // header buffer used for writes
|
||||
|
||||
saved_handler paused_rd; // paused read op
|
||||
saved_handler paused_wr; // paused write op
|
||||
saved_handler paused_ping; // paused ping op
|
||||
saved_handler paused_close; // paused close op
|
||||
saved_handler paused_r_rd; // paused read op (async read)
|
||||
saved_handler paused_r_close; // paused close op (async read)
|
||||
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)
|
||||
|
||||
boost::optional<bool> tm_auto_ping;
|
||||
bool tm_opt /* true if auto-timeout option is set */ = false;
|
||||
bool tm_idle; // set to false on incoming frames
|
||||
time_point::duration tm_dur /* duration of timer */ = std::chrono::seconds(1);
|
||||
bool secure_prng_ = true;
|
||||
|
||||
bool ec_delivered = false;
|
||||
bool timed_out = false;
|
||||
int idle_counter = 0;
|
||||
|
||||
// settings
|
||||
|
||||
timeout timeout_opt;
|
||||
|
||||
//
|
||||
|
||||
template<class... Args>
|
||||
impl_type(Args&&... args)
|
||||
@@ -91,6 +115,9 @@ struct stream<NextLayer, deflateSupported>::impl_type
|
||||
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;
|
||||
@@ -106,13 +133,10 @@ struct stream<NextLayer, deflateSupported>::impl_type
|
||||
// stream exhibits undefined behavior.
|
||||
wr_block.reset();
|
||||
rd_block.reset();
|
||||
cr.code = close_code::none;
|
||||
|
||||
wr_cont = false;
|
||||
wr_buf_size = 0;
|
||||
|
||||
tm_idle = false;
|
||||
|
||||
this->open_pmd(role);
|
||||
}
|
||||
|
||||
@@ -128,6 +152,8 @@ struct stream<NextLayer, deflateSupported>::impl_type
|
||||
reset()
|
||||
{
|
||||
BOOST_ASSERT(status_ != status::open);
|
||||
timer.expires_at(never());
|
||||
cr.code = close_code::none;
|
||||
rd_remain = 0;
|
||||
rd_cont = false;
|
||||
rd_done = true;
|
||||
@@ -141,8 +167,6 @@ struct stream<NextLayer, deflateSupported>::impl_type
|
||||
// stream exhibits undefined behavior.
|
||||
wr_block.reset();
|
||||
rd_block.reset();
|
||||
cr.code = close_code::none;
|
||||
tm_idle = false;
|
||||
|
||||
// VFALCO Is this needed?
|
||||
timer.cancel();
|
||||
@@ -173,126 +197,88 @@ struct stream<NextLayer, deflateSupported>::impl_type
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
bool
|
||||
is_timer_set() const
|
||||
{
|
||||
return timer.expiry() == never();
|
||||
}
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
// returns `true` if we try sending a ping and
|
||||
// getting a pong before closing an idle stream.
|
||||
bool
|
||||
is_auto_ping_enabled() const
|
||||
{
|
||||
if(tm_auto_ping.has_value())
|
||||
return *tm_auto_ping;
|
||||
if(role == role_type::server)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
template<class Decorator>
|
||||
request_type
|
||||
build_request(
|
||||
detail::sec_ws_key_type& key,
|
||||
string_view host, string_view target,
|
||||
Decorator const& decorator);
|
||||
|
||||
template<class Executor>
|
||||
class timeout_handler
|
||||
: boost::empty_value<Executor>
|
||||
{
|
||||
std::weak_ptr<impl_type> wp_;
|
||||
|
||||
public:
|
||||
timeout_handler(
|
||||
Executor const& ex,
|
||||
std::shared_ptr<impl_type> const& sp)
|
||||
: boost::empty_value<Executor>(
|
||||
boost::empty_init_t{}, ex)
|
||||
, wp_(sp)
|
||||
{
|
||||
}
|
||||
|
||||
using executor_type = Executor;
|
||||
|
||||
executor_type
|
||||
get_executor() const
|
||||
{
|
||||
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;
|
||||
|
||||
close_socket(get_lowest_layer(impl.stream));
|
||||
#if 0
|
||||
if(! impl.tm_idle)
|
||||
{
|
||||
impl.tm_idle = true;
|
||||
BOOST_VERIFY(
|
||||
impl.timer.expires_after(impl.tm_dur) == 0);
|
||||
return impl.timer.async_wait(std::move(*this));
|
||||
}
|
||||
if(impl.is_auto_ping_enabled())
|
||||
{
|
||||
// send ping
|
||||
}
|
||||
else
|
||||
{
|
||||
// timeout
|
||||
}
|
||||
#endif
|
||||
}
|
||||
};
|
||||
public:
|
||||
|
||||
// called when there is qualified activity
|
||||
void
|
||||
activity()
|
||||
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()
|
||||
{
|
||||
tm_idle = false;
|
||||
auto g = detail::make_prng(secure_prng_);
|
||||
for(;;)
|
||||
if(auto key = g())
|
||||
return key;
|
||||
}
|
||||
|
||||
// Maintain the expiration timer
|
||||
template<class Executor>
|
||||
void
|
||||
update_timer(Executor const& ex)
|
||||
std::size_t
|
||||
read_size_hint(std::size_t initial_size) const
|
||||
{
|
||||
if(role == role_type::server)
|
||||
{
|
||||
if(! is_timer_set())
|
||||
{
|
||||
// turn timer on
|
||||
timer.expires_after(tm_dur);
|
||||
timer.async_wait(
|
||||
timeout_handler<Executor>(
|
||||
ex, this->shared_from_this()));
|
||||
}
|
||||
}
|
||||
else if(tm_opt && ! is_timer_set())
|
||||
{
|
||||
// turn timer on
|
||||
timer.expires_after(tm_dur);
|
||||
timer.async_wait(
|
||||
timeout_handler<Executor>(
|
||||
ex, this->shared_from_this()));
|
||||
}
|
||||
else if(! tm_opt && is_timer_set())
|
||||
{
|
||||
// turn timer off
|
||||
timer.cancel();
|
||||
}
|
||||
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);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
bool ec_delivered = false;
|
||||
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.
|
||||
@@ -305,6 +291,14 @@ public:
|
||||
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)
|
||||
@@ -339,8 +333,11 @@ public:
|
||||
{
|
||||
switch(new_status)
|
||||
{
|
||||
case status::handshake:
|
||||
break;
|
||||
|
||||
case status::closing:
|
||||
BOOST_ASSERT(status_ == status::open);
|
||||
//BOOST_ASSERT(status_ == status::open);
|
||||
break;
|
||||
|
||||
case status::failed:
|
||||
@@ -353,8 +350,639 @@ public:
|
||||
}
|
||||
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;
|
||||
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
|
||||
{
|
||||
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)
|
||||
{
|
||||
// <- send ping
|
||||
|
||||
++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(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);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
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 =
|
||||
[&decorator](response_type& res)
|
||||
{
|
||||
decorator(res);
|
||||
if(! res.count(http::field::server))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// 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_size(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_size(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_size(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_size(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_size(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_size(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_size(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
|
||||
|
||||
Reference in new issue
Block a user