From 1a679595761be5c4b5075a19ce02906c8a33b71e Mon Sep 17 00:00:00 2001 From: Jace Browning Date: Sat, 1 Apr 2023 18:06:15 -0400 Subject: [PATCH] Add workaround for Sanic request.url bug --- app/utils/meta.py | 7 +++---- app/utils/urls.py | 4 ++++ app/views/images.py | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/utils/meta.py b/app/utils/meta.py index f46d0990a..2c52ebbe1 100644 --- a/app/utils/meta.py +++ b/app/utils/meta.py @@ -1,5 +1,4 @@ from pathlib import Path -from urllib.parse import unquote import aiohttp from aiocache import cached @@ -7,7 +6,7 @@ from sanic.request import Request from .. import settings -from . import http +from . import http, urls def version() -> str: @@ -79,7 +78,7 @@ async def custom_watermarks_allowed(request: Request) -> bool: token = request.args.get("token") if token: logger.info(f"Authenticating with token: {token}") - _url, updated = await tokenize(request, request.url) + _url, updated = await tokenize(request, urls.clean(request.url)) return not updated return False @@ -121,7 +120,7 @@ async def track(request: Request, lines: list[str]): return async with aiohttp.ClientSession() as session: - params = dict(text=text, referer=referer, result=unquote(request.url)) + params = dict(text=text, referer=referer, result=urls.clean(request.url)) logger.info(f"Tracking request: {params}") headers = {"X-API-KEY": _get_api_key(request) or ""} status, message = await http.fetch(api, params=params, headers=headers) diff --git a/app/utils/urls.py b/app/utils/urls.py index f6f8a715b..dace8e9fd 100644 --- a/app/utils/urls.py +++ b/app/utils/urls.py @@ -69,4 +69,8 @@ def clean(url: str) -> str: while "/_." in url: url = url.replace("/_.", ".") + # TODO: Fix Sanic bug? + # https://github.com/jacebrowning/memegen/issues/799 + url = url.replace("::", ":") + return url diff --git a/app/views/images.py b/app/views/images.py index f664a5b68..4a36ea121 100644 --- a/app/views/images.py +++ b/app/views/images.py @@ -223,7 +223,7 @@ async def detail_text(request, template_id, text_filepath): ) return response.redirect(utils.urls.clean(url), status=301) - url, updated = await utils.meta.tokenize(request, request.url) + url, updated = await utils.meta.tokenize(request, utils.urls.clean(request.url)) if updated: return response.redirect(url, status=302)