From 12ce174b9a6a7aaa57b2c0ea9b299ce11bc9842e Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Mon, 30 Jan 2023 12:13:46 -0700 Subject: [PATCH] Include url prefix for reverse proxied instances The url prefix was not included when reconstructing the root url using X-Forwarded-* headers, causing some elements to fail to load properly. Fixes #937 --- app/utils/misc.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/utils/misc.py b/app/utils/misc.py index 9a1b471..8066725 100644 --- a/app/utils/misc.py +++ b/app/utils/misc.py @@ -2,6 +2,7 @@ from bs4 import BeautifulSoup as bsoup from flask import Request import hashlib import os +import re from requests import exceptions, get from urllib.parse import urlparse @@ -40,8 +41,16 @@ def get_request_url(url: str) -> str: def get_proxy_host_url(r: Request, default: str, root=False) -> str: scheme = r.headers.get('X-Forwarded-Proto', 'https') http_host = r.headers.get('X-Forwarded-Host') + + full_path = r.full_path if not root else '' + if full_path.startswith('/'): + full_path = f'/{full_path}' + if http_host: - return f'{scheme}://{http_host}{r.full_path if not root else "/"}' + prefix = os.environ.get('WHOOGLE_URL_PREFIX', '') + if prefix: + prefix = f'/{re.sub("[^0-9a-zA-Z]+", "", prefix)}' + return f'{scheme}://{http_host}{prefix}{full_path}' return default