Commit e402f022 authored by Alex Hultman's avatar Alex Hultman

Rewrite uri parser (ipv6, non-numeric port crash)

parent 7facffcf
......@@ -84,43 +84,80 @@ uS::Socket *allocateHttpSocket(uS::Socket *s) {
return (uS::Socket *) new HttpSocket<CLIENT>(s);
}
void Hub::connect(std::string uri, void *user, std::map<std::string, std::string> extraHeaders, int timeoutMs, Group<CLIENT> *eh) {
if (!eh) {
eh = (Group<CLIENT> *) this;
bool parseURI(std::string &uri, bool &secure, std::string &hostname, int &port, std::string &path) {
port = 80;
secure = false;
size_t offset = 5;
if (!uri.compare(0, 6, "wss://")) {
port = 443;
secure = true;
offset = 6;
} else if (uri.compare(0, 5, "ws://")) {
return false;
}
size_t offset = 0;
std::string protocol = uri.substr(offset, uri.find("://")), hostname, portStr, path;
if ((offset += protocol.length() + 3) < uri.length()) {
hostname = uri.substr(offset, uri.find_first_of(":/", offset) - offset);
if (offset == uri.length()) {
return false;
}
offset += hostname.length();
if (uri[offset] == ':') {
offset++;
portStr = uri.substr(offset, uri.find("/", offset) - offset);
if (uri[offset] == '[') {
if (++offset == uri.length()) {
return false;
}
offset += portStr.length();
if (uri[offset] == '/') {
path = uri.substr(++offset);
size_t endBracket = uri.find(']', offset);
if (endBracket == std::string::npos) {
return false;
}
hostname = uri.substr(offset, endBracket - offset);
offset = endBracket + 1;
} else {
hostname = uri.substr(offset, uri.find_first_of(":/", offset) - offset);
offset += hostname.length();
}
if (hostname.length()) {
int port = 80;
bool secure = false;
if (protocol == "wss") {
port = 443;
secure = true;
} else if (protocol != "ws") {
eh->errorHandler(user);
return;
}
if (offset == uri.length()) {
path.clear();
return true;
}
if (uri[offset] == ':') {
offset++;
std::string portStr = uri.substr(offset, uri.find('/', offset) - offset);
if (portStr.length()) {
port = stoi(portStr);
try {
port = stoi(portStr);
} catch (...) {
return false;
}
} else {
return false;
}
offset += portStr.length();
}
if (offset == uri.length()) {
path.clear();
return true;
}
if (uri[offset] == '/') {
path = uri.substr(++offset);
}
return true;
}
void Hub::connect(std::string uri, void *user, std::map<std::string, std::string> extraHeaders, int timeoutMs, Group<CLIENT> *eh) {
if (!eh) {
eh = (Group<CLIENT> *) this;
}
int port;
bool secure;
std::string hostname, path;
if (!parseURI(uri, secure, hostname, port, path)) {
eh->errorHandler(user);
} else {
HttpSocket<CLIENT> *httpSocket = (HttpSocket<CLIENT> *) uS::Node::connect<allocateHttpSocket, onClientConnection>(hostname.c_str(), port, secure, eh);
if (httpSocket) {
// startTimeout occupies the user
......@@ -147,8 +184,6 @@ void Hub::connect(std::string uri, void *user, std::map<std::string, std::string
} else {
eh->errorHandler(user);
}
} else {
eh->errorHandler(user);
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment