diff --git a/test/roles/server.cpp b/test/roles/server.cpp index ba2dd36a03..522be9bdcb 100644 --- a/test/roles/server.cpp +++ b/test/roles/server.cpp @@ -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); -}*/ \ No newline at end of file +}*/ diff --git a/websocketpp/connection.hpp b/websocketpp/connection.hpp index 2e62cd8312..1e31e85103 100644 --- a/websocketpp/connection.hpp +++ b/websocketpp/connection.hpp @@ -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 & 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 m_requested_subprotocols; + const bool m_is_server; alog_type& m_alog; elog_type& m_elog; diff --git a/websocketpp/impl/connection_impl.hpp b/websocketpp/impl/connection_impl.hpp index dd0f3cc5e7..6914de4636 100644 --- a/websocketpp/impl/connection_impl.hpp +++ b/websocketpp/impl/connection_impl.hpp @@ -321,6 +321,12 @@ const std::string & connection::get_subprotocol() const { return m_subprotocol; } +template +const std::vector & +connection::get_requested_subprotocols() const { + return m_requested_subprotocols; +} + @@ -841,6 +847,21 @@ bool connection::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); diff --git a/websocketpp/logger/basic.hpp b/websocketpp/logger/basic.hpp index 66a77be563..13492ce08b 100644 --- a/websocketpp/logger/basic.hpp +++ b/websocketpp/logger/basic.hpp @@ -107,7 +107,7 @@ private: } mutex_type m_lock; - + char buffer[40]; const level m_static_channels; level m_dynamic_channels;