Skip to content
Myr

config.py

This file

Path

httpcore/config.py

188 lines · 5.9 KiB · Python

Last changed by

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

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–36why, one step back
30 def __hash__(self) -> int:
31 as_tuple = (self.cert, self.verify)32 return hash(as_tuple)3334 def __repr__(self) -> str:35 class_name = self.__class__.__name__36 return f"{class_name}(cert={self.cert}, verify={self.verify})"

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 37–75why, one step back
37
38 async def load_ssl_context(self) -> ssl.SSLContext:39 if not hasattr(self, "ssl_context"):40 if not self.verify:41 self.ssl_context = self.load_ssl_context_no_verify()42 else:43 # Run the SSL loading in a threadpool, since it makes disk accesses.44 loop = asyncio.get_event_loop()45 self.ssl_context = await loop.run_in_executor(46 None, self.load_ssl_context_verify47 )4849 return self.ssl_context5051 def load_ssl_context_no_verify(self) -> ssl.SSLContext:52 """53 Return an SSL context for unverified connections.54 """55 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)56 context.options |= ssl.OP_NO_SSLv257 context.options |= ssl.OP_NO_SSLv358 context.options |= ssl.OP_NO_COMPRESSION59 context.set_default_verify_paths()60 return context6162 def load_ssl_context_verify(self) -> ssl.SSLContext:63 """64 Return an SSL context for verified connections.65 """66 if isinstance(self.verify, bool):67 ca_bundle_path = DEFAULT_CA_BUNDLE_PATH68 elif os.path.exists(self.verify):69 ca_bundle_path = self.verify70 else:71 raise IOError(72 "Could not find a suitable TLS CA certificate bundle, "73 "invalid path: {}".format(self.verify)74 )75

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 76–92why, one step back
76 context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
7778 context.options |= ssl.OP_NO_SSLv279 context.options |= ssl.OP_NO_SSLv380 context.options |= ssl.OP_NO_COMPRESSION8182 # RFC 7540 Section 9.2.2: "deployments of HTTP/2 that use TLS 1.2 MUST83 # support TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256". In practice, the84 # blacklist defined in this section allows only the AES GCM and ChaCha2085 # cipher suites with ephemeral key negotiation.86 context.set_ciphers("ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20")8788 if ssl.HAS_ALPN:89 context.set_alpn_protocols(["h2", "http/1.1"])90 if ssl.HAS_NPN:91 context.set_npn_protocols(["h2", "http/1.1"])92

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 93–104why, one step back
93 if os.path.isfile(ca_bundle_path):
94 context.load_verify_locations(cafile=ca_bundle_path)95 elif os.path.isdir(ca_bundle_path):96 context.load_verify_locations(capath=ca_bundle_path)9798 if self.cert is not None:99 if isinstance(self.cert, str):100 context.load_cert_chain(certfile=self.cert)101 else:102 context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1])103104 return context

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 105–106why, one step back
105
106

StuffTom Christiebefore adoption30530d4

lines 107–117why, one step back
107class TimeoutConfig:
108 """109 Timeout values.110 """111112 def __init__(113 self,114 timeout: float = None,115 *,116 connect_timeout: float = None,117 read_timeout: float = None,

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

line 118why, one step back
118 pool_timeout: float = None,

StuffTom Christiebefore adoption30530d4

lines 119–128why, one step back
119 ):
120 if timeout is not None:121 # Specified as a single timeout value122 assert connect_timeout is None123 assert read_timeout is None124 assert pool_timeout is None125 connect_timeout = timeout126 read_timeout = timeout127 pool_timeout = timeout128

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

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

StuffTom Christiebefore adoption30530d4

lines 130–133why, one step back
130 self.connect_timeout = connect_timeout
131 self.read_timeout = read_timeout132 self.pool_timeout = pool_timeout133

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 134–140why, one step back
134 def __eq__(self, other: typing.Any) -> bool:
135 return (136 isinstance(other, self.__class__)137 and self.connect_timeout == other.connect_timeout138 and self.read_timeout == other.read_timeout139 and self.pool_timeout == other.pool_timeout140 )

StuffTom Christiebefore adoption30530d4

line 141why, one step back
141

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 142–152why, one step back
142 def __hash__(self) -> int:
143 as_tuple = (self.connect_timeout, self.read_timeout, self.pool_timeout)144 return hash(as_tuple)145146 def __repr__(self) -> str:147 class_name = self.__class__.__name__148 if self.timeout is not None:149 return f"{class_name}(timeout={self.timeout})"150 return f"{class_name}(connect_timeout={self.connect_timeout}, read_timeout={self.read_timeout}, pool_timeout={self.pool_timeout})"151152

StuffTom Christiebefore adoption30530d4

lines 153–157why, one step back
153class PoolLimits:
154 """155 Limits on the number of connections in a connection pool.156 """157

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

lines 158–161why, one step back
158 def __init__(
159 self,160 *,161 soft_limit: typing.Optional[int] = None,

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

line 162why, one step back
162 hard_limit: typing.Optional[int] = None,

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

lines 163–164why, one step back
163 ):
164 self.soft_limit = soft_limit

StuffTom Christiebefore adoption30530d4

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

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 166–182why, one step back
166
167 def __eq__(self, other: typing.Any) -> bool:168 return (169 isinstance(other, self.__class__)170 and self.soft_limit == other.soft_limit171 and self.hard_limit == other.hard_limit172 )173174 def __hash__(self) -> int:175 as_tuple = (self.soft_limit, self.hard_limit)176 return hash(as_tuple)177178 def __repr__(self) -> str:179 class_name = self.__class__.__name__180 return (181 f"{class_name}(soft_limit={self.soft_limit}, hard_limit={self.hard_limit})"182 )

StuffTom Christiebefore adoption30530d4

lines 183–186why, one step back
183
184185DEFAULT_SSL_CONFIG = SSLConfig(cert=None, verify=True)186DEFAULT_TIMEOUT_CONFIG = TimeoutConfig(timeout=5.0)

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

line 187why, one step back
187DEFAULT_POOL_LIMITS = PoolLimits(soft_limit=10, hard_limit=100)

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

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