Skip to content

Commit 3ad38b6

Browse files
Replace deprecated HTTPAdapter.get_connection method with get_connection_with_tls_context (#17536)
1 parent 44ac2aa commit 3ad38b6

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

changelog.d/17536.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace override of deprecated method `HTTPAdapter.get_connection` with `get_connection_with_tls_context`.

scripts-dev/federation_client.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import base64
4444
import json
4545
import sys
46-
from typing import Any, Dict, Optional, Tuple
46+
from typing import Any, Dict, Mapping, Optional, Tuple, Union
4747
from urllib import parse as urlparse
4848

4949
import requests
@@ -75,7 +75,7 @@ def encode_canonical_json(value: object) -> bytes:
7575
value,
7676
# Encode code-points outside of ASCII as UTF-8 rather than \u escapes
7777
ensure_ascii=False,
78-
# Remove unecessary white space.
78+
# Remove unnecessary white space.
7979
separators=(",", ":"),
8080
# Sort the keys of dictionaries.
8181
sort_keys=True,
@@ -298,12 +298,23 @@ def send(
298298

299299
return super().send(request, *args, **kwargs)
300300

301-
def get_connection(
302-
self, url: str, proxies: Optional[Dict[str, str]] = None
301+
def get_connection_with_tls_context(
302+
self,
303+
request: PreparedRequest,
304+
verify: Optional[Union[bool, str]],
305+
proxies: Optional[Mapping[str, str]] = None,
306+
cert: Optional[Union[Tuple[str, str], str]] = None,
303307
) -> HTTPConnectionPool:
304-
# overrides the get_connection() method in the base class
305-
parsed = urlparse.urlsplit(url)
306-
(host, port, ssl_server_name) = self._lookup(parsed.netloc)
308+
# overrides the get_connection_with_tls_context() method in the base class
309+
parsed = urlparse.urlsplit(request.url)
310+
311+
# Extract the server name from the request URL, and ensure it's a str.
312+
hostname = parsed.netloc
313+
if isinstance(hostname, bytes):
314+
hostname = hostname.decode("utf-8")
315+
assert isinstance(hostname, str)
316+
317+
(host, port, ssl_server_name) = self._lookup(hostname)
307318
print(
308319
f"Connecting to {host}:{port} with SNI {ssl_server_name}", file=sys.stderr
309320
)

0 commit comments

Comments
 (0)