Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 192
Fix tunnel proxy: HTTP requests only#57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
4ccd444cb64474bb332887b282e5e66f63a0d565490538c2781c77deae0fe5cb0d21671aff8b892c749ad4cc5e7a6c38a7b9acb72b03255e1c8a3a3e74665dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| from ssl import SSLContext | ||
| from typing import List, Optional, Tuple, Union | ||
| from .._backends.auto import AsyncLock, AutoBackend | ||
| from .._backends.auto import AsyncLock, AsyncSocketStream, AutoBackend | ||
| from .._types import URL, Headers, Origin, TimeoutDict | ||
| from .base import ( | ||
| AsyncByteStream, | ||
| @@ -15,11 +15,16 @@ | ||
| class AsyncHTTPConnection(AsyncHTTPTransport): | ||
| def __init__( | ||
| self, origin: Origin, http2: bool = False, ssl_context: SSLContext = None, | ||
| self, | ||
| origin: Origin, | ||
| http2: bool = False, | ||
| ssl_context: SSLContext = None, | ||
| socket: AsyncSocketStream = None, | ||
| ): | ||
| self.origin = origin | ||
| self.http2 = http2 | ||
| self.ssl_context = SSLContext() if ssl_context is None else ssl_context | ||
| self.socket = socket | ||
| if self.http2: | ||
| self.ssl_context.set_alpn_protocols(["http/1.1", "h2"]) | ||
| @@ -48,14 +53,11 @@ async def request( | ||
| timeout: TimeoutDict = None, | ||
| ) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], AsyncByteStream]: | ||
| assert url[:3] == self.origin | ||
| async with self.request_lock: | ||
| if self.state == ConnectionState.PENDING: | ||
| try: | ||
| await self._connect(timeout) | ||
| except Exception: | ||
| self.connect_failed = True | ||
| raise | ||
| if not self.socket: | ||
| self.socket = await self._open_socket(timeout) | ||
| self._create_connection(self.socket) | ||
| elif self.state in (ConnectionState.READY, ConnectionState.IDLE): | ||
| pass | ||
| elif self.state == ConnectionState.ACTIVE and self.is_http2: | ||
| @@ -66,20 +68,30 @@ async def request( | ||
| assert self.connection is not None | ||
| return await self.connection.request(method, url, headers, stream, timeout) | ||
| async def _connect(self, timeout: TimeoutDict = None) -> None: | ||
| async def _open_socket(self, timeout: TimeoutDict = None) -> AsyncSocketStream: | ||
| scheme, hostname, port = self.origin | ||
| timeout = {} if timeout is None else timeout | ||
| ssl_context = self.ssl_context if scheme == b"https" else None | ||
| socket = await self.backend.open_tcp_stream( | ||
| hostname, port, ssl_context, timeout | ||
| ) | ||
| try: | ||
| return await self.backend.open_tcp_stream( | ||
| hostname, port, ssl_context, timeout | ||
| ) | ||
| except Exception: | ||
| self.connect_failed = True | ||
| raise | ||
| def _create_connection(self, socket: AsyncSocketStream) -> None: | ||
| http_version = socket.get_http_version() | ||
| if http_version == "HTTP/2": | ||
| self.is_http2 = True | ||
| self.connection = AsyncHTTP2Connection(socket=socket, backend=self.backend) | ||
| self.connection = AsyncHTTP2Connection( | ||
| socket=socket, backend=self.backend, ssl_context=self.ssl_context | ||
| ) | ||
| else: | ||
| self.is_http11 = True | ||
| self.connection = AsyncHTTP11Connection(socket=socket) | ||
| self.connection = AsyncHTTP11Connection( | ||
| socket=socket, ssl_context=self.ssl_context | ||
| ) | ||
| @property | ||
| def state(self) -> ConnectionState: | ||
| @@ -99,3 +111,4 @@ def mark_as_ready(self) -> None: | ||
| async def start_tls(self, hostname: bytes, timeout: TimeoutDict = None) -> None: | ||
| if self.connection is not None: | ||
| await self.connection.start_tls(hostname, timeout) | ||
| self.socket = self.connection.socket | ||
florimondmanca marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -123,7 +123,7 @@ async def _receive_response_data( | ||
| event = await self._receive_event(timeout) | ||
| if isinstance(event, h11.Data): | ||
| yield bytes(event.data) | ||
| elif isinstance(event, h11.EndOfMessage): | ||
| elif isinstance(event, (h11.EndOfMessage, h11.PAUSED)): | ||
yeraydiazdiaz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| break | ||
| async def _receive_event(self, timeout: TimeoutDict) -> H11Event: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -107,9 +107,9 @@ async def start_tls( | ||
| transport = await asyncio.wait_for( | ||
| loop_start_tls( | ||
| transport=transport, | ||
| protocol=protocol, | ||
| sslcontext=ssl_context, | ||
| transport, | ||
| protocol, | ||
| ssl_context, | ||
florimondmanca marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| server_hostname=hostname.decode("ascii"), | ||
| ), | ||
| timeout=timeout.get("connect"), | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.