Redirect old % URLs to new - encoded URLs, closes #1650

Refs #1439
dash-encoding
Simon Willison 2022-03-07 08:01:03 -08:00
rodzic 32548b88fd
commit eeb58a09ee
2 zmienionych plików z 13 dodań i 0 usunięć

Wyświetl plik

@ -1211,6 +1211,13 @@ class DatasetteRouter:
return await self.handle_404(request, send)
async def handle_404(self, request, send, exception=None):
# If path contains % encoding, redirect to dash encoding
if "%" in request.path:
# Try the same path but with "%" replaced by "-"
# and "-" replaced with "-2D"
new_path = request.path.replace("-", "-2D").replace("%", "-")
await asgi_send_redirect(send, new_path)
return
# If URL has a trailing slash, redirect to URL without it
path = request.scope.get(
"raw_path", request.scope["path"].encode("utf8")

Wyświetl plik

@ -952,3 +952,9 @@ def test_no_alternate_url_json(app_client, path):
assert (
'<link rel="alternate" type="application/json+datasette"' not in response.text
)
def test_redirect_percent_encoding_to_dash_encoding(app_client):
response = app_client.get("/fivethirtyeight/twitter-ratio%2Fsenators")
assert response.status == 302
assert response.headers["location"] == "/fivethirtyeight/twitter-2Dratio-2Fsenators"