summaryrefslogtreecommitdiff
path: root/boost/beast/websocket/impl/stream.ipp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/beast/websocket/impl/stream.ipp')
-rw-r--r--boost/beast/websocket/impl/stream.ipp545
1 files changed, 361 insertions, 184 deletions
diff --git a/boost/beast/websocket/impl/stream.ipp b/boost/beast/websocket/impl/stream.ipp
index f8e4a5fc01..cf747c230d 100644
--- a/boost/beast/websocket/impl/stream.ipp
+++ b/boost/beast/websocket/impl/stream.ipp
@@ -40,55 +40,20 @@ namespace boost {
namespace beast {
namespace websocket {
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
template<class... Args>
-stream<NextLayer>::
+stream<NextLayer, deflateSupported>::
stream(Args&&... args)
: stream_(std::forward<Args>(args)...)
- , tok_(1)
{
BOOST_ASSERT(rd_buf_.max_size() >=
max_control_frame_size);
}
-template<class NextLayer>
-std::size_t
-stream<NextLayer>::
-read_size_hint(
- std::size_t initial_size) const
-{
- using beast::detail::clamp;
- std::size_t result;
- BOOST_ASSERT(initial_size > 0);
- if(! pmd_ || (! rd_done_ && ! pmd_->rd_set))
- {
- // current message is uncompressed
-
- if(rd_done_)
- {
- // first message frame
- result = initial_size;
- goto done;
- }
- else if(rd_fh_.fin)
- {
- // last message frame
- BOOST_ASSERT(rd_remain_ > 0);
- result = clamp(rd_remain_);
- goto done;
- }
- }
- result = (std::max)(
- initial_size, clamp(rd_remain_));
-done:
- BOOST_ASSERT(result != 0);
- return result;
-}
-
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
template<class DynamicBuffer, class>
std::size_t
-stream<NextLayer>::
+stream<NextLayer, deflateSupported>::
read_size_hint(DynamicBuffer& buffer) const
{
static_assert(
@@ -102,10 +67,12 @@ read_size_hint(DynamicBuffer& buffer) const
return read_size_hint(initial_size);
}
-template<class NextLayer>
+//------------------------------------------------------------------------------
+
+template<class NextLayer, bool deflateSupported>
void
-stream<NextLayer>::
-set_option(permessage_deflate const& o)
+stream<NextLayer, deflateSupported>::
+set_option(permessage_deflate const& o, std::true_type)
{
if( o.server_max_window_bits > 15 ||
o.server_max_window_bits < 9)
@@ -123,14 +90,27 @@ set_option(permessage_deflate const& o)
o.memLevel > 9)
BOOST_THROW_EXCEPTION(std::invalid_argument{
"invalid memLevel"});
- pmd_opts_ = o;
+ this->pmd_opts_ = o;
}
-//------------------------------------------------------------------------------
+template<class NextLayer, bool deflateSupported>
+void
+stream<NextLayer, deflateSupported>::
+set_option(permessage_deflate const& o, std::false_type)
+{
+ if(o.client_enable || o.server_enable)
+ {
+ // Can't enable permessage-deflate
+ // when deflateSupported == false.
+ //
+ BOOST_THROW_EXCEPTION(std::invalid_argument{
+ "deflateSupported == false"});
+ }
+}
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
void
-stream<NextLayer>::
+stream<NextLayer, deflateSupported>::
open(role_type role)
{
// VFALCO TODO analyze and remove dupe code in reset()
@@ -144,6 +124,9 @@ open(role_type role)
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();
cr_.code = close_code::none;
@@ -151,47 +134,59 @@ open(role_type role)
wr_cont_ = false;
wr_buf_size_ = 0;
- if(((role_ == role_type::client && pmd_opts_.client_enable) ||
- (role_ == role_type::server && pmd_opts_.server_enable)) &&
- pmd_config_.accept)
+ open_pmd(is_deflate_supported{});
+}
+
+template<class NextLayer, bool deflateSupported>
+inline
+void
+stream<NextLayer, deflateSupported>::
+open_pmd(std::true_type)
+{
+ if(((role_ == role_type::client &&
+ this->pmd_opts_.client_enable) ||
+ (role_ == role_type::server &&
+ this->pmd_opts_.server_enable)) &&
+ this->pmd_config_.accept)
{
- pmd_normalize(pmd_config_);
- pmd_.reset(new pmd_t);
+ pmd_normalize(this->pmd_config_);
+ this->pmd_.reset(new typename
+ detail::stream_base<deflateSupported>::pmd_type);
if(role_ == role_type::client)
{
- pmd_->zi.reset(
- pmd_config_.server_max_window_bits);
- pmd_->zo.reset(
- pmd_opts_.compLevel,
- pmd_config_.client_max_window_bits,
- pmd_opts_.memLevel,
+ this->pmd_->zi.reset(
+ this->pmd_config_.server_max_window_bits);
+ this->pmd_->zo.reset(
+ this->pmd_opts_.compLevel,
+ this->pmd_config_.client_max_window_bits,
+ this->pmd_opts_.memLevel,
zlib::Strategy::normal);
}
else
{
- pmd_->zi.reset(
- pmd_config_.client_max_window_bits);
- pmd_->zo.reset(
- pmd_opts_.compLevel,
- pmd_config_.server_max_window_bits,
- pmd_opts_.memLevel,
+ this->pmd_->zi.reset(
+ this->pmd_config_.client_max_window_bits);
+ this->pmd_->zo.reset(
+ this->pmd_opts_.compLevel,
+ this->pmd_config_.server_max_window_bits,
+ this->pmd_opts_.memLevel,
zlib::Strategy::normal);
}
}
}
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
void
-stream<NextLayer>::
+stream<NextLayer, deflateSupported>::
close()
{
wr_buf_.reset();
- pmd_.reset();
+ close_pmd(is_deflate_supported{});
}
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
void
-stream<NextLayer>::
+stream<NextLayer, deflateSupported>::
reset()
{
BOOST_ASSERT(status_ != status::open);
@@ -203,19 +198,23 @@ reset()
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();
cr_.code = close_code::none;
}
// Called before each write frame
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
+inline
void
-stream<NextLayer>::
-begin_msg()
+stream<NextLayer, deflateSupported>::
+begin_msg(std::true_type)
{
wr_frag_ = wr_frag_opt_;
- wr_compress_ = static_cast<bool>(pmd_);
+ wr_compress_ = static_cast<bool>(this->pmd_);
// Maintain the write buffer
if( wr_compress_ ||
@@ -235,31 +234,118 @@ begin_msg()
}
}
+// Called before each write frame
+template<class NextLayer, bool deflateSupported>
+inline
+void
+stream<NextLayer, deflateSupported>::
+begin_msg(std::false_type)
+{
+ wr_frag_ = wr_frag_opt_;
+
+ // Maintain the write buffer
+ if(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 NextLayer, bool deflateSupported>
+std::size_t
+stream<NextLayer, deflateSupported>::
+read_size_hint(
+ std::size_t initial_size,
+ std::true_type) const
+{
+ using beast::detail::clamp;
+ std::size_t result;
+ BOOST_ASSERT(initial_size > 0);
+ if(! this->pmd_ || (! rd_done_ && ! this->pmd_->rd_set))
+ {
+ // current message is uncompressed
+
+ if(rd_done_)
+ {
+ // first message frame
+ result = initial_size;
+ goto done;
+ }
+ else if(rd_fh_.fin)
+ {
+ // last message frame
+ BOOST_ASSERT(rd_remain_ > 0);
+ result = clamp(rd_remain_);
+ goto done;
+ }
+ }
+ result = (std::max)(
+ initial_size, clamp(rd_remain_));
+done:
+ BOOST_ASSERT(result != 0);
+ return result;
+}
+
+template<class NextLayer, bool deflateSupported>
+std::size_t
+stream<NextLayer, deflateSupported>::
+read_size_hint(
+ std::size_t initial_size,
+ std::false_type) const
+{
+ using beast::detail::clamp;
+ std::size_t result;
+ BOOST_ASSERT(initial_size > 0);
+ // compression is not supported
+ if(rd_done_)
+ {
+ // first message frame
+ result = initial_size;
+ }
+ else if(rd_fh_.fin)
+ {
+ // last message frame
+ BOOST_ASSERT(rd_remain_ > 0);
+ result = clamp(rd_remain_);
+ }
+ else
+ {
+ result = (std::max)(
+ initial_size, clamp(rd_remain_));
+ }
+ BOOST_ASSERT(result != 0);
+ return result;
+}
+
//------------------------------------------------------------------------------
// Attempt to read a complete frame header.
// Returns `false` if more bytes are needed
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
template<class DynamicBuffer>
bool
-stream<NextLayer>::
+stream<NextLayer, deflateSupported>::
parse_fh(
detail::frame_header& fh,
DynamicBuffer& b,
- close_code& code)
+ error_code& ec)
{
using boost::asio::buffer;
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
- auto const err =
- [&](close_code cv)
- {
- code = cv;
- return false;
- };
if(buffer_size(b.data()) < 2)
{
- code = close_code::none;
+ // need more bytes
+ ec.assign(0, ec.category());
return false;
}
buffers_suffix<typename
@@ -282,7 +368,8 @@ parse_fh(
need += 4;
if(buffer_size(cb) < need)
{
- code = close_code::none;
+ // need more bytes
+ ec.assign(0, ec.category());
return false;
}
fh.op = static_cast<
@@ -299,28 +386,30 @@ parse_fh(
if(rd_cont_)
{
// new data frame when continuation expected
- return err(close_code::protocol_error);
+ ec = error::bad_data_frame;
+ return false;
}
- if((fh.rsv1 && ! pmd_) ||
- fh.rsv2 || fh.rsv3)
+ if(fh.rsv2 || fh.rsv3 ||
+ ! this->rd_deflated(fh.rsv1))
{
// reserved bits not cleared
- return err(close_code::protocol_error);
+ ec = error::bad_reserved_bits;
+ return false;
}
- if(pmd_)
- pmd_->rd_set = fh.rsv1;
break;
case detail::opcode::cont:
if(! rd_cont_)
{
// continuation without an active message
- return err(close_code::protocol_error);
+ ec = error::bad_continuation;
+ return false;
}
if(fh.rsv1 || fh.rsv2 || fh.rsv3)
{
// reserved bits not cleared
- return err(close_code::protocol_error);
+ ec = error::bad_reserved_bits;
+ return false;
}
break;
@@ -328,31 +417,41 @@ parse_fh(
if(detail::is_reserved(fh.op))
{
// reserved opcode
- return err(close_code::protocol_error);
+ ec = error::bad_opcode;
+ return false;
}
if(! fh.fin)
{
// fragmented control message
- return err(close_code::protocol_error);
+ ec = error::bad_control_fragment;
+ return false;
}
if(fh.len > 125)
{
// invalid length for control message
- return err(close_code::protocol_error);
+ ec = error::bad_control_size;
+ return false;
}
if(fh.rsv1 || fh.rsv2 || fh.rsv3)
{
// reserved bits not cleared
- return err(close_code::protocol_error);
+ ec = error::bad_reserved_bits;
+ return false;
}
break;
}
- // unmasked frame from client
if(role_ == role_type::server && ! fh.mask)
- return err(close_code::protocol_error);
- // masked frame from server
+ {
+ // unmasked frame from client
+ ec = error::bad_unmasked_frame;
+ return false;
+ }
if(role_ == role_type::client && fh.mask)
- return err(close_code::protocol_error);
+ {
+ // masked frame from server
+ ec = error::bad_masked_frame;
+ return false;
+ }
if(detail::is_control(fh.op) &&
buffer_size(cb) < need + fh.len)
{
@@ -368,9 +467,12 @@ parse_fh(
BOOST_ASSERT(buffer_size(cb) >= sizeof(tmp));
cb.consume(buffer_copy(buffer(tmp), cb));
fh.len = detail::big_uint16_to_native(&tmp[0]);
- // length not canonical
if(fh.len < 126)
- return err(close_code::protocol_error);
+ {
+ // length not canonical
+ ec = error::bad_size;
+ return false;
+ }
break;
}
case 127:
@@ -379,9 +481,12 @@ parse_fh(
BOOST_ASSERT(buffer_size(cb) >= sizeof(tmp));
cb.consume(buffer_copy(buffer(tmp), cb));
fh.len = detail::big_uint64_to_native(&tmp[0]);
- // length not canonical
if(fh.len < 65536)
- return err(close_code::protocol_error);
+ {
+ // length not canonical
+ ec = error::bad_size;
+ return false;
+ }
break;
}
}
@@ -409,26 +514,34 @@ parse_fh(
{
if(rd_size_ > (std::numeric_limits<
std::uint64_t>::max)() - fh.len)
- return err(close_code::too_big);
+ {
+ // message size exceeds configured limit
+ ec = error::message_too_big;
+ return false;
+ }
}
- if(! pmd_ || ! pmd_->rd_set)
+ if(! this->rd_deflated())
{
if(rd_msg_max_ && beast::detail::sum_exceeds(
rd_size_, fh.len, rd_msg_max_))
- return err(close_code::too_big);
+ {
+ // 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));
- code = close_code::none;
+ ec.assign(0, ec.category());
return true;
}
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
template<class DynamicBuffer>
void
-stream<NextLayer>::
+stream<NextLayer, deflateSupported>::
write_close(DynamicBuffer& db, close_reason const& cr)
{
using namespace boost::endian;
@@ -479,10 +592,10 @@ write_close(DynamicBuffer& db, close_reason const& cr)
}
}
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
template<class DynamicBuffer>
void
-stream<NextLayer>::
+stream<NextLayer, deflateSupported>::
write_ping(DynamicBuffer& db,
detail::opcode code, ping_data const& data)
{
@@ -513,14 +626,13 @@ write_ping(DynamicBuffer& db,
//------------------------------------------------------------------------------
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
template<class Decorator>
request_type
-stream<NextLayer>::
+stream<NextLayer, deflateSupported>::
build_request(detail::sec_ws_key_type& key,
- string_view host,
- string_view target,
- Decorator const& decorator)
+ string_view host, string_view target,
+ Decorator const& decorator)
{
request_type req;
req.target(target);
@@ -532,34 +644,45 @@ build_request(detail::sec_ws_key_type& key,
detail::make_sec_ws_key(key, wr_gen_);
req.set(http::field::sec_websocket_key, key);
req.set(http::field::sec_websocket_version, "13");
- if(pmd_opts_.client_enable)
+ build_request_pmd(req, is_deflate_supported{});
+ decorator(req);
+ if(! req.count(http::field::user_agent))
+ req.set(http::field::user_agent,
+ BOOST_BEAST_VERSION_STRING);
+ return req;
+}
+
+template<class NextLayer, bool deflateSupported>
+inline
+void
+stream<NextLayer, deflateSupported>::
+build_request_pmd(request_type& req, std::true_type)
+{
+ if(this->pmd_opts_.client_enable)
{
detail::pmd_offer config;
config.accept = true;
config.server_max_window_bits =
- pmd_opts_.server_max_window_bits;
+ this->pmd_opts_.server_max_window_bits;
config.client_max_window_bits =
- pmd_opts_.client_max_window_bits;
+ this->pmd_opts_.client_max_window_bits;
config.server_no_context_takeover =
- pmd_opts_.server_no_context_takeover;
+ this->pmd_opts_.server_no_context_takeover;
config.client_no_context_takeover =
- pmd_opts_.client_no_context_takeover;
+ this->pmd_opts_.client_no_context_takeover;
detail::pmd_write(req, config);
}
- decorator(req);
- if(! req.count(http::field::user_agent))
- req.set(http::field::user_agent,
- BOOST_BEAST_VERSION_STRING);
- return req;
}
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
template<class Body, class Allocator, class Decorator>
response_type
-stream<NextLayer>::
-build_response(http::request<Body,
- http::basic_fields<Allocator>> const& req,
- Decorator const& decorator)
+stream<NextLayer, deflateSupported>::
+build_response(
+ http::request<Body,
+ http::basic_fields<Allocator>> const& req,
+ Decorator const& decorator,
+ error_code& result)
{
auto const decorate =
[&decorator](response_type& res)
@@ -573,40 +696,58 @@ build_response(http::request<Body,
}
};
auto err =
- [&](std::string const& text)
+ [&](error e)
{
+ result = e;
response_type res;
res.version(req.version());
res.result(http::status::bad_request);
- res.body() = text;
+ res.body() = result.message();
res.prepare_payload();
decorate(res);
return res;
};
- if(req.version() < 11)
- return err("HTTP version 1.1 required");
+ if(req.version() != 11)
+ return err(error::bad_http_version);
if(req.method() != http::verb::get)
- return err("Wrong method");
- if(! is_upgrade(req))
- return err("Expected Upgrade request");
+ return err(error::bad_method);
if(! req.count(http::field::host))
- return err("Missing Host");
- if(! req.count(http::field::sec_websocket_key))
- return err("Missing Sec-WebSocket-Key");
- auto const key = req[http::field::sec_websocket_key];
- if(key.size() > detail::sec_ws_key_type::max_size_n)
- return err("Invalid Sec-WebSocket-Key");
- {
- auto const version =
- req[http::field::sec_websocket_version];
- if(version.empty())
- return err("Missing Sec-WebSocket-Version");
- if(version != "13")
+ 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;
@@ -614,12 +755,6 @@ build_response(http::request<Body,
}
response_type res;
- {
- detail::pmd_offer offer;
- detail::pmd_offer unused;
- pmd_read(offer, req);
- pmd_negotiate(res, unused, offer, pmd_opts_);
- }
res.result(http::status::switching_protocols);
res.version(req.version());
res.set(http::field::upgrade, "websocket");
@@ -629,53 +764,95 @@ build_response(http::request<Body,
detail::make_sec_ws_accept(acc, key);
res.set(http::field::sec_websocket_accept, acc);
}
+ build_response_pmd(res, req, is_deflate_supported{});
decorate(res);
+ result = {};
return res;
}
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
+template<class Body, class Allocator>
+inline
+void
+stream<NextLayer, deflateSupported>::
+build_response_pmd(
+ response_type& res,
+ http::request<Body,
+ http::basic_fields<Allocator>> const& req,
+ std::true_type)
+{
+ detail::pmd_offer offer;
+ detail::pmd_offer unused;
+ pmd_read(offer, req);
+ pmd_negotiate(res, unused, offer, this->pmd_opts_);
+}
+
+// Called when the WebSocket Upgrade response is received
+template<class NextLayer, bool deflateSupported>
void
-stream<NextLayer>::
-on_response(response_type const& res,
- detail::sec_ws_key_type const& key, error_code& ec)
+stream<NextLayer, deflateSupported>::
+on_response(
+ response_type const& res,
+ detail::sec_ws_key_type const& key,
+ error_code& ec)
{
- bool const success = [&]()
+ 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);
{
- if(res.version() < 11)
- return false;
- if(res.result() != http::status::switching_protocols)
- return false;
- if(! http::token_list{res[http::field::connection]}.exists("upgrade"))
- return false;
- if(! http::token_list{res[http::field::upgrade]}.exists("websocket"))
- return false;
- if(res.count(http::field::sec_websocket_accept) != 1)
- return false;
+ 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(
- res[http::field::sec_websocket_accept]) != 0)
- return false;
- return true;
- }();
- if(! success)
- {
- ec = error::handshake_failed;
- return;
+ if(acc.compare(it->value()) != 0)
+ return err(error::bad_sec_accept);
}
+
ec.assign(0, ec.category());
+ on_response_pmd(res, is_deflate_supported{});
+ open(role_type::client);
+}
+
+template<class NextLayer, bool deflateSupported>
+inline
+void
+stream<NextLayer, deflateSupported>::
+on_response_pmd(
+ response_type const& res,
+ std::true_type)
+{
detail::pmd_offer offer;
pmd_read(offer, res);
// VFALCO see if offer satisfies pmd_config_,
// return an error if not.
- pmd_config_ = offer; // overwrite for now
- open(role_type::client);
+ this->pmd_config_ = offer; // overwrite for now
}
// _Fail the WebSocket Connection_
-template<class NextLayer>
+template<class NextLayer, bool deflateSupported>
void
-stream<NextLayer>::
+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