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

Expose redirect path in RequestRedirect #3001

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Version 3.2.0

Unreleased

- The ``RequestRedirect`` exception now exposes ``new_path`` that
contains the request path used to compute the ``new_url``.

Version 3.1.2
-------------
Expand Down
3 changes: 2 additions & 1 deletion src/werkzeug/routing/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ class RequestRedirect(HTTPException, RoutingException):

code = 308

def __init__(self, new_url: str) -> None:
def __init__(self, new_url: str, new_path: t.Optional[str] = None) -> None:
super().__init__(new_url)
self.new_url = new_url
self.new_path = new_path

def get_response(
self,
Expand Down
21 changes: 14 additions & 7 deletions src/werkzeug/routing/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,8 @@ def match(
# safe = https://url.spec.whatwg.org/#url-path-segment-string
new_path = quote(e.path_info, safe="!$&'()*+,/:;=@")
raise RequestRedirect(
self.make_redirect_url(new_path, query_args)
self.make_redirect_url(new_path, query_args),
new_path,
) from None
except RequestAliasRedirect as e:
raise RequestRedirect(
Expand All @@ -617,7 +618,8 @@ def match(
e.matched_values,
method,
query_args,
)
),
path_part,
) from None
except NoMatch as e:
if e.have_match_for:
Expand All @@ -631,9 +633,11 @@ def match(
rule, rv = result

if self.map.redirect_defaults:
redirect_url = self.get_default_redirect(rule, method, rv, query_args)
(redirect_url, redirect_path) = self.get_default_redirect(
rule, method, rv, query_args
)
if redirect_url is not None:
raise RequestRedirect(redirect_url)
raise RequestRedirect(redirect_url, redirect_path)

if rule.redirect_to is not None:
if isinstance(rule.redirect_to, str):
Expand All @@ -655,7 +659,8 @@ def _handle_match(match: t.Match[str]) -> str:
urljoin(
f"{self.url_scheme or 'http'}://{netloc}{self.script_name}",
redirect_url,
)
),
redirect_url,
)

if return_rule:
Expand Down Expand Up @@ -736,8 +741,10 @@ def get_default_redirect(
if r.provides_defaults_for(rule) and r.suitable_for(values, method):
values.update(r.defaults) # type: ignore
domain_part, path = r.build(values) # type: ignore
return self.make_redirect_url(path, query_args, domain_part=domain_part)
return None
return self.make_redirect_url(
path, query_args, domain_part=domain_part
), path
return None, None

def encode_query_args(self, query_args: t.Mapping[str, t.Any] | str) -> str:
if not isinstance(query_args, str):
Expand Down
Loading