From 424572337c94def5216d49f7748be51cb9ce61f9 Mon Sep 17 00:00:00 2001 From: Burakceliks Date: Tue, 24 Nov 2026 20:23:36 +0300 Subject: [PATCH] Cache default SSLContext in create_ssl_context() --- httpx/_config.py | 34 +++++++++++++++++++++++++++++++--- tests/test_config.py | 6 ++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/httpx/_config.py b/httpx/_config.py index 467a6c90ae..d2feac8d17 100644 --- a/httpx/_config.py +++ b/httpx/_config.py @@ -1,5 +1,6 @@ from __future__ import annotations +import functools import os import typing @@ -20,6 +21,17 @@ class UnsetType: UNSET = UnsetType() +@functools.lru_cache(maxsize=8) +def _cached_default_ssl_context( + cafile: str | None = None, + capath: str | None = None, + keylog_file: str | None = None, +) -> ssl.SSLContext: + import ssl + + return ssl.create_default_context(cafile=cafile, capath=capath) + + def create_ssl_context( verify: ssl.SSLContext | str | bool = True, cert: CertTypes | None = None, @@ -31,13 +43,29 @@ def create_ssl_context( import certifi if verify is True: + keylog_file = os.environ.get("SSLKEYLOGFILE") if trust_env and os.environ.get("SSL_CERT_FILE"): # pragma: nocover - ctx = ssl.create_default_context(cafile=os.environ["SSL_CERT_FILE"]) + cafile = os.environ["SSL_CERT_FILE"] + ctx = ( + ssl.create_default_context(cafile=cafile) + if cert + else _cached_default_ssl_context(cafile=cafile, keylog_file=keylog_file) + ) elif trust_env and os.environ.get("SSL_CERT_DIR"): # pragma: nocover - ctx = ssl.create_default_context(capath=os.environ["SSL_CERT_DIR"]) + capath = os.environ["SSL_CERT_DIR"] + ctx = ( + ssl.create_default_context(capath=capath) + if cert + else _cached_default_ssl_context(capath=capath, keylog_file=keylog_file) + ) else: # Default case... - ctx = ssl.create_default_context(cafile=certifi.where()) + cafile = certifi.where() + ctx = ( + ssl.create_default_context(cafile=cafile) + if cert + else _cached_default_ssl_context(cafile=cafile, keylog_file=keylog_file) + ) elif verify is False: ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.check_hostname = False diff --git a/tests/test_config.py b/tests/test_config.py index 22abd4c22c..0aeb62e061 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -182,3 +182,9 @@ def test_proxy_with_auth_from_url(): def test_invalid_proxy_scheme(): with pytest.raises(ValueError): httpx.Proxy("invalid://example.com") + + +def test_default_ssl_context_is_cached() -> None: + ctx1 = httpx.create_ssl_context() + ctx2 = httpx.create_ssl_context() + assert ctx1 is ctx2