Browse Source

Ignore whitespace around components of host-and-port specification

This is to make the code more tolerant of typos when entering a hostname
tags/v1.12.90
Catherine Tower 1 year ago
parent
commit
645c1f2aea
No account linked to committer's email address
2 changed files with 34 additions and 2 deletions
  1. 22
    2
      common/rfb/Hostname.h
  2. 12
    0
      tests/unit/hostport.cxx

+ 22
- 2
common/rfb/Hostname.h View File

@@ -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))

+ 12
- 0
tests/unit/hostport.cxx View File

@@ -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;
}

Loading…
Cancel
Save