urlmatch: Finish port parsing

This commit is contained in:
Florian Bruhin 2018-02-15 14:29:39 +01:00
parent a2a95f5fee
commit fa329c698e
2 changed files with 11 additions and 8 deletions

View File

@ -127,11 +127,11 @@ class UrlPattern:
"""Parse the port from the given URL.
Deviation from Chromium:
- file://foo:1234/bar is invalid instead of falling back to *
- We use None instead of "*" if there's no port filter.
"""
if parsed.netloc.endswith(':*'):
# We can't access parsed.port as it tries to run int()
self._port = '*'
self._port = None
elif parsed.netloc.endswith(':'):
raise ParseError("Empty port")
else:
@ -147,9 +147,5 @@ class UrlPattern:
raise ParseError("Ports are unsupported with {} scheme".format(
self._scheme))
if self._port is None and self._scheme == 'file':
# FIXME compatibility with Chromium, but is this needed?
self._port = '*'
def __repr__(self):
return utils.get_repr(self, pattern=self._pattern, constructor=True)

View File

@ -74,8 +74,15 @@ def test_invalid_patterns(pattern, error):
("http://*.foo:1234/bar", 1234),
# FIXME Why is this valid in Chromium?
# ("http://:1234/", 1234),
("http://foo:*/", "*"),
("file://foo:1234/bar", "*"),
("http://foo:*/", None),
("file://foo:1234/bar", None),
# Port-like strings in the path should not trigger a warning.
("http://*/:1234", None),
("http://*.foo/bar:1234", None),
("http://foo/bar:1234/path", None),
# We don't implement ALLOW_WILDCARD_FOR_EFFECTIVE_TLD yet.
# ("http://*.foo.*/:1234", None),
])
def test_port(pattern, port):
up = urlmatch.UrlPattern(pattern)