Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix changing scheme/host in Response.clone() for absolute URLs #8990

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading