From d53a66ea7efd95f0150eb91b25f289e8c22301ea Mon Sep 17 00:00:00 2001 From: brecht stamper Date: Tue, 10 Jun 2025 13:07:36 +0200 Subject: [PATCH 1/3] prioritize h2 --- httpcore/_async/connection.py | 2 +- httpcore/_async/http_proxy.py | 2 +- httpcore/_async/socks_proxy.py | 2 +- httpcore/_sync/connection.py | 2 +- httpcore/_sync/http_proxy.py | 2 +- httpcore/_sync/socks_proxy.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/httpcore/_async/connection.py b/httpcore/_async/connection.py index b42581df..f0a1f281 100644 --- a/httpcore/_async/connection.py +++ b/httpcore/_async/connection.py @@ -143,7 +143,7 @@ async def _connect(self, request: Request) -> AsyncNetworkStream: if self._ssl_context is None else self._ssl_context ) - alpn_protocols = ["http/1.1", "h2"] if self._http2 else ["http/1.1"] + alpn_protocols = ["h2", "http/1.1"] if self._http2 else ["http/1.1"] ssl_context.set_alpn_protocols(alpn_protocols) kwargs = { diff --git a/httpcore/_async/http_proxy.py b/httpcore/_async/http_proxy.py index cc9d9206..6a0ef86a 100644 --- a/httpcore/_async/http_proxy.py +++ b/httpcore/_async/http_proxy.py @@ -304,7 +304,7 @@ async def handle_async_request(self, request: Request) -> Response: if self._ssl_context is None else self._ssl_context ) - alpn_protocols = ["http/1.1", "h2"] if self._http2 else ["http/1.1"] + alpn_protocols = ["h2", "http/1.1"] if self._http2 else ["http/1.1"] ssl_context.set_alpn_protocols(alpn_protocols) kwargs = { diff --git a/httpcore/_async/socks_proxy.py b/httpcore/_async/socks_proxy.py index b363f55a..74093531 100644 --- a/httpcore/_async/socks_proxy.py +++ b/httpcore/_async/socks_proxy.py @@ -252,7 +252,7 @@ async def handle_async_request(self, request: Request) -> Response: else self._ssl_context ) alpn_protocols = ( - ["http/1.1", "h2"] if self._http2 else ["http/1.1"] + ["h2", "http/1.1"] if self._http2 else ["http/1.1"] ) ssl_context.set_alpn_protocols(alpn_protocols) diff --git a/httpcore/_sync/connection.py b/httpcore/_sync/connection.py index 363f8be8..03817022 100644 --- a/httpcore/_sync/connection.py +++ b/httpcore/_sync/connection.py @@ -143,7 +143,7 @@ def _connect(self, request: Request) -> NetworkStream: if self._ssl_context is None else self._ssl_context ) - alpn_protocols = ["http/1.1", "h2"] if self._http2 else ["http/1.1"] + alpn_protocols = ["h2", "http/1.1"] if self._http2 else ["http/1.1"] ssl_context.set_alpn_protocols(alpn_protocols) kwargs = { diff --git a/httpcore/_sync/http_proxy.py b/httpcore/_sync/http_proxy.py index ecca88f7..89261094 100644 --- a/httpcore/_sync/http_proxy.py +++ b/httpcore/_sync/http_proxy.py @@ -304,7 +304,7 @@ def handle_request(self, request: Request) -> Response: if self._ssl_context is None else self._ssl_context ) - alpn_protocols = ["http/1.1", "h2"] if self._http2 else ["http/1.1"] + alpn_protocols = ["h2", "http/1.1"] if self._http2 else ["http/1.1"] ssl_context.set_alpn_protocols(alpn_protocols) kwargs = { diff --git a/httpcore/_sync/socks_proxy.py b/httpcore/_sync/socks_proxy.py index 0ca96ddf..60f70b58 100644 --- a/httpcore/_sync/socks_proxy.py +++ b/httpcore/_sync/socks_proxy.py @@ -252,7 +252,7 @@ def handle_request(self, request: Request) -> Response: else self._ssl_context ) alpn_protocols = ( - ["http/1.1", "h2"] if self._http2 else ["http/1.1"] + ["h2", "http/1.1"] if self._http2 else ["http/1.1"] ) ssl_context.set_alpn_protocols(alpn_protocols) From 01f17884a35644edb8d69f11669cce3dff19abc1 Mon Sep 17 00:00:00 2001 From: brecht stamper Date: Tue, 10 Jun 2025 13:20:17 +0200 Subject: [PATCH 2/3] not always h1 --- httpcore/_async/connection.py | 6 +++++- httpcore/_async/http_proxy.py | 6 +++++- httpcore/_async/socks_proxy.py | 8 +++++--- httpcore/_sync/connection.py | 6 +++++- httpcore/_sync/http_proxy.py | 6 +++++- httpcore/_sync/socks_proxy.py | 8 +++++--- 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/httpcore/_async/connection.py b/httpcore/_async/connection.py index f0a1f281..7894d0e0 100644 --- a/httpcore/_async/connection.py +++ b/httpcore/_async/connection.py @@ -143,7 +143,11 @@ async def _connect(self, request: Request) -> AsyncNetworkStream: if self._ssl_context is None else self._ssl_context ) - alpn_protocols = ["h2", "http/1.1"] if self._http2 else ["http/1.1"] + alpn_protocols = [] + if self._http2: + alpn_protocols.append("h2") + if self._http1: + alpn_protocols.append("http/1.1") ssl_context.set_alpn_protocols(alpn_protocols) kwargs = { diff --git a/httpcore/_async/http_proxy.py b/httpcore/_async/http_proxy.py index 6a0ef86a..3b7c3c24 100644 --- a/httpcore/_async/http_proxy.py +++ b/httpcore/_async/http_proxy.py @@ -304,7 +304,11 @@ async def handle_async_request(self, request: Request) -> Response: if self._ssl_context is None else self._ssl_context ) - alpn_protocols = ["h2", "http/1.1"] if self._http2 else ["http/1.1"] + alpn_protocols = [] + if self._http2: + alpn_protocols.append("h2") + if self._http1: + alpn_protocols.append("http/1.1") ssl_context.set_alpn_protocols(alpn_protocols) kwargs = { diff --git a/httpcore/_async/socks_proxy.py b/httpcore/_async/socks_proxy.py index 74093531..1bd9c704 100644 --- a/httpcore/_async/socks_proxy.py +++ b/httpcore/_async/socks_proxy.py @@ -251,9 +251,11 @@ async def handle_async_request(self, request: Request) -> Response: if self._ssl_context is None else self._ssl_context ) - alpn_protocols = ( - ["h2", "http/1.1"] if self._http2 else ["http/1.1"] - ) + alpn_protocols = [] + if self._http2: + alpn_protocols.append("h2") + if self._http1: + alpn_protocols.append("http/1.1") ssl_context.set_alpn_protocols(alpn_protocols) kwargs = { diff --git a/httpcore/_sync/connection.py b/httpcore/_sync/connection.py index 03817022..d5d70212 100644 --- a/httpcore/_sync/connection.py +++ b/httpcore/_sync/connection.py @@ -143,7 +143,11 @@ def _connect(self, request: Request) -> NetworkStream: if self._ssl_context is None else self._ssl_context ) - alpn_protocols = ["h2", "http/1.1"] if self._http2 else ["http/1.1"] + alpn_protocols = [] + if self._http2: + alpn_protocols.append("h2") + if self._http1: + alpn_protocols.append("http/1.1") ssl_context.set_alpn_protocols(alpn_protocols) kwargs = { diff --git a/httpcore/_sync/http_proxy.py b/httpcore/_sync/http_proxy.py index 89261094..fec02a53 100644 --- a/httpcore/_sync/http_proxy.py +++ b/httpcore/_sync/http_proxy.py @@ -304,7 +304,11 @@ def handle_request(self, request: Request) -> Response: if self._ssl_context is None else self._ssl_context ) - alpn_protocols = ["h2", "http/1.1"] if self._http2 else ["http/1.1"] + alpn_protocols = [] + if self._http2: + alpn_protocols.append("h2") + if self._http1: + alpn_protocols.append("http/1.1") ssl_context.set_alpn_protocols(alpn_protocols) kwargs = { diff --git a/httpcore/_sync/socks_proxy.py b/httpcore/_sync/socks_proxy.py index 60f70b58..4195f4c3 100644 --- a/httpcore/_sync/socks_proxy.py +++ b/httpcore/_sync/socks_proxy.py @@ -251,9 +251,11 @@ def handle_request(self, request: Request) -> Response: if self._ssl_context is None else self._ssl_context ) - alpn_protocols = ( - ["h2", "http/1.1"] if self._http2 else ["http/1.1"] - ) + alpn_protocols = [] + if self._http2: + alpn_protocols.append("h2") + if self._http1: + alpn_protocols.append("http/1.1") ssl_context.set_alpn_protocols(alpn_protocols) kwargs = { From 6b0f6a42b482bdebe45cd17bb7f7762ac149705c Mon Sep 17 00:00:00 2001 From: brecht stamper Date: Tue, 10 Jun 2025 13:42:56 +0200 Subject: [PATCH 3/3] coverage --- tests/_async/test_socks_proxy.py | 2 ++ tests/_sync/test_socks_proxy.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/_async/test_socks_proxy.py b/tests/_async/test_socks_proxy.py index 907594a4..01e184e2 100644 --- a/tests/_async/test_socks_proxy.py +++ b/tests/_async/test_socks_proxy.py @@ -27,6 +27,8 @@ async def test_socks5_request(): async with httpcore.AsyncConnectionPool( proxy=httpcore.Proxy("socks5://localhost:8080/"), network_backend=network_backend, + # We also enable h2, but will negotiated http1.1 + http2=True, ) as proxy: # Sending an intial request, which once complete will return to the pool, IDLE. async with proxy.stream("GET", "https://example.com/") as response: diff --git a/tests/_sync/test_socks_proxy.py b/tests/_sync/test_socks_proxy.py index 89ec9fae..072cb3f9 100644 --- a/tests/_sync/test_socks_proxy.py +++ b/tests/_sync/test_socks_proxy.py @@ -27,6 +27,8 @@ def test_socks5_request(): with httpcore.ConnectionPool( proxy=httpcore.Proxy("socks5://localhost:8080/"), network_backend=network_backend, + # We also enable h2, but will negotiated http1.1 + http2=True, ) as proxy: # Sending an intial request, which once complete will return to the pool, IDLE. with proxy.stream("GET", "https://example.com/") as response: