summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M. Larson <SethMichaelLarson@users.noreply.github.com>2018-04-30 16:03:23 -0500
committerGitHub <noreply@github.com>2018-04-30 16:03:23 -0500
commit9f09cb4b9d69bd8944c881f61b8fe933ad425b5b (patch)
treec57ae5181a7e99cf7b69cae30fbd3177940c8a86
parent5243c46939e664494e640b8e459d6dad726287ad (diff)
parentfdc9b39d69292c0d7235da8b39689caaa54ee357 (diff)
downloadurllib3-9f09cb4b9d69bd8944c881f61b8fe933ad425b5b.tar.gz
Don't normalize hostnames when using custom protocol (#1361)
Do not lowercase hostnames with custom-protocol
-rw-r--r--test/test_connectionpool.py38
-rw-r--r--urllib3/connectionpool.py11
2 files changed, 45 insertions, 4 deletions
diff --git a/test/test_connectionpool.py b/test/test_connectionpool.py
index d4f4407a..71fe24ac 100644
--- a/test/test_connectionpool.py
+++ b/test/test_connectionpool.py
@@ -32,6 +32,19 @@ from ssl import SSLError as BaseSSLError
from dummyserver.server import DEFAULT_CA
+class HTTPUnixConnection(HTTPConnection):
+ def __init__(self, host, timeout=60, **kwargs):
+ super(HTTPUnixConnection, self).__init__('localhost')
+ self.unix_socket = host
+ self.timeout = timeout
+ self.sock = None
+
+
+class HTTPUnixConnectionPool(HTTPConnectionPool):
+ scheme = 'http+unix'
+ ConnectionCls = HTTPUnixConnection
+
+
class TestConnectionPool(object):
"""
Tests in this suite should exercise the ConnectionPool functionality
@@ -140,6 +153,31 @@ class TestConnectionPool(object):
with HTTPSConnectionPool(b) as c:
assert not c.is_same_host(a)
+ @pytest.mark.parametrize('a, b', [
+ ('%2Fvar%2Frun%2Fdocker.sock',
+ 'http+unix://%2Fvar%2Frun%2Fdocker.sock'),
+ ('%2Fvar%2Frun%2Fdocker.sock',
+ 'http+unix://%2Fvar%2Frun%2Fdocker.sock/'),
+ ('%2Fvar%2Frun%2Fdocker.sock',
+ 'http+unix://%2Fvar%2Frun%2Fdocker.sock/abracadabra'),
+ ('%2Ftmp%2FTEST.sock', 'http+unix://%2Ftmp%2FTEST.sock'),
+ ('%2Ftmp%2FTEST.sock', 'http+unix://%2Ftmp%2FTEST.sock/'),
+ ('%2Ftmp%2FTEST.sock', 'http+unix://%2Ftmp%2FTEST.sock/abracadabra'),
+ ])
+ def test_same_host_custom_protocol(self, a, b):
+ with HTTPUnixConnectionPool(a) as c:
+ assert c.is_same_host(b)
+
+ @pytest.mark.parametrize('a, b', [
+ ('%2Ftmp%2Ftest.sock', 'http+unix://%2Ftmp%2FTEST.sock'),
+ ('%2Ftmp%2Ftest.sock', 'http+unix://%2Ftmp%2FTEST.sock/'),
+ ('%2Ftmp%2Ftest.sock', 'http+unix://%2Ftmp%2FTEST.sock/abracadabra'),
+ ('%2Fvar%2Frun%2Fdocker.sock', 'http+unix://%2Ftmp%2FTEST.sock'),
+ ])
+ def test_not_same_host_custom_protocol(self, a, b):
+ with HTTPUnixConnectionPool(a) as c:
+ assert not c.is_same_host(b)
+
def test_max_connections(self):
with HTTPConnectionPool(host='localhost', maxsize=1, block=True) as pool:
pool._get_conn(timeout=0.01)
diff --git a/urllib3/connectionpool.py b/urllib3/connectionpool.py
index 0b38963a..8fcb0bce 100644
--- a/urllib3/connectionpool.py
+++ b/urllib3/connectionpool.py
@@ -40,9 +40,10 @@ from .util.request import set_file_position
from .util.response import assert_header_parsing
from .util.retry import Retry
from .util.timeout import Timeout
-from .util.url import get_host, Url
+from .util.url import get_host, Url, NORMALIZABLE_SCHEMES
from .util.queue import LifoQueue
+
xrange = six.moves.xrange
log = logging.getLogger(__name__)
@@ -64,7 +65,7 @@ class ConnectionPool(object):
if not host:
raise LocationValueError("No host specified.")
- self.host = _ipv6_host(host).lower()
+ self.host = _ipv6_host(host, self.scheme)
self._proxy_host = host.lower()
self.port = port
@@ -432,7 +433,7 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
# TODO: Add optional support for socket.gethostbyname checking.
scheme, host, port = get_host(url)
- host = _ipv6_host(host).lower()
+ host = _ipv6_host(host, self.scheme)
# Use explicit default port for comparison when none is given
if self.port and not port:
@@ -884,7 +885,7 @@ def connection_from_url(url, **kw):
return HTTPConnectionPool(host, port=port, **kw)
-def _ipv6_host(host):
+def _ipv6_host(host, scheme):
"""
Process IPv6 address literals
"""
@@ -900,4 +901,6 @@ def _ipv6_host(host):
# percent sign might be URIencoded, convert it back into ASCII
if host.startswith('[') and host.endswith(']'):
host = host.replace('%25', '%').strip('[]')
+ if scheme in NORMALIZABLE_SCHEMES:
+ host = host.lower()
return host