From 8f812aafd584954b85df85c1cbf6402525f35b6a Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sat, 6 Apr 2013 11:09:11 -0500 Subject: [PATCH] adds subprotocol processing to handshake processors --- test/processors/hybi00.cpp | 2 +- test/processors/hybi07.cpp | 2 +- test/processors/hybi08.cpp | 2 +- test/processors/hybi13.cpp | 2 +- websocketpp/processors/hybi00.hpp | 4 ++-- websocketpp/processors/hybi13.hpp | 12 ++++++++++-- websocketpp/processors/processor.hpp | 10 +++++----- 7 files changed, 21 insertions(+), 13 deletions(-) diff --git a/test/processors/hybi00.cpp b/test/processors/hybi00.cpp index 6de746e8fe..3c2eb2133d 100644 --- a/test/processors/hybi00.cpp +++ b/test/processors/hybi00.cpp @@ -79,7 +79,7 @@ BOOST_AUTO_TEST_CASE( exact_match ) { BOOST_CHECK(u->get_resource() == "/"); BOOST_CHECK(u->get_port() == websocketpp::URI_DEFAULT_PORT); - p.process_handshake(r,response); + p.process_handshake(r,"",response); BOOST_CHECK(response.get_header("Connection") == "Upgrade"); BOOST_CHECK(response.get_header("Upgrade") == "websocket"); diff --git a/test/processors/hybi07.cpp b/test/processors/hybi07.cpp index 02310400d6..6231a58e4b 100644 --- a/test/processors/hybi07.cpp +++ b/test/processors/hybi07.cpp @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE( exact_match ) { BOOST_CHECK(u->get_resource() == "/"); BOOST_CHECK(u->get_port() == websocketpp::uri_default_port); - p.process_handshake(r,response); + p.process_handshake(r,"",response); BOOST_CHECK(response.get_header("Connection") == "upgrade"); BOOST_CHECK(response.get_header("Upgrade") == "websocket"); diff --git a/test/processors/hybi08.cpp b/test/processors/hybi08.cpp index df3b16d39e..b704604cc8 100644 --- a/test/processors/hybi08.cpp +++ b/test/processors/hybi08.cpp @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE( exact_match ) { BOOST_CHECK(u->get_resource() == "/"); BOOST_CHECK(u->get_port() == websocketpp::uri_default_port); - p.process_handshake(r,response); + p.process_handshake(r,"",response); BOOST_CHECK(response.get_header("Connection") == "upgrade"); BOOST_CHECK(response.get_header("Upgrade") == "websocket"); diff --git a/test/processors/hybi13.cpp b/test/processors/hybi13.cpp index e97a86707f..738d91e800 100644 --- a/test/processors/hybi13.cpp +++ b/test/processors/hybi13.cpp @@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE( exact_match ) { BOOST_CHECK_EQUAL(u->get_resource(), "/"); BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port); - env.p.process_handshake(env.req,env.res); + env.p.process_handshake(env.req,"",env.res); BOOST_CHECK_EQUAL(env.res.get_header("Connection"), "upgrade"); BOOST_CHECK_EQUAL(env.res.get_header("Upgrade"), "websocket"); diff --git a/websocketpp/processors/hybi00.hpp b/websocketpp/processors/hybi00.hpp index 28d5e7fbf1..7fbfe5f079 100644 --- a/websocketpp/processors/hybi00.hpp +++ b/websocketpp/processors/hybi00.hpp @@ -91,8 +91,8 @@ public: return lib::error_code(); } - lib::error_code process_handshake(const request_type& req, - response_type& res) const + lib::error_code process_handshake(const request_type& req, const + std::string & subprotocol, response_type& res) const { char key_final[16]; diff --git a/websocketpp/processors/hybi13.hpp b/websocketpp/processors/hybi13.hpp index 91a5604a15..e06d764a71 100644 --- a/websocketpp/processors/hybi13.hpp +++ b/websocketpp/processors/hybi13.hpp @@ -165,8 +165,12 @@ public: return lib::error_code(); } - lib::error_code process_handshake(const request_type& request, - response_type& response) const + /* TODO: the 'subprotocol' parameter may need to be expanded into a more + * generic struct if other user input parameters to the processed handshake + * are found. + */ + lib::error_code process_handshake(const request_type& request, const + std::string & subprotocol, response_type& response) const { std::string server_key = request.get_header("Sec-WebSocket-Key"); @@ -180,6 +184,10 @@ public: response.append_header("Upgrade",constants::upgrade_token); response.append_header("Connection",constants::connection_token); + if (!subprotocol.empty()) { + response.replace_header("Sec-WebSocket-Protocol",subprotocol); + } + return lib::error_code(); } diff --git a/websocketpp/processors/processor.hpp b/websocketpp/processors/processor.hpp index fa8837d677..72f4df4b3e 100644 --- a/websocketpp/processors/processor.hpp +++ b/websocketpp/processors/processor.hpp @@ -128,9 +128,7 @@ int get_websocket_version(request_type& r) { // // // handle msg; // } -// } -// -// +// } template class processor { @@ -186,12 +184,14 @@ public: /** * @param req The request to process * + * @param subprotocol The subprotocol in use + * * @param res The response to store the processed response in * * @return An error code, 0 on success, non-zero for other errors */ - virtual lib::error_code process_handshake(const request_type& req, - response_type& res) const = 0; + virtual lib::error_code process_handshake(const request_type& req, const + std::string & subprotocol, response_type& res) const = 0; /// Fill in an HTTP request for an outgoing connection handshake /**