27
27
from urllib3 .util import Timeout as TimeoutSauce
28
28
from urllib3 .util import parse_url
29
29
from urllib3 .util .retry import Retry
30
- from urllib3 .util .ssl_ import create_urllib3_context
31
30
32
31
from .auth import _basic_auth_str
33
32
from .compat import basestring , urlparse
@@ -74,19 +73,6 @@ def SOCKSProxyManager(*args, **kwargs):
74
73
DEFAULT_POOL_TIMEOUT = None
75
74
76
75
77
- try :
78
- import ssl # noqa: F401
79
-
80
- _preloaded_ssl_context = create_urllib3_context ()
81
- _preloaded_ssl_context .load_verify_locations (
82
- extract_zipped_paths (DEFAULT_CA_BUNDLE_PATH )
83
- )
84
- except ImportError :
85
- # Bypass default SSLContext creation when Python
86
- # interpreter isn't built with the ssl module.
87
- _preloaded_ssl_context = None
88
-
89
-
90
76
def _urllib3_request_context (
91
77
request : "PreparedRequest" ,
92
78
verify : "bool | str | None" ,
@@ -99,19 +85,9 @@ def _urllib3_request_context(
99
85
scheme = parsed_request_url .scheme .lower ()
100
86
port = parsed_request_url .port
101
87
102
- # Determine if we have and should use our default SSLContext
103
- # to optimize performance on standard requests.
104
- poolmanager_kwargs = getattr (poolmanager , "connection_pool_kw" , {})
105
- has_poolmanager_ssl_context = poolmanager_kwargs .get ("ssl_context" )
106
- should_use_default_ssl_context = (
107
- _preloaded_ssl_context is not None and not has_poolmanager_ssl_context
108
- )
109
-
110
88
cert_reqs = "CERT_REQUIRED"
111
89
if verify is False :
112
90
cert_reqs = "CERT_NONE"
113
- elif verify is True and should_use_default_ssl_context :
114
- pool_kwargs ["ssl_context" ] = _preloaded_ssl_context
115
91
elif isinstance (verify , str ):
116
92
if not os .path .isdir (verify ):
117
93
pool_kwargs ["ca_certs" ] = verify
@@ -314,26 +290,27 @@ def cert_verify(self, conn, url, verify, cert):
314
290
:param cert: The SSL certificate to verify.
315
291
"""
316
292
if url .lower ().startswith ("https" ) and verify :
317
- conn . cert_reqs = "CERT_REQUIRED"
293
+ cert_loc = None
318
294
319
- # Only load the CA certificates if 'verify' is a string indicating the CA bundle to use.
320
- # Otherwise, if verify is a boolean, we don't load anything since
321
- # the connection will be using a context with the default certificates already loaded,
322
- # and this avoids a call to the slow load_verify_locations()
295
+ # Allow self-specified cert location.
323
296
if verify is not True :
324
- # `verify` must be a str with a path then
325
297
cert_loc = verify
326
298
327
- if not os .path .exists (cert_loc ):
328
- raise OSError (
329
- f"Could not find a suitable TLS CA certificate bundle, "
330
- f"invalid path: { cert_loc } "
331
- )
299
+ if not cert_loc :
300
+ cert_loc = extract_zipped_paths (DEFAULT_CA_BUNDLE_PATH )
332
301
333
- if not os .path .isdir (cert_loc ):
334
- conn .ca_certs = cert_loc
335
- else :
336
- conn .ca_cert_dir = cert_loc
302
+ if not cert_loc or not os .path .exists (cert_loc ):
303
+ raise OSError (
304
+ f"Could not find a suitable TLS CA certificate bundle, "
305
+ f"invalid path: { cert_loc } "
306
+ )
307
+
308
+ conn .cert_reqs = "CERT_REQUIRED"
309
+
310
+ if not os .path .isdir (cert_loc ):
311
+ conn .ca_certs = cert_loc
312
+ else :
313
+ conn .ca_cert_dir = cert_loc
337
314
else :
338
315
conn .cert_reqs = "CERT_NONE"
339
316
conn .ca_certs = None
0 commit comments