Skip to content
Myr

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

4 files+27 added−20 removed

modified:httpx/concurrency/asyncio.py+14 added-13 removed
59 unmodified lines
60 self.stream_reader = stream_reader61 self.stream_writer = stream_writer62 self.timeout = timeout6364 def get_http_version(self) -> str:65 ssl_object = self.stream_writer.get_extra_info("ssl_object")6667 if ssl_object is None:68 return "HTTP/1.1"6970 ident = ssl_object.selected_alpn_protocol()71 if ident is None:72 ident = ssl_object.selected_npn_protocol()7374 return "HTTP/2" if ident == "h2" else "HTTP/1.1"7576 async def read(77 self, n: int, timeout: TimeoutConfig = None, flag: TimeoutFlag = None78 ) -> bytes:79 if timeout is None:
100 unmodified lines
180 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()192181 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()188189 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"196194 return stream, http_version195197 async def run_in_threadpool(198 self, func: typing.Callable, *args: typing.Any, **kwargs: typing.Any199 ) -> typing.Any:200 if kwargs:
49 unmodified lines
modified:httpx/concurrency/base.py+4 added-1 removed
42 unmodified lines
43 interfaces into a more generic base class, that we can use with alternate44 backends, or for stand-alone test cases.45 """4647 def get_http_version(self) -> str:48 raise NotImplementedError() # pragma: no cover4950 async def read(51 self, n: int, timeout: TimeoutConfig = None, flag: typing.Any = None52 ) -> bytes:53 raise NotImplementedError() # pragma: no cover
58 unmodified lines
112 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 cover118119 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:120 raise NotImplementedError() # pragma: no cover
57 unmodified lines
modified:httpx/dispatch/connection.py+4 added-4 removed
77 unmodified lines
78 on_release = None79 else:80 on_release = functools.partial(self.release_func, self)8182 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()8485 if http_version == "HTTP/2":86 self.h2_connection = HTTP2Connection(87 stream, self.backend, on_release=on_release88 )
6 unmodified lines
95 async def get_ssl_context(self, ssl: SSLConfig) -> typing.Optional[ssl.SSLContext]:96 if not self.origin.is_ssl:97 return None9899 # 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 lines
modified:tests/dispatch/utils.py+5 added-2 removed
18 unmodified lines
19 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.server262728class MockHTTP2Server(BaseStream):29 def __init__(self, app):
5 unmodified lines
35 self.close_connection = False3637 # Stream interface3839 def get_http_version(self) -> str:40 return "HTTP/2"4142 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 send
67 unmodified lines