From 4ec1892bf7393ad66af303504cc8e857b424f72c Mon Sep 17 00:00:00 2001 From: p1-ra Date: Fri, 21 May 2021 13:18:58 +0200 Subject: [PATCH 1/2] http_proxy / propagate http1 support for http2-prior-to-knowledge --- httpx/_client.py | 7 +++++++ httpx/_transports/default.py | 2 ++ 2 files changed, 9 insertions(+) diff --git a/httpx/_client.py b/httpx/_client.py index 11c8e12351..6078775523 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -580,6 +580,10 @@ class Client(BaseClient): to authenticate the client. Either a path to an SSL certificate file, or two-tuple of (certificate file, key file), or a three-tuple of (certificate file, key file, password). + * **http1** - *(optional)* A boolean indicating if HTTP/1 support should be + enabled. Defaults to `True`. + * **http2** - *(optional)* A boolean indicating if HTTP/2 support should be + enabled. Default to `False`. * **proxies** - *(optional)* A dictionary mapping proxy keys to proxy URLs. * **timeout** - *(optional)* The timeout configuration to use when sending @@ -1277,6 +1281,8 @@ class AsyncClient(BaseClient): to authenticate the client. Either a path to an SSL certificate file, or two-tuple of (certificate file, key file), or a three-tuple of (certificate file, key file, password). + * **http1** - *(optional)* A boolean indicating if HTTP/1 support should be + enabled. Defaults to `True`. * **http2** - *(optional)* A boolean indicating if HTTP/2 support should be enabled. Defaults to `False`. * **proxies** - *(optional)* A dictionary mapping HTTP protocols to proxy @@ -1412,6 +1418,7 @@ def _init_proxy_transport( return AsyncHTTPTransport( verify=verify, cert=cert, + http1=http1, http2=http2, limits=limits, trust_env=trust_env, diff --git a/httpx/_transports/default.py b/httpx/_transports/default.py index ae6c2d1777..dde22f1958 100644 --- a/httpx/_transports/default.py +++ b/httpx/_transports/default.py @@ -150,6 +150,7 @@ def __init__( max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, + http1=http1, http2=http2, backend=backend, ) @@ -247,6 +248,7 @@ def __init__( max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, + http1=http1, http2=http2, backend=backend, ) From 5e5c43c0636149da4645c8083380aad834c50839 Mon Sep 17 00:00:00 2001 From: p1-ra Date: Fri, 21 May 2021 13:36:20 +0200 Subject: [PATCH 2/2] proxy / allow override of client http{1,2} support --- httpx/_config.py | 15 ++++++++++++++- httpx/_transports/default.py | 8 ++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/httpx/_config.py b/httpx/_config.py index 837519afb5..3297342252 100644 --- a/httpx/_config.py +++ b/httpx/_config.py @@ -319,7 +319,8 @@ def __repr__(self) -> str: class Proxy: def __init__( - self, url: URLTypes, *, headers: HeaderTypes = None, mode: str = "DEFAULT" + self, url: URLTypes, *, headers: HeaderTypes = None, mode: str = "DEFAULT", + http1_override: typing.Optional[bool] = None, http2_override: typing.Optional[bool] = None ): url = URL(url) headers = Headers(headers) @@ -341,6 +342,18 @@ def __init__( self.url = url self.headers = headers self.mode = mode + self._http1_override = http1_override + self._http2_override = http2_override + + def http1_support(self, default: bool) -> bool: + if self._http1_override is not None: + return self._http1_override + return default + + def http2_support(self, default: bool) -> bool: + if self._http2_override is not None: + return self._http2_override + return default def _build_auth_header(self, username: str, password: str) -> str: userpass = (username.encode("utf-8"), password.encode("utf-8")) diff --git a/httpx/_transports/default.py b/httpx/_transports/default.py index dde22f1958..25c2c658da 100644 --- a/httpx/_transports/default.py +++ b/httpx/_transports/default.py @@ -150,8 +150,8 @@ def __init__( max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, - http1=http1, - http2=http2, + http1=proxy.http1_support(http1), + http2=proxy.http2_support(http2), backend=backend, ) @@ -248,8 +248,8 @@ def __init__( max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, - http1=http1, - http2=http2, + http1=proxy.http1_support(http1), + http2=proxy.http2_support(http2), backend=backend, )