Skip to content
11 changes: 9 additions & 2 deletions httpcore/_async/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions httpcore/_sync/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down