From 5f082349d56b628b200c9ff3d9eed5498dd424cf Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Mon, 26 Dec 2022 09:52:08 -0800 Subject: [PATCH] /r/ handler: include Accept header in flask cache key requires pending bug fix https://github.com/pallets-eco/flask-caching/pull/431 --- redirect.py | 12 ++++++++++++ requirements.txt | 2 +- tests/test_redirect.py | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/redirect.py b/redirect.py index 9e5536c..5a7c118 100644 --- a/redirect.py +++ b/redirect.py @@ -90,6 +90,18 @@ def redir(to): logger.info(f'redirecting to {to}') return redirect(to, code=301) +# offically-supported-monkey-patch flask_caching to include Accept header in +# cache key: +# https://flask-caching.readthedocs.io/en/latest/api.html#flask_caching.Cache.cached +# requires this pending bug fix: +# https://github.com/pallets-eco/flask-caching/pull/431 +orig_cache_key = redir.make_cache_key + +def accept_header_cache_key(*args, **kwargs): + return f'{orig_cache_key(*args, **kwargs)} {request.headers.get("Accept")}' + +redir.make_cache_key = accept_header_cache_key + def convert_to_as2(url, domain): """Fetch a URL as HTML, convert it to AS2, and return it. diff --git a/requirements.txt b/requirements.txt index 386dd99..aabc7d6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ git+https://github.com/snarfed/oauth-dropins.git#egg=oauth_dropins git+https://github.com/snarfed/granary.git#egg=granary git+https://github.com/snarfed/negotiator.git@py3#egg=negotiator git+https://github.com/dvska/gdata-python3.git#egg=gdata +git+https://github.com/snarfed/flask-caching.git@fix-97#egg=Flask-Caching beautifulsoup4==4.11.1 brevity==0.2.17 @@ -20,7 +21,6 @@ feedgen==0.9.0 feedparser==6.0.10 fixtures==4.0.1 Flask==2.2.2 -Flask-Caching==2.0.1 flask-gae-static==1.0 google-api-core==2.11.0 google-auth==2.15.0 diff --git a/tests/test_redirect.py b/tests/test_redirect.py index c6d988f..a7783eb 100644 --- a/tests/test_redirect.py +++ b/tests/test_redirect.py @@ -6,6 +6,7 @@ from unittest.mock import patch from granary import as2 from oauth_dropins.webutil.testutil import requests_response +from app import app, cache import common from models import User from .test_webmention import REPOST_HTML, REPOST_AS2 @@ -50,6 +51,22 @@ class RedirectTest(testutil.TestCase): def test_as2_ld(self): self._test_as2(common.CONTENT_TYPE_AS2_LD) + def test_accept_header_cache_key(self): + app.config['CACHE_TYPE'] = 'SimpleCache' + cache.init_app(app) + self.client = app.test_client() + + got = self.client.get('/r/https://foo.com/bar') + self.assertEqual(301, got.status_code) + self.assertEqual('https://foo.com/bar', got.headers['Location']) + + self._test_as2(common.CONTENT_TYPE_AS2) + + got = self.client.get('/r/https://foo.com/bar', + headers={'Accept': 'text/html'}) + self.assertEqual(301, got.status_code) + self.assertEqual('https://foo.com/bar', got.headers['Location']) + @patch('requests.get') def _test_as2(self, accept, mock_get): """Currently mainly for Pixelfed.