Skip to content
Myr

Revert caching a default SSLContext (#6767)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nate Prewitt · committed by GitHub · parent 8ff173b · Browse files · 1 file+16 added−39 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+16 added−39 removed

  • other:+16 added−39 removed

1 file+16 added−39 removed

modified:src/requests/adapters.py+16 added-39 removed
25 unmodified lines
26from urllib3.poolmanager import PoolManager, proxy_from_url27from urllib3.util import Timeout as TimeoutSauce28from urllib3.util import parse_url29from urllib3.util.retry import Retry30from urllib3.util.ssl_ import create_urllib3_context3031from .auth import _basic_auth_str32from .compat import basestring, urlparse33from .cookies import extract_cookies_to_jar
38 unmodified lines
72DEFAULT_RETRIES = 073DEFAULT_POOL_TIMEOUT = None747577try:78 import ssl # noqa: F4017980 _preloaded_ssl_context = create_urllib3_context()81 _preloaded_ssl_context.load_verify_locations(82 extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)83 )84except ImportError:85 # Bypass default SSLContext creation when Python86 # interpreter isn't built with the ssl module.87 _preloaded_ssl_context = None888976def _urllib3_request_context(77 request: "PreparedRequest",78 verify: "bool | str | None",79 client_cert: "typing.Tuple[str, str] | str | None",
4 unmodified lines
84 parsed_request_url = urlparse(request.url)85 scheme = parsed_request_url.scheme.lower()86 port = parsed_request_url.port87102 # Determine if we have and should use our default SSLContext103 # 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_context108 )10988 cert_reqs = "CERT_REQUIRED"89 if verify is False:90 cert_reqs = "CERT_NONE"113 elif verify is True and should_use_default_ssl_context:114 pool_kwargs["ssl_context"] = _preloaded_ssl_context91 elif isinstance(verify, str):92 if not os.path.isdir(verify):93 pool_kwargs["ca_certs"] = verify94 else:
194 unmodified lines
289 to a CA bundle to use290 :param cert: The SSL certificate to verify.291 """292 if url.lower().startswith("https") and verify:317 conn.cert_reqs = "CERT_REQUIRED"293 cert_loc = None294319 # 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 since321 # 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.296 if verify is not True:324 # `verify` must be a str with a path then297 cert_loc = verify298327 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)301302 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 )307308 conn.cert_reqs = "CERT_REQUIRED"309333 if not os.path.isdir(cert_loc):334 conn.ca_certs = cert_loc335 else:336 conn.ca_cert_dir = cert_loc310 if not os.path.isdir(cert_loc):311 conn.ca_certs = cert_loc312 else:313 conn.ca_cert_dir = cert_loc314 else:315 conn.cert_reqs = "CERT_NONE"316 conn.ca_certs = None317 conn.ca_cert_dir = None
379 unmodified lines