Skip to content
Myr

Accept SSLContext into SSLConfig(verify=...) (#215)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Seth Michael Larson · committed by GitHub · parent af907e8 · Browse files · 2 files+37 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+37 added−14 removed

  • tests:+9 added−0 removed
  • other:+28 added−14 removed

2 files+37 added−14 removed

modified:httpx/config.py+28 added-14 removed
5 unmodified lines
67from .__version__ import __version__89CertTypes = typing.Union[str, typing.Tuple[str, str], typing.Tuple[str, str, str]]10VerifyTypes = typing.Union[str, bool]10VerifyTypes = typing.Union[str, bool, ssl.SSLContext]11TimeoutTypes = typing.Union[float, typing.Tuple[float, float, float], "TimeoutConfig"]121314USER_AGENT = f"python-httpx/{__version__}"
24 unmodified lines
39 """4041 def __init__(self, *, cert: CertTypes = None, verify: VerifyTypes = True):42 self.cert = cert43 self.verify = verify4345 self.ssl_context: typing.Optional[ssl.SSLContext] = None44 # Allow passing in our own SSLContext object that's pre-configured.45 # If you do this we assume that you want verify=True as well.46 ssl_context = None47 if isinstance(verify, ssl.SSLContext):48 ssl_context = verify49 verify = True50 self._load_client_certs(ssl_context)5152 self.ssl_context: typing.Optional[ssl.SSLContext] = ssl_context53 self.verify: typing.Union[str, bool] = verify5455 def __eq__(self, other: typing.Any) -> bool:56 return (57 isinstance(other, self.__class__)58 and self.cert == other.cert
69 unmodified lines
128 context.load_verify_locations(cafile=str(ca_bundle_path))129 elif ca_bundle_path.is_dir():130 context.load_verify_locations(capath=str(ca_bundle_path))131124 if self.cert is not None:125 if isinstance(self.cert, str):126 context.load_cert_chain(certfile=self.cert)127 elif isinstance(self.cert, tuple) and len(self.cert) == 2:128 context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1])129 elif isinstance(self.cert, tuple) and len(self.cert) == 3:130 context.load_cert_chain(131 certfile=self.cert[0],132 keyfile=self.cert[1],133 password=self.cert[2], # type: ignore134 )132 self._load_client_certs(context)133134 return context135136 def _create_default_ssl_context(self) -> ssl.SSLContext:
15 unmodified lines
152 context.set_npn_protocols(["h2", "http/1.1"])153154 return context155156 def _load_client_certs(self, ssl_context: ssl.SSLContext) -> None:157 """158 Loads client certificates into our SSLContext object159 """160 if self.cert is not None:161 if isinstance(self.cert, str):162 ssl_context.load_cert_chain(certfile=self.cert)163 elif isinstance(self.cert, tuple) and len(self.cert) == 2:164 ssl_context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1])165 elif isinstance(self.cert, tuple) and len(self.cert) == 3:166 ssl_context.load_cert_chain(167 certfile=self.cert[0],168 keyfile=self.cert[1],169 password=self.cert[2], # type: ignore170 )171172173class TimeoutConfig:174 """175 Timeout values.176 """
86 unmodified lines
modified:tests/test_config.py+9 added
74 unmodified lines
75 assert context.verify_mode == ssl.VerifyMode.CERT_NONE76 assert context.check_hostname is False777879def test_load_ssl_context():80 ssl_context = ssl.create_default_context()81 ssl_config = httpx.SSLConfig(verify=ssl_context)8283 assert ssl_config.verify is True84 assert ssl_config.ssl_context is ssl_context85 assert repr(ssl_config) == "SSLConfig(cert=None, verify=True)"868788def test_ssl_repr():89 ssl = httpx.SSLConfig(verify=False)90 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"91
42 unmodified lines