| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from importlib.metadata import version |
| 4 |
|
| 5 |
__all__ = [ |
| 6 |
"inject_into_urllib3", |
| 7 |
"extract_from_urllib3", |
| 8 |
] |
| 9 |
|
| 10 |
import typing |
| 11 |
|
| 12 |
orig_HTTPSConnection: typing.Any = None |
| 13 |
|
| 14 |
|
| 15 |
def inject_into_urllib3() -> None: |
| 16 |
# First check if h2 version is valid |
| 17 |
h2_version = version("h2") |
| 18 |
if not h2_version.startswith("4."): |
| 19 |
raise ImportError( |
| 20 |
"urllib3 v2 supports h2 version 4.x.x, currently " |
| 21 |
f"the 'h2' module is compiled with {h2_version!r}. " |
| 22 |
"See: https://github.com/urllib3/urllib3/issues/3290" |
| 23 |
) |
| 24 |
|
| 25 |
# Import here to avoid circular dependencies. |
| 26 |
from .. import connection as urllib3_connection |
| 27 |
from .. import util as urllib3_util |
| 28 |
from ..connectionpool import HTTPSConnectionPool |
| 29 |
from ..util import ssl_ as urllib3_util_ssl |
| 30 |
from .connection import HTTP2Connection |
| 31 |
|
| 32 |
global orig_HTTPSConnection |
| 33 |
orig_HTTPSConnection = urllib3_connection.HTTPSConnection |
| 34 |
|
| 35 |
HTTPSConnectionPool.ConnectionCls = HTTP2Connection |
| 36 |
urllib3_connection.HTTPSConnection = HTTP2Connection # type: ignore[misc] |
| 37 |
|
| 38 |
# TODO: Offer 'http/1.1' as well, but for testing purposes this is handy. |
| 39 |
urllib3_util.ALPN_PROTOCOLS = ["h2"] |
| 40 |
urllib3_util_ssl.ALPN_PROTOCOLS = ["h2"] |
| 41 |
|
| 42 |
|
| 43 |
def extract_from_urllib3() -> None: |
| 44 |
from .. import connection as urllib3_connection |
| 45 |
from .. import util as urllib3_util |
| 46 |
from ..connectionpool import HTTPSConnectionPool |
| 47 |
from ..util import ssl_ as urllib3_util_ssl |
| 48 |
|
| 49 |
HTTPSConnectionPool.ConnectionCls = orig_HTTPSConnection |
| 50 |
urllib3_connection.HTTPSConnection = orig_HTTPSConnection # type: ignore[misc] |
| 51 |
|
| 52 |
urllib3_util.ALPN_PROTOCOLS = ["http/1.1"] |
| 53 |
urllib3_util_ssl.ALPN_PROTOCOLS = ["http/1.1"] |