config.py
This file
Path
httpcore/config.py
172 lines · 5.2 KiB · Python
Last changed by
Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
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–36why, one step back30 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–88why, one step back3738 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 )7576 context = ssl.create_default_context()77 if os.path.isfile(ca_bundle_path):78 context.load_verify_locations(cafile=ca_bundle_path)79 elif os.path.isdir(ca_bundle_path):80 context.load_verify_locations(capath=ca_bundle_path)8182 if self.cert is not None:83 if isinstance(self.cert, str):84 context.load_cert_chain(certfile=self.cert)85 else:86 context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1])8788 return contextMerge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 89–90why, one step back8990StuffTom Christiebefore adoption30530d4
lines 91–101why, one step back91class TimeoutConfig:92 """93 Timeout values.94 """9596 def __init__(97 self,98 timeout: float = None,99 *,100 connect_timeout: float = None,101 read_timeout: float = None,Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
line 102why, one step back102 pool_timeout: float = None,StuffTom Christiebefore adoption30530d4
lines 103–112why, one step back103 ):104 if timeout is not None:105 # Specified as a single timeout value106 assert connect_timeout is None107 assert read_timeout is None108 assert pool_timeout is None109 connect_timeout = timeout110 read_timeout = timeout111 pool_timeout = timeout112Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
line 113why, one step back113 self.timeout = timeoutStuffTom Christiebefore adoption30530d4
lines 114–117why, one step back114 self.connect_timeout = connect_timeout115 self.read_timeout = read_timeout116 self.pool_timeout = pool_timeout117Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 118–124why, one step back118 def __eq__(self, other: typing.Any) -> bool:119 return (120 isinstance(other, self.__class__)121 and self.connect_timeout == other.connect_timeout122 and self.read_timeout == other.read_timeout123 and self.pool_timeout == other.pool_timeout124 )StuffTom Christiebefore adoption30530d4
line 125why, one step back125Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 126–136why, one step back126 def __hash__(self) -> int:127 as_tuple = (self.connect_timeout, self.read_timeout, self.pool_timeout)128 return hash(as_tuple)129130 def __repr__(self) -> str:131 class_name = self.__class__.__name__132 if self.timeout is not None:133 return f"{class_name}(timeout={self.timeout})"134 return f"{class_name}(connect_timeout={self.connect_timeout}, read_timeout={self.read_timeout}, pool_timeout={self.pool_timeout})"135136StuffTom Christiebefore adoption30530d4
lines 137–141why, one step back137class PoolLimits:138 """139 Limits on the number of connections in a connection pool.140 """141Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 142–145why, one step back142 def __init__(143 self,144 *,145 soft_limit: typing.Optional[int] = None,Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
line 146why, one step back146 hard_limit: typing.Optional[int] = None,Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 147–148why, one step back147 ):148 self.soft_limit = soft_limitStuffTom Christiebefore adoption30530d4
line 149why, one step back149 self.hard_limit = hard_limitMerge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 150–166why, one step back150151 def __eq__(self, other: typing.Any) -> bool:152 return (153 isinstance(other, self.__class__)154 and self.soft_limit == other.soft_limit155 and self.hard_limit == other.hard_limit156 )157158 def __hash__(self) -> int:159 as_tuple = (self.soft_limit, self.hard_limit)160 return hash(as_tuple)161162 def __repr__(self) -> str:163 class_name = self.__class__.__name__164 return (165 f"{class_name}(soft_limit={self.soft_limit}, hard_limit={self.hard_limit})"166 )StuffTom Christiebefore adoption30530d4
lines 167–170why, one step back167168169DEFAULT_SSL_CONFIG = SSLConfig(cert=None, verify=True)170DEFAULT_TIMEOUT_CONFIG = TimeoutConfig(timeout=5.0)Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
line 171why, one step back171DEFAULT_POOL_LIMITS = PoolLimits(soft_limit=10, hard_limit=100)Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 172why, one step back172DEFAULT_CA_BUNDLE_PATH = certifi.where()