Encapsulate http_version into BaseStream
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by florimondmanca · committed by florimondmanca · parent 14eb81f · Browse files · 4 files·+27 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+27 added−20 removed
- tests:+5 added−2 removed
- other:+22 added−18 removed
Provenance
- Commit
fdb1359- Tree
b4d7284- Author
florimondmanca <florimond.manca@gmail.com>- Committer
florimondmanca <florimond.manca@gmail.com>- Parent
14eb81f
4 files changed+27 added−20 removed
modified:httpx/concurrency/asyncio.py+14 added-13 removed
59 unmodified lines60 self.stream_reader = stream_reader61 self.stream_writer = stream_writer62 self.timeout = timeout63 64+ def get_http_version(self) -> str:65+ ssl_object = self.stream_writer.get_extra_info("ssl_object")66+67+ if ssl_object is None:68+ return "HTTP/1.1"69+70+ ident = ssl_object.selected_alpn_protocol()71+ if ident is None:72+ ident = ssl_object.selected_npn_protocol()73+74+ return "HTTP/2" if ident == "h2" else "HTTP/1.1"75+76 async def read(77 self, n: int, timeout: TimeoutConfig = None, flag: TimeoutFlag = None78 ) -> bytes:79 if timeout is None:100 unmodified lines180 hostname: str,181 port: int,182 ssl_context: typing.Optional[ssl.SSLContext],183 timeout: TimeoutConfig,172- ) -> typing.Tuple[BaseStream, str]:184+ ) -> BaseStream:185 try:186 stream_reader, stream_writer = await asyncio.wait_for( # type: ignore187 asyncio.open_connection(hostname, port, ssl=ssl_context),188 timeout.connect_timeout,189 )190 except asyncio.TimeoutError:191 raise ConnectTimeout()192 181- ssl_object = stream_writer.get_extra_info("ssl_object")182- if ssl_object is None:183- ident = "http/1.1"184- else:185- ident = ssl_object.selected_alpn_protocol()186- if ident is None:187- ident = ssl_object.selected_npn_protocol()188-189- stream = Stream(193+ return Stream(194 stream_reader=stream_reader, stream_writer=stream_writer, timeout=timeout195 )192- http_version = "HTTP/2" if ident == "h2" else "HTTP/1.1"196 194- return stream, http_version195-197 async def run_in_threadpool(198 self, func: typing.Callable, *args: typing.Any, **kwargs: typing.Any199 ) -> typing.Any:200 if kwargs:49 unmodified linesmodified:httpx/concurrency/base.py+4 added-1 removed
42 unmodified lines43 interfaces into a more generic base class, that we can use with alternate44 backends, or for stand-alone test cases.45 """46 47+ def get_http_version(self) -> str:48+ raise NotImplementedError() # pragma: no cover49+50 async def read(51 self, n: int, timeout: TimeoutConfig = None, flag: typing.Any = None52 ) -> bytes:53 raise NotImplementedError() # pragma: no cover58 unmodified lines112 hostname: str,113 port: int,114 ssl_context: typing.Optional[ssl.SSLContext],115 timeout: TimeoutConfig,113- ) -> typing.Tuple[BaseStream, str]:116+ ) -> BaseStream:117 raise NotImplementedError() # pragma: no cover118 119 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:120 raise NotImplementedError() # pragma: no cover57 unmodified linesmodified:httpx/dispatch/connection.py+4 added-4 removed
77 unmodified lines78 on_release = None79 else:80 on_release = functools.partial(self.release_func, self)81 82- stream, http_version = await self.backend.connect(83- host, port, ssl_context, timeout84- )82+ stream = await self.backend.connect(host, port, ssl_context, timeout)83+ http_version = stream.get_http_version()84+85 if http_version == "HTTP/2":86 self.h2_connection = HTTP2Connection(87 stream, self.backend, on_release=on_release88 )6 unmodified lines95 async def get_ssl_context(self, ssl: SSLConfig) -> typing.Optional[ssl.SSLContext]:96 if not self.origin.is_ssl:97 return None98 99- # Run the SSL loading in a threadpool, since it may makes disk accesses.99+ # Run the SSL loading in a threadpool, since it may make disk accesses.100 return await self.backend.run_in_threadpool(101 ssl.load_ssl_context, self.http_versions102 )103 24 unmodified linesmodified:tests/dispatch/utils.py+5 added-2 removed
18 unmodified lines19 hostname: str,20 port: int,21 ssl_context: typing.Optional[ssl.SSLContext],22 timeout: TimeoutConfig,23- ) -> typing.Tuple[BaseStream, str]:23+ ) -> BaseStream:24 self.server = MockHTTP2Server(self.app)25- return self.server, "HTTP/2"25+ return self.server26 27 28 class MockHTTP2Server(BaseStream):29 def __init__(self, app):5 unmodified lines35 self.close_connection = False36 37 # Stream interface38 39+ def get_http_version(self) -> str:40+ return "HTTP/2"41+42 async def read(self, n, timeout, flag=None) -> bytes:43 await asyncio.sleep(0)44 send, self.buffer = self.buffer[:n], self.buffer[n:]45 return send67 unmodified lines