diff --git a/classes.py b/classes.py index c963d9d..a532665 100644 --- a/classes.py +++ b/classes.py @@ -774,16 +774,16 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore): """ return str(obj).startswith('#') - @staticmethod - def _isASCII(s): - """Returns whether the given string only contains non-whitespace ASCII characters.""" - chars = string.ascii_letters + string.digits + string.punctuation - return all(char in chars for char in str(s)) + # Modified from https://stackoverflow.com/a/106223 (RFC 1123): + # - Allow hostnames that end in '.' + # - Require at least one '.' in the hostname + _HOSTNAME_RE = re.compile(r'^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+' + r'([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])*$') @classmethod - def is_server_name(cls, s): + def is_server_name(cls, text): """Returns whether the string given is a valid server name.""" - return cls._isASCII(s) and '.' in s and not s.startswith('.') + return bool(cls._HOSTNAME_RE.match(text)) _HOSTMASK_RE = re.compile(r'^\S+!\S+@\S+$') @classmethod diff --git a/test/protocol_test_fixture.py b/test/protocol_test_fixture.py index 8bf80e8..d410f4f 100644 --- a/test/protocol_test_fixture.py +++ b/test/protocol_test_fixture.py @@ -178,15 +178,6 @@ class BaseProtocolTest(unittest.TestCase): assertF('&channel') # we don't support these yet assertF('lorem ipsum') - def test_is_ascii(self): - assertT = lambda inp: self.assertTrue(self.p._isASCII(inp)) - assertF = lambda inp: self.assertFalse(self.p._isASCII(inp)) - - assertT('iotgjw@sy9!4py645ujg990rEYghiwaK0r4{SEFIre') - assertT('touche') - assertF('touché') - assertF('测试1') - def test_is_server_name(self): self.assertTrue(self.p.is_server_name('test.local')) self.assertTrue(self.p.is_server_name('IRC.example.com'))