Avoid reloading root certificates to improve concurrent performance (#6667)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by agubelu · committed by GitHub · parent 0c030f7 · Browse files · 1 file·+28 added−18 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+28 added−18 removed
- other:+28 added−18 removed
Provenance
- Commit
9a40d12- Tree
c9efab2- Author
agubelu <agubelu@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
0c030f7
1 file changed+28 added−18 removed
modified:src/requests/adapters.py+28 added-18 removed
24 unmodified lines25 from urllib3.poolmanager import PoolManager, proxy_from_url26 from urllib3.util import Timeout as TimeoutSauce27 from urllib3.util import parse_url28 from urllib3.util.retry import Retry29+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_jar37 unmodified lines71 DEFAULT_POOLSIZE = 1072 DEFAULT_RETRIES = 073 DEFAULT_POOL_TIMEOUT = None74 75+_preloaded_ssl_context = create_urllib3_context()76+_preloaded_ssl_context.load_verify_locations(77+ extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)78+)79 80+81 def _urllib3_request_context(82 request: "PreparedRequest",83 verify: "bool | str | None",84 client_cert: "typing.Tuple[str, str] | str | None",5 unmodified lines90 port = parsed_request_url.port91 cert_reqs = "CERT_REQUIRED"92 if verify is False:93 cert_reqs = "CERT_NONE"88- if isinstance(verify, str):89- pool_kwargs["ca_certs"] = verify94+ elif verify is True:95+ pool_kwargs["ssl_context"] = _preloaded_ssl_context96+ elif isinstance(verify, str):97+ if not os.path.isdir(verify):98+ pool_kwargs["ca_certs"] = verify99+ else:100+ pool_kwargs["ca_cert_dir"] = verify101 pool_kwargs["cert_reqs"] = cert_reqs102 if client_cert is not None:103 if isinstance(client_cert, tuple) and len(client_cert) == 2:104 pool_kwargs["cert_file"] = client_cert[0]189 unmodified lines294 to a CA bundle to use295 :param cert: The SSL certificate to verify.296 """297 if url.lower().startswith("https") and verify:287- cert_loc = None298+ conn.cert_reqs = "CERT_REQUIRED"299 289- # Allow self-specified cert location.300+ # Only load the CA certificates if 'verify' is a string indicating the CA bundle to use.301+ # Otherwise, if verify is a boolean, we don't load anything since302+ # the connection will be using a context with the default certificates already loaded,303+ # and this avoids a call to the slow load_verify_locations()304 if verify is not True:305+ # `verify` must be a str with a path then306 cert_loc = verify307 293- if not cert_loc:294- cert_loc = extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)308+ if not os.path.exists(cert_loc):309+ raise OSError(310+ f"Could not find a suitable TLS CA certificate bundle, "311+ f"invalid path: {cert_loc}"312+ )313 296- if not cert_loc or not os.path.exists(cert_loc):297- raise OSError(298- f"Could not find a suitable TLS CA certificate bundle, "299- f"invalid path: {cert_loc}"300- )301-302- conn.cert_reqs = "CERT_REQUIRED"303-304- if not os.path.isdir(cert_loc):305- conn.ca_certs = cert_loc306- else:307- conn.ca_cert_dir = cert_loc314+ if not os.path.isdir(cert_loc):315+ conn.ca_certs = cert_loc316+ else:317+ conn.ca_cert_dir = cert_loc318 else:319 conn.cert_reqs = "CERT_NONE"320 conn.ca_certs = None321 conn.ca_cert_dir = None295 unmodified lines