diff options
author | Marius Gedminas <marius@gedmin.as> | 2019-08-07 15:44:09 +0300 |
---|---|---|
committer | Marius Gedminas <marius@gedmin.as> | 2019-08-07 15:47:43 +0300 |
commit | 147b96ad430a21b1d926b35573e09a4d3e4f0965 (patch) | |
tree | 763cf9b5a7fd5333fee4ad2ac202f1681bef00c9 /src | |
parent | ac6435403aeb9b597e30597e5ec8aa0be13ade1f (diff) | |
download | zope-publisher-147b96ad430a21b1d926b35573e09a4d3e4f0965.tar.gz |
Avoid using urllib.parse.splitport()
The function is an undocumented internal helper and emits a deprecation
warning in Python 3.8.
The suggested replacement is urllib.parse.urlparse(), but it's awkward
for our use case (you have to add a scheme so it doesn't treat the
argument as a relative URL, and it unwraps IPv6 addresses that we'd have
to wrap right back into [], and it converts the port to an int whereas
we expect it to be a string) so let's just reimplement the function
using a regexp (which is what the stdlib did anyway).
Fixes #38.
Diffstat (limited to 'src')
-rw-r--r-- | src/zope/publisher/http.py | 35 | ||||
-rw-r--r-- | src/zope/publisher/tests/test_http.py | 3 |
2 files changed, 35 insertions, 3 deletions
diff --git a/src/zope/publisher/http.py b/src/zope/publisher/http.py index 20a02aa..e1801cf 100644 --- a/src/zope/publisher/http.py +++ b/src/zope/publisher/http.py @@ -45,12 +45,12 @@ from zope.publisher._compat import PYTHON2, CLASS_TYPES, to_unicode if PYTHON2: import Cookie as cookies - from urllib import splitport, quote + from urllib import quote from urlparse import urlsplit from cgi import escape else: import http.cookies as cookies - from urllib.parse import splitport, quote, urlsplit + from urllib.parse import quote, urlsplit from html import escape unicode = str basestring = (str, bytes) @@ -71,6 +71,37 @@ class CookieMapper(RequestDataMapper): class HeaderGetter(RequestDataGetter): _gettrname = 'getHeader' + +host_port_re = re.compile( + r"^(.*):([0-9]*)$", re.DOTALL) + +def splitport(host): + """Split port number off the hostname. + + >>> splitport('example.com:80') + ('example.com', '80') + + >>> splitport('localhost') + ('localhost', None) + + >>> splitport('[::1]') + ('[::1]', None) + + >>> splitport('[::1]:443') + ('[::1]', '443') + + >>> splitport('localhost:') + ('localhost', None) + + """ + match = host_port_re.match(host) + if match: + host, port = match.groups() + else: + port = None + return host, port or None + + def sane_environment(env): # return an environment mapping which has been cleaned of # funny business such as REDIRECT_ prefixes added by Apache diff --git a/src/zope/publisher/tests/test_http.py b/src/zope/publisher/tests/test_http.py index f07b8d9..6aec25e 100644 --- a/src/zope/publisher/tests/test_http.py +++ b/src/zope/publisher/tests/test_http.py @@ -18,7 +18,7 @@ import sys import tempfile import unittest from io import BytesIO -from doctest import DocFileSuite +from doctest import DocFileSuite, DocTestSuite import zope.event from zope.component import provideAdapter @@ -1041,6 +1041,7 @@ def test_suite(): unittest.makeSuite(ConcreteHTTPTests), unittest.makeSuite(TestHTTPResponse), unittest.makeSuite(HTTPInputStreamTests), + DocTestSuite('zope.publisher.http'), DocFileSuite('../httpresults.txt', setUp=cleanUp, tearDown=cleanUp, |