adds preliminary server side subprotocol negotiation

This commit is contained in:
Peter Thorson
2013-04-05 21:10:19 -05:00
parent 10db03d710
commit 6d1b956aff
4 changed files with 48 additions and 9 deletions

View File

@@ -85,15 +85,20 @@ void echo_func(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
s->send(hdl, msg->get_payload(), msg->get_opcode());
}
/*void validate_func_subprotocol(server* s, std::string* out, websocketpp::connection_hdl hdl) {
bool validate_func_subprotocol(server* s, std::string* out, websocketpp::connection_hdl hdl) {
server::connection_ptr con = s->get_con_from_hdl(hdl);
std::stringstream o;
o << con->get_subprotocols.size();
o << con->get_requested_subprotocols().size();
if (con->get_requested_subprotocols().size() == 1) {
o << "," << con->get_requested_subprotocols()[0];
}
*out = o.str();
}*/
return true;
}
void open_func_subprotocol(server* s, std::string* out, websocketpp::connection_hdl hdl) {
server::connection_ptr con = s->get_con_from_hdl(hdl);
@@ -149,7 +154,7 @@ BOOST_AUTO_TEST_CASE( accept_subprotocol_empty ) {
BOOST_CHECK_EQUAL(subprotocol, "");
}
/*BOOST_AUTO_TEST_CASE( accept_subprotocol_one ) {
BOOST_AUTO_TEST_CASE( accept_subprotocol_one ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
@@ -163,9 +168,9 @@ BOOST_AUTO_TEST_CASE( accept_subprotocol_empty ) {
s.set_open_handler(bind(&open_func_subprotocol,&s,&open,::_1));
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
BOOST_CHECK_EQUAL(validate, "1");
BOOST_CHECK_EQUAL(validate, "1,foo");
BOOST_CHECK_EQUAL(open, "");
}*/
}
/*BOOST_AUTO_TEST_CASE( user_reject_origin ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example2.com\r\n\r\n";
@@ -175,4 +180,4 @@ BOOST_AUTO_TEST_CASE( accept_subprotocol_empty ) {
s.set_user_agent("test");
BOOST_CHECK(run_server_test(s,input) == output);
}*/
}*/

View File

@@ -533,6 +533,15 @@ public:
*/
const std::string& get_subprotocol() const;
/// Gets all of the subprotocols requested by the client
/**
* Retrieves the subprotocols that were requested during the handshake. This
* method is valid in the validate handler and later.
*
* @return A vector of the requested subprotocol
*/
const std::vector<std::string> & get_requested_subprotocols() const;
/////////////////////////////////////////////////////////////
// Pass-through access to the request and response objects //
/////////////////////////////////////////////////////////////
@@ -967,6 +976,10 @@ private:
uri_ptr m_uri;
std::string m_subprotocol;
// connection data that might not be necessary to keep around for the life
// of the whole connection.
std::vector<std::string> m_requested_subprotocols;
const bool m_is_server;
alog_type& m_alog;
elog_type& m_elog;

View File

@@ -321,6 +321,12 @@ const std::string & connection<config>::get_subprotocol() const {
return m_subprotocol;
}
template <typename config>
const std::vector<std::string> &
connection<config>::get_requested_subprotocols() const {
return m_requested_subprotocols;
}
@@ -841,6 +847,21 @@ bool connection<config>::process_handshake_request() {
return false;
}
// extract subprotocols
if (!m_request.get_header("Sec-WebSocket-Protocol").empty()) {
typename request_type::parameter_list p;
if (!m_request.get_header_as_plist("Sec-WebSocket-Protocol",p)) {
typename request_type::parameter_list::const_iterator it;
for (it = p.begin(); it != p.end(); ++it) {
m_requested_subprotocols.push_back(it->first);
}
} else {
// TODO: just ignore? log? fail?
}
}
// Ask application to validate the connection
if (!m_validate_handler || m_validate_handler(m_connection_hdl)) {
m_response.set_status(http::status_code::switching_protocols);

View File

@@ -107,7 +107,7 @@ private:
}
mutex_type m_lock;
char buffer[40];
const level m_static_channels;
level m_dynamic_channels;