Skip to content
Myr

config.py

This file

Path

httpcore/config.py

180 lines · 5.7 KiB · Python

Last changed by

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

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

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

line 8why, one step back
8

StuffTom Christiebefore adoption30530d4

lines 9–13why, one step back
9class SSLConfig:
10 """11 SSL Configuration.12 """13

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 14–19why, one step back
14 def __init__(
15 self,16 *,17 cert: typing.Union[None, str, typing.Tuple[str, str]] = None,18 verify: typing.Union[str, bool] = True,19 ):

StuffTom Christiebefore adoption30530d4

lines 20–22why, one step back
20 self.cert = cert
21 self.verify = verify22

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 23–28why, one step back
23 def __eq__(self, other: typing.Any) -> bool:
24 return (25 isinstance(other, self.__class__)26 and self.cert == other.cert27 and self.verify == other.verify28 )

StuffTom Christiebefore adoption30530d4

line 29why, one step back
29

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

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

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 33–71why, one step back
33
34 async def load_ssl_context(self) -> ssl.SSLContext:35 if not hasattr(self, "ssl_context"):36 if not self.verify:37 self.ssl_context = self.load_ssl_context_no_verify()38 else:39 # Run the SSL loading in a threadpool, since it makes disk accesses.40 loop = asyncio.get_event_loop()41 self.ssl_context = await loop.run_in_executor(42 None, self.load_ssl_context_verify43 )4445 return self.ssl_context4647 def load_ssl_context_no_verify(self) -> ssl.SSLContext:48 """49 Return an SSL context for unverified connections.50 """51 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)52 context.options |= ssl.OP_NO_SSLv253 context.options |= ssl.OP_NO_SSLv354 context.options |= ssl.OP_NO_COMPRESSION55 context.set_default_verify_paths()56 return context5758 def load_ssl_context_verify(self) -> ssl.SSLContext:59 """60 Return an SSL context for verified connections.61 """62 if isinstance(self.verify, bool):63 ca_bundle_path = DEFAULT_CA_BUNDLE_PATH64 elif os.path.exists(self.verify):65 ca_bundle_path = self.verify66 else:67 raise IOError(68 "Could not find a suitable TLS CA certificate bundle, "69 "invalid path: {}".format(self.verify)70 )71

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

lines 72–90why, one step back
72 context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
7374 context.verify_mode = ssl.CERT_REQUIRED7576 context.options |= ssl.OP_NO_SSLv277 context.options |= ssl.OP_NO_SSLv378 context.options |= ssl.OP_NO_COMPRESSION7980 # RFC 7540 Section 9.2.2: "deployments of HTTP/2 that use TLS 1.2 MUST81 # support TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256". In practice, the82 # blacklist defined in this section allows only the AES GCM and ChaCha2083 # cipher suites with ephemeral key negotiation.84 context.set_ciphers("ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20")8586 if ssl.HAS_ALPN:87 context.set_alpn_protocols(["h2", "http/1.1"])88 if ssl.HAS_NPN:89 context.set_npn_protocols(["h2", "http/1.1"])90

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 91–102why, one step back
91 if os.path.isfile(ca_bundle_path):
92 context.load_verify_locations(cafile=ca_bundle_path)93 elif os.path.isdir(ca_bundle_path):94 context.load_verify_locations(capath=ca_bundle_path)9596 if self.cert is not None:97 if isinstance(self.cert, str):98 context.load_cert_chain(certfile=self.cert)99 else:100 context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1])101102 return context

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 103–104why, one step back
103
104

StuffTom Christiebefore adoption30530d4

lines 105–115why, one step back
105class TimeoutConfig:
106 """107 Timeout values.108 """109110 def __init__(111 self,112 timeout: float = None,113 *,114 connect_timeout: float = None,115 read_timeout: float = None,

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

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

StuffTom Christiebefore adoption30530d4

lines 117–121why, one step back
117 ):
118 if timeout is not None:119 # Specified as a single timeout value120 assert connect_timeout is None121 assert read_timeout is None

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

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

StuffTom Christiebefore adoption30530d4

lines 123–124why, one step back
123 connect_timeout = timeout
124 read_timeout = timeout

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

line 125why, one step back
125 write_timeout = timeout

StuffTom Christiebefore adoption30530d4

line 126why, one step back
126

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

line 127why, one step back
127 self.timeout = timeout

StuffTom Christiebefore adoption30530d4

lines 128–129why, one step back
128 self.connect_timeout = connect_timeout
129 self.read_timeout = read_timeout

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

line 130why, one step back
130 self.write_timeout = write_timeout

StuffTom Christiebefore adoption30530d4

line 131why, one step back
131

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 132–136why, one step back
132 def __eq__(self, other: typing.Any) -> bool:
133 return (134 isinstance(other, self.__class__)135 and self.connect_timeout == other.connect_timeout136 and self.read_timeout == other.read_timeout

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

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

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

line 138why, one step back
138 )

StuffTom Christiebefore adoption30530d4

line 139why, one step back
139

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 140–143why, one step back
140 def __repr__(self) -> str:
141 class_name = self.__class__.__name__142 if self.timeout is not None:143 return f"{class_name}(timeout={self.timeout})"

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

line 144why, one step back
144 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 145–146why, one step back
145
146

StuffTom Christiebefore adoption30530d4

lines 147–151why, one step back
147class PoolLimits:
148 """149 Limits on the number of connections in a connection pool.150 """151

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

lines 152–154why, one step back
152 def __init__(
153 self,154 *,

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

lines 155–157why, one step back
155 soft_limit: int = None,
156 hard_limit: int = None,157 pool_timeout: float = None,

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

lines 158–159why, one step back
158 ):
159 self.soft_limit = soft_limit

StuffTom Christiebefore adoption30530d4

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

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

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

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 162–167why, one step back
162
163 def __eq__(self, other: typing.Any) -> bool:164 return (165 isinstance(other, self.__class__)166 and self.soft_limit == other.soft_limit167 and self.hard_limit == other.hard_limit

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

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

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

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

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

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

StuffTom Christiebefore adoption30530d4

lines 174–177why, one step back
174
175176DEFAULT_SSL_CONFIG = SSLConfig(cert=None, verify=True)177DEFAULT_TIMEOUT_CONFIG = TimeoutConfig(timeout=5.0)

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

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

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

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

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

line 180why, one step back
180DEFAULT_MAX_REDIRECTS = 20