Verified Commit 1dff477e authored by Max Rees's avatar Max Rees
Browse files

cgi: work around a bug in NetSurf's <BASE HREF> support

It must have a hostname otherwise it will be ignored. We will at least
assume HTTP_HOST is present instead of trying to fall back on
SERVER_NAME and SERVER_PORT for simplicity sake. NetSurf also has the
peculiar behavior that a protocol-relative BASE HREF URI is rewritten to
be HTTP even if the document URI is HTTPS, but whatever.
parent c11bc22d
......@@ -337,20 +337,26 @@ class APKVitrineApp: # pylint: disable=too-many-instance-attributes
self.data = Path(self.conf[apkvitrine.DEFAULT]["data_dir"])
self._response = self.base = self.env = self.path = self.query = None
self._response = self.env = None
self.base = self.path = self.query = None
def get_host(self):
host = self.env.get("HTTP_HOST", "")
return f"//{host}/" if host else ""
def handle(self, env, response):
self.env = env
self._response = response
self.base = Path(env.get("SCRIPT_NAME", ""))
self.base = self.get_host() + env.get("SCRIPT_NAME", "").lstrip("/")
self.base = self.base.rstrip("/") + "/"
self.path = Path(env.get("PATH_INFO", "").lstrip("/"))
self.query = urllib.parse.parse_qs(env.get("QUERY_STRING", ""))
self.query = {i: j[-1] for i, j in self.query.items()}
self.jinja.globals["base"] = self.base
# Used for pagination on search pages so that "page=x" isn't repeated
self.jinja.globals["request"] = str(self.base / self.path) + "?"
self.jinja.globals["request"] = self.base + str(self.path) + "?"
self.jinja.globals["request"] += urllib.parse.urlencode(
{i: j for i, j in self.query.items() if i != "page"}
)
......@@ -386,7 +392,7 @@ class APKVitrineApp: # pylint: disable=too-many-instance-attributes
if self.query.get("purge") == "1":
if (time.time() - cache.stat().st_mtime) > 600:
cache.unlink()
return self.redirect(self.base / self.path)
return self.redirect(self.path)
return None
......@@ -425,7 +431,7 @@ class APKVitrineApp: # pylint: disable=too-many-instance-attributes
def redirect(self, location):
self.response(
http.HTTPStatus.TEMPORARY_REDIRECT,
headers=[("Location", str(self.base / location))],
headers=[("Location", self.base + str(location))],
)
return []
......
......@@ -82,12 +82,12 @@
<html>
<head>
<title>{{ title }}</title>
<base href="{{ base }}/">
<base href="{{ base }}">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="//static.adelielinux.org/css/bulma.min.css" type="text/css"/>
<link rel="stylesheet" href="//static.adelielinux.org/css/adelie-nouveau.css" type="text/css"/>
<link rel="stylesheet" href="//static.adelielinux.org/font/font-amal.css" type="text/css"/>
<link rel="stylesheet" href="{{ base }}/static/style.css" type="text/css">
<link rel="stylesheet" href="{{ base }}static/style.css" type="text/css">
</head>
<body>
<nav class="navbar is-dark" aria-label="Main menu">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment