From 30b752c72c2d2be0aa8fc790ee110ff2120e7e08 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 10 Aug 2017 13:46:09 +0200 Subject: [PATCH] StaticController: Set a maximum age and check E-Tags of module image requests refs #2917 --- application/controllers/StaticController.php | 25 ++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/application/controllers/StaticController.php b/application/controllers/StaticController.php index 5d127a646..57e7ce513 100644 --- a/application/controllers/StaticController.php +++ b/application/controllers/StaticController.php @@ -86,13 +86,24 @@ class StaticController extends Controller } $s = stat($filePath); - $this->getResponse() - ->setHeader('Pragma', 'cache') - ->setHeader('Content-Type', 'image/' . $extension) - ->setHeader('Cache-Control', 'public, max-age=3600') - ->setHeader('Last-Modified', gmdate('D, d M Y H:i:s', $s['mtime']) . ' GMT') - ->setHeader('ETag', sprintf('%x-%x-%x', $s['ino'], $s['size'], (float) str_pad($s['mtime'], 16, '0'))); + $eTag = sprintf('%x-%x-%x', $s['ino'], $s['size'], (float) str_pad($s['mtime'], 16, '0')); - readfile($filePath); + $this->getResponse()->setHeader( + 'Cache-Control', + 'public, max-age=1814400, stale-while-revalidate=604800', + true + ); + + if ($this->getRequest()->getServer('HTTP_IF_NONE_MATCH') === $eTag) { + $this->getResponse() + ->setHttpResponseCode(304); + } else { + $this->getResponse() + ->setHeader('ETag', $eTag) + ->setHeader('Content-Type', 'image/' . $extension, true) + ->setHeader('Last-Modified', gmdate('D, d M Y H:i:s', $s['mtime']) . ' GMT'); + + readfile($filePath); + } } }