Skip to content

Commit

Permalink
Rewrite uri parser (ipv6, non-numeric port crash)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Hultman committed Aug 10, 2017
1 parent 7facffc commit e402f02
Showing 1 changed file with 63 additions and 28 deletions.
91 changes: 63 additions & 28 deletions src/Hub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down

0 comments on commit e402f02

Please sign in to comment.