From d5277a8b4fabafb3635c41f9ba135a83c59d550d Mon Sep 17 00:00:00 2001 From: Yeray Diaz Diaz Date: Sun, 3 May 2020 17:21:07 +0100 Subject: [PATCH 1/2] Pass http2 flag to connections in proxy --- httpcore/_async/http_proxy.py | 8 +++++--- httpcore/_sync/http_proxy.py | 8 +++++--- tests/async_tests/test_interfaces.py | 5 ++++- tests/conftest.py | 2 +- tests/sync_tests/test_interfaces.py | 5 ++++- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/httpcore/_async/http_proxy.py b/httpcore/_async/http_proxy.py index 1b043f233..5728be872 100644 --- a/httpcore/_async/http_proxy.py +++ b/httpcore/_async/http_proxy.py @@ -94,7 +94,7 @@ async def _forward_request( if connection is None: connection = AsyncHTTPConnection( - origin=origin, http2=False, ssl_context=self._ssl_context, + origin=origin, http2=self._http2, ssl_context=self._ssl_context, ) await self._add_to_pool(connection) @@ -133,7 +133,9 @@ async def _tunnel_request( if connection is None: # First, create a connection to the proxy server proxy_connection = AsyncHTTPConnection( - origin=self.proxy_origin, http2=False, ssl_context=self._ssl_context, + origin=self.proxy_origin, + http2=self._http2, + ssl_context=self._ssl_context, ) # Issue a CONNECT request... @@ -169,7 +171,7 @@ async def _tunnel_request( # retain the tunnel. connection = AsyncHTTPConnection( origin=origin, - http2=False, + http2=self._http2, ssl_context=self._ssl_context, socket=proxy_connection.socket, ) diff --git a/httpcore/_sync/http_proxy.py b/httpcore/_sync/http_proxy.py index 7f0a3545a..ff3896a69 100644 --- a/httpcore/_sync/http_proxy.py +++ b/httpcore/_sync/http_proxy.py @@ -94,7 +94,7 @@ def _forward_request( if connection is None: connection = SyncHTTPConnection( - origin=origin, http2=False, ssl_context=self._ssl_context, + origin=origin, http2=self._http2, ssl_context=self._ssl_context, ) self._add_to_pool(connection) @@ -133,7 +133,9 @@ def _tunnel_request( if connection is None: # First, create a connection to the proxy server proxy_connection = SyncHTTPConnection( - origin=self.proxy_origin, http2=False, ssl_context=self._ssl_context, + origin=self.proxy_origin, + http2=self._http2, + ssl_context=self._ssl_context, ) # Issue a CONNECT request... @@ -169,7 +171,7 @@ def _tunnel_request( # retain the tunnel. connection = SyncHTTPConnection( origin=origin, - http2=False, + http2=self._http2, ssl_context=self._ssl_context, socket=proxy_connection.socket, ) diff --git a/tests/async_tests/test_interfaces.py b/tests/async_tests/test_interfaces.py index e3aaf9e1d..a0fbdaf6d 100644 --- a/tests/async_tests/test_interfaces.py +++ b/tests/async_tests/test_interfaces.py @@ -211,10 +211,12 @@ async def test_http_proxy( # mitmproxy does not support forwarding HTTPS requests @pytest.mark.parametrize("proxy_mode", ["DEFAULT", "TUNNEL_ONLY"]) @pytest.mark.usefixtures("async_environment") +@pytest.mark.parametrize("http2", [False, True]) async def test_proxy_https_requests( proxy_server: typing.Tuple[bytes, bytes, int], ca_ssl_context: ssl.SSLContext, proxy_mode: str, + http2: bool, ) -> None: method = b"GET" url = (b"https", b"example.org", 443, b"/") @@ -228,12 +230,13 @@ async def test_proxy_https_requests( ssl_context=ca_ssl_context, max_connections=max_connections, max_keepalive=max_keepalive, + http2=http2, ) as http: http_version, status_code, reason, headers, stream = await http.request( method, url, headers ) _ = await read_body(stream) - assert http_version == b"HTTP/1.1" + assert http_version == (b"HTTP/2" if http2 else b"HTTP/1.1") assert status_code == 200 assert reason == b"OK" diff --git a/tests/conftest.py b/tests/conftest.py index f5b244d43..bb701e7b2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -81,7 +81,7 @@ def cert_authority() -> trustme.CA: return trustme.CA() -@pytest.fixture(scope="session") +@pytest.fixture() def ca_ssl_context(cert_authority: trustme.CA) -> ssl.SSLContext: ctx = ssl.create_default_context() cert_authority.configure_trust(ctx) diff --git a/tests/sync_tests/test_interfaces.py b/tests/sync_tests/test_interfaces.py index 276d23c28..798060d41 100644 --- a/tests/sync_tests/test_interfaces.py +++ b/tests/sync_tests/test_interfaces.py @@ -211,10 +211,12 @@ def test_http_proxy( # mitmproxy does not support forwarding HTTPS requests @pytest.mark.parametrize("proxy_mode", ["DEFAULT", "TUNNEL_ONLY"]) +@pytest.mark.parametrize("http2", [False, True]) def test_proxy_https_requests( proxy_server: typing.Tuple[bytes, bytes, int], ca_ssl_context: ssl.SSLContext, proxy_mode: str, + http2: bool, ) -> None: method = b"GET" url = (b"https", b"example.org", 443, b"/") @@ -228,12 +230,13 @@ def test_proxy_https_requests( ssl_context=ca_ssl_context, max_connections=max_connections, max_keepalive=max_keepalive, + http2=http2, ) as http: http_version, status_code, reason, headers, stream = http.request( method, url, headers ) _ = read_body(stream) - assert http_version == b"HTTP/1.1" + assert http_version == (b"HTTP/2" if http2 else b"HTTP/1.1") assert status_code == 200 assert reason == b"OK" From 85c89c8b1c19e19fcc1b04f11cab1dcfc909a3a6 Mon Sep 17 00:00:00 2001 From: Yeray Diaz Diaz Date: Mon, 4 May 2020 11:19:15 +0100 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3910c2423..c1cf6923b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## Unreleased +### Fixed + +- HTTP/2 flag not being passed down to proxy connections. + ## 0.8.2 (May 3rd, 2020) ### Fixed