diff options
| author | Magnus Hagander | 2022-10-05 15:19:26 +0000 |
|---|---|---|
| committer | Magnus Hagander | 2022-10-05 15:19:26 +0000 |
| commit | c87e07feab9a400f6d971d042b61ccb762e3e932 (patch) | |
| tree | a8d05717b4b167d0cdb97c59c8261b6c446fbace | |
| parent | 11f6abc833dcaa2dcfe647c5d4077220a7b7dc2d (diff) | |
Properly tag 404s from redirector with xkeys
If the 404s are beceause an item is hidden, we need them to be tagged
with the appropriate xkeys so we can automatically unhide them.
| -rwxr-xr-x | redirector/redirector.py | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/redirector/redirector.py b/redirector/redirector.py index d3bb83f..666d57d 100755 --- a/redirector/redirector.py +++ b/redirector/redirector.py @@ -56,7 +56,7 @@ def application(environ, start_response): # bother with any connection pooling. conn = psycopg2.connect(connstr) c = conn.cursor() - c.execute("SELECT link, feed FROM posts WHERE id=%(id)s AND NOT hidden", { + c.execute("SELECT link, feed, hidden FROM posts WHERE id=%(id)s", { 'id': id }) r = c.fetchall() @@ -66,19 +66,30 @@ def application(environ, start_response): if len(r) != 1: raise Make404() - # We have a link, return a redirect to it - start_response('301 Moved Permanently', [ - ('Content-type', 'text/html'), - ('Location', r[0][0]), - ('X-Planet', str(id)), - ('X-Planet-Feed', str(r[0][1])), - ('xkey', 'post_{} feed_{}'.format(id, r[0][1])), - ]) - return [ - b"<html>\n<head>\n<title>postgr.es</title>\n</head>\n<body>\n", - b"<a href=\"%s\">moved here</a>\n" % r[0][0].encode('utf8'), - b"</body>\n</html>\n" - ] + if r[0][2]: + # Entry is hidden. We want a 404 that is tagged with the feed, so we can xkey + # purge it properly! + start_response('404 Not Found', [ + ('Content-type', 'text/plain'), + ('X-Planet', str(id)), + ('X-Planet-Feed', str(r[0][1])), + ('xkey', 'post_{} feed_{}'.format(id, r[0][1])), + ]) + return [b"Link not found\n"] + else: + # We have a link, return a redirect to it + start_response('301 Moved Permanently', [ + ('Content-type', 'text/html'), + ('Location', r[0][0]), + ('X-Planet', str(id)), + ('X-Planet-Feed', str(r[0][1])), + ('xkey', 'post_{} feed_{}'.format(id, r[0][1])), + ]) + return [ + b"<html>\n<head>\n<title>postgr.es</title>\n</head>\n<body>\n", + b"<a href=\"%s\">moved here</a>\n" % r[0][0].encode('utf8'), + b"</body>\n</html>\n" + ] except Make404: start_response('404 Not Found', [ ('Content-type', 'text/plain'), |
