From 46d385aaec13f624ee2f370f7a6df2ce18d42e06 Mon Sep 17 00:00:00 2001 From: avy Date: Thu, 28 Jan 2021 10:19:47 +0100 Subject: [PATCH 1/7] introduce http2_prior_knowledge option for http connections --- httpcore/_async/connection.py | 7 +++++++ httpcore/_async/connection_pool.py | 5 +++++ httpcore/_async/http_proxy.py | 6 +++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/httpcore/_async/connection.py b/httpcore/_async/connection.py index 530663d83..f8b68ab22 100644 --- a/httpcore/_async/connection.py +++ b/httpcore/_async/connection.py @@ -24,6 +24,7 @@ def __init__( self, origin: Origin, http2: bool = False, + http2_prior_knowledge: bool = False, uds: str = None, ssl_context: SSLContext = None, socket: AsyncSocketStream = None, @@ -33,6 +34,10 @@ def __init__( ): self.origin = origin self.http2 = http2 + self.http2_prior_knowledge = http2_prior_knowledge + if http2_prior_knowledge: + self.http2 = True + self.uds = uds self.ssl_context = SSLContext() if ssl_context is None else ssl_context self.socket = socket @@ -140,6 +145,8 @@ async def _open_socket(self, timeout: TimeoutDict = None) -> AsyncSocketStream: def _create_connection(self, socket: AsyncSocketStream) -> None: http_version = socket.get_http_version() + if self.http2_prior_knowledge: + http_version = "HTTP/2" logger.trace( "create_connection socket=%r http_version=%r", socket, http_version ) diff --git a/httpcore/_async/connection_pool.py b/httpcore/_async/connection_pool.py index 46ede0ba4..6ba137141 100644 --- a/httpcore/_async/connection_pool.py +++ b/httpcore/_async/connection_pool.py @@ -106,6 +106,7 @@ def __init__( max_keepalive_connections: int = None, keepalive_expiry: float = None, http2: bool = False, + http2_prior_knowledge: bool = False, uds: str = None, local_address: str = None, retries: int = 0, @@ -127,6 +128,9 @@ def __init__( self._max_keepalive_connections = max_keepalive_connections self._keepalive_expiry = keepalive_expiry self._http2 = http2 + self._http2_prior_knowledge = http2_prior_knowledge + if http2_prior_knowledge: + self._http2 = True self._uds = uds self._local_address = local_address self._retries = retries @@ -171,6 +175,7 @@ def _create_connection( return AsyncHTTPConnection( origin=origin, http2=self._http2, + http2_prior_knowledge=self._http2_prior_knowledge, uds=self._uds, ssl_context=self._ssl_context, local_address=self._local_address, diff --git a/httpcore/_async/http_proxy.py b/httpcore/_async/http_proxy.py index d9df762b0..b316a3c16 100644 --- a/httpcore/_async/http_proxy.py +++ b/httpcore/_async/http_proxy.py @@ -68,6 +68,7 @@ def __init__( max_keepalive_connections: int = None, keepalive_expiry: float = None, http2: bool = False, + http2_prior_knowledge: bool = False, backend: str = "auto", # Deprecated argument style: max_keepalive: int = None, @@ -83,6 +84,7 @@ def __init__( max_keepalive_connections=max_keepalive_connections, keepalive_expiry=keepalive_expiry, http2=http2, + http2_prior_knowledge=http2_prior_knowledge, backend=backend, max_keepalive=max_keepalive, ) @@ -144,7 +146,7 @@ async def _forward_request( if connection is None: connection = AsyncHTTPConnection( - origin=origin, http2=self._http2, ssl_context=self._ssl_context + origin=origin, http2=self._http2, http2_prior_knowledge=self._http2_prior_knowledge, ssl_context=self._ssl_context ) await self._add_to_pool(connection, timeout) @@ -196,6 +198,7 @@ async def _tunnel_request( proxy_connection = AsyncHTTPConnection( origin=self.proxy_origin, http2=self._http2, + http2_prior_knowledge=self._http2_prior_knowledge, ssl_context=self._ssl_context, ) @@ -248,6 +251,7 @@ async def _tunnel_request( connection = AsyncHTTPConnection( origin=origin, http2=self._http2, + http2_prior_knowledge=self._http2_prior_knowledge, ssl_context=self._ssl_context, socket=proxy_connection.socket, ) From f5082dc00d69ab017e9e1b8592e2b89a4424a3cb Mon Sep 17 00:00:00 2001 From: avy Date: Mon, 1 Feb 2021 08:10:54 +0100 Subject: [PATCH 2/7] add commentary to http2_prior_knowledge var --- httpcore/_async/connection_pool.py | 1 + httpcore/_async/http_proxy.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/httpcore/_async/connection_pool.py b/httpcore/_async/connection_pool.py index 6ba137141..a1da9c915 100644 --- a/httpcore/_async/connection_pool.py +++ b/httpcore/_async/connection_pool.py @@ -88,6 +88,7 @@ class AsyncConnectionPool(AsyncHTTPTransport): * **keepalive_expiry** - `Optional[float]` - The maximum time to allow before closing a keep-alive connection. * **http2** - `bool` - Enable HTTP/2 support. + * **http2_prior_knowledge** - `bool` - Enforce HTTP/2 usage. * **uds** - `str` - Path to a Unix Domain Socket to use instead of TCP sockets. * **local_address** - `Optional[str]` - Local address to connect from. Can also be used to connect using a particular address family. Using diff --git a/httpcore/_async/http_proxy.py b/httpcore/_async/http_proxy.py index b316a3c16..be99fed8d 100644 --- a/httpcore/_async/http_proxy.py +++ b/httpcore/_async/http_proxy.py @@ -56,6 +56,7 @@ class AsyncHTTPProxy(AsyncConnectionPool): * **max_keepalive_connections** - `Optional[int]` - The maximum number of connections to allow before closing keep-alive connections. * **http2** - `bool` - Enable HTTP/2 support. + * **http2_prior_knowledge** - `bool` - Enforce HTTP/2 usage. """ def __init__( @@ -146,7 +147,10 @@ async def _forward_request( if connection is None: connection = AsyncHTTPConnection( - origin=origin, http2=self._http2, http2_prior_knowledge=self._http2_prior_knowledge, ssl_context=self._ssl_context + origin=origin, + http2=self._http2, + http2_prior_knowledge=self._http2_prior_knowledge, + ssl_context=self._ssl_context, ) await self._add_to_pool(connection, timeout) From 436de5e95d047f21d9bbacc3e42068c19790690b Mon Sep 17 00:00:00 2001 From: avy Date: Mon, 1 Feb 2021 08:11:28 +0100 Subject: [PATCH 3/7] unasync http2_prior_knowledge code --- httpcore/_sync/connection.py | 6 ++++++ httpcore/_sync/connection_pool.py | 6 ++++++ httpcore/_sync/http_proxy.py | 10 +++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/httpcore/_sync/connection.py b/httpcore/_sync/connection.py index 040422273..9ee211e54 100644 --- a/httpcore/_sync/connection.py +++ b/httpcore/_sync/connection.py @@ -24,6 +24,7 @@ def __init__( self, origin: Origin, http2: bool = False, + http2_prior_knowledge: bool = False, uds: str = None, ssl_context: SSLContext = None, socket: SyncSocketStream = None, @@ -33,6 +34,9 @@ def __init__( ): self.origin = origin self.http2 = http2 + self.http2_prior_knowledge = http2_prior_knowledge + if http2_prior_knowledge: + self.http2 = True self.uds = uds self.ssl_context = SSLContext() if ssl_context is None else ssl_context self.socket = socket @@ -140,6 +144,8 @@ def _open_socket(self, timeout: TimeoutDict = None) -> SyncSocketStream: def _create_connection(self, socket: SyncSocketStream) -> None: http_version = socket.get_http_version() + if self.http2_prior_knowledge: + http_version = "HTTP/2" logger.trace( "create_connection socket=%r http_version=%r", socket, http_version ) diff --git a/httpcore/_sync/connection_pool.py b/httpcore/_sync/connection_pool.py index 4702184b8..320c9f56d 100644 --- a/httpcore/_sync/connection_pool.py +++ b/httpcore/_sync/connection_pool.py @@ -88,6 +88,7 @@ class SyncConnectionPool(SyncHTTPTransport): * **keepalive_expiry** - `Optional[float]` - The maximum time to allow before closing a keep-alive connection. * **http2** - `bool` - Enable HTTP/2 support. + * **http2_prior_knowledge** - `bool` - Enforce HTTP/2 usage. * **uds** - `str` - Path to a Unix Domain Socket to use instead of TCP sockets. * **local_address** - `Optional[str]` - Local address to connect from. Can also be used to connect using a particular address family. Using @@ -106,6 +107,7 @@ def __init__( max_keepalive_connections: int = None, keepalive_expiry: float = None, http2: bool = False, + http2_prior_knowledge: bool = False, uds: str = None, local_address: str = None, retries: int = 0, @@ -127,6 +129,9 @@ def __init__( self._max_keepalive_connections = max_keepalive_connections self._keepalive_expiry = keepalive_expiry self._http2 = http2 + self._http2_prior_knowledge = http2_prior_knowledge + if http2_prior_knowledge: + self._http2 = True self._uds = uds self._local_address = local_address self._retries = retries @@ -171,6 +176,7 @@ def _create_connection( return SyncHTTPConnection( origin=origin, http2=self._http2, + http2_prior_knowledge=self._http2_prior_knowledge, uds=self._uds, ssl_context=self._ssl_context, local_address=self._local_address, diff --git a/httpcore/_sync/http_proxy.py b/httpcore/_sync/http_proxy.py index f5576c010..62365afe6 100644 --- a/httpcore/_sync/http_proxy.py +++ b/httpcore/_sync/http_proxy.py @@ -56,6 +56,7 @@ class SyncHTTPProxy(SyncConnectionPool): * **max_keepalive_connections** - `Optional[int]` - The maximum number of connections to allow before closing keep-alive connections. * **http2** - `bool` - Enable HTTP/2 support. + * **http2_prior_knowledge** - `bool` - Enforce HTTP/2 usage. """ def __init__( @@ -68,6 +69,7 @@ def __init__( max_keepalive_connections: int = None, keepalive_expiry: float = None, http2: bool = False, + http2_prior_knowledge: bool = False, backend: str = "sync", # Deprecated argument style: max_keepalive: int = None, @@ -83,6 +85,7 @@ def __init__( max_keepalive_connections=max_keepalive_connections, keepalive_expiry=keepalive_expiry, http2=http2, + http2_prior_knowledge=http2_prior_knowledge, backend=backend, max_keepalive=max_keepalive, ) @@ -144,7 +147,10 @@ def _forward_request( if connection is None: connection = SyncHTTPConnection( - origin=origin, http2=self._http2, ssl_context=self._ssl_context + origin=origin, + http2=self._http2, + http2_prior_knowledge=self._http2_prior_knowledge, + ssl_context=self._ssl_context, ) self._add_to_pool(connection, timeout) @@ -196,6 +202,7 @@ def _tunnel_request( proxy_connection = SyncHTTPConnection( origin=self.proxy_origin, http2=self._http2, + http2_prior_knowledge=self._http2_prior_knowledge, ssl_context=self._ssl_context, ) @@ -248,6 +255,7 @@ def _tunnel_request( connection = SyncHTTPConnection( origin=origin, http2=self._http2, + http2_prior_knowledge=self._http2_prior_knowledge, ssl_context=self._ssl_context, socket=proxy_connection.socket, ) From 7311c605789e4612e19ed4b7ab883146699fd50c Mon Sep 17 00:00:00 2001 From: avy Date: Mon, 1 Feb 2021 13:43:40 +0100 Subject: [PATCH 4/7] add http2 enforcing in alpn negociation --- httpcore/_async/connection.py | 4 +++- httpcore/_async/http2.py | 4 ++++ httpcore/_sync/connection.py | 5 ++++- httpcore/_sync/http2.py | 4 ++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/httpcore/_async/connection.py b/httpcore/_async/connection.py index f8b68ab22..d8a92c9f6 100644 --- a/httpcore/_async/connection.py +++ b/httpcore/_async/connection.py @@ -44,7 +44,9 @@ def __init__( self.local_address = local_address self.retries = retries - if self.http2: + if self.http2_prior_knowledge: + self.ssl_context.set_alpn_protocols(["h2"]) + elif self.http2: self.ssl_context.set_alpn_protocols(["http/1.1", "h2"]) self.connection: Optional[AsyncBaseHTTPConnection] = None diff --git a/httpcore/_async/http2.py b/httpcore/_async/http2.py index 3c7404aa0..12f361795 100644 --- a/httpcore/_async/http2.py +++ b/httpcore/_async/http2.py @@ -205,6 +205,10 @@ async def receive_events(self, timeout: TimeoutDict) -> None: data = await self.socket.read(self.READ_NUM_BYTES, timeout) if data == b"": raise RemoteProtocolError("Server disconnected") + elif data.find(b"HTTP/1.1") != -1: + raise RemoteProtocolError("HTTP/1.1 received") + elif data.find(b"HTTP/1.0") != -1: + raise RemoteProtocolError("HTTP/1.0 received") events = self.h2_state.receive_data(data) for event in events: diff --git a/httpcore/_sync/connection.py b/httpcore/_sync/connection.py index 9ee211e54..75b2b537c 100644 --- a/httpcore/_sync/connection.py +++ b/httpcore/_sync/connection.py @@ -37,13 +37,16 @@ def __init__( self.http2_prior_knowledge = http2_prior_knowledge if http2_prior_knowledge: self.http2 = True + 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.http2_prior_knowledge: + self.ssl_context.set_alpn_protocols(["h2"]) + elif self.http2: self.ssl_context.set_alpn_protocols(["http/1.1", "h2"]) self.connection: Optional[SyncBaseHTTPConnection] = None diff --git a/httpcore/_sync/http2.py b/httpcore/_sync/http2.py index fe2d55ebe..505fa318b 100644 --- a/httpcore/_sync/http2.py +++ b/httpcore/_sync/http2.py @@ -205,6 +205,10 @@ def receive_events(self, timeout: TimeoutDict) -> None: data = self.socket.read(self.READ_NUM_BYTES, timeout) if data == b"": raise RemoteProtocolError("Server disconnected") + elif data.find(b"HTTP/1.1") != -1: + raise RemoteProtocolError("HTTP/1.1 received") + elif data.find(b"HTTP/1.0") != -1: + raise RemoteProtocolError("HTTP/1.0 received") events = self.h2_state.receive_data(data) for event in events: From ed53c58e166b296113e3712c8c28f6b90abf924a Mon Sep 17 00:00:00 2001 From: avy Date: Mon, 1 Feb 2021 14:30:23 +0100 Subject: [PATCH 5/7] revert proxy changes for http2_prior_knowledge --- httpcore/_async/http_proxy.py | 10 +--------- httpcore/_sync/http_proxy.py | 10 +--------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/httpcore/_async/http_proxy.py b/httpcore/_async/http_proxy.py index be99fed8d..d9df762b0 100644 --- a/httpcore/_async/http_proxy.py +++ b/httpcore/_async/http_proxy.py @@ -56,7 +56,6 @@ class AsyncHTTPProxy(AsyncConnectionPool): * **max_keepalive_connections** - `Optional[int]` - The maximum number of connections to allow before closing keep-alive connections. * **http2** - `bool` - Enable HTTP/2 support. - * **http2_prior_knowledge** - `bool` - Enforce HTTP/2 usage. """ def __init__( @@ -69,7 +68,6 @@ def __init__( max_keepalive_connections: int = None, keepalive_expiry: float = None, http2: bool = False, - http2_prior_knowledge: bool = False, backend: str = "auto", # Deprecated argument style: max_keepalive: int = None, @@ -85,7 +83,6 @@ def __init__( max_keepalive_connections=max_keepalive_connections, keepalive_expiry=keepalive_expiry, http2=http2, - http2_prior_knowledge=http2_prior_knowledge, backend=backend, max_keepalive=max_keepalive, ) @@ -147,10 +144,7 @@ async def _forward_request( if connection is None: connection = AsyncHTTPConnection( - origin=origin, - http2=self._http2, - http2_prior_knowledge=self._http2_prior_knowledge, - ssl_context=self._ssl_context, + origin=origin, http2=self._http2, ssl_context=self._ssl_context ) await self._add_to_pool(connection, timeout) @@ -202,7 +196,6 @@ async def _tunnel_request( proxy_connection = AsyncHTTPConnection( origin=self.proxy_origin, http2=self._http2, - http2_prior_knowledge=self._http2_prior_knowledge, ssl_context=self._ssl_context, ) @@ -255,7 +248,6 @@ async def _tunnel_request( connection = AsyncHTTPConnection( origin=origin, http2=self._http2, - http2_prior_knowledge=self._http2_prior_knowledge, ssl_context=self._ssl_context, socket=proxy_connection.socket, ) diff --git a/httpcore/_sync/http_proxy.py b/httpcore/_sync/http_proxy.py index 62365afe6..f5576c010 100644 --- a/httpcore/_sync/http_proxy.py +++ b/httpcore/_sync/http_proxy.py @@ -56,7 +56,6 @@ class SyncHTTPProxy(SyncConnectionPool): * **max_keepalive_connections** - `Optional[int]` - The maximum number of connections to allow before closing keep-alive connections. * **http2** - `bool` - Enable HTTP/2 support. - * **http2_prior_knowledge** - `bool` - Enforce HTTP/2 usage. """ def __init__( @@ -69,7 +68,6 @@ def __init__( max_keepalive_connections: int = None, keepalive_expiry: float = None, http2: bool = False, - http2_prior_knowledge: bool = False, backend: str = "sync", # Deprecated argument style: max_keepalive: int = None, @@ -85,7 +83,6 @@ def __init__( max_keepalive_connections=max_keepalive_connections, keepalive_expiry=keepalive_expiry, http2=http2, - http2_prior_knowledge=http2_prior_knowledge, backend=backend, max_keepalive=max_keepalive, ) @@ -147,10 +144,7 @@ def _forward_request( if connection is None: connection = SyncHTTPConnection( - origin=origin, - http2=self._http2, - http2_prior_knowledge=self._http2_prior_knowledge, - ssl_context=self._ssl_context, + origin=origin, http2=self._http2, ssl_context=self._ssl_context ) self._add_to_pool(connection, timeout) @@ -202,7 +196,6 @@ def _tunnel_request( proxy_connection = SyncHTTPConnection( origin=self.proxy_origin, http2=self._http2, - http2_prior_knowledge=self._http2_prior_knowledge, ssl_context=self._ssl_context, ) @@ -255,7 +248,6 @@ def _tunnel_request( connection = SyncHTTPConnection( origin=origin, http2=self._http2, - http2_prior_knowledge=self._http2_prior_knowledge, ssl_context=self._ssl_context, socket=proxy_connection.socket, ) From c4a21de3285a3127d0374a2ae09fdd94c90cace2 Mon Sep 17 00:00:00 2001 From: avy Date: Fri, 26 Mar 2021 08:34:17 +0100 Subject: [PATCH 6/7] use http1 boolean instead of http2_prior_knowledge variable --- httpcore/_async/connection.py | 18 ++++++++---------- httpcore/_async/connection_pool.py | 10 ++++------ httpcore/_sync/connection.py | 18 ++++++++---------- httpcore/_sync/connection_pool.py | 10 ++++------ 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/httpcore/_async/connection.py b/httpcore/_async/connection.py index d8a92c9f6..e39b714ec 100644 --- a/httpcore/_async/connection.py +++ b/httpcore/_async/connection.py @@ -24,7 +24,7 @@ def __init__( self, origin: Origin, http2: bool = False, - http2_prior_knowledge: bool = False, + http1: bool = True, uds: str = None, ssl_context: SSLContext = None, socket: AsyncSocketStream = None, @@ -34,9 +34,7 @@ def __init__( ): self.origin = origin self.http2 = http2 - self.http2_prior_knowledge = http2_prior_knowledge - if http2_prior_knowledge: - self.http2 = True + self.http1 = http1 self.uds = uds self.ssl_context = SSLContext() if ssl_context is None else ssl_context @@ -44,10 +42,12 @@ def __init__( self.local_address = local_address self.retries = retries - if self.http2_prior_knowledge: - self.ssl_context.set_alpn_protocols(["h2"]) - elif 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,12 +147,10 @@ async def _open_socket(self, timeout: TimeoutDict = None) -> AsyncSocketStream: def _create_connection(self, socket: AsyncSocketStream) -> None: http_version = socket.get_http_version() - if self.http2_prior_knowledge: - http_version = "HTTP/2" 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 a1da9c915..d2de9df18 100644 --- a/httpcore/_async/connection_pool.py +++ b/httpcore/_async/connection_pool.py @@ -88,7 +88,7 @@ class AsyncConnectionPool(AsyncHTTPTransport): * **keepalive_expiry** - `Optional[float]` - The maximum time to allow before closing a keep-alive connection. * **http2** - `bool` - Enable HTTP/2 support. - * **http2_prior_knowledge** - `bool` - Enforce HTTP/2 usage. + * **http1** - `bool` - Enable HTTP/1 support, default to True. * **uds** - `str` - Path to a Unix Domain Socket to use instead of TCP sockets. * **local_address** - `Optional[str]` - Local address to connect from. Can also be used to connect using a particular address family. Using @@ -107,7 +107,7 @@ def __init__( max_keepalive_connections: int = None, keepalive_expiry: float = None, http2: bool = False, - http2_prior_knowledge: bool = False, + http1: bool = True, uds: str = None, local_address: str = None, retries: int = 0, @@ -129,9 +129,7 @@ def __init__( self._max_keepalive_connections = max_keepalive_connections self._keepalive_expiry = keepalive_expiry self._http2 = http2 - self._http2_prior_knowledge = http2_prior_knowledge - if http2_prior_knowledge: - self._http2 = True + self._http1 = http1 self._uds = uds self._local_address = local_address self._retries = retries @@ -176,7 +174,7 @@ def _create_connection( return AsyncHTTPConnection( origin=origin, http2=self._http2, - http2_prior_knowledge=self._http2_prior_knowledge, + 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 75b2b537c..387534dcb 100644 --- a/httpcore/_sync/connection.py +++ b/httpcore/_sync/connection.py @@ -24,7 +24,7 @@ def __init__( self, origin: Origin, http2: bool = False, - http2_prior_knowledge: bool = False, + http1: bool = True, uds: str = None, ssl_context: SSLContext = None, socket: SyncSocketStream = None, @@ -34,9 +34,7 @@ def __init__( ): self.origin = origin self.http2 = http2 - self.http2_prior_knowledge = http2_prior_knowledge - if http2_prior_knowledge: - self.http2 = True + self.http1 = http1 self.uds = uds self.ssl_context = SSLContext() if ssl_context is None else ssl_context @@ -44,10 +42,12 @@ def __init__( self.local_address = local_address self.retries = retries - if self.http2_prior_knowledge: - self.ssl_context.set_alpn_protocols(["h2"]) - elif 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,12 +147,10 @@ def _open_socket(self, timeout: TimeoutDict = None) -> SyncSocketStream: def _create_connection(self, socket: SyncSocketStream) -> None: http_version = socket.get_http_version() - if self.http2_prior_knowledge: - http_version = "HTTP/2" 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 320c9f56d..4716dae33 100644 --- a/httpcore/_sync/connection_pool.py +++ b/httpcore/_sync/connection_pool.py @@ -88,7 +88,7 @@ class SyncConnectionPool(SyncHTTPTransport): * **keepalive_expiry** - `Optional[float]` - The maximum time to allow before closing a keep-alive connection. * **http2** - `bool` - Enable HTTP/2 support. - * **http2_prior_knowledge** - `bool` - Enforce HTTP/2 usage. + * **http1** - `bool` - Enable HTTP/1 support, default to True. * **uds** - `str` - Path to a Unix Domain Socket to use instead of TCP sockets. * **local_address** - `Optional[str]` - Local address to connect from. Can also be used to connect using a particular address family. Using @@ -107,7 +107,7 @@ def __init__( max_keepalive_connections: int = None, keepalive_expiry: float = None, http2: bool = False, - http2_prior_knowledge: bool = False, + http1: bool = True, uds: str = None, local_address: str = None, retries: int = 0, @@ -129,9 +129,7 @@ def __init__( self._max_keepalive_connections = max_keepalive_connections self._keepalive_expiry = keepalive_expiry self._http2 = http2 - self._http2_prior_knowledge = http2_prior_knowledge - if http2_prior_knowledge: - self._http2 = True + self._http1 = http1 self._uds = uds self._local_address = local_address self._retries = retries @@ -176,7 +174,7 @@ def _create_connection( return SyncHTTPConnection( origin=origin, http2=self._http2, - http2_prior_knowledge=self._http2_prior_knowledge, + http1=self._http1, uds=self._uds, ssl_context=self._ssl_context, local_address=self._local_address, From 2985d7fd32f12128289c857fb72343960a6b3fb9 Mon Sep 17 00:00:00 2001 From: avy Date: Thu, 6 May 2021 10:08:22 +0200 Subject: [PATCH 7/7] remove RemoteProtocolError when finding http1 string in http2 payload --- httpcore/_async/http2.py | 4 ---- httpcore/_sync/http2.py | 4 ---- 2 files changed, 8 deletions(-) diff --git a/httpcore/_async/http2.py b/httpcore/_async/http2.py index 12f361795..3c7404aa0 100644 --- a/httpcore/_async/http2.py +++ b/httpcore/_async/http2.py @@ -205,10 +205,6 @@ async def receive_events(self, timeout: TimeoutDict) -> None: data = await self.socket.read(self.READ_NUM_BYTES, timeout) if data == b"": raise RemoteProtocolError("Server disconnected") - elif data.find(b"HTTP/1.1") != -1: - raise RemoteProtocolError("HTTP/1.1 received") - elif data.find(b"HTTP/1.0") != -1: - raise RemoteProtocolError("HTTP/1.0 received") events = self.h2_state.receive_data(data) for event in events: diff --git a/httpcore/_sync/http2.py b/httpcore/_sync/http2.py index 505fa318b..fe2d55ebe 100644 --- a/httpcore/_sync/http2.py +++ b/httpcore/_sync/http2.py @@ -205,10 +205,6 @@ def receive_events(self, timeout: TimeoutDict) -> None: data = self.socket.read(self.READ_NUM_BYTES, timeout) if data == b"": raise RemoteProtocolError("Server disconnected") - elif data.find(b"HTTP/1.1") != -1: - raise RemoteProtocolError("HTTP/1.1 received") - elif data.find(b"HTTP/1.0") != -1: - raise RemoteProtocolError("HTTP/1.0 received") events = self.h2_state.receive_data(data) for event in events: