add HTTP caching to /r/ redirect URLs

for #432
pull/1103/head
Ryan Barrett 2024-05-30 15:12:19 -07:00
rodzic 9c4888b02e
commit 11c541b8f0
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
1 zmienionych plików z 11 dodań i 8 usunięć

Wyświetl plik

@ -27,7 +27,7 @@ from oauth_dropins.webutil.util import json_dumps, json_loads
from activitypub import ActivityPub
from common import CACHE_TIME, CONTENT_TYPE_HTML
from flask_app import app, cache
from flask_app import app
from protocol import Protocol
from web import Web
@ -43,7 +43,10 @@ DOMAIN_ALLOWLIST = frozenset((
'bsky.app',
))
VARY_HEADER = {'Vary': 'Accept'}
HEADERS = {
'Vary': 'Accept',
'Cache-Control': f'public, max-age={CACHE_TIME.total_seconds()}'
}
@app.get(r'/r/<path:to>')
def redir(to):
@ -95,7 +98,7 @@ def redir(to):
break
else:
if not accept_as2:
return f'No web user found for any of {domains}', 404, VARY_HEADER
return f'No web user found for any of {domains}', 404, HEADERS
if not accept_as2:
# redirect. include rel-alternate link to make posts discoverable by entering
@ -113,23 +116,23 @@ def redir(to):
</html>
""", 301, {
'Location': to,
**VARY_HEADER,
**HEADERS,
}
# AS2 requested, fetch and convert and serve
proto = Protocol.for_id(to)
if not proto:
return f"Couldn't determine protocol for {to}", 404, VARY_HEADER
return f"Couldn't determine protocol for {to}", 404, HEADERS
obj = proto.load(to)
if not obj or obj.deleted:
return f'Object not found: {to}', 404, VARY_HEADER
return f'Object not found: {to}', 404, HEADERS
# TODO: do this for other protocols too?
if proto == Web and not web_user:
web_user = Web.get_or_create(util.domain_from_link(to), direct=False, obj=obj)
if not web_user:
return f'Object not found: {to}', 404, VARY_HEADER
return f'Object not found: {to}', 404, HEADERS
ret = ActivityPub.convert(obj, from_user=web_user)
# logger.info(f'Returning: {json_dumps(ret, indent=2)}')
@ -138,6 +141,6 @@ def redir(to):
if accept_type == as2.CONTENT_TYPE_LD
else accept_type),
'Access-Control-Allow-Origin': '*',
**VARY_HEADER,
**HEADERS,
}