From 4804c534f82d34d70a8ee7d797caefcf39d2d3d2 Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Sat, 10 Oct 2020 16:23:29 +0530 Subject: [PATCH] Update mypy (#449) --- examples/README.md | 21 +++++++++++++++++++++ examples/connect_tunnel.py | 15 +++++++++------ proxy/common/utils.py | 3 +-- requirements-testing.txt | 2 +- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/examples/README.md b/examples/README.md index 69e3084c..04b38a00 100644 --- a/examples/README.md +++ b/examples/README.md @@ -12,6 +12,7 @@ Table of Contents * [SSL Echo Server](#ssl-echo-server) * [SSL Echo Client](#ssl-echo-client) * [PubSub Eventing](#pubsub-eventing) +* [Connect Tunnel](#connect-tunnel) ## WebSocket Client @@ -115,3 +116,23 @@ bye!!! DEBUG:proxy.core.event.subscriber:Un-subscribed relay sub id 5eb22010764f4d44900f41e2fb408ca6 from core events Received 52724 events from main thread, 60172 events from another process, in 21.50117802619934 seconds ``` + +## Connect Tunnel + +A simple HTTP proxy server supporting only CONNECT (https) requests. + +1. Uses `HttpParser` for request parsing. +2. Uses `TcpServerConnection` to establish upstream connection. +3. Overrides `BaseServer` methods to also register read/write events for upstream connection. + +Start `connect_tunnel.py` as: + +``` +❯ PYTHONPATH=. python examples/connect_tunnel.py +``` + +Send https requests via tunnel as: + +``` +❯ curl -x localhost:12345 https://httpbin.org/get +``` diff --git a/examples/connect_tunnel.py b/examples/connect_tunnel.py index 2b1aeefa..2ebac0fb 100644 --- a/examples/connect_tunnel.py +++ b/examples/connect_tunnel.py @@ -66,12 +66,7 @@ class ConnectTunnelHandler(BaseServerHandler): # type: ignore assert self.request.state == httpParserStates.COMPLETE # Establish connection with upstream - assert self.request.host and self.request.port - self.upstream = TcpServerConnection( - text_(self.request.host), self.request.port) - self.upstream.connect() - print('Connection established with upstream {0}:{1}'.format( - text_(self.request.host), self.request.port)) + self.connect_upstream() # Queue tunnel established response to client self.client.queue( @@ -113,6 +108,14 @@ class ConnectTunnelHandler(BaseServerHandler): # type: ignore self.upstream.flush() return False + def connect_upstream(self) -> None: + assert self.request.host and self.request.port + self.upstream = TcpServerConnection( + text_(self.request.host), self.request.port) + self.upstream.connect() + print('Connection established with upstream {0}:{1}'.format( + text_(self.request.host), self.request.port)) + def main() -> None: # This example requires `threadless=True` diff --git a/proxy/common/utils.py b/proxy/common/utils.py index 3edc6327..e125a3d4 100644 --- a/proxy/common/utils.py +++ b/proxy/common/utils.py @@ -211,8 +211,7 @@ class socket_connection(contextlib.ContextDecorator): if self.conn: self.conn.close() - def __call__(self, func: Callable[..., Any] - ) -> Callable[[Tuple[Any, ...], Dict[str, Any]], Any]: + def __call__(self, func: Callable[..., Any]) -> Callable[[Tuple[Any, ...], Dict[str, Any]], Any]: # type: ignore @functools.wraps(func) def decorated(*args: Any, **kwargs: Any) -> Any: with self as conn: diff --git a/requirements-testing.txt b/requirements-testing.txt index 4a52f7d5..93a89606 100644 --- a/requirements-testing.txt +++ b/requirements-testing.txt @@ -4,7 +4,7 @@ flake8==3.8.4 pytest==6.1.1 pytest-cov==2.10.1 autopep8==1.5.4 -mypy==0.782 +mypy==0.790 py-spy==0.3.3 codecov==2.1.10 tox==3.20.1