WebSocket Decorator is a socket option (API Change):

This changes the interface used to apply a decorator to the HTTP
request or response messages used to perform the WebSocket handshake
as follows:

* Add the `stream_base::decorator` option object

* Add `stream::set_option` overload to set the decorator from
  the option

* The decorator applies to all client and server handshakes
  performed on the stream after the option is set.

* Overloads of the following functions which accept a Decorator
  are deprecated:
  - accept, accept_ex
  - handshake, handshake_ex
  - async_accept, async_accept_ex
  - async_handshake, async_handshake_ex

Actions Required:

* Code which passes decorator to any `websocket::stream` member
  function should call `stream::set_option` instead with a newly
  constructed `stream_base::decorator` object containing the
  decorator. Alternatively, the macro `BOOST_BEAST_ALLOW_DEPRECATED`
  may be defined to 1.
This commit is contained in:
Vinnie Falco
2019-02-19 16:23:30 -08:00
parent 085fb66b26
commit 3f50efa138
41 changed files with 1097 additions and 1391 deletions
@@ -132,6 +132,15 @@ public:
websocket::stream_base::suggested_settings(
websocket::role_type::client));
// Set a decorator to change the User-Agent of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-async-ssl");
}));
// Perform the websocket handshake
ws_.async_handshake(host_, "/",
beast::bind_front_handler(
@@ -110,6 +110,15 @@ public:
websocket::stream_base::suggested_settings(
websocket::role_type::client));
// Set a decorator to change the User-Agent of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-async");
}));
// Perform the websocket handshake
ws_.async_handshake(host_, "/",
beast::bind_front_handler(
@@ -74,6 +74,15 @@ do_session(
// Set a timeout on the operation
beast::get_lowest_layer(ws).expires_after(std::chrono::seconds(30));
// Set a decorator to change the User-Agent of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
// Perform the SSL handshake
ws.next_layer().async_handshake(ssl::stream_base::client, yield[ec]);
if(ec)
@@ -74,6 +74,15 @@ do_session(
websocket::stream_base::suggested_settings(
websocket::role_type::client));
// Set a decorator to change the User-Agent of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
// Perform the websocket handshake
ws.async_handshake(host, "/", yield[ec]);
if(ec)
@@ -73,6 +73,15 @@ int main(int argc, char** argv)
// Perform the SSL handshake
ws.next_layer().handshake(ssl::stream_base::client);
// Set a decorator to change the User-Agent of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
// Perform the websocket handshake
ws.handshake(host, "/");
@@ -60,6 +60,15 @@ int main(int argc, char** argv)
// Make the connection on the IP address we get from a lookup
net::connect(ws.next_layer(), results.begin(), results.end());
// Set a decorator to change the User-Agent of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
// Perform the websocket handshake
ws.handshake(host, "/");
@@ -89,6 +89,15 @@ public:
websocket::stream_base::suggested_settings(
websocket::role_type::server));
// Set a decorator to change the Server of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-async-ssl");
}));
// Accept the websocket handshake
ws_.async_accept(
beast::bind_front_handler(
@@ -52,16 +52,26 @@ public:
session(tcp::socket socket)
: ws_(std::move(socket))
{
// Set suggested timeout settings for the websocket
ws_.set_option(
websocket::stream_base::suggested_settings(
websocket::role_type::server));
}
// Start the asynchronous operation
void
run()
{
// Set suggested timeout settings for the websocket
ws_.set_option(
websocket::stream_base::suggested_settings(
websocket::role_type::server));
// Set a decorator to change the Server of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-async");
}));
// Accept the websocket handshake
ws_.async_accept(
beast::bind_front_handler(
@@ -17,10 +17,6 @@ websocket_session(
: ws_(std::move(socket))
, state_(state)
{
// Set suggested timeout settings for the websocket
ws_.set_option(
websocket::stream_base::suggested_settings(
websocket::role_type::server));
}
websocket_session::
@@ -57,6 +57,20 @@ void
websocket_session::
run(http::request<Body, http::basic_fields<Allocator>> req)
{
// Set suggested timeout settings for the websocket
ws_.set_option(
websocket::stream_base::suggested_settings(
websocket::role_type::server));
// Set a decorator to change the Server of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-chat-multi");
}));
// Accept the websocket handshake
ws_.async_accept(
req,
@@ -75,6 +75,15 @@ do_session(
websocket::stream_base::suggested_settings(
websocket::role_type::server));
// Set a decorator to change the Server of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-coro-ssl");
}));
// Accept the websocket handshake
ws.async_accept(yield[ec]);
if(ec)
@@ -56,6 +56,15 @@ do_session(ws_type& ws, net::yield_context yield)
websocket::stream_base::suggested_settings(
websocket::role_type::server));
// Set a decorator to change the Server of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-coro");
}));
// Accept the websocket handshake
ws.async_accept(yield[ec]);
if(ec)
@@ -90,13 +90,15 @@ do_sync_session(ws_type& ws)
setup_stream(ws);
ws.accept_ex(
// Set a decorator to change the Server of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
"Boost.Beast/" + std::to_string(BOOST_BEAST_VERSION) + "-Sync");
},
ec);
res.set(http::field::server, std::string(
BOOST_BEAST_VERSION_STRING) + "-Sync");
}));
ws.accept(ec);
if(ec)
return fail(ec, "accept");
@@ -158,13 +160,16 @@ public:
void
run()
{
// Accept the websocket handshake
ws_.async_accept_ex(
// Set a decorator to change the Server of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
"Boost.Beast/" + std::to_string(BOOST_BEAST_VERSION) + "-Async");
},
res.set(http::field::server, std::string(
BOOST_BEAST_VERSION_STRING) + "-Async");
}));
// Accept the websocket handshake
ws_.async_accept(
beast::bind_front_handler(
&async_session::on_accept,
shared_from_this()));
@@ -324,13 +329,15 @@ do_coro_session(ws_type& ws, net::yield_context yield)
setup_stream(ws);
ws.async_accept_ex(
[&](websocket::response_type& res)
// Set a decorator to change the Server of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
"Boost.Beast/" + std::to_string(BOOST_BEAST_VERSION) + "-Coro");
},
yield[ec]);
res.set(http::field::server, std::string(
BOOST_BEAST_VERSION_STRING) + "-Fiber");
}));
ws.async_accept(yield[ec]);
if(ec)
return fail(ec, "accept");
@@ -102,6 +102,15 @@ public:
websocket::stream_base::suggested_settings(
websocket::role_type::server));
// Set a decorator to change the Server of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-stackless-ssl");
}));
// Accept the websocket handshake
yield ws_.async_accept(
std::bind(
@@ -79,6 +79,15 @@ public:
websocket::stream_base::suggested_settings(
websocket::role_type::server));
// Set a decorator to change the Server of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-stackless");
}));
// Accept the websocket handshake
yield ws_.async_accept(
std::bind(
@@ -48,6 +48,15 @@ do_session(tcp::socket& socket, ssl::context& ctx)
// Perform the SSL handshake
ws.next_layer().handshake(ssl::stream_base::server);
// Set a decorator to change the Server of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-sync-ssl");
}));
// Accept the websocket handshake
ws.accept();
@@ -39,6 +39,15 @@ do_session(tcp::socket& socket)
// Construct the stream by moving in the socket
websocket::stream<tcp::socket> ws{std::move(socket)};
// Set a decorator to change the Server of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-sync");
}));
// Accept the websocket handshake
ws.accept();