diff options
-rw-r--r-- | common/rfb/Hostname.h | 24 | ||||
-rw-r--r-- | tests/unit/hostport.cxx | 12 |
2 files changed, 34 insertions, 2 deletions
diff --git a/common/rfb/Hostname.h b/common/rfb/Hostname.h index bd68dc02..341f69e1 100644 --- a/common/rfb/Hostname.h +++ b/common/rfb/Hostname.h @@ -20,12 +20,24 @@ #define __RFB_HOSTNAME_H__ #include <assert.h> +#include <ctype.h> #include <stdlib.h> #include <rdr/Exception.h> #include <rfb/util.h> namespace rfb { + static bool isAllSpace(const char *string) { + if (string == NULL) + return false; + while(*string != '\0') { + if (! isspace(*string)) + return false; + string++; + } + return true; + } + static void getHostAndPort(const char* hi, char** host, int* port, int basePort=5900) { const char* hostStart; const char* hostEnd; @@ -34,6 +46,10 @@ namespace rfb { if (hi == NULL) throw rdr::Exception("NULL host specified"); + // Trim leading whitespace + while(isspace(*hi)) + hi++; + assert(host); assert(port); @@ -44,7 +60,7 @@ namespace rfb { throw rdr::Exception("unmatched [ in host"); portStart = hostEnd + 1; - if (*portStart == '\0') + if (isAllSpace(portStart)) portStart = NULL; } else { hostStart = &hi[0]; @@ -65,6 +81,10 @@ namespace rfb { } } + // Back up past trailing space + while(isspace(*(hostEnd - 1)) && hostEnd > hostStart) + hostEnd--; + if (hostStart == hostEnd) *host = strDup("localhost"); else { @@ -87,7 +107,7 @@ namespace rfb { *port = strtol(portStart + 1, &end, 10); else *port = strtol(portStart + 2, &end, 10); - if (*end != '\0') + if (*end != '\0' && ! isAllSpace(end)) throw rdr::Exception("invalid port specified"); if ((portStart[1] != ':') && (*port < 100)) diff --git a/tests/unit/hostport.cxx b/tests/unit/hostport.cxx index af60643a..6ca5df8f 100644 --- a/tests/unit/hostport.cxx +++ b/tests/unit/hostport.cxx @@ -80,5 +80,17 @@ int main(int argc, char** argv) doTest("[2001:1234::20:1]:100", "2001:1234::20:1", 100); doTest("[2001:1234::20:1]:5901", "2001:1234::20:1", 5901); + doTest(" 1.2.3.4 ", "1.2.3.4", 5900); + doTest(" 1.2.3.4:5901 ", "1.2.3.4", 5901); + doTest(" 1.2.3.4: 5901 ", "1.2.3.4", 5901); + doTest(" 1.2.3.4 :5901 ", "1.2.3.4", 5901); + doTest(" [1.2.3.4]:5902 ", "1.2.3.4", 5902); + doTest(" :5903 ", "localhost", 5903); + doTest(" ::4 ", "localhost", 4); + doTest(" [::1] ", "::1", 5900); + doTest(" 2001:1234::20:1 ", "2001:1234::20:1", 5900); + doTest(" [2001:1234::20:1] ", "2001:1234::20:1", 5900); + doTest(" [2001:1234::20:1]:5905 ", "2001:1234::20:1", 5905); + return 0; } |