From d1532aa466021ca31ffc7a13e2ffc1b6ae03c657 Mon Sep 17 00:00:00 2001 From: Unrud Date: Tue, 18 Jan 2022 18:20:15 +0100 Subject: [PATCH] Extract httputils.redirect --- radicale/app/__init__.py | 6 ++---- radicale/app/get.py | 5 +---- radicale/httputils.py | 6 ++++++ radicale/web/internal.py | 5 +---- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/radicale/app/__init__.py b/radicale/app/__init__.py index 09cc2d39..ca8594ec 100644 --- a/radicale/app/__init__.py +++ b/radicale/app/__init__.py @@ -197,10 +197,8 @@ class Application(ApplicationPartDelete, ApplicationPartHead, location = base_prefix + path logger.info("Redirecting to sanitized path: %r ==> %r", base_prefix + unsafe_path, location) - return response( - client.MOVED_PERMANENTLY, - {"Location": location, "Content-Type": "text/plain"}, - "Redirected to %s" % location) + return response(*httputils.redirect( + location, client.MOVED_PERMANENTLY)) logger.debug("Sanitized path: %r", path) # Get function corresponding to method diff --git a/radicale/app/get.py b/radicale/app/get.py index 3b26815e..ce953244 100644 --- a/radicale/app/get.py +++ b/radicale/app/get.py @@ -62,10 +62,7 @@ class ApplicationPartGet(ApplicationBase): """Manage GET request.""" # Redirect to .web if the root URL is requested if not pathutils.strip_path(path): - location = ".web" - return (client.FOUND, - {"Location": location, "Content-Type": "text/plain"}, - "Redirected to %s" % location) + return httputils.redirect(".web") # Dispatch .web URL to web module if path == "/.web" or path.startswith("/.web/"): return self._web.get(environ, base_prefix, path, user) diff --git a/radicale/httputils.py b/radicale/httputils.py index 000bee78..98c77d4f 100644 --- a/radicale/httputils.py +++ b/radicale/httputils.py @@ -114,3 +114,9 @@ def read_request_body(configuration: "config.Configuration", read_raw_request_body(configuration, environ)) logger.debug("Request content:\n%s", content) return content + + +def redirect(location: str, status: int = client.FOUND) -> types.WSGIResponse: + return (status, + {"Location": location, "Content-Type": "text/plain"}, + "Redirected to %s" % location) diff --git a/radicale/web/internal.py b/radicale/web/internal.py index 7bed5e99..4ee62c81 100644 --- a/radicale/web/internal.py +++ b/radicale/web/internal.py @@ -75,10 +75,7 @@ class Web(web.BaseWeb): path, e, exc_info=True) return httputils.NOT_FOUND if os.path.isdir(filesystem_path) and not path.endswith("/"): - location = posixpath.basename(path) + "/" - return (client.FOUND, - {"Location": location, "Content-Type": "text/plain"}, - "Redirected to %s" % location) + return httputils.redirect(posixpath.basename(path) + "/") if os.path.isdir(filesystem_path): filesystem_path = os.path.join(filesystem_path, "index.html") if not os.path.isfile(filesystem_path):