Fixed URL parsing. Set default value for path.

Was matching parsing "ws://echo.websocket.org/" as host="echo.websocket.org/" which shouldn't have the "/" at the end.
Was also getting bogus values for path when it should have taken on a default value.
This commit is contained in:
David Baird
2013-07-12 13:08:45 -06:00
parent b7825802b9
commit ebd4a607fb
+7 -4
View File
@@ -279,20 +279,23 @@ WebSocket::pointer WebSocket::from_url(std::string url) {
int port; int port;
char path[128]; char path[128];
if (false) { } if (false) { }
else if (sscanf(url.c_str(), "ws://%[^:]:%d/%s", host, &port, path) == 3) { else if (sscanf(url.c_str(), "ws://%[^:/]:%d/%s", host, &port, path) == 3) {
} }
else if (sscanf(url.c_str(), "ws://%[^/]/%s", host, path) == 2) { else if (sscanf(url.c_str(), "ws://%[^:/]/%s", host, path) == 2) {
port = 80; port = 80;
} }
else if (sscanf(url.c_str(), "ws://%[^:]:%d", host, &port) == 2) { else if (sscanf(url.c_str(), "ws://%[^:/]:%d", host, &port) == 2) {
path[0] = '\0';
} }
else if (sscanf(url.c_str(), "ws://%[^:]", host) == 1) { else if (sscanf(url.c_str(), "ws://%[^:/]", host) == 1) {
port = 80; port = 80;
path[0] = '\0';
} }
else { else {
fprintf(stderr, "ERROR: Could not parse WebSocket url: %s\n", url.c_str()); fprintf(stderr, "ERROR: Could not parse WebSocket url: %s\n", url.c_str());
return NULL; return NULL;
} }
fprintf(stderr, "easywsclient: connecting: host=%s port=%d path=/%s\n", host, port, path);
int sockfd = hostname_connect(host, port); int sockfd = hostname_connect(host, port);
if (sockfd == -1) { if (sockfd == -1) {
fprintf(stderr, "Unable to connect to %s:%d\n", host, port); fprintf(stderr, "Unable to connect to %s:%d\n", host, port);