diff --git a/src/Sender/Frontend/Http/StaticFiles.php b/src/Sender/Frontend/Http/StaticFiles.php index a5d96d50..4e7cb389 100644 --- a/src/Sender/Frontend/Http/StaticFiles.php +++ b/src/Sender/Frontend/Http/StaticFiles.php @@ -26,16 +26,19 @@ public function handle(ServerRequestInterface $request, callable $next): Respons } if (\preg_match('#^/((?:[a-zA-Z0-9\\-_]+/)*[a-zA-Z0-9.\\-\\[\\]() _]+?\\.([a-zA-Z0-4]++))$#', $path, $matches)) { - $file = \sprintf("%s/resources/frontend/%s", Info::TRAP_ROOT, $matches[1]); + $file = \sprintf('%s/resources/frontend/%s', Info::TRAP_ROOT, $matches[1]); - /** @var array $cache */ - static $cache = []; + /** @var array $cacheContent */ + static $cacheContent = []; + /** @var array $cacheHash */ + static $cacheHash = []; + /** @var array> $cacheSize */ + static $cacheSize = []; - if (!\array_key_exists($file, $cache) && !\is_file($file)) { + if (!\array_key_exists($file, $cacheContent) && !\is_file($file)) { return new Response(404); } - $content = null; $headers = []; $type = match($matches[2]) { @@ -54,11 +57,11 @@ public function handle(ServerRequestInterface $request, callable $next): Respons if ($path === '/index.html') { if (empty($this->earlyResponse)) { - $cache[$file] ??= \file_get_contents($file); + $cacheContent[$file] ??= \file_get_contents($file); // Find all CSS files \preg_match_all( '#\\bhref="([^"]+?\\.css)"#i', - $cache[$file], + $cacheContent[$file], $matches, ); $this->earlyResponse = \array_unique($matches[1]); @@ -74,18 +77,18 @@ public function handle(ServerRequestInterface $request, callable $next): Respons // (new \Buggregator\Trap\Support\Timer(2))->wait(); // to test early hints } - $cache[$file] ??= \file_get_contents($file); - + $cacheContent[$file] ??= \file_get_contents($file); return new Response( 200, [ 'Content-Type' => [$type], - 'Content-Length' => [\filesize($file)], + 'Content-Length' => [$cacheSize[$file] ??= \filesize($file)], 'Date' => [\gmdate('D, d M Y H:i:s T')], 'Cache-Control' => ['max-age=604801'], - 'ETag' => [\sha1($cache[$file])], + 'Accept-Ranges' => ['none'], + 'ETag' => [$cacheHash[$file] ??= \sha1($cacheContent[$file])], ] + $headers, - $cache[$file] ??= \file_get_contents($file), + $cacheContent[$file], ); }