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

feat: Add Redirect map #1181

Open
wants to merge 5 commits into
base: develop
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
6 changes: 6 additions & 0 deletions src/viur/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,12 @@ class Conf(ConfigType):
}
"""Describing the Script module"""

redirect_map: dict[str, str] = {}
"""Map for redirect Urls"""

redirect_map_advanced_mode: bool = False
"""If True the fnmatch is activated for key match"""

render_html_download_url_expiration: t.Optional[float | int] = None
"""The default duration, for which downloadURLs generated by the html renderer will stay valid"""

Expand Down
17 changes: 17 additions & 0 deletions src/viur/core/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Router:
The basic control flow is
- Setting up internal variables
- Running the Request validators
- Check the Redirect Map
- Emitting the headers (especially the security related ones)
- Run the TLS check (ensure it's a secure connection or check if the URL is whitelisted)
- Load or initialize a new session
Expand Down Expand Up @@ -271,6 +272,22 @@ def _process(self):

path = self.request.path

if conf.redirect_map:
redirect_url: str | None = None
if conf.redirect_map_advanced_mode:
for pattern, target in conf.redirect_map.items():
if fnmatch.fnmatch(path, pattern):
redirect_url = target
break
else:
redirect_url = conf.redirect_map.get(path)

if redirect_url:
self.response.status = "302 Redirect"
self.response.headers["Location"] = redirect_url
self.response.write("")
return

# Add CSP headers early (if any)
if conf.security.content_security_policy and conf.security.content_security_policy["_headerCache"]:
for k, v in conf.security.content_security_policy["_headerCache"].items():
Expand Down
Loading