From 6063e2266da9f307281662bb7fed2ce2bab768bb Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Thu, 29 Sep 2011 07:45:59 -0500 Subject: [PATCH] Added utility class for parsing WS urls --- src/network_utilities.cpp | 40 ++++++++++++++++++++++++++++++++++++++- src/network_utilities.hpp | 13 +++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/network_utilities.cpp b/src/network_utilities.cpp index a6c870f135..b7da9f932e 100644 --- a/src/network_utilities.cpp +++ b/src/network_utilities.cpp @@ -132,4 +132,42 @@ std::string lookup_ws_close_status_string(uint16_t code) { default: return "Unknown"; } -} \ No newline at end of file +} + +bool websocketpp::ws_uri::parse(const std::string& uri) { + boost::cmatch what; + static const boost::regex expression("(ws|wss)://([^/:\\[]+|\\[[0-9:]+\\])(:\\d{1,5})?(/.*)?"); + + if (boost::regex_match(uri.c_str(), what, expression)) { + if (what[1] == "wss") { + secure = true; + } else { + secure = false; + } + + host = what[2]; + + if (what[3] == "") { + port = (secure ? 443 : 80); + } else { + unsigned int t_port = atoi(std::string(what[3]).substr(1).c_str()); + + if (t_port > 65535) { + return false; + } + + port = atoi(std::string(what[3]).substr(1).c_str()); + } + + if (what[4] == "") { + resource = "/"; + } else { + resource = what[4]; + } + + return true; + } else { + return false; + } + +} diff --git a/src/network_utilities.hpp b/src/network_utilities.hpp index 911464a772..a5871219e8 100644 --- a/src/network_utilities.hpp +++ b/src/network_utilities.hpp @@ -30,6 +30,7 @@ #include #include +#include // http://www.viva64.com/en/k/0018/ // TODO: impliment stuff from here: @@ -45,4 +46,16 @@ uint64_t ntohll(uint64_t src); std::string lookup_http_error_string(int code); std::string lookup_ws_close_status_string(uint16_t code); +namespace websocketpp { +struct ws_uri { + bool parse(const std::string& uri); + + bool secure; + std::string host; + uint16_t port; + std::string resource; +}; +} + + #endif // NETWORK_UTILITIES_HPP