Skip to content
Myr

config.py

This file

Path

httpcore/config.py

196 lines · 6.7 KiB · Python

Last changed by

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

History of this file

Take it

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 1–3why, one step back
1import asyncio
2import os3import ssl

StuffTom Christiebefore adoption30530d4

lines 4–5why, one step back
4import typing
5

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 6why, one step back
6import certifi

StuffTom Christiebefore adoption30530d4

line 7why, one step back
7

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 8–10why, one step back
8CertTypes = typing.Union[str, typing.Tuple[str, str]]
9VerifyTypes = typing.Union[str, bool]10TimeoutTypes = typing.Union[float, typing.Tuple[float, float, float], "TimeoutConfig"]

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 11why, one step back
11

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 12why, one step back
12

StuffTom Christiebefore adoption30530d4

lines 13–17why, one step back
13class SSLConfig:
14 """15 SSL Configuration.16 """17

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 18why, one step back
18 def __init__(self, *, cert: CertTypes = None, verify: VerifyTypes = True):

StuffTom Christiebefore adoption30530d4

lines 19–21why, one step back
19 self.cert = cert
20 self.verify = verify21

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 22–27why, one step back
22 def __eq__(self, other: typing.Any) -> bool:
23 return (24 isinstance(other, self.__class__)25 and self.cert == other.cert26 and self.verify == other.verify27 )

StuffTom Christiebefore adoption30530d4

line 28why, one step back
28

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 29–31why, one step back
29 def __repr__(self) -> str:
30 class_name = self.__class__.__name__31 return f"{class_name}(cert={self.cert}, verify={self.verify})"

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 32why, one step back
32

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 33–41why, one step back
33 def with_overrides(
34 self, cert: CertTypes = None, verify: VerifyTypes = None35 ) -> "SSLConfig":36 cert = self.cert if cert is None else cert37 verify = self.verify if verify is None else verify38 if (cert == self.cert) and (verify == self.verify):39 return self40 return SSLConfig(cert=cert, verify=verify)41

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 42–79why, one step back
42 async def load_ssl_context(self) -> ssl.SSLContext:
43 if not hasattr(self, "ssl_context"):44 if not self.verify:45 self.ssl_context = self.load_ssl_context_no_verify()46 else:47 # Run the SSL loading in a threadpool, since it makes disk accesses.48 loop = asyncio.get_event_loop()49 self.ssl_context = await loop.run_in_executor(50 None, self.load_ssl_context_verify51 )5253 return self.ssl_context5455 def load_ssl_context_no_verify(self) -> ssl.SSLContext:56 """57 Return an SSL context for unverified connections.58 """59 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)60 context.options |= ssl.OP_NO_SSLv261 context.options |= ssl.OP_NO_SSLv362 context.options |= ssl.OP_NO_COMPRESSION63 context.set_default_verify_paths()64 return context6566 def load_ssl_context_verify(self) -> ssl.SSLContext:67 """68 Return an SSL context for verified connections.69 """70 if isinstance(self.verify, bool):71 ca_bundle_path = DEFAULT_CA_BUNDLE_PATH72 elif os.path.exists(self.verify):73 ca_bundle_path = self.verify74 else:75 raise IOError(76 "Could not find a suitable TLS CA certificate bundle, "77 "invalid path: {}".format(self.verify)78 )79

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 80–81why, one step back
80 context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
81

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 82–83why, one step back
82 context.verify_mode = ssl.CERT_REQUIRED
83

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 84–98why, one step back
84 context.options |= ssl.OP_NO_SSLv2
85 context.options |= ssl.OP_NO_SSLv386 context.options |= ssl.OP_NO_COMPRESSION8788 # RFC 7540 Section 9.2.2: "deployments of HTTP/2 that use TLS 1.2 MUST89 # support TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256". In practice, the90 # blacklist defined in this section allows only the AES GCM and ChaCha2091 # cipher suites with ephemeral key negotiation.92 context.set_ciphers("ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20")9394 if ssl.HAS_ALPN:95 context.set_alpn_protocols(["h2", "http/1.1"])96 if ssl.HAS_NPN:97 context.set_npn_protocols(["h2", "http/1.1"])98

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 99–110why, one step back
99 if os.path.isfile(ca_bundle_path):
100 context.load_verify_locations(cafile=ca_bundle_path)101 elif os.path.isdir(ca_bundle_path):102 context.load_verify_locations(capath=ca_bundle_path)103104 if self.cert is not None:105 if isinstance(self.cert, str):106 context.load_cert_chain(certfile=self.cert)107 else:108 context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1])109110 return context

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 111–112why, one step back
111
112

StuffTom Christiebefore adoption30530d4

lines 113–119why, one step back
113class TimeoutConfig:
114 """115 Timeout values.116 """117118 def __init__(119 self,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 120why, one step back
120 timeout: TimeoutTypes = None,

StuffTom Christiebefore adoption30530d4

lines 121–123why, one step back
121 *,
122 connect_timeout: float = None,123 read_timeout: float = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 124why, one step back
124 write_timeout: float = None,

StuffTom Christiebefore adoption30530d4

line 125why, one step back
125 ):

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 126–130why, one step back
126 if timeout is None:
127 self.connect_timeout = connect_timeout128 self.read_timeout = read_timeout129 self.write_timeout = write_timeout130 else:

StuffTom Christiebefore adoption30530d4

lines 131–133why, one step back
131 # Specified as a single timeout value
132 assert connect_timeout is None133 assert read_timeout is None

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 134why, one step back
134 assert write_timeout is None

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 135–146why, one step back
135 if isinstance(timeout, TimeoutConfig):
136 self.connect_timeout = timeout.connect_timeout137 self.read_timeout = timeout.read_timeout138 self.write_timeout = timeout.write_timeout139 elif isinstance(timeout, tuple):140 self.connect_timeout = timeout[0]141 self.read_timeout = timeout[1]142 self.write_timeout = timeout[2]143 else:144 self.connect_timeout = timeout145 self.read_timeout = timeout146 self.write_timeout = timeout

StuffTom Christiebefore adoption30530d4

line 147why, one step back
147

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 148–152why, one step back
148 def __eq__(self, other: typing.Any) -> bool:
149 return (150 isinstance(other, self.__class__)151 and self.connect_timeout == other.connect_timeout152 and self.read_timeout == other.read_timeout

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 153why, one step back
153 and self.write_timeout == other.write_timeout

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

line 154why, one step back
154 )

StuffTom Christiebefore adoption30530d4

line 155why, one step back
155

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 156–157why, one step back
156 def __repr__(self) -> str:
157 class_name = self.__class__.__name__

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 158–159why, one step back
158 if len(set([self.connect_timeout, self.read_timeout, self.write_timeout])) == 1:
159 return f"{class_name}(timeout={self.connect_timeout})"

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 160why, one step back
160 return f"{class_name}(connect_timeout={self.connect_timeout}, read_timeout={self.read_timeout}, write_timeout={self.write_timeout})"

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 161–162why, one step back
161
162

StuffTom Christiebefore adoption30530d4

lines 163–167why, one step back
163class PoolLimits:
164 """165 Limits on the number of connections in a connection pool.166 """167

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

lines 168–170why, one step back
168 def __init__(
169 self,170 *,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 171–173why, one step back
171 soft_limit: int = None,
172 hard_limit: int = None,173 pool_timeout: float = None,

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

lines 174–175why, one step back
174 ):
175 self.soft_limit = soft_limit

StuffTom Christiebefore adoption30530d4

line 176why, one step back
176 self.hard_limit = hard_limit

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 177why, one step back
177 self.pool_timeout = pool_timeout

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 178–183why, one step back
178
179 def __eq__(self, other: typing.Any) -> bool:180 return (181 isinstance(other, self.__class__)182 and self.soft_limit == other.soft_limit183 and self.hard_limit == other.hard_limit

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 184why, one step back
184 and self.pool_timeout == other.pool_timeout

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 185–188why, one step back
185 )
186187 def __repr__(self) -> str:188 class_name = self.__class__.__name__

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 189why, one step back
189 return f"{class_name}(soft_limit={self.soft_limit}, hard_limit={self.hard_limit}, pool_timeout={self.pool_timeout})"

StuffTom Christiebefore adoption30530d4

lines 190–193why, one step back
190
191192DEFAULT_SSL_CONFIG = SSLConfig(cert=None, verify=True)193DEFAULT_TIMEOUT_CONFIG = TimeoutConfig(timeout=5.0)

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 194why, one step back
194DEFAULT_POOL_LIMITS = PoolLimits(soft_limit=10, hard_limit=100, pool_timeout=5.0)

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 195why, one step back
195DEFAULT_CA_BUNDLE_PATH = certifi.where()

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 196why, one step back
196DEFAULT_MAX_REDIRECTS = 20