Blob Blame History Raw
From d267a4d236f7676ffc2def28fdaa16538f7da6c6 Mon Sep 17 00:00:00 2001
From: Lukas Slebodnik <lslebodn@fedoraproject.org>
Date: Sat, 5 May 2018 10:37:32 +0200
Subject: [PATCH 2/2] Do not lowercase hostnames with custom-protocol(#1267)

Unix sockets are are case sensitive the same as other files
on standard unix file systems.
---
 test/test_connectionpool.py | 40 +++++++++++++++++++++++++++++++++++++
 urllib3/connectionpool.py   | 10 ++++++----
 2 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/test/test_connectionpool.py b/test/test_connectionpool.py
index c49e416e8e8cfeb5e35d7ed6246560d186b08159..cf8dcbf32983007cde5fbeec536b5476631588e6 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(unittest.TestCase):
     """
     Tests in this suite should exercise the ConnectionPool functionality
@@ -140,6 +153,33 @@ class TestConnectionPool(unittest.TestCase):
             c = HTTPSConnectionPool(b)
             self.assertFalse(c.is_same_host(a), "%s =? %s" % (b, a))
 
+    def test_same_host_custom_protocol(self):
+        same_host_custom_protocol = [
+            ('%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/abracadab'),
+        ]
+        for a, b in same_host_custom_protocol:
+            with HTTPUnixConnectionPool(a) as c:
+                assert c.is_same_host(b)
+
+    def test_not_same_host_custom_protocol(self):
+        not_same_host_custom_protocol = [
+            ('%2Ftmp%2Ftest.sock', 'http+unix://%2Ftmp%2FTEST.sock'),
+            ('%2Ftmp%2Ftest.sock', 'http+unix://%2Ftmp%2FTEST.sock/'),
+            ('%2Ftmp%2Ftest.sock', 'http+unix://%2Ftmp%2FTEST.sock/abracadab'),
+            ('%2Fvar%2Frun%2Fdocker.sock', 'http+unix://%2Ftmp%2FTEST.sock'),
+        ]
+        for a, b in not_same_host_custom_protocol:
+            with HTTPUnixConnectionPool(a) as c:
+                assert not c.is_same_host(b)
+
     def test_max_connections(self):
         pool = HTTPConnectionPool(host='localhost', maxsize=1, block=True)
 
diff --git a/urllib3/connectionpool.py b/urllib3/connectionpool.py
index b4f1166a694c3055339bd7e9049252a45a84d630..8c49c170c1dede4edc30e57f23d6983db8643530 100644
--- a/urllib3/connectionpool.py
+++ b/urllib3/connectionpool.py
@@ -40,7 +40,7 @@ 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
 
 
 if six.PY2:
@@ -68,7 +68,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.port = port
 
     def __str__(self):
@@ -433,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:
@@ -880,7 +880,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
     """
@@ -896,4 +896,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
-- 
2.17.0