Skip to content

Commit

Permalink
Fix changing scheme/host in Response.clone() for absolute URLs (#8990)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored Sep 2, 2024
1 parent 1ba3011 commit 731ba4d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/8990.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed changing scheme/host in ``Response.clone()`` for absolute URLs -- by :user:`Dreamsorcerer`.
12 changes: 8 additions & 4 deletions aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ def __init__(
self._cache: Dict[str, Any] = {}
url = message.url
if url.is_absolute():
if scheme is not None:
url = url.with_scheme(scheme)
if host is not None:
url = url.with_host(host)
# absolute URL is given,
# override auto-calculating url, host, and scheme
# all other properties should be good
Expand All @@ -186,6 +190,10 @@ def __init__(
self._rel_url = url.relative()
else:
self._rel_url = message.url
if scheme is not None:
self._cache["scheme"] = scheme
if host is not None:
self._cache["host"] = host
self._post: Optional[MultiDictProxy[Union[str, bytes, FileField]]] = None
self._read_bytes: Optional[bytes] = None

Expand All @@ -199,10 +207,6 @@ def __init__(
self._transport_sslcontext = transport.get_extra_info("sslcontext")
self._transport_peername = transport.get_extra_info("peername")

if scheme is not None:
self._cache["scheme"] = scheme
if host is not None:
self._cache["host"] = host
if remote is not None:
self._cache["remote"] = remote

Expand Down
21 changes: 21 additions & 0 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ def test_absolute_url() -> None:
assert req.rel_url == URL.build(path="/path/to", query={"a": "1"})


def test_clone_absolute_scheme() -> None:
req = make_mocked_request("GET", "https://example.com/path/to?a=1")
assert req.scheme == "https"
req2 = req.clone(scheme="http")
assert req2.scheme == "http"
assert req2.url.scheme == "http"


def test_clone_absolute_host() -> None:
req = make_mocked_request("GET", "https://example.com/path/to?a=1")
assert req.host == "example.com"
req2 = req.clone(host="foo.test")
assert req2.host == "foo.test"
assert req2.url.host == "foo.test"


def test_content_length() -> None:
req = make_mocked_request("Get", "/", CIMultiDict([("CONTENT-LENGTH", "123")]))

Expand Down Expand Up @@ -707,18 +723,23 @@ def test_save_state_on_clone() -> None:

def test_clone_scheme() -> None:
req = make_mocked_request("GET", "/")
assert req.scheme == "http"
req2 = req.clone(scheme="https")
assert req2.scheme == "https"
assert req2.url.scheme == "https"


def test_clone_host() -> None:
req = make_mocked_request("GET", "/")
assert req.host != "example.com"
req2 = req.clone(host="example.com")
assert req2.host == "example.com"
assert req2.url.host == "example.com"


def test_clone_remote() -> None:
req = make_mocked_request("GET", "/")
assert req.remote != "11.11.11.11"
req2 = req.clone(remote="11.11.11.11")
assert req2.remote == "11.11.11.11"

Expand Down

0 comments on commit 731ba4d

Please sign in to comment.