2017-07-20 08:01:46 -07:00
|
|
|
//
|
2017-02-06 20:07:03 -05:00
|
|
|
// Copyright (c) 2013-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)
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef BEAST_WEBSOCKET_IMPL_STREAM_IPP
|
|
|
|
|
#define BEAST_WEBSOCKET_IMPL_STREAM_IPP
|
|
|
|
|
|
2017-06-07 05:00:18 -07:00
|
|
|
#include <beast/websocket/rfc6455.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <beast/websocket/teardown.hpp>
|
|
|
|
|
#include <beast/websocket/detail/hybi13.hpp>
|
2016-10-24 18:41:25 -04:00
|
|
|
#include <beast/websocket/detail/pmd_extension.hpp>
|
2017-04-25 09:35:22 -07:00
|
|
|
#include <beast/version.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <beast/http/read.hpp>
|
|
|
|
|
#include <beast/http/write.hpp>
|
2016-05-24 06:17:04 -04:00
|
|
|
#include <beast/http/rfc7230.hpp>
|
2016-05-07 14:57:15 -04:00
|
|
|
#include <beast/core/buffer_cat.hpp>
|
2017-05-14 08:23:37 -07:00
|
|
|
#include <beast/core/buffer_prefix.hpp>
|
2016-05-07 14:57:15 -04:00
|
|
|
#include <beast/core/consuming_buffers.hpp>
|
2017-05-04 18:50:25 -07:00
|
|
|
#include <beast/core/static_buffer.hpp>
|
2017-05-10 12:03:00 -07:00
|
|
|
#include <beast/core/type_traits.hpp>
|
2016-11-02 16:02:43 -04:00
|
|
|
#include <beast/core/detail/type_traits.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-05-22 15:30:12 -07:00
|
|
|
#include <boost/throw_exception.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <memory>
|
2016-10-24 18:41:25 -04:00
|
|
|
#include <stdexcept>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2017-05-10 12:03:00 -07:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
namespace beast {
|
|
|
|
|
namespace websocket {
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class... Args>
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
stream(Args&&... args)
|
2016-04-30 13:00:33 -04:00
|
|
|
: stream_(std::forward<Args>(args)...)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-04-30 13:00:33 -04:00
|
|
|
}
|
|
|
|
|
|
2016-10-24 18:41:25 -04:00
|
|
|
template<class NextLayer>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
set_option(permessage_deflate const& o)
|
|
|
|
|
{
|
|
|
|
|
if( o.server_max_window_bits > 15 ||
|
|
|
|
|
o.server_max_window_bits < 9)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument{
|
|
|
|
|
"invalid server_max_window_bits"});
|
2016-10-24 18:41:25 -04:00
|
|
|
if( o.client_max_window_bits > 15 ||
|
|
|
|
|
o.client_max_window_bits < 9)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument{
|
|
|
|
|
"invalid client_max_window_bits"});
|
2016-10-24 18:41:25 -04:00
|
|
|
if( o.compLevel < 0 ||
|
|
|
|
|
o.compLevel > 9)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument{
|
|
|
|
|
"invalid compLevel"});
|
2016-10-24 18:41:25 -04:00
|
|
|
if( o.memLevel < 1 ||
|
|
|
|
|
o.memLevel > 9)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument{
|
|
|
|
|
"invalid memLevel"});
|
2016-10-24 18:41:25 -04:00
|
|
|
pmd_opts_ = o;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2016-05-15 16:22:25 -04:00
|
|
|
template<class NextLayer>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
reset()
|
|
|
|
|
{
|
|
|
|
|
failed_ = false;
|
2016-10-24 18:41:25 -04:00
|
|
|
rd_.cont = false;
|
2016-05-15 16:22:25 -04:00
|
|
|
wr_close_ = false;
|
2016-06-10 15:48:39 -04:00
|
|
|
wr_.cont = false;
|
2016-05-15 16:22:25 -04:00
|
|
|
wr_block_ = nullptr; // should be nullptr on close anyway
|
2017-02-03 16:22:28 -05:00
|
|
|
ping_data_ = nullptr; // should be nullptr on close anyway
|
2016-05-15 16:22:25 -04:00
|
|
|
|
|
|
|
|
stream_.buffer().consume(
|
|
|
|
|
stream_.buffer().size());
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 10:12:43 -07:00
|
|
|
template<class NextLayer>
|
2017-04-25 09:35:22 -07:00
|
|
|
template<class Decorator>
|
2017-04-25 10:12:43 -07:00
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
2017-04-25 09:35:22 -07:00
|
|
|
do_accept(
|
|
|
|
|
Decorator const& decorator, error_code& ec)
|
2017-04-25 10:12:43 -07:00
|
|
|
{
|
2017-06-05 07:20:58 -07:00
|
|
|
http::request_parser<http::empty_body> p;
|
|
|
|
|
http::read_header(next_layer(),
|
|
|
|
|
stream_.buffer(), p, ec);
|
2017-04-25 10:12:43 -07:00
|
|
|
if(ec)
|
|
|
|
|
return;
|
2017-04-25 09:35:22 -07:00
|
|
|
do_accept(p.get(), decorator, ec);
|
2017-04-25 10:12:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
2017-06-19 13:01:59 -07:00
|
|
|
template<class Allocator, class Decorator>
|
2017-04-25 10:12:43 -07:00
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
2017-06-19 13:01:59 -07:00
|
|
|
do_accept(http::header<true,
|
|
|
|
|
http::basic_fields<Allocator>> const& req,
|
|
|
|
|
Decorator const& decorator, error_code& ec)
|
2017-04-25 10:12:43 -07:00
|
|
|
{
|
2017-04-25 09:35:22 -07:00
|
|
|
auto const res = build_response(req, decorator);
|
2017-04-25 10:12:43 -07:00
|
|
|
http::write(stream_, res, ec);
|
|
|
|
|
if(ec)
|
|
|
|
|
return;
|
2017-06-04 12:38:42 -07:00
|
|
|
if(res.result() != http::status::switching_protocols)
|
2017-04-25 10:12:43 -07:00
|
|
|
{
|
|
|
|
|
ec = error::handshake_failed;
|
|
|
|
|
// VFALCO TODO Respect keep alive setting, perform
|
|
|
|
|
// teardown if Connection: close.
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-05 19:28:17 -07:00
|
|
|
pmd_read(pmd_config_, req);
|
2017-04-25 10:12:43 -07:00
|
|
|
open(detail::role_type::server);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
template<class NextLayer>
|
2017-04-25 09:35:22 -07:00
|
|
|
template<class RequestDecorator>
|
|
|
|
|
void
|
2017-07-20 08:01:46 -07:00
|
|
|
stream<NextLayer>::
|
2017-04-25 09:35:22 -07:00
|
|
|
do_handshake(response_type* res_p,
|
2017-06-04 10:52:28 -07:00
|
|
|
string_view host,
|
|
|
|
|
string_view target,
|
2017-04-25 09:35:22 -07:00
|
|
|
RequestDecorator const& decorator,
|
|
|
|
|
error_code& ec)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-04-25 09:35:22 -07:00
|
|
|
response_type res;
|
|
|
|
|
reset();
|
2017-04-29 15:50:05 -07:00
|
|
|
detail::sec_ws_key_type key;
|
2017-04-25 09:35:22 -07:00
|
|
|
{
|
|
|
|
|
auto const req = build_request(
|
2017-05-02 15:49:22 -07:00
|
|
|
key, host, target, decorator);
|
2017-06-05 19:28:17 -07:00
|
|
|
pmd_read(pmd_config_, req);
|
2017-04-25 09:35:22 -07:00
|
|
|
http::write(stream_, req, ec);
|
|
|
|
|
}
|
|
|
|
|
if(ec)
|
|
|
|
|
return;
|
|
|
|
|
http::read(next_layer(), stream_.buffer(), res, ec);
|
|
|
|
|
if(ec)
|
|
|
|
|
return;
|
|
|
|
|
do_response(res, key, ec);
|
|
|
|
|
if(res_p)
|
2017-06-13 07:08:52 -07:00
|
|
|
*res_p = std::move(res);
|
2017-04-25 09:35:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class Decorator>
|
|
|
|
|
request_type
|
|
|
|
|
stream<NextLayer>::
|
2017-04-29 15:50:05 -07:00
|
|
|
build_request(detail::sec_ws_key_type& key,
|
2017-06-04 10:52:28 -07:00
|
|
|
string_view host,
|
|
|
|
|
string_view target,
|
2017-04-25 09:35:22 -07:00
|
|
|
Decorator const& decorator)
|
|
|
|
|
{
|
|
|
|
|
request_type req;
|
2017-05-02 15:49:22 -07:00
|
|
|
req.target(target);
|
2017-07-20 08:01:46 -07:00
|
|
|
req.version = 11;
|
2017-05-30 06:58:40 -07:00
|
|
|
req.method(http::verb::get);
|
2017-06-18 14:57:32 -07:00
|
|
|
req.set(http::field::host, host);
|
|
|
|
|
req.set(http::field::upgrade, "websocket");
|
|
|
|
|
req.set(http::field::connection, "upgrade");
|
2017-04-29 15:50:05 -07:00
|
|
|
detail::make_sec_ws_key(key, maskgen_);
|
2017-06-18 14:57:32 -07:00
|
|
|
req.set(http::field::sec_websocket_key, key);
|
|
|
|
|
req.set(http::field::sec_websocket_version, "13");
|
2016-10-24 18:41:25 -04:00
|
|
|
if(pmd_opts_.client_enable)
|
|
|
|
|
{
|
|
|
|
|
detail::pmd_offer config;
|
|
|
|
|
config.accept = true;
|
|
|
|
|
config.server_max_window_bits =
|
|
|
|
|
pmd_opts_.server_max_window_bits;
|
|
|
|
|
config.client_max_window_bits =
|
|
|
|
|
pmd_opts_.client_max_window_bits;
|
|
|
|
|
config.server_no_context_takeover =
|
|
|
|
|
pmd_opts_.server_no_context_takeover;
|
|
|
|
|
config.client_no_context_takeover =
|
|
|
|
|
pmd_opts_.client_no_context_takeover;
|
2017-06-05 19:28:17 -07:00
|
|
|
detail::pmd_write(req, config);
|
2016-10-24 18:41:25 -04:00
|
|
|
}
|
2017-04-25 09:35:22 -07:00
|
|
|
decorator(req);
|
2017-06-10 15:05:32 -07:00
|
|
|
if(! req.count(http::field::user_agent))
|
2017-06-18 14:57:32 -07:00
|
|
|
req.set(http::field::user_agent,
|
2017-06-10 15:05:32 -07:00
|
|
|
BEAST_VERSION_STRING);
|
2017-07-20 08:01:46 -07:00
|
|
|
return req;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
2017-06-19 13:01:59 -07:00
|
|
|
template<class Allocator, class Decorator>
|
2017-04-25 09:35:22 -07:00
|
|
|
response_type
|
2017-07-20 08:01:46 -07:00
|
|
|
stream<NextLayer>::
|
2017-06-19 13:01:59 -07:00
|
|
|
build_response(http::header<true,
|
|
|
|
|
http::basic_fields<Allocator>> const& req,
|
|
|
|
|
Decorator const& decorator)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-04-25 09:35:22 -07:00
|
|
|
auto const decorate =
|
|
|
|
|
[&decorator](response_type& res)
|
|
|
|
|
{
|
|
|
|
|
decorator(res);
|
2017-06-10 15:05:32 -07:00
|
|
|
if(! res.count(http::field::server))
|
2017-05-07 17:33:03 -07:00
|
|
|
{
|
2017-06-06 12:49:03 -07:00
|
|
|
BOOST_STATIC_ASSERT(sizeof(BEAST_VERSION_STRING) < 20);
|
2017-05-07 17:33:03 -07:00
|
|
|
static_string<20> s(BEAST_VERSION_STRING);
|
2017-06-18 14:57:32 -07:00
|
|
|
res.set(http::field::server, s);
|
2017-05-07 17:33:03 -07:00
|
|
|
}
|
2017-04-25 09:35:22 -07:00
|
|
|
};
|
2017-07-20 08:01:46 -07:00
|
|
|
auto err =
|
|
|
|
|
[&](std::string const& text)
|
|
|
|
|
{
|
2017-04-25 09:35:22 -07:00
|
|
|
response_type res;
|
2016-05-07 15:18:22 -04:00
|
|
|
res.version = req.version;
|
2017-06-05 21:11:33 -07:00
|
|
|
res.result(http::status::bad_request);
|
2016-05-07 15:18:22 -04:00
|
|
|
res.body = text;
|
2017-06-19 15:37:39 -07:00
|
|
|
res.prepare_payload();
|
2017-04-25 09:35:22 -07:00
|
|
|
decorate(res);
|
2016-05-07 15:18:22 -04:00
|
|
|
return res;
|
2017-07-20 08:01:46 -07:00
|
|
|
};
|
|
|
|
|
if(req.version < 11)
|
|
|
|
|
return err("HTTP version 1.1 required");
|
2017-06-04 10:03:36 -07:00
|
|
|
if(req.method() != http::verb::get)
|
2017-07-20 08:01:46 -07:00
|
|
|
return err("Wrong method");
|
|
|
|
|
if(! is_upgrade(req))
|
|
|
|
|
return err("Expected Upgrade request");
|
2017-06-10 15:05:32 -07:00
|
|
|
if(! req.count(http::field::host))
|
2017-07-20 08:01:46 -07:00
|
|
|
return err("Missing Host");
|
2017-06-10 15:05:32 -07:00
|
|
|
if(! req.count(http::field::sec_websocket_key))
|
2017-07-20 08:01:46 -07:00
|
|
|
return err("Missing Sec-WebSocket-Key");
|
2017-06-10 15:05:32 -07:00
|
|
|
if(! http::token_list{req[http::field::upgrade]}.exists("websocket"))
|
2016-05-15 16:22:25 -04:00
|
|
|
return err("Missing websocket Upgrade token");
|
2017-06-10 15:05:32 -07:00
|
|
|
auto const key = req[http::field::sec_websocket_key];
|
2017-04-29 15:50:05 -07:00
|
|
|
if(key.size() > detail::sec_ws_key_type::max_size_n)
|
|
|
|
|
return err("Invalid Sec-WebSocket-Key");
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
|
auto const version =
|
2017-06-10 15:05:32 -07:00
|
|
|
req[http::field::sec_websocket_version];
|
2017-07-20 08:01:46 -07:00
|
|
|
if(version.empty())
|
|
|
|
|
return err("Missing Sec-WebSocket-Version");
|
|
|
|
|
if(version != "13")
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2017-04-25 09:35:22 -07:00
|
|
|
response_type res;
|
2017-06-04 12:38:42 -07:00
|
|
|
res.result(http::status::upgrade_required);
|
2016-05-15 16:22:25 -04:00
|
|
|
res.version = req.version;
|
2017-06-18 14:57:32 -07:00
|
|
|
res.set(http::field::sec_websocket_version, "13");
|
2017-06-19 15:37:39 -07:00
|
|
|
res.prepare_payload();
|
2017-04-25 09:35:22 -07:00
|
|
|
decorate(res);
|
2016-05-15 16:22:25 -04:00
|
|
|
return res;
|
|
|
|
|
}
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
2017-04-29 15:50:05 -07:00
|
|
|
|
2017-04-25 09:35:22 -07:00
|
|
|
response_type res;
|
2016-10-24 18:41:25 -04:00
|
|
|
{
|
|
|
|
|
detail::pmd_offer offer;
|
|
|
|
|
detail::pmd_offer unused;
|
2017-06-05 19:28:17 -07:00
|
|
|
pmd_read(offer, req);
|
|
|
|
|
pmd_negotiate(res, unused, offer, pmd_opts_);
|
2016-10-24 18:41:25 -04:00
|
|
|
}
|
2017-06-04 12:38:42 -07:00
|
|
|
res.result(http::status::switching_protocols);
|
2016-05-07 15:18:22 -04:00
|
|
|
res.version = req.version;
|
2017-06-18 14:57:32 -07:00
|
|
|
res.set(http::field::upgrade, "websocket");
|
|
|
|
|
res.set(http::field::connection, "upgrade");
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-06-18 14:57:32 -07:00
|
|
|
detail::sec_ws_accept_type acc;
|
|
|
|
|
detail::make_sec_ws_accept(acc, key);
|
|
|
|
|
res.set(http::field::sec_websocket_accept, acc);
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
2017-04-25 09:35:22 -07:00
|
|
|
decorate(res);
|
2016-05-07 15:18:22 -04:00
|
|
|
return res;
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
2017-05-27 22:49:22 -07:00
|
|
|
do_response(http::header<false> const& res,
|
2017-04-29 15:50:05 -07:00
|
|
|
detail::sec_ws_key_type const& key, error_code& ec)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-05-01 20:49:51 -07:00
|
|
|
bool const success = [&]()
|
|
|
|
|
{
|
|
|
|
|
if(res.version < 11)
|
|
|
|
|
return false;
|
2017-06-04 12:38:42 -07:00
|
|
|
if(res.result() != http::status::switching_protocols)
|
2017-05-01 20:49:51 -07:00
|
|
|
return false;
|
2017-06-10 15:05:32 -07:00
|
|
|
if(! http::token_list{res[http::field::connection]}.exists("upgrade"))
|
2017-05-01 20:49:51 -07:00
|
|
|
return false;
|
2017-06-10 15:05:32 -07:00
|
|
|
if(! http::token_list{res[http::field::upgrade]}.exists("websocket"))
|
2017-05-01 20:49:51 -07:00
|
|
|
return false;
|
2017-06-10 15:05:32 -07:00
|
|
|
if(res.count(http::field::sec_websocket_accept) != 1)
|
2017-05-01 20:49:51 -07:00
|
|
|
return false;
|
2017-06-18 14:57:32 -07:00
|
|
|
detail::sec_ws_accept_type acc;
|
|
|
|
|
detail::make_sec_ws_accept(acc, key);
|
|
|
|
|
if(acc.compare(
|
2017-06-10 15:05:32 -07:00
|
|
|
res[http::field::sec_websocket_accept]) != 0)
|
2017-05-01 20:49:51 -07:00
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}();
|
|
|
|
|
if(! success)
|
|
|
|
|
{
|
|
|
|
|
ec = error::handshake_failed;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-19 16:57:12 -07:00
|
|
|
ec.assign(0, ec.category());
|
2016-10-24 18:41:25 -04:00
|
|
|
detail::pmd_offer offer;
|
2017-06-05 19:28:17 -07:00
|
|
|
pmd_read(offer, res);
|
2016-10-24 18:41:25 -04:00
|
|
|
// VFALCO see if offer satisfies pmd_config_,
|
|
|
|
|
// return an error if not.
|
|
|
|
|
pmd_config_ = offer; // overwrite for now
|
2016-05-15 16:22:25 -04:00
|
|
|
open(detail::role_type::client);
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // websocket
|
|
|
|
|
} // beast
|
|
|
|
|
|
|
|
|
|
#endif
|