config.py
This file
Path
httpcore/config.py
180 lines · 5.7 KiB · Python
Last changed by
Finessing interfaceTom Christiebefore adoptiond7619a9
History of this fileTake it
Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 1–3why, one step back1import asyncio2import os3import sslStuffTom Christiebefore adoption30530d4
lines 4–5why, one step back4import typing5Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 6why, one step back6import certifiStuffTom Christiebefore adoption30530d4
line 7why, one step back7Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 8why, one step back8StuffTom Christiebefore adoption30530d4
lines 9–13why, one step back9class SSLConfig:10 """11 SSL Configuration.12 """13Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 14–19why, one step back14 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 back20 self.cert = cert21 self.verify = verify22Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 23–28why, one step back23 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 back29Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 30–32why, one step back30 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 back3334 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 )71Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 72–73why, one step back72 context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)73Finessing interfaceTom Christiebefore adoptiond7619a9
lines 74–75why, one step back74 context.verify_mode = ssl.CERT_REQUIRED75Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 76–90why, one step back76 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"])90Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 91–102why, one step back91 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 contextMerge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 103–104why, one step back103104StuffTom Christiebefore adoption30530d4
lines 105–115why, one step back105class 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 pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 116why, one step back116 write_timeout: float = None,StuffTom Christiebefore adoption30530d4
lines 117–121why, one step back117 ):118 if timeout is not None:119 # Specified as a single timeout value120 assert connect_timeout is None121 assert read_timeout is NoneMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 122why, one step back122 assert write_timeout is NoneStuffTom Christiebefore adoption30530d4
lines 123–124why, one step back123 connect_timeout = timeout124 read_timeout = timeoutMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 125why, one step back125 write_timeout = timeoutStuffTom Christiebefore adoption30530d4
line 126why, one step back126Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
line 127why, one step back127 self.timeout = timeoutStuffTom Christiebefore adoption30530d4
lines 128–129why, one step back128 self.connect_timeout = connect_timeout129 self.read_timeout = read_timeoutMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 130why, one step back130 self.write_timeout = write_timeoutStuffTom Christiebefore adoption30530d4
line 131why, one step back131Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 132–136why, one step back132 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_timeoutMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 137why, one step back137 and self.write_timeout == other.write_timeoutMerge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
line 138why, one step back138 )StuffTom Christiebefore adoption30530d4
line 139why, one step back139Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 140–143why, one step back140 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 pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 144why, one step back144 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 back145146StuffTom Christiebefore adoption30530d4
lines 147–151why, one step back147class PoolLimits:148 """149 Limits on the number of connections in a connection pool.150 """151Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 152–154why, one step back152 def __init__(153 self,154 *,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 155–157why, one step back155 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 back158 ):159 self.soft_limit = soft_limitStuffTom Christiebefore adoption30530d4
line 160why, one step back160 self.hard_limit = hard_limitMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 161why, one step back161 self.pool_timeout = pool_timeoutMerge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 162–167why, one step back162163 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_limitMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 168why, one step back168 and self.pool_timeout == other.pool_timeoutMerge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 169–172why, one step back169 )170171 def __repr__(self) -> str:172 class_name = self.__class__.__name__Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 173why, one step back173 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 back174175176DEFAULT_SSL_CONFIG = SSLConfig(cert=None, verify=True)177DEFAULT_TIMEOUT_CONFIG = TimeoutConfig(timeout=5.0)Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 178why, one step back178DEFAULT_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 back179DEFAULT_CA_BUNDLE_PATH = certifi.where()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 180why, one step back180DEFAULT_MAX_REDIRECTS = 20