Skip to content
Myr

connection.py

This file

Path

httpcore/dispatch/connection.py

94 lines · 3.1 KiB · Python

Last changed by

Reorganise structureTom Christiebefore adoption0c6f618

History of this file

Take it

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 1–6why, one step back
1import functools
2import typing34import h2.connection5import h116

Reorganise structureTom Christiebefore adoption0c6f618

line 7why, one step back
7from ..backends.default import connect

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 8–13why, one step back
8from ..config import (
9 DEFAULT_SSL_CONFIG,10 DEFAULT_TIMEOUT_CONFIG,11 SSLConfig,12 TimeoutConfig,13)

Reorganise structureTom Christiebefore adoption0c6f618

line 14why, one step back
14from ..constants import Protocol

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 15–60why, one step back
15from ..exceptions import ConnectTimeout
16from ..interfaces import Adapter17from ..models import Origin, Request, Response18from .http2 import HTTP2Connection19from .http11 import HTTP11Connection2021# Callback signature: async def callback(conn: HTTPConnection) -> None22ReleaseCallback = typing.Callable[["HTTPConnection"], typing.Awaitable[None]]232425class HTTPConnection(Adapter):26 def __init__(27 self,28 origin: typing.Union[str, Origin],29 ssl: SSLConfig = DEFAULT_SSL_CONFIG,30 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,31 release_func: typing.Optional[ReleaseCallback] = None,32 ):33 self.origin = Origin(origin) if isinstance(origin, str) else origin34 self.ssl = ssl35 self.timeout = timeout36 self.release_func = release_func37 self.h11_connection = None # type: typing.Optional[HTTP11Connection]38 self.h2_connection = None # type: typing.Optional[HTTP2Connection]3940 def prepare_request(self, request: Request) -> None:41 request.prepare()4243 async def send(self, request: Request, **options: typing.Any) -> Response:44 if self.h11_connection is None and self.h2_connection is None:45 await self.connect(**options)4647 if self.h2_connection is not None:48 response = await self.h2_connection.send(request, **options)49 else:50 assert self.h11_connection is not None51 response = await self.h11_connection.send(request, **options)5253 return response5455 async def connect(self, **options: typing.Any) -> None:56 ssl = options.get("ssl", self.ssl)57 timeout = options.get("timeout", self.timeout)58 assert isinstance(ssl, SSLConfig)59 assert isinstance(timeout, TimeoutConfig)60

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

line 61why, one step back
61 host = self.origin.host

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 62–69why, one step back
62 port = self.origin.port
63 ssl_context = await ssl.load_ssl_context() if self.origin.is_ssl else None6465 if self.release_func is None:66 on_release = None67 else:68 on_release = functools.partial(self.release_func, self)69

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

line 70why, one step back
70 reader, writer, protocol = await connect(host, port, ssl_context, timeout)

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 71–94why, one step back
71 if protocol == Protocol.HTTP_2:
72 self.h2_connection = HTTP2Connection(reader, writer, on_release=on_release)73 else:74 self.h11_connection = HTTP11Connection(75 reader, writer, on_release=on_release76 )7778 async def close(self) -> None:79 if self.h2_connection is not None:80 await self.h2_connection.close()81 elif self.h11_connection is not None:82 await self.h11_connection.close()8384 @property85 def is_http2(self) -> bool:86 return self.h2_connection is not None8788 @property89 def is_closed(self) -> bool:90 if self.h2_connection is not None:91 return self.h2_connection.is_closed92 else:93 assert self.h11_connection is not None94 return self.h11_connection.is_closed