Serve Static + Routing improvement
This commit is contained in:
parent
106fae305e
commit
0f21a6941d
@ -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]
|
||||
FallbackResource /index.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 "<b>Access restricted:</b> 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 "<b>Unable to read file:</b> " . 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 "";
|
||||
}
|
||||
|
10
index.php
10
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 <a href=\"/\">/</a>";
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user