Skip to content

Commit 57e8c3b

Browse files
committed
Trim excess leading path separators
A URL with excess leading / (path-separator)s would cause urllib3 to attempt to reparse the request-uri as a full URI with a host and port. This bypasses that logic in ConnectionPool.urlopen by replacing these leading /s with just a single /. Closes #6643
1 parent 7a13c04 commit 57e8c3b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/requests/adapters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import os.path
10+
import re
1011
import socket # noqa: F401
1112

1213
from urllib3.exceptions import ClosedPoolError, ConnectTimeoutError
@@ -389,7 +390,7 @@ def request_url(self, request, proxies):
389390
proxy_scheme = urlparse(proxy).scheme.lower()
390391
using_socks_proxy = proxy_scheme.startswith("socks")
391392

392-
url = request.path_url
393+
url = re.sub("^/+", "/", request.path_url)
393394
if is_proxied_http_request and not using_socks_proxy:
394395
url = urldefragauth(request.url)
395396

tests/test_adapters.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import requests.adapters
2+
3+
4+
def test_request_url_trims_leading_path_separators():
5+
"""See also https://github.com/psf/requests/issues/6643."""
6+
a = requests.adapters.HTTPAdapter()
7+
p = requests.Request(method="GET", url="http://127.0.0.1:10000//v:h").prepare()
8+
assert "/v:h" == a.request_url(p, {})

0 commit comments

Comments
 (0)