2017-07-20 08:01:46 -07:00
|
|
|
//
|
2017-07-24 09:42:36 -07:00
|
|
|
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2017-07-20 08:01:46 -07:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
2017-07-20 13:40:34 -07:00
|
|
|
// Official repository: https://github.com/boostorg/beast
|
|
|
|
//
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2019-01-19 07:24:00 -08:00
|
|
|
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_STREAM_HPP
|
|
|
|
#define BOOST_BEAST_WEBSOCKET_IMPL_STREAM_HPP
|
2017-07-20 13:40:34 -07:00
|
|
|
|
2019-02-02 20:44:04 -08:00
|
|
|
#include <boost/beast/core/buffer_size.hpp>
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/websocket/rfc6455.hpp>
|
|
|
|
#include <boost/beast/websocket/teardown.hpp>
|
|
|
|
#include <boost/beast/websocket/detail/hybi13.hpp>
|
2019-01-19 07:24:00 -08:00
|
|
|
#include <boost/beast/websocket/detail/mask.hpp>
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/version.hpp>
|
|
|
|
#include <boost/beast/http/read.hpp>
|
|
|
|
#include <boost/beast/http/write.hpp>
|
|
|
|
#include <boost/beast/http/rfc7230.hpp>
|
2017-09-15 10:03:59 -07:00
|
|
|
#include <boost/beast/core/buffers_cat.hpp>
|
2017-09-15 09:55:03 -07:00
|
|
|
#include <boost/beast/core/buffers_prefix.hpp>
|
2017-09-15 12:52:45 -07:00
|
|
|
#include <boost/beast/core/buffers_suffix.hpp>
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/core/flat_static_buffer.hpp>
|
|
|
|
#include <boost/beast/core/detail/clamp.hpp>
|
|
|
|
#include <boost/beast/core/detail/type_traits.hpp>
|
2019-01-19 07:24:00 -08:00
|
|
|
#include <boost/asio/bind_executor.hpp>
|
|
|
|
#include <boost/asio/steady_timer.hpp>
|
2016-09-25 12:17:32 -04:00
|
|
|
#include <boost/assert.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <boost/endian/buffers.hpp>
|
2017-07-08 20:45:35 -07:00
|
|
|
#include <boost/make_unique.hpp>
|
2017-05-22 15:30:12 -07:00
|
|
|
#include <boost/throw_exception.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <algorithm>
|
2019-01-19 07:24:00 -08:00
|
|
|
#include <chrono>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <memory>
|
2016-10-24 18:41:25 -04:00
|
|
|
#include <stdexcept>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <utility>
|
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2017-07-20 08:01:46 -07:00
|
|
|
namespace beast {
|
|
|
|
namespace websocket {
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-07-20 08:01:46 -07:00
|
|
|
template<class... Args>
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-07-20 08:01:46 -07:00
|
|
|
stream(Args&&... args)
|
2019-01-19 07:24:00 -08:00
|
|
|
: impl_(std::make_shared<impl_type>(
|
|
|
|
std::forward<Args>(args)...))
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(impl_->rd_buf.max_size() >=
|
2017-07-15 17:05:24 -07:00
|
|
|
max_control_frame_size);
|
|
|
|
}
|
|
|
|
|
2019-01-19 07:24:00 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-07-15 17:05:24 -07:00
|
|
|
template<class DynamicBuffer, class>
|
|
|
|
std::size_t
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-08-01 20:15:07 -07:00
|
|
|
read_size_hint(DynamicBuffer& buffer) const
|
2017-07-15 17:05:24 -07:00
|
|
|
{
|
2017-09-07 07:39:52 -07:00
|
|
|
static_assert(
|
2018-11-30 14:58:38 -08:00
|
|
|
net::is_dynamic_buffer<DynamicBuffer>::value,
|
2017-07-15 17:05:24 -07:00
|
|
|
"DynamicBuffer requirements not met");
|
2017-08-01 20:15:07 -07:00
|
|
|
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);
|
2016-04-30 13:00:33 -04:00
|
|
|
}
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-06-24 10:13:17 -07:00
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2019-01-19 07:24:00 -08:00
|
|
|
set_option(permessage_deflate const& o)
|
2017-06-24 10:13:17 -07:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->set_option_pmd(o);
|
|
|
|
}
|
2017-06-24 10:13:17 -07:00
|
|
|
|
2019-01-19 07:24:00 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
|
|
|
void
|
|
|
|
stream<NextLayer, deflateSupported>::
|
|
|
|
get_option(permessage_deflate& o)
|
|
|
|
{
|
|
|
|
impl_->get_option_pmd(o);
|
|
|
|
}
|
2017-06-24 10:13:17 -07:00
|
|
|
|
2019-01-19 07:24:00 -08:00
|
|
|
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;
|
2017-06-24 10:13:17 -07:00
|
|
|
}
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-06-24 10:13:17 -07:00
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2019-01-19 07:24:00 -08:00
|
|
|
binary(bool value)
|
2017-06-24 10:13:17 -07:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
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;
|
2017-06-24 10:13:17 -07:00
|
|
|
}
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-07-14 12:11:44 -07:00
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2019-01-19 07:24:00 -08:00
|
|
|
control_callback(std::function<
|
|
|
|
void(frame_type, string_view)> cb)
|
2017-07-14 12:11:44 -07:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->ctrl_cb = std::move(cb);
|
2017-07-14 12:11:44 -07:00
|
|
|
}
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-07-14 12:11:44 -07:00
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2019-01-19 07:24:00 -08:00
|
|
|
control_callback()
|
2017-07-14 12:11:44 -07:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->ctrl_cb = {};
|
|
|
|
}
|
2017-07-14 12:11:44 -07:00
|
|
|
|
2019-01-19 07:24:00 -08:00
|
|
|
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->secure_prng_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NextLayer, bool deflateSupported>
|
|
|
|
void
|
|
|
|
stream<NextLayer, deflateSupported>::
|
|
|
|
write_buffer_size(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_size() 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;
|
2017-07-14 12:11:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2017-07-15 17:05:24 -07:00
|
|
|
// Attempt to read a complete frame header.
|
|
|
|
// Returns `false` if more bytes are needed
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-07-15 17:05:24 -07:00
|
|
|
template<class DynamicBuffer>
|
|
|
|
bool
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-07-15 17:05:24 -07:00
|
|
|
parse_fh(
|
|
|
|
detail::frame_header& fh,
|
|
|
|
DynamicBuffer& b,
|
2018-01-01 17:49:19 -08:00
|
|
|
error_code& ec)
|
2017-07-15 17:05:24 -07:00
|
|
|
{
|
|
|
|
if(buffer_size(b.data()) < 2)
|
|
|
|
{
|
2018-01-01 17:49:19 -08:00
|
|
|
// need more bytes
|
2019-01-19 07:24:00 -08:00
|
|
|
ec = {};
|
2017-07-15 17:05:24 -07:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-15 12:52:45 -07:00
|
|
|
buffers_suffix<typename
|
2017-07-15 17:05:24 -07:00
|
|
|
DynamicBuffer::const_buffers_type> cb{
|
|
|
|
b.data()};
|
2017-08-13 17:25:48 -07:00
|
|
|
std::size_t need;
|
2017-07-15 17:05:24 -07:00
|
|
|
{
|
|
|
|
std::uint8_t tmp[2];
|
2019-02-02 10:53:54 -08:00
|
|
|
cb.consume(net::buffer_copy(
|
|
|
|
net::buffer(tmp), cb));
|
2017-07-15 17:05:24 -07:00
|
|
|
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)
|
|
|
|
{
|
2018-01-01 17:49:19 -08:00
|
|
|
// need more bytes
|
2019-01-19 07:24:00 -08:00
|
|
|
ec = {};
|
2017-07-15 17:05:24 -07:00
|
|
|
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:
|
2019-01-19 07:24:00 -08:00
|
|
|
if(impl_->rd_cont)
|
2017-07-15 17:05:24 -07:00
|
|
|
{
|
|
|
|
// new data frame when continuation expected
|
2018-01-01 17:49:19 -08:00
|
|
|
ec = error::bad_data_frame;
|
|
|
|
return false;
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
2017-11-18 16:52:18 -08:00
|
|
|
if(fh.rsv2 || fh.rsv3 ||
|
2019-01-19 07:24:00 -08:00
|
|
|
! impl_->rd_deflated(fh.rsv1))
|
2017-07-15 17:05:24 -07:00
|
|
|
{
|
|
|
|
// reserved bits not cleared
|
2018-01-01 17:49:19 -08:00
|
|
|
ec = error::bad_reserved_bits;
|
|
|
|
return false;
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case detail::opcode::cont:
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! impl_->rd_cont)
|
2017-07-15 17:05:24 -07:00
|
|
|
{
|
|
|
|
// continuation without an active message
|
2018-01-01 17:49:19 -08:00
|
|
|
ec = error::bad_continuation;
|
|
|
|
return false;
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
if(fh.rsv1 || fh.rsv2 || fh.rsv3)
|
|
|
|
{
|
|
|
|
// reserved bits not cleared
|
2018-01-01 17:49:19 -08:00
|
|
|
ec = error::bad_reserved_bits;
|
|
|
|
return false;
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if(detail::is_reserved(fh.op))
|
|
|
|
{
|
|
|
|
// reserved opcode
|
2018-01-01 17:49:19 -08:00
|
|
|
ec = error::bad_opcode;
|
|
|
|
return false;
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
if(! fh.fin)
|
|
|
|
{
|
|
|
|
// fragmented control message
|
2018-01-01 17:49:19 -08:00
|
|
|
ec = error::bad_control_fragment;
|
|
|
|
return false;
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
if(fh.len > 125)
|
|
|
|
{
|
|
|
|
// invalid length for control message
|
2018-01-01 17:49:19 -08:00
|
|
|
ec = error::bad_control_size;
|
|
|
|
return false;
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
if(fh.rsv1 || fh.rsv2 || fh.rsv3)
|
|
|
|
{
|
|
|
|
// reserved bits not cleared
|
2018-01-01 17:49:19 -08:00
|
|
|
ec = error::bad_reserved_bits;
|
|
|
|
return false;
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
if(impl_->role == role_type::server && ! fh.mask)
|
2018-01-01 17:49:19 -08:00
|
|
|
{
|
|
|
|
// unmasked frame from client
|
|
|
|
ec = error::bad_unmasked_frame;
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
if(impl_->role == role_type::client && fh.mask)
|
2018-01-01 17:49:19 -08:00
|
|
|
{
|
|
|
|
// masked frame from server
|
|
|
|
ec = error::bad_masked_frame;
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-15 17:05:24 -07:00
|
|
|
if(detail::is_control(fh.op) &&
|
2017-08-13 17:25:48 -07:00
|
|
|
buffer_size(cb) < need + fh.len)
|
2017-07-15 17:05:24 -07:00
|
|
|
{
|
|
|
|
// 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));
|
2019-02-02 10:53:54 -08:00
|
|
|
cb.consume(net::buffer_copy(net::buffer(tmp), cb));
|
2017-07-15 17:05:24 -07:00
|
|
|
fh.len = detail::big_uint16_to_native(&tmp[0]);
|
|
|
|
if(fh.len < 126)
|
2018-01-01 17:49:19 -08:00
|
|
|
{
|
|
|
|
// length not canonical
|
|
|
|
ec = error::bad_size;
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-15 17:05:24 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 127:
|
|
|
|
{
|
|
|
|
std::uint8_t tmp[8];
|
|
|
|
BOOST_ASSERT(buffer_size(cb) >= sizeof(tmp));
|
2019-02-02 10:53:54 -08:00
|
|
|
cb.consume(net::buffer_copy(net::buffer(tmp), cb));
|
2017-07-15 17:05:24 -07:00
|
|
|
fh.len = detail::big_uint64_to_native(&tmp[0]);
|
|
|
|
if(fh.len < 65536)
|
2018-01-01 17:49:19 -08:00
|
|
|
{
|
|
|
|
// length not canonical
|
|
|
|
ec = error::bad_size;
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-15 17:05:24 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(fh.mask)
|
|
|
|
{
|
|
|
|
std::uint8_t tmp[4];
|
|
|
|
BOOST_ASSERT(buffer_size(cb) >= sizeof(tmp));
|
2019-02-02 10:53:54 -08:00
|
|
|
cb.consume(net::buffer_copy(net::buffer(tmp), cb));
|
2017-07-15 17:05:24 -07:00
|
|
|
fh.key = detail::little_uint32_to_native(&tmp[0]);
|
2019-01-19 07:24:00 -08:00
|
|
|
detail::prepare_key(impl_->rd_key, fh.key);
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// initialize this otherwise operator== breaks
|
|
|
|
fh.key = 0;
|
|
|
|
}
|
|
|
|
if(! detail::is_control(fh.op))
|
|
|
|
{
|
|
|
|
if(fh.op != detail::opcode::cont)
|
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->rd_size = 0;
|
|
|
|
impl_->rd_op = fh.op;
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
if(impl_->rd_size > (std::numeric_limits<
|
2019-02-02 10:53:54 -08:00
|
|
|
std::uint64_t>::max)() - fh.len)
|
2018-01-01 17:49:19 -08:00
|
|
|
{
|
|
|
|
// message size exceeds configured limit
|
|
|
|
ec = error::message_too_big;
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! impl_->rd_deflated())
|
2017-07-15 17:05:24 -07:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
if(impl_->rd_msg_max && beast::detail::sum_exceeds(
|
|
|
|
impl_->rd_size, fh.len, impl_->rd_msg_max))
|
2018-01-01 17:49:19 -08:00
|
|
|
{
|
|
|
|
// message size exceeds configured limit
|
|
|
|
ec = error::message_too_big;
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->rd_cont = ! fh.fin;
|
|
|
|
impl_->rd_remain = fh.len;
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
b.consume(b.size() - buffer_size(cb));
|
2019-01-19 07:24:00 -08:00
|
|
|
ec = {};
|
2017-07-15 17:05:24 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-06-24 10:13:17 -07:00
|
|
|
template<class DynamicBuffer>
|
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-06-24 10:13:17 -07:00
|
|
|
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();
|
2019-01-19 07:24:00 -08:00
|
|
|
if(impl_->role == role_type::client)
|
2017-08-26 20:10:04 -07:00
|
|
|
{
|
|
|
|
fh.mask = true;
|
2018-07-05 16:47:07 -07:00
|
|
|
fh.key = this->create_mask();
|
2017-08-26 20:10:04 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fh.mask = false;
|
|
|
|
}
|
2017-06-24 10:13:17 -07:00
|
|
|
detail::write(db, fh);
|
|
|
|
if(cr.code != close_code::none)
|
|
|
|
{
|
|
|
|
detail::prepared_key key;
|
|
|
|
if(fh.mask)
|
|
|
|
detail::prepare_key(key, fh.key);
|
|
|
|
{
|
2017-08-13 17:25:48 -07:00
|
|
|
std::uint8_t tmp[2];
|
|
|
|
::new(&tmp[0]) big_uint16_buf_t{
|
2017-06-24 10:13:17 -07:00
|
|
|
(std::uint16_t)cr.code};
|
2017-08-13 17:25:48 -07:00
|
|
|
auto mb = db.prepare(2);
|
2018-11-30 14:58:38 -08:00
|
|
|
net::buffer_copy(mb,
|
|
|
|
net::buffer(tmp));
|
2017-06-24 10:13:17 -07:00
|
|
|
if(fh.mask)
|
2017-08-13 17:25:48 -07:00
|
|
|
detail::mask_inplace(mb, key);
|
2017-06-24 10:13:17 -07:00
|
|
|
db.commit(2);
|
|
|
|
}
|
|
|
|
if(! cr.reason.empty())
|
|
|
|
{
|
2017-08-13 17:25:48 -07:00
|
|
|
auto mb = db.prepare(cr.reason.size());
|
2018-11-30 14:58:38 -08:00
|
|
|
net::buffer_copy(mb,
|
|
|
|
net::const_buffer(
|
2017-06-24 10:13:17 -07:00
|
|
|
cr.reason.data(), cr.reason.size()));
|
|
|
|
if(fh.mask)
|
2017-08-13 17:25:48 -07:00
|
|
|
detail::mask_inplace(mb, key);
|
2017-06-24 10:13:17 -07:00
|
|
|
db.commit(cr.reason.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-06-24 10:13:17 -07:00
|
|
|
template<class DynamicBuffer>
|
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-06-24 10:13:17 -07:00
|
|
|
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();
|
2019-01-19 07:24:00 -08:00
|
|
|
fh.mask = impl_->role == role_type::client;
|
2017-06-24 10:13:17 -07:00
|
|
|
if(fh.mask)
|
2018-07-05 16:47:07 -07:00
|
|
|
fh.key = this->create_mask();
|
2017-06-24 10:13:17 -07:00
|
|
|
detail::write(db, fh);
|
|
|
|
if(data.empty())
|
|
|
|
return;
|
|
|
|
detail::prepared_key key;
|
|
|
|
if(fh.mask)
|
|
|
|
detail::prepare_key(key, fh.key);
|
2017-08-13 17:25:48 -07:00
|
|
|
auto mb = db.prepare(data.size());
|
2018-11-30 14:58:38 -08:00
|
|
|
net::buffer_copy(mb,
|
|
|
|
net::const_buffer(
|
2017-06-24 10:13:17 -07:00
|
|
|
data.data(), data.size()));
|
|
|
|
if(fh.mask)
|
2017-08-13 17:25:48 -07:00
|
|
|
detail::mask_inplace(mb, key);
|
2017-06-24 10:13:17 -07:00
|
|
|
db.commit(data.size());
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
2017-07-14 12:11:44 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-07-14 12:11:44 -07:00
|
|
|
template<class Decorator>
|
|
|
|
request_type
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-07-14 12:11:44 -07:00
|
|
|
build_request(detail::sec_ws_key_type& key,
|
2017-11-18 16:52:18 -08:00
|
|
|
string_view host, string_view target,
|
|
|
|
Decorator const& decorator)
|
2017-07-14 12:11:44 -07:00
|
|
|
{
|
|
|
|
request_type req;
|
|
|
|
req.target(target);
|
2017-09-12 13:49:45 -07:00
|
|
|
req.version(11);
|
2017-07-14 12:11:44 -07:00
|
|
|
req.method(http::verb::get);
|
|
|
|
req.set(http::field::host, host);
|
|
|
|
req.set(http::field::upgrade, "websocket");
|
|
|
|
req.set(http::field::connection, "upgrade");
|
2018-07-05 16:47:07 -07:00
|
|
|
detail::make_sec_ws_key(key);
|
2017-07-14 12:11:44 -07:00
|
|
|
req.set(http::field::sec_websocket_key, key);
|
|
|
|
req.set(http::field::sec_websocket_version, "13");
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->build_request_pmd(req);
|
2017-11-18 16:52:18 -08:00
|
|
|
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>
|
2017-07-14 13:00:09 -07:00
|
|
|
template<class Body, class Allocator, class Decorator>
|
2017-07-14 12:11:44 -07:00
|
|
|
response_type
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
|
|
|
build_response(
|
|
|
|
http::request<Body,
|
|
|
|
http::basic_fields<Allocator>> const& req,
|
2018-01-01 17:49:19 -08:00
|
|
|
Decorator const& decorator,
|
|
|
|
error_code& result)
|
2017-07-14 12:11:44 -07:00
|
|
|
{
|
|
|
|
auto const decorate =
|
|
|
|
[&decorator](response_type& res)
|
|
|
|
{
|
|
|
|
decorator(res);
|
|
|
|
if(! res.count(http::field::server))
|
|
|
|
{
|
2017-07-20 13:40:34 -07:00
|
|
|
BOOST_STATIC_ASSERT(sizeof(BOOST_BEAST_VERSION_STRING) < 20);
|
|
|
|
static_string<20> s(BOOST_BEAST_VERSION_STRING);
|
2017-07-14 12:11:44 -07:00
|
|
|
res.set(http::field::server, s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto err =
|
2018-01-01 17:49:19 -08:00
|
|
|
[&](error e)
|
2017-07-14 12:11:44 -07:00
|
|
|
{
|
2018-01-01 17:49:19 -08:00
|
|
|
result = e;
|
2017-07-14 12:11:44 -07:00
|
|
|
response_type res;
|
2017-09-12 13:49:45 -07:00
|
|
|
res.version(req.version());
|
2017-07-14 12:11:44 -07:00
|
|
|
res.result(http::status::bad_request);
|
2018-01-01 17:49:19 -08:00
|
|
|
res.body() = result.message();
|
2017-07-14 12:11:44 -07:00
|
|
|
res.prepare_payload();
|
|
|
|
decorate(res);
|
|
|
|
return res;
|
|
|
|
};
|
2018-01-01 17:49:19 -08:00
|
|
|
if(req.version() != 11)
|
|
|
|
return err(error::bad_http_version);
|
2017-07-14 12:11:44 -07:00
|
|
|
if(req.method() != http::verb::get)
|
2018-01-01 17:49:19 -08:00
|
|
|
return err(error::bad_method);
|
2017-07-14 12:11:44 -07:00
|
|
|
if(! req.count(http::field::host))
|
2018-01-01 17:49:19 -08:00
|
|
|
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")
|
2017-07-14 12:11:44 -07:00
|
|
|
{
|
|
|
|
response_type res;
|
|
|
|
res.result(http::status::upgrade_required);
|
2017-09-12 13:49:45 -07:00
|
|
|
res.version(req.version());
|
2017-07-14 12:11:44 -07:00
|
|
|
res.set(http::field::sec_websocket_version, "13");
|
2018-01-01 17:49:19 -08:00
|
|
|
result = error::bad_sec_version;
|
|
|
|
res.body() = result.message();
|
2017-07-14 12:11:44 -07:00
|
|
|
res.prepare_payload();
|
|
|
|
decorate(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response_type res;
|
|
|
|
res.result(http::status::switching_protocols);
|
2017-09-12 13:49:45 -07:00
|
|
|
res.version(req.version());
|
2017-07-14 12:11:44 -07:00
|
|
|
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);
|
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->build_response_pmd(res, req);
|
2017-07-14 12:11:44 -07:00
|
|
|
decorate(res);
|
2018-01-01 17:49:19 -08:00
|
|
|
result = {};
|
2017-07-14 12:11:44 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
// Called when the WebSocket Upgrade response is received
|
|
|
|
template<class NextLayer, bool deflateSupported>
|
|
|
|
void
|
|
|
|
stream<NextLayer, deflateSupported>::
|
|
|
|
on_response(
|
|
|
|
response_type const& res,
|
|
|
|
detail::sec_ws_key_type const& key,
|
|
|
|
error_code& ec)
|
2017-07-14 12:11:44 -07:00
|
|
|
{
|
2018-01-01 17:49:19 -08:00
|
|
|
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);
|
2017-07-14 12:11:44 -07:00
|
|
|
{
|
2018-01-01 17:49:19 -08:00
|
|
|
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);
|
2017-07-14 12:11:44 -07:00
|
|
|
detail::sec_ws_accept_type acc;
|
|
|
|
detail::make_sec_ws_accept(acc, key);
|
2018-01-01 17:49:19 -08:00
|
|
|
if(acc.compare(it->value()) != 0)
|
|
|
|
return err(error::bad_sec_accept);
|
2017-07-14 12:11:44 -07:00
|
|
|
}
|
2018-01-01 17:49:19 -08:00
|
|
|
|
2019-01-19 07:24:00 -08:00
|
|
|
ec = {};
|
|
|
|
impl_->on_response_pmd(res);
|
|
|
|
impl_->open(role_type::client);
|
2017-11-18 16:52:18 -08:00
|
|
|
}
|
|
|
|
|
2017-08-26 20:10:04 -07:00
|
|
|
// _Fail the WebSocket Connection_
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-08-26 20:10:04 -07:00
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-08-26 20:10:04 -07:00
|
|
|
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);
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->status_ = status::closing;
|
|
|
|
if(code != close_code::none && ! impl_->wr_close)
|
2017-08-26 20:10:04 -07:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->wr_close = true;
|
2017-08-26 20:10:04 -07:00
|
|
|
detail::frame_buffer fb;
|
|
|
|
write_close<
|
|
|
|
flat_static_buffer_base>(fb, code);
|
2019-01-19 07:24:00 -08:00
|
|
|
net::write(impl_->stream, fb.data(), ec);
|
2019-01-20 12:25:30 -08:00
|
|
|
if(impl_->check_stop_now(ec))
|
2017-08-26 20:10:04 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
using beast::websocket::teardown;
|
2019-01-19 07:24:00 -08:00
|
|
|
teardown(impl_->role, impl_->stream, ec);
|
2018-11-30 14:58:38 -08:00
|
|
|
if(ec == net::error::eof)
|
2017-08-26 20:10:04 -07:00
|
|
|
{
|
|
|
|
// Rationale:
|
|
|
|
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
|
2019-01-19 07:24:00 -08:00
|
|
|
ec = {};
|
2017-08-26 20:10:04 -07:00
|
|
|
}
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! ec)
|
|
|
|
ec = ev;
|
|
|
|
if(ec && ec != error::closed)
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->status_ = status::failed;
|
2017-08-31 17:52:09 -07:00
|
|
|
else
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->status_ = status::closed;
|
|
|
|
impl_->close();
|
2017-08-26 20:10:04 -07:00
|
|
|
}
|
2017-07-14 12:11:44 -07:00
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
} // websocket
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
#endif
|