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
This commit is contained in:
Ben Busby 2023-01-30 12:13:46 -07:00
parent e318594d9b
commit 12ce174b9a
No known key found for this signature in database
GPG Key ID: B9B7231E01D924A1

View File

@ -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