connection.py
This file
Path
httpcore/dispatch/connection.py
112 lines · 3.6 KiB · Python
Last changed by
Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
History of this fileTake it
Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 1–6why, one step back1import functools2import typing34import h2.connection5import h116Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 7why, one step back7from ..concurrency import AsyncioBackendMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 8–10why, one step back8from ..config import (9 DEFAULT_SSL_CONFIG,10 DEFAULT_TIMEOUT_CONFIG,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 11why, one step back11 CertTypes,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 12–13why, one step back12 SSLConfig,13 TimeoutConfig,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 14–15why, one step back14 TimeoutTypes,15 VerifyTypes,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 16–17why, one step back16)17from ..exceptions import ConnectTimeoutMerge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 18why, one step back18from ..interfaces import ConcurrencyBackend, Dispatcher, ProtocolMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 19–26why, one step back19from ..models import Origin, Request, Response20from .http2 import HTTP2Connection21from .http11 import HTTP11Connection2223# Callback signature: async def callback(conn: HTTPConnection) -> None24ReleaseCallback = typing.Callable[["HTTPConnection"], typing.Awaitable[None]]2526Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 27why, one step back27class HTTPConnection(Dispatcher):Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 28–30why, one step back28 def __init__(29 self,30 origin: typing.Union[str, Origin],Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 31–33why, one step back31 verify: VerifyTypes = True,32 cert: CertTypes = None,33 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 34why, one step back34 backend: ConcurrencyBackend = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 35–37why, one step back35 release_func: typing.Optional[ReleaseCallback] = None,36 ):37 self.origin = Origin(origin) if isinstance(origin, str) else originWork on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 38–39why, one step back38 self.ssl = SSLConfig(cert=cert, verify=verify)39 self.timeout = TimeoutConfig(timeout)Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 40why, one step back40 self.backend = AsyncioBackend() if backend is None else backendMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 41–44why, one step back41 self.release_func = release_func42 self.h11_connection = None # type: typing.Optional[HTTP11Connection]43 self.h2_connection = None # type: typing.Optional[HTTP2Connection]44Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 45–48why, one step back45 async def send(46 self,47 request: Request,48 stream: bool = False,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 49–51why, one step back49 verify: VerifyTypes = None,50 cert: CertTypes = None,51 timeout: TimeoutTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 52why, one step back52 ) -> Response:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 53why, one step back53 if self.h11_connection is None and self.h2_connection is None:Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 54why, one step back54 await self.connect(verify=verify, cert=cert, timeout=timeout)Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 55–56why, one step back5556 if self.h2_connection is not None:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 57–59why, one step back57 response = await self.h2_connection.send(58 request, stream=stream, timeout=timeout59 )Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 60–61why, one step back60 else:61 assert self.h11_connection is not NoneMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 62–64why, one step back62 response = await self.h11_connection.send(63 request, stream=stream, timeout=timeout64 )Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 65–67why, one step back6566 return response67Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 68why, one step back68 async def connect(Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 69–72why, one step back69 self,70 verify: VerifyTypes = None,71 cert: CertTypes = None,72 timeout: TimeoutTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 73why, one step back73 ) -> None:Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 74–75why, one step back74 ssl = self.ssl.with_overrides(verify=verify, cert=cert)75 timeout = self.timeout if timeout is None else TimeoutConfig(timeout)Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 76why, one step back76Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
line 77why, one step back77 host = self.origin.hostMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 78–85why, one step back78 port = self.origin.port79 ssl_context = await ssl.load_ssl_context() if self.origin.is_ssl else None8081 if self.release_func is None:82 on_release = None83 else:84 on_release = functools.partial(self.release_func, self)85Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
lines 86–88why, one step back86 reader, writer, protocol = await self.backend.connect(87 host, port, ssl_context, timeout88 )Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 89–112why, one step back89 if protocol == Protocol.HTTP_2:90 self.h2_connection = HTTP2Connection(reader, writer, on_release=on_release)91 else:92 self.h11_connection = HTTP11Connection(93 reader, writer, on_release=on_release94 )9596 async def close(self) -> None:97 if self.h2_connection is not None:98 await self.h2_connection.close()99 elif self.h11_connection is not None:100 await self.h11_connection.close()101102 @property103 def is_http2(self) -> bool:104 return self.h2_connection is not None105106 @property107 def is_closed(self) -> bool:108 if self.h2_connection is not None:109 return self.h2_connection.is_closed110 else:111 assert self.h11_connection is not None112 return self.h11_connection.is_closed