Skip to content
Myr

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

1 file+28 added−18 removed

modified:src/requests/adapters.py+28 added-18 removed
24 unmodified lines
25from urllib3.poolmanager import PoolManager, proxy_from_url26from urllib3.util import Timeout as TimeoutSauce27from urllib3.util import parse_url28from urllib3.util.retry import Retry29from urllib3.util.ssl_ import create_urllib3_context3031from .auth import _basic_auth_str32from .compat import basestring, urlparse33from .cookies import extract_cookies_to_jar
37 unmodified lines
71DEFAULT_POOLSIZE = 1072DEFAULT_RETRIES = 073DEFAULT_POOL_TIMEOUT = None7475_preloaded_ssl_context = create_urllib3_context()76_preloaded_ssl_context.load_verify_locations(77 extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)78)798081def _urllib3_request_context(82 request: "PreparedRequest",83 verify: "bool | str | None",84 client_cert: "typing.Tuple[str, str] | str | None",
5 unmodified lines
90 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 lines
294 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"299289 # 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 = verify307293 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 )313296 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 )301302 conn.cert_reqs = "CERT_REQUIRED"303304 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 = None
295 unmodified lines