Skip to content

Commit 8220ced

Browse files
[PR #9309/e4028333 backport][3.10] Fix building the URL in BaseRequest when the host contains a port or IPv6 address (#9318)
Co-authored-by: J. Nick Koston <[email protected]> fixes #9307
1 parent d32d580 commit 8220ced

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

CHANGES/9309.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed assembling the :class:`~yarl.URL` for web requests when the host contains a non-default port or IPv6 address -- by :user:`bdraco`.

aiohttp/web_request.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ def host(self) -> str:
431431
- overridden value by .clone(host=new_host) call.
432432
- HOST HTTP header
433433
- socket.getfqdn() value
434+
435+
For example, 'example.com' or 'localhost:8080'.
436+
437+
For historical reasons, the port number may be included.
434438
"""
435439
host = self._message.headers.get(hdrs.HOST)
436440
if host is not None:
@@ -454,8 +458,10 @@ def remote(self) -> Optional[str]:
454458

455459
@reify
456460
def url(self) -> URL:
457-
url = URL.build(scheme=self.scheme, host=self.host)
458-
return url.join(self._rel_url)
461+
"""The full URL of the request."""
462+
# authority is used here because it may include the port number
463+
# and we want yarl to parse it correctly
464+
return URL.build(scheme=self.scheme, authority=self.host).join(self._rel_url)
459465

460466
@reify
461467
def path(self) -> str:

tests/test_web_request.py

+10
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,16 @@ def test_url_url() -> None:
526526
assert URL("http://example.com/path") == req.url
527527

528528

529+
def test_url_non_default_port() -> None:
530+
req = make_mocked_request("GET", "/path", headers={"HOST": "example.com:8123"})
531+
assert req.url == URL("http://example.com:8123/path")
532+
533+
534+
def test_url_ipv6() -> None:
535+
req = make_mocked_request("GET", "/path", headers={"HOST": "[::1]:8123"})
536+
assert req.url == URL("http://[::1]:8123/path")
537+
538+
529539
def test_clone() -> None:
530540
req = make_mocked_request("GET", "/path")
531541
req2 = req.clone()

0 commit comments

Comments
 (0)