diff --git a/httpcore/_async/connection.py b/httpcore/_async/connection.py index 2f713d49d..5d15b1092 100644 --- a/httpcore/_async/connection.py +++ b/httpcore/_async/connection.py @@ -24,6 +24,7 @@ def __init__( self, origin: Origin, http2: bool = False, + http1: bool = True, uds: str = None, ssl_context: SSLContext = None, socket: AsyncSocketStream = None, @@ -33,14 +34,20 @@ def __init__( ): self.origin = origin self.http2 = http2 + self.http1 = http1 + self.uds = uds self.ssl_context = SSLContext() if ssl_context is None else ssl_context self.socket = socket self.local_address = local_address self.retries = retries - if self.http2: + if self.http1 and self.http2: self.ssl_context.set_alpn_protocols(["http/1.1", "h2"]) + elif self.http1: + self.ssl_context.set_alpn_protocols(["http/1.1"]) + elif self.http2: + self.ssl_context.set_alpn_protocols(["h2"]) self.connection: Optional[AsyncBaseHTTPConnection] = None self.is_http11 = False @@ -147,7 +154,7 @@ def _create_connection(self, socket: AsyncSocketStream) -> None: logger.trace( "create_connection socket=%r http_version=%r", socket, http_version ) - if http_version == "HTTP/2": + if http_version == "HTTP/2" or (self.http2 and not self.http1): from .http2 import AsyncHTTP2Connection self.is_http2 = True diff --git a/httpcore/_async/connection_pool.py b/httpcore/_async/connection_pool.py index 650abfc47..feede6c9c 100644 --- a/httpcore/_async/connection_pool.py +++ b/httpcore/_async/connection_pool.py @@ -89,8 +89,10 @@ class AsyncConnectionPool(AsyncHTTPTransport): connections. keepalive_expiry: The maximum time to allow before closing a keep-alive connection. + http1: + Enable/Disable HTTP/1.1 support. Defaults to True. http2: - Enable HTTP/2 support. + Enable/Disable HTTP/2 support. Defaults to False. uds: Path to a Unix Domain Socket to use instead of TCP sockets. local_address: @@ -111,6 +113,7 @@ def __init__( max_keepalive_connections: int = None, keepalive_expiry: float = None, http2: bool = False, + http1: bool = True, uds: str = None, local_address: str = None, retries: int = 0, @@ -132,6 +135,7 @@ def __init__( self._max_keepalive_connections = max_keepalive_connections self._keepalive_expiry = keepalive_expiry self._http2 = http2 + self._http1 = http1 self._uds = uds self._local_address = local_address self._retries = retries @@ -176,6 +180,7 @@ def _create_connection( return AsyncHTTPConnection( origin=origin, http2=self._http2, + http1=self._http1, uds=self._uds, ssl_context=self._ssl_context, local_address=self._local_address, diff --git a/httpcore/_sync/connection.py b/httpcore/_sync/connection.py index 26e69e0ac..9c43b6516 100644 --- a/httpcore/_sync/connection.py +++ b/httpcore/_sync/connection.py @@ -24,6 +24,7 @@ def __init__( self, origin: Origin, http2: bool = False, + http1: bool = True, uds: str = None, ssl_context: SSLContext = None, socket: SyncSocketStream = None, @@ -33,14 +34,20 @@ def __init__( ): self.origin = origin self.http2 = http2 + self.http1 = http1 + self.uds = uds self.ssl_context = SSLContext() if ssl_context is None else ssl_context self.socket = socket self.local_address = local_address self.retries = retries - if self.http2: + if self.http1 and self.http2: self.ssl_context.set_alpn_protocols(["http/1.1", "h2"]) + elif self.http1: + self.ssl_context.set_alpn_protocols(["http/1.1"]) + elif self.http2: + self.ssl_context.set_alpn_protocols(["h2"]) self.connection: Optional[SyncBaseHTTPConnection] = None self.is_http11 = False @@ -147,7 +154,7 @@ def _create_connection(self, socket: SyncSocketStream) -> None: logger.trace( "create_connection socket=%r http_version=%r", socket, http_version ) - if http_version == "HTTP/2": + if http_version == "HTTP/2" or (self.http2 and not self.http1): from .http2 import SyncHTTP2Connection self.is_http2 = True diff --git a/httpcore/_sync/connection_pool.py b/httpcore/_sync/connection_pool.py index 7d2ba9eb4..ace2e6b9f 100644 --- a/httpcore/_sync/connection_pool.py +++ b/httpcore/_sync/connection_pool.py @@ -89,8 +89,10 @@ class SyncConnectionPool(SyncHTTPTransport): connections. keepalive_expiry: The maximum time to allow before closing a keep-alive connection. + http1: + Enable/Disable HTTP/1.1 support. Defaults to True. http2: - Enable HTTP/2 support. + Enable/Disable HTTP/2 support. Defaults to False. uds: Path to a Unix Domain Socket to use instead of TCP sockets. local_address: @@ -111,6 +113,7 @@ def __init__( max_keepalive_connections: int = None, keepalive_expiry: float = None, http2: bool = False, + http1: bool = True, uds: str = None, local_address: str = None, retries: int = 0, @@ -132,6 +135,7 @@ def __init__( self._max_keepalive_connections = max_keepalive_connections self._keepalive_expiry = keepalive_expiry self._http2 = http2 + self._http1 = http1 self._uds = uds self._local_address = local_address self._retries = retries @@ -176,6 +180,7 @@ def _create_connection( return SyncHTTPConnection( origin=origin, http2=self._http2, + http1=self._http1, uds=self._uds, ssl_context=self._ssl_context, local_address=self._local_address,