Added utility class for parsing WS urls

This commit is contained in:
Peter Thorson
2011-09-29 07:45:59 -05:00
parent b1c2243872
commit 6063e2266d
2 changed files with 52 additions and 1 deletions

View File

@@ -132,4 +132,42 @@ std::string lookup_ws_close_status_string(uint16_t code) {
default:
return "Unknown";
}
}
}
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;
}
}

View File

@@ -30,6 +30,7 @@
#include <stdint.h>
#include <string>
#include <boost/regex.hpp>
// 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