Skip to content

Commit 2ef4a57

Browse files
committed
Fix/refactor default cert loading
1 parent ff74258 commit 2ef4a57

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

httpie/compat.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import sys
2+
from ssl import SSLContext
23
from typing import Any, Optional, Iterable
34

45
from httpie.cookies import HTTPieCookiePolicy
5-
from http import cookiejar # noqa
6+
from http import cookiejar # noqa
67

78

89
# Request does not carry the original policy attached to the
910
# cookie jar, so until it is resolved we change the global cookie
1011
# policy. <https://github.com/psf/requests/issues/5449>
1112
cookiejar.DefaultCookiePolicy = HTTPieCookiePolicy
1213

13-
1414
is_windows = 'win32' in str(sys.platform).lower()
1515
is_frozen = getattr(sys, 'frozen', False)
1616

@@ -66,7 +66,6 @@ def __get__(self, instance, cls=None):
6666
res = instance.__dict__[self.name] = self.func(instance)
6767
return res
6868

69-
7069
# importlib_metadata was a provisional module, so the APIs changed quite a few times
7170
# between 3.8-3.10. It was also not included in the standard library until 3.8, so
7271
# we install the backport for <3.8.
@@ -100,3 +99,15 @@ def get_dist_name(entry_point: importlib_metadata.EntryPoint) -> Optional[str]:
10099
return None
101100
else:
102101
return metadata.get('name')
102+
103+
104+
def ensure_default_certs_loaded(ssl_context: SSLContext) -> None:
105+
"""
106+
Workaround for a bug in Requests 2.32.3
107+
108+
See <https://github.com/httpie/cli/issues/1583>
109+
110+
"""
111+
if hasattr(ssl_context, 'load_default_certs'):
112+
if not ssl_context.get_ca_certs():
113+
ssl_context.load_default_certs()

httpie/ssl_.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import ssl
22
from typing import NamedTuple, Optional
33

4-
from httpie.adapters import HTTPAdapter
54
# noinspection PyPackageRequirements
65
from urllib3.util.ssl_ import (
76
create_urllib3_context,
87
resolve_ssl_version,
98
)
109

10+
from .adapters import HTTPAdapter
11+
from .compat import ensure_default_certs_loaded
12+
1113

1214
SSL_VERSION_ARG_MAPPING = {
1315
'ssl2.3': 'PROTOCOL_SSLv23',
@@ -71,19 +73,16 @@ def _create_ssl_context(
7173
ssl_version: str = None,
7274
ciphers: str = None,
7375
) -> 'ssl.SSLContext':
74-
context = create_urllib3_context(
76+
ssl_context = create_urllib3_context(
7577
ciphers=ciphers,
7678
ssl_version=resolve_ssl_version(ssl_version),
7779
# Since we are using a custom SSL context, we need to pass this
7880
# here manually, even though it’s also passed to the connection
7981
# in `super().cert_verify()`.
8082
cert_reqs=ssl.CERT_REQUIRED if verify else ssl.CERT_NONE
8183
)
82-
if not context.get_ca_certs():
83-
# Workaround for a bug in requests 2.32.3
84-
# See <https://github.com/httpie/cli/issues/1583>
85-
context.load_default_certs()
86-
return context
84+
ensure_default_certs_loaded(ssl_context)
85+
return ssl_context
8786

8887
@classmethod
8988
def get_default_ciphers_names(cls):

0 commit comments

Comments
 (0)