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
Provenance
- Commit
90fee08- Tree
ce26626- Author
Nate Prewitt <nate.prewitt@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
8ff173b
1 file changed+16 added−39 removed
modified:src/requests/adapters.py+16 added-39 removed
25 unmodified lines26 from urllib3.poolmanager import PoolManager, proxy_from_url27 from urllib3.util import Timeout as TimeoutSauce28 from urllib3.util import parse_url29 from urllib3.util.retry import Retry30-from urllib3.util.ssl_ import create_urllib3_context30 31 from .auth import _basic_auth_str32 from .compat import basestring, urlparse33 from .cookies import extract_cookies_to_jar38 unmodified lines72 DEFAULT_RETRIES = 073 DEFAULT_POOL_TIMEOUT = None74 75 77-try:78- import ssl # noqa: F40179-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 Python86- # interpreter isn't built with the ssl module.87- _preloaded_ssl_context = None88-89-76 def _urllib3_request_context(77 request: "PreparedRequest",78 verify: "bool | str | None",79 client_cert: "typing.Tuple[str, str] | str | None",4 unmodified lines84 parsed_request_url = urlparse(request.url)85 scheme = parsed_request_url.scheme.lower()86 port = parsed_request_url.port87 102- # 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- )109-88 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 lines289 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 = None294 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 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 = verify298 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)301 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+333- 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 = None379 unmodified lines