From d290b84933dc9c94e4c47b91bf092dd51456448b Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Fri, 25 Dec 2020 16:15:51 +0100 Subject: [PATCH] Generate proper 404's in redirector Instead of generating a 500 error, because that is both wrong and will prevent caching. --- redirector/redirector.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/redirector/redirector.py b/redirector/redirector.py index d6dcf87..cb9f141 100755 --- a/redirector/redirector.py +++ b/redirector/redirector.py @@ -18,10 +18,16 @@ _urlvalmap = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', ' connstr = "" +class Make404(Exception): + pass + + def iddecode(idstr): idval = 0 for c in idstr: idval *= 64 + if c not in _urlvalmap: + raise Make404() idval += _urlvalmap.index(c) return idval @@ -52,10 +58,7 @@ def application(environ, start_response): conn.close() if len(r) != 1: - start_response('404 Not Found', [ - ('Content-type', 'text/plain'), - ]) - return [b"Link not found\n"] + raise Make404() # We have a link, return a redirect to it start_response('301 Moved Permanently', [ @@ -68,6 +71,11 @@ def application(environ, start_response): b"moved here\n" % r[0][0].encode('utf8'), b"\n\n" ] + except Make404: + start_response('404 Not Found', [ + ('Content-type', 'text/plain'), + ]) + return [b"Link not found\n"] except Exception as ex: start_response('500 Internal Server Error', [ ('Content-type', 'text/plain') -- 2.39.5