|
| 1 | +# Patch taken from https://github.com/psf/requests/commit/a58d7f2ffb4d00b46dca2d70a3932a0b37e22fac |
| 2 | +diff --git a/requests/adapters.py b/requests/adapters.py |
| 3 | +index fe22ff450..4fa5163de 100644 |
| 4 | +--- a/requests/adapters.py |
| 5 | ++++ b/requests/adapters.py |
| 6 | +@@ -10,6 +10,7 @@ and maintain connections. |
| 7 | + |
| 8 | + import os.path |
| 9 | + import socket |
| 10 | ++import typing |
| 11 | + |
| 12 | + from urllib3.poolmanager import PoolManager, proxy_from_url |
| 13 | + from urllib3.response import HTTPResponse |
| 14 | +@@ -47,12 +48,39 @@ except ImportError: |
| 15 | + def SOCKSProxyManager(*args, **kwargs): |
| 16 | + raise InvalidSchema("Missing dependencies for SOCKS support.") |
| 17 | + |
| 18 | ++ |
| 19 | ++if typing.TYPE_CHECKING: |
| 20 | ++ from .models import PreparedRequest |
| 21 | ++ |
| 22 | ++ |
| 23 | + DEFAULT_POOLBLOCK = False |
| 24 | + DEFAULT_POOLSIZE = 10 |
| 25 | + DEFAULT_RETRIES = 0 |
| 26 | + DEFAULT_POOL_TIMEOUT = None |
| 27 | + |
| 28 | + |
| 29 | ++def _urllib3_request_context( |
| 30 | ++ request: "PreparedRequest", verify: "bool | str | None" |
| 31 | ++) -> "(typing.Dict[str, typing.Any], typing.Dict[str, typing.Any])": |
| 32 | ++ host_params = {} |
| 33 | ++ pool_kwargs = {} |
| 34 | ++ parsed_request_url = urlparse(request.url) |
| 35 | ++ scheme = parsed_request_url.scheme.lower() |
| 36 | ++ port = parsed_request_url.port |
| 37 | ++ cert_reqs = "CERT_REQUIRED" |
| 38 | ++ if verify is False: |
| 39 | ++ cert_reqs = "CERT_NONE" |
| 40 | ++ if isinstance(verify, str): |
| 41 | ++ pool_kwargs["ca_certs"] = verify |
| 42 | ++ pool_kwargs["cert_reqs"] = cert_reqs |
| 43 | ++ host_params = { |
| 44 | ++ "scheme": scheme, |
| 45 | ++ "host": parsed_request_url.hostname, |
| 46 | ++ "port": port, |
| 47 | ++ } |
| 48 | ++ return host_params, pool_kwargs |
| 49 | ++ |
| 50 | ++ |
| 51 | + class BaseAdapter(object): |
| 52 | + """The Base Transport Adapter""" |
| 53 | + |
| 54 | +@@ -290,6 +318,35 @@ class HTTPAdapter(BaseAdapter): |
| 55 | + |
| 56 | + return response |
| 57 | + |
| 58 | ++ def _get_connection(self, request, verify, proxies=None): |
| 59 | ++ # Replace the existing get_connection without breaking things and |
| 60 | ++ # ensure that TLS settings are considered when we interact with |
| 61 | ++ # urllib3 HTTP Pools |
| 62 | ++ proxy = select_proxy(request.url, proxies) |
| 63 | ++ try: |
| 64 | ++ host_params, pool_kwargs = _urllib3_request_context(request, verify) |
| 65 | ++ except ValueError as e: |
| 66 | ++ raise InvalidURL(e, request=request) |
| 67 | ++ if proxy: |
| 68 | ++ proxy = prepend_scheme_if_needed(proxy, "http") |
| 69 | ++ proxy_url = parse_url(proxy) |
| 70 | ++ if not proxy_url.host: |
| 71 | ++ raise InvalidProxyURL( |
| 72 | ++ "Please check proxy URL. It is malformed " |
| 73 | ++ "and could be missing the host." |
| 74 | ++ ) |
| 75 | ++ proxy_manager = self.proxy_manager_for(proxy) |
| 76 | ++ conn = proxy_manager.connection_from_host( |
| 77 | ++ **host_params, pool_kwargs=pool_kwargs |
| 78 | ++ ) |
| 79 | ++ else: |
| 80 | ++ # Only scheme should be lower case |
| 81 | ++ conn = self.poolmanager.connection_from_host( |
| 82 | ++ **host_params, pool_kwargs=pool_kwargs |
| 83 | ++ ) |
| 84 | ++ |
| 85 | ++ return conn |
| 86 | ++ |
| 87 | + def get_connection(self, url, proxies=None): |
| 88 | + """Returns a urllib3 connection for the given URL. This should not be |
| 89 | + called from user code, and is only exposed for use when subclassing the |
| 90 | +@@ -410,7 +467,7 @@ class HTTPAdapter(BaseAdapter): |
| 91 | + """ |
| 92 | + |
| 93 | + try: |
| 94 | +- conn = self.get_connection(request.url, proxies) |
| 95 | ++ conn = self._get_connection(request, verify, proxies) |
| 96 | + except LocationValueError as e: |
| 97 | + raise InvalidURL(e, request=request) |
| 98 | + |
| 99 | +diff --git a/tests/test_requests.py b/tests/test_requests.py |
| 100 | +index 29b3aca84..13cbabcee 100644 |
| 101 | +--- a/tests/test_requests.py |
| 102 | ++++ b/tests/test_requests.py |
| 103 | +@@ -2587,3 +2607,10 @@ class TestPreparingURLs(object): |
| 104 | + r = requests.get(httpbin('bytes/20')) |
| 105 | + with pytest.raises(requests.exceptions.JSONDecodeError): |
| 106 | + r.json() |
| 107 | ++ |
| 108 | ++ def test_different_connection_pool_for_tls_settings(self): |
| 109 | ++ s = requests.Session() |
| 110 | ++ r1 = s.get("https://invalid.badssl.com", verify=False) |
| 111 | ++ assert r1.status_code == 421 |
| 112 | ++ with pytest.raises(requests.exceptions.SSLError): |
| 113 | ++ s.get("https://invalid.badssl.com") |
| 114 | +diff --git a/tox.ini b/tox.ini |
| 115 | +index 5e3d53774..d4c25a8b4 100644 |
| 116 | +--- a/tox.ini |
| 117 | ++++ b/tox.ini |
| 118 | +@@ -7,7 +7,7 @@ extras = |
| 119 | + security |
| 120 | + socks |
| 121 | + commands = |
| 122 | +- pytest tests |
| 123 | ++ pytest {posargs:tests} |
| 124 | + |
| 125 | + [testenv:default] |
| 126 | + |
0 commit comments