From b268a12e2d2fe50b4fe8af967c1deda2295f4dc9 Mon Sep 17 00:00:00 2001 From: Daniel Aleksandersen Date: Thu, 14 Apr 2022 04:18:16 +0200 Subject: [PATCH] Add an optional redirect(cache=sec) argument The argument sets an optional Cache-Control header for redirects. Allows for more fined-grained control over browser caching for known short- and long- lived redirects. --- bottle.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bottle.py b/bottle.py index 48aefbb6..c8a512fa 100755 --- a/bottle.py +++ b/bottle.py @@ -2813,13 +2813,15 @@ def abort(code=500, text='Unknown Error.'): raise HTTPError(code, text) -def redirect(url, code=None): +def redirect(url, code=None, cache=None): """ Aborts execution and causes a 303 or 302 redirect, depending on the HTTP protocol version. """ if not code: code = 303 if request.get('SERVER_PROTOCOL') == "HTTP/1.1" else 302 res = response.copy(cls=HTTPResponse) res.status = code + if type(cache) is int: + res.set_header('Cache-Control', 'max-age=%s' % cache) res.body = "" res.set_header('Location', urljoin(request.url, url)) raise res