diff --git a/.htaccess b/.htaccess index 33564f1..f1e9744 100644 --- a/.htaccess +++ b/.htaccess @@ -8,9 +8,6 @@ RewriteRule ^api(/.*)?$ /index.php?api=$1 [L,QSA] RewriteEngine On RewriteOptions AllowNoSlash -RewriteRule ^((\.idea|\.git|src|test|core|static)(/.*)?)$ /index.php?site=$1 [L,QSA] +RewriteRule ^((\.idea|\.git|src|test|core)(/.*)?)$ /index.php?site=$1 [L,QSA] -RewriteEngine On -RewriteCond %{REQUEST_FILENAME} !-f -RewriteCond %{REQUEST_FILENAME} !-d -RewriteRule ^(.*)$ /index.php?site=$1 [L,QSA] \ No newline at end of file +FallbackResource /index.php \ No newline at end of file diff --git a/core/core.php b/core/core.php index f229914..d7b2f92 100644 --- a/core/core.php +++ b/core/core.php @@ -108,7 +108,7 @@ function serveStatic(string $webRoot, string $file) { $pathInfo = pathinfo($path); - // maybe I will allow more later… + // TODO: add more file extensions here $allowedExtension = array("html", "htm"); $ext = $pathInfo["extension"] ?? ""; if (!in_array($ext, $allowedExtension)) { @@ -116,7 +116,44 @@ function serveStatic(string $webRoot, string $file) { return "Access restricted: Extension '" . htmlspecialchars($ext) . "' not allowed."; } + $size = filesize($path); $mimeType = mime_content_type($path); - header("Content-Type: $mimeType"); - readfile($path); + header("Content-Type: $mimeType"); // TODO: do we need to check mime type? + header("Content-Length: $size"); + header('Accept-Ranges: bytes'); + + if (strcasecmp($_SERVER["REQUEST_METHOD"], "HEAD") !== 0) { + $bufferSize = 1024*16; + $handle = fopen($path, "rb"); + if($handle === false) { + http_response_code(500); + return "Unable to read file: " . htmlspecialchars($path); + } + + $offset = 0; + $length = $size; + + if (isset($_SERVER['HTTP_RANGE'])) { + $partialContent = true; + preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches); + $offset = intval($matches[1]); + $length = intval($matches[2]) - $offset; + http_response_code(206); + header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $size); + } + + if ($offset > 0) { + fseek($handle, $offset); + } + + $bytesRead = 0; + while (!feof($handle) && $bytesRead < $length) { + $chunkSize = min($length - $bytesRead, $bufferSize); + echo fread($handle, $chunkSize); + } + + fclose($handle); + } + + return ""; } diff --git a/index.php b/index.php index 5136212..a3c221f 100644 --- a/index.php +++ b/index.php @@ -74,9 +74,13 @@ if(isset($_GET["api"]) && is_string($_GET["api"])) { } } } else { - $documentName = $_GET["site"] ?? "/"; + $requestedUri = $_GET["site"] ?? $_SERVER["REQUEST_URI"]; + if (startsWith($requestedUri, "/")) { + $requestedUri = substr($requestedUri, 1); + } + if ($installation) { - if ($documentName !== "" && $documentName !== "index.php") { + if ($requestedUri !== "" && $requestedUri !== "index.php") { $response = "Redirecting to /"; header("Location: /"); } else { @@ -86,7 +90,7 @@ if(isset($_GET["api"]) && is_string($_GET["api"])) { } else { $req = new \Api\Routes\Find($user); - $success = $req->execute(array("request" => $documentName)); + $success = $req->execute(array("request" => $requestedUri)); $response = ""; if (!$success) { http_response_code(500);