From 972c0879c3fafbca9a88a8edb082db4bd3343b82 Mon Sep 17 00:00:00 2001 From: "codeberg.org" Date: Mon, 4 May 2020 20:15:28 +0200 Subject: [PATCH] avoid 302 redirect, rewrite URL for path with guessed index.html instead --- var/www/pages/index.php | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/var/www/pages/index.php b/var/www/pages/index.php index 9c56041..5cf38b7 100644 --- a/var/www/pages/index.php +++ b/var/www/pages/index.php @@ -47,18 +47,12 @@ if (end($parts) === '') { array_shift($parts); } -if (sizeof($parts) === 0 || strpos(end($parts), ".") === false) { - $h = "Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; - if (substr($h, -1) != "/") { - $h .= "/"; - } - $h .= "index.html"; - header($h); - exit(); -} - $file_url = implode("/", $parts); +if ($file_url === "") { + $file_url = "index.html"; +} + $ext = pathinfo($file_url, PATHINFO_EXTENSION); $ext = strtolower($ext); @@ -91,14 +85,19 @@ $command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:$file_url'\""; ## which seems wasteful, but it seems exec+echo cannot do raw binary output? Is this true? exec($command, $output, $retval); if ($retval != 0) { - # Render user-provided 404.html if exists. - $command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:404.html'\""; + # Try to append index.html to path, in case it was an abbreviated folder. Whatever happens from now on, we will always render HTML: + header("Content-Type: text/html"); + $command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:$file_url/index.html'\""; exec($command, $output, $retval); if ($retval != 0) { - send_response(404 , "no such file in repo: '" . htmlspecialchars($file_url) . "'"); + # Render user-provided 404.html if exists, generic 404 message if not: + $command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:404.html'\""; + exec($command, $output, $retval); + if ($retval != 0) { + send_response(404 , "no such file in repo: '" . htmlspecialchars($file_url) . "'"); + } + http_response_code(404); } - http_response_code(404); - header("Content-Type: text/html"); } ## If we could directly exec+echo raw output from above, we wouldn't need to execute command twice: