From 366f7c545977d19544143fb2928531caccbe2d94 Mon Sep 17 00:00:00 2001 From: Cole Green <17110935+nox7@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:05:27 -0500 Subject: [PATCH 1/5] Move content-type above --- src/Router/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Router/Router.php b/src/Router/Router.php index 1e4fb33..f00ba28 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -79,6 +79,7 @@ public function processRequestAsStaticFile(): void{ * setting for that mime type. */ $cacheTime = $this->noxInstance->staticFileHandler->getCacheTimeForMime($mimeType); + header("content-type: $mimeType"); if ($cacheTime !== null) { header(sprintf("cache-control: max-age=%d", $cacheTime)); @@ -105,7 +106,6 @@ public function processRequestAsStaticFile(): void{ } $fileContents = file_get_contents(realpath($staticFilePath)); - header("content-type: $mimeType"); header("content-length: " . strlen($fileContents)); // Only output for GET methods and not HEAD From c4a2808b36348398fa9782e187e3efbdf7883cee Mon Sep 17 00:00:00 2001 From: Cole Green <17110935+nox7@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:05:40 -0500 Subject: [PATCH 2/5] Change cache-control to hint must revalidate --- src/Router/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Router/Router.php b/src/Router/Router.php index f00ba28..00b6468 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -82,8 +82,8 @@ public function processRequestAsStaticFile(): void{ header("content-type: $mimeType"); if ($cacheTime !== null) { - header(sprintf("cache-control: max-age=%d", $cacheTime)); header("Vary: If-None-Match, etag, last-modified, cache-control"); + header(sprintf("cache-control: max-age=%d; must-revalidate", $cacheTime)); $lastModifiedTime = filemtime($staticFilePath); if ($lastModifiedTime !== false){ From 2212f57a9a177e45b1c57b78d51bf43fc5dedd55 Mon Sep 17 00:00:00 2001 From: Cole Green <17110935+nox7@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:05:48 -0500 Subject: [PATCH 3/5] Change vary to asterisk --- src/Router/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Router/Router.php b/src/Router/Router.php index 00b6468..6d88112 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -82,8 +82,8 @@ public function processRequestAsStaticFile(): void{ header("content-type: $mimeType"); if ($cacheTime !== null) { - header("Vary: If-None-Match, etag, last-modified, cache-control"); header(sprintf("cache-control: max-age=%d; must-revalidate", $cacheTime)); + header("vary: *"); $lastModifiedTime = filemtime($staticFilePath); if ($lastModifiedTime !== false){ From 29fe64829fac7463f0daa6b49895f30188fe2dd0 Mon Sep 17 00:00:00 2001 From: Cole Green <17110935+nox7@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:05:59 -0500 Subject: [PATCH 4/5] Do not end HEAD transmission before content-length --- src/Router/Router.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Router/Router.php b/src/Router/Router.php index 6d88112..9fd4a4e 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -96,11 +96,10 @@ public function processRequestAsStaticFile(): void{ // Check if the client sent an "If-None-Match" header with the same etag used above // If so, simply respond with 304 Not Modified and exit. // Else, don't exit - $ifNoneMatch = Request::getFirstHeaderValue("If-None-Match"); + $ifNoneMatch = $this->currentRequest->getHeaderValue("If-None-Match"); if ((string) $lastModifiedTime === $ifNoneMatch){ - // Etags match, no need to send file. It's not stale + // Etags match, set 304 status http_response_code(304); - exit(); } } } From f8e46e65bd5585a64219bf5c5dc7ce9381dd5c09 Mon Sep 17 00:00:00 2001 From: Cole Green <17110935+nox7@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:16:14 -0500 Subject: [PATCH 5/5] Test --- example/nox-request.php | 2 +- example/src/HomeController.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/example/nox-request.php b/example/nox-request.php index d834704..48e614f 100644 --- a/example/nox-request.php +++ b/example/nox-request.php @@ -34,7 +34,7 @@ $nox->mapExtensionToMimeType("svg", "image/svg+xml"); // Mime caches - $nox->addCacheTimeForMime("image/png", 86400 * 60); + $nox->addCacheTimeForMime("image/png", 600); $nox->addCacheTimeForMime("image/jpeg", 86400 * 60); $nox->addCacheTimeForMime("text/css", 86400 * 60); $nox->addCacheTimeForMime("text/plain", 86400 * 60); diff --git a/example/src/HomeController.php b/example/src/HomeController.php index f2afd16..7d71fba 100644 --- a/example/src/HomeController.php +++ b/example/src/HomeController.php @@ -17,7 +17,7 @@ class HomeController extends BaseController{ * @throws ViewFileDoesNotExist * @throws LayoutDoesNotExist */ - #[Route("PUT", "/")] + #[Route("GET", "/")] public function homeView(Request $request): string{ return Renderer::renderView("home.html"); }