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
|
|
|
|
|
|
|
|
|
|
#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>
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
#include <beast/version.hpp>
|
2016-05-01 12:33:35 -04:00
|
|
|
#include <beast/http/read.hpp>
|
|
|
|
|
#include <beast/http/write.hpp>
|
|
|
|
|
#include <beast/http/reason.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>
|
|
|
|
|
#include <beast/core/buffer_concepts.hpp>
|
|
|
|
|
#include <beast/core/consuming_buffers.hpp>
|
|
|
|
|
#include <beast/core/prepare_buffers.hpp>
|
|
|
|
|
#include <beast/core/static_streambuf.hpp>
|
|
|
|
|
#include <beast/core/stream_concepts.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>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <memory>
|
2016-10-24 18:41:25 -04:00
|
|
|
#include <stdexcept>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
throw std::invalid_argument{
|
|
|
|
|
"invalid server_max_window_bits"};
|
|
|
|
|
if( o.client_max_window_bits > 15 ||
|
|
|
|
|
o.client_max_window_bits < 9)
|
|
|
|
|
throw std::invalid_argument{
|
|
|
|
|
"invalid client_max_window_bits"};
|
|
|
|
|
if( o.compLevel < 0 ||
|
|
|
|
|
o.compLevel > 9)
|
|
|
|
|
throw std::invalid_argument{
|
|
|
|
|
"invalid compLevel"};
|
|
|
|
|
if( o.memLevel < 1 ||
|
|
|
|
|
o.memLevel > 9)
|
|
|
|
|
throw std::invalid_argument{
|
|
|
|
|
"invalid memLevel"};
|
|
|
|
|
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>
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
template<class Decorator>
|
2017-04-25 10:12:43 -07:00
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
do_accept(
|
|
|
|
|
Decorator const& decorator, error_code& ec)
|
2017-04-25 10:12:43 -07:00
|
|
|
{
|
|
|
|
|
http::header_parser<true, http::fields> p;
|
|
|
|
|
auto const bytes_used = http::read_some(
|
|
|
|
|
next_layer(), stream_.buffer(), p, ec);
|
|
|
|
|
if(ec)
|
|
|
|
|
return;
|
|
|
|
|
BOOST_ASSERT(p.got_header());
|
|
|
|
|
stream_.buffer().consume(bytes_used);
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
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>
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
template<class Fields, class Decorator>
|
2017-04-25 10:12:43 -07:00
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
do_accept(http::header<true, Fields> const& req,
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
Decorator const& decorator, error_code& ec)
|
2017-04-25 10:12:43 -07:00
|
|
|
{
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
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;
|
|
|
|
|
if(res.status != 101)
|
|
|
|
|
{
|
|
|
|
|
ec = error::handshake_failed;
|
|
|
|
|
// VFALCO TODO Respect keep alive setting, perform
|
|
|
|
|
// teardown if Connection: close.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pmd_read(pmd_config_, req.fields);
|
|
|
|
|
open(detail::role_type::server);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
template<class NextLayer>
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
template<class RequestDecorator>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
do_handshake(response_type* res_p,
|
|
|
|
|
boost::string_ref const& host,
|
2017-05-02 15:49:22 -07:00
|
|
|
boost::string_ref const& target,
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
RequestDecorator const& decorator,
|
|
|
|
|
error_code& ec)
|
|
|
|
|
{
|
|
|
|
|
response_type res;
|
|
|
|
|
reset();
|
2017-04-29 15:50:05 -07:00
|
|
|
detail::sec_ws_key_type key;
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
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);
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
pmd_read(pmd_config_, req.fields);
|
|
|
|
|
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)
|
|
|
|
|
swap(res, *res_p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class Decorator>
|
|
|
|
|
request_type
|
2017-07-20 08:01:46 -07:00
|
|
|
stream<NextLayer>::
|
2017-04-29 15:50:05 -07:00
|
|
|
build_request(detail::sec_ws_key_type& key,
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
boost::string_ref const& host,
|
2017-05-02 15:49:22 -07:00
|
|
|
boost::string_ref const& target,
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
Decorator const& decorator)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
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-02 15:49:22 -07:00
|
|
|
req.method("GET");
|
2016-11-10 05:34:49 -05:00
|
|
|
req.fields.insert("Host", host);
|
|
|
|
|
req.fields.insert("Upgrade", "websocket");
|
2016-11-20 07:32:41 -05:00
|
|
|
req.fields.insert("Connection", "upgrade");
|
2017-04-29 15:50:05 -07:00
|
|
|
detail::make_sec_ws_key(key, maskgen_);
|
2016-11-10 05:34:49 -05:00
|
|
|
req.fields.insert("Sec-WebSocket-Key", key);
|
|
|
|
|
req.fields.insert("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;
|
|
|
|
|
detail::pmd_write(
|
|
|
|
|
req.fields, config);
|
|
|
|
|
}
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
decorator(req);
|
2017-04-29 15:50:05 -07:00
|
|
|
// VFALCO Use static_string here
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
if(! req.fields.exists("User-Agent"))
|
|
|
|
|
req.fields.insert("User-Agent",
|
|
|
|
|
std::string("Beast/") + BEAST_VERSION_STRING);
|
2017-07-20 08:01:46 -07:00
|
|
|
return req;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
template<class Decorator>
|
|
|
|
|
response_type
|
2017-07-20 08:01:46 -07:00
|
|
|
stream<NextLayer>::
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
build_response(request_type const& req,
|
|
|
|
|
Decorator const& decorator)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
auto const decorate =
|
|
|
|
|
[&decorator](response_type& res)
|
|
|
|
|
{
|
|
|
|
|
decorator(res);
|
2017-04-29 15:50:05 -07:00
|
|
|
// VFALCO Use static_string here
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
if(! res.fields.exists("Server"))
|
|
|
|
|
res.fields.insert("Server",
|
|
|
|
|
std::string("Beast/") +
|
|
|
|
|
BEAST_VERSION_STRING);
|
|
|
|
|
};
|
2017-07-20 08:01:46 -07:00
|
|
|
auto err =
|
|
|
|
|
[&](std::string const& text)
|
|
|
|
|
{
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
response_type res;
|
2016-05-07 15:18:22 -04:00
|
|
|
res.status = 400;
|
2017-05-02 15:49:22 -07:00
|
|
|
res.reason(http::reason_string(res.status));
|
2016-05-07 15:18:22 -04:00
|
|
|
res.version = req.version;
|
|
|
|
|
res.body = text;
|
2017-05-01 21:06:38 -07:00
|
|
|
prepare(res);
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
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-05-02 15:49:22 -07:00
|
|
|
if(req.method() != "GET")
|
2017-07-20 08:01:46 -07:00
|
|
|
return err("Wrong method");
|
|
|
|
|
if(! is_upgrade(req))
|
|
|
|
|
return err("Expected Upgrade request");
|
2016-11-10 05:34:49 -05:00
|
|
|
if(! req.fields.exists("Host"))
|
2017-07-20 08:01:46 -07:00
|
|
|
return err("Missing Host");
|
2016-11-10 05:34:49 -05:00
|
|
|
if(! req.fields.exists("Sec-WebSocket-Key"))
|
2017-07-20 08:01:46 -07:00
|
|
|
return err("Missing Sec-WebSocket-Key");
|
2016-11-10 05:34:49 -05:00
|
|
|
if(! http::token_list{req.fields["Upgrade"]}.exists("websocket"))
|
2016-05-15 16:22:25 -04:00
|
|
|
return err("Missing websocket Upgrade token");
|
2017-04-29 15:50:05 -07:00
|
|
|
auto const key = req.fields["Sec-WebSocket-Key"];
|
|
|
|
|
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 =
|
2016-11-10 05:34:49 -05:00
|
|
|
req.fields["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
|
|
|
{
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
2017-04-25 09:35:22 -07:00
|
|
|
response_type res;
|
2016-05-15 16:22:25 -04:00
|
|
|
res.status = 426;
|
2017-05-02 15:49:22 -07:00
|
|
|
res.reason(http::reason_string(res.status));
|
2016-05-15 16:22:25 -04:00
|
|
|
res.version = req.version;
|
2016-11-10 05:34:49 -05:00
|
|
|
res.fields.insert("Sec-WebSocket-Version", "13");
|
2017-05-01 21:06:38 -07:00
|
|
|
prepare(res);
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
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
|
|
|
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
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;
|
|
|
|
|
pmd_read(offer, req.fields);
|
|
|
|
|
pmd_negotiate(
|
|
|
|
|
res.fields, unused, offer, pmd_opts_);
|
|
|
|
|
}
|
2016-05-07 15:18:22 -04:00
|
|
|
res.status = 101;
|
2017-05-02 15:49:22 -07:00
|
|
|
res.reason(http::reason_string(res.status));
|
2016-05-07 15:18:22 -04:00
|
|
|
res.version = req.version;
|
2016-11-10 05:34:49 -05:00
|
|
|
res.fields.insert("Upgrade", "websocket");
|
2016-11-20 07:32:41 -05:00
|
|
|
res.fields.insert("Connection", "upgrade");
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-04-29 15:50:05 -07:00
|
|
|
detail::sec_ws_accept_type accept;
|
|
|
|
|
detail::make_sec_ws_accept(accept, key);
|
|
|
|
|
res.fields.insert("Sec-WebSocket-Accept", accept);
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
Refactor websocket decorators (API Change):
fix #80, #212, fix #303, fix #314, fix #317
websocket::stream now provides the following families of
functions for performing handshakes:
When operating in the server role:
* stream::accept
* stream::accept_ex
* stream::async_accept
* stream::async_accept_ex
When operating in the client role:
* stream::handshake
* stream::handshake_ex
* stream::async_handshake
* stream::async_handshake_ex
Member functions ending with "_ex" allow an additional
RequestDecorator parameter (for the accept family of
functions) or ResponseDecorator parameter (for the
handshake family of functions).
The decorator is called to optionally modify the contents
of the HTTP request or HTTP response object generated by
the implementation, before the message is sent. This
permits callers to set the User-Agent or Server fields,
add or modify HTTP fields related to subprotocols, or
perform any required transformation of the HTTP message
for application-specific needs.
The handshake() family of functions now have an additional
set of overloads accepting a parameter of type response_type&,
allowing the caller to receive the HTTP Response to the
Upgrade handshake. This permits inspection of the response
to handle things like subprotocols, authentication, or
other application-specific needs.
The new implementation does not require any state to be
stored in the stream object. Therefore, websocket::stream
objects are now smaller in size.
The overload of set_option for setting a decorator on the
stream is removed. The only way to set decorators now is
with a suitable overload of accept or handshake.
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>::
|
2016-11-20 07:32:41 -05:00
|
|
|
do_response(http::response_header 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;
|
|
|
|
|
if(res.status != 101)
|
|
|
|
|
return false;
|
|
|
|
|
if(! is_upgrade(res))
|
|
|
|
|
return false;
|
|
|
|
|
if(! http::token_list{res.fields["Upgrade"]}.exists("websocket"))
|
|
|
|
|
return false;
|
|
|
|
|
if(! res.fields.exists("Sec-WebSocket-Accept"))
|
|
|
|
|
return false;
|
|
|
|
|
detail::sec_ws_accept_type accept;
|
|
|
|
|
detail::make_sec_ws_accept(accept, key);
|
|
|
|
|
if(accept.compare(
|
|
|
|
|
res.fields["Sec-WebSocket-Accept"]) != 0)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}();
|
|
|
|
|
if(! success)
|
|
|
|
|
{
|
|
|
|
|
ec = error::handshake_failed;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-10-24 18:41:25 -04:00
|
|
|
detail::pmd_offer offer;
|
|
|
|
|
pmd_read(offer, res.fields);
|
|
|
|
|
// 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
|