From 6c28918ebe9f46c098593ed5b86c7eab9fe035f8 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Fri, 19 Dec 2014 12:56:44 +0800 Subject: [PATCH] HTTP caching stuff, nice memes,. --- Controllers/FileController.php | 53 ++++++++++++++++++++++++++++----------- Controllers/SimfileController.php | 22 +++++++++++++--- Services/Http/HttpRequest.php | 5 ++++ Services/Http/HttpResponse.php | 4 +-- Services/Http/IHttpRequest.php | 1 + 5 files changed, 65 insertions(+), 20 deletions(-) diff --git a/Controllers/FileController.php b/Controllers/FileController.php index 22b000f..bd0c9e6 100644 --- a/Controllers/FileController.php +++ b/Controllers/FileController.php @@ -50,26 +50,49 @@ class FileController implements IDivineController // list simfiles public function serveBannerAction($hash) { - $file = $this->_fileRepository->findByHash($hash); - if($hash == 'default') $this->serveDefaultBanner(); - if(!$file) $this->notFound(); + //TODO: This DOES NOT check that the file the request is asking for is _actually_ + //the file we are server. This is because at this stage banners cannot change, we should + //be careful maybe. + if($this->_request->getHeader('HTTP_IF_MODIFIED_SINCE')) + { + $this->_response->setHeader("HTTP/1.1 304 Not Modified", 'Nice meme!'); + } else { + $file = $this->_fileRepository->findByHash($hash); + if($hash == 'default') $this->serveDefaultBanner(); + if(!$file) $this->notFound(); - $matches = glob(realpath('../files/' . $file->getPath()) . '/' . $file->getHash() . '.*'); - $match = reset($matches); - - $this->_response->setHeader('Content-Type', $file->getMimetype()) - ->setHeader('Content-Length', $file->getSize()) - ->setBody(file_get_contents($match)) - ->sendResponse(); + $matches = glob(realpath('../files/' . $file->getPath()) . '/' . $file->getHash() . '.*'); + $match = reset($matches); + + $this->_response->setHeader('Content-Type', $file->getMimetype()) + ->setHeader('Content-Length', $file->getSize()) + ->setHeader('etag', $file->getHash()) + ->setHeader('last-modified', gmdate("D, d M Y H:i:s", $file->getUploadDate()) . " GMT") + ->setHeader('cache-control', 'max-age=-1') + ->setBody(file_get_contents($match)); + } + + $this->_response->sendResponse(); } private function serveDefaultBanner() { - $file = '../files/banners/default.png'; - $this->_response->setHeader('Content-Type', 'image/png') - ->setHeader('Content-Length', filesize($file)) - ->setBody(file_get_contents($file)) - ->sendResponse(); + //XXX: As above + if($this->_request->getHeader('HTTP_IF_MODIFIED_SINCE')) + { + $this->_response->setHeader("HTTP/1.1 304 Not Modified", 'Nice meme!'); + } else { + $path = '../files/banners/default.png'; + $file = realpath($path); + $this->_response->setHeader('Content-Type', 'image/png') + ->setHeader('Content-Length', filesize($file)) + ->setBody(file_get_contents($file)) + ->setHeader('etag', md5_file($file)) + ->setHeader('last-modified', gmdate("D, d M Y H:i:s", filemtime($file)) . " GMT") + ->setHeader('cache-control', 'max-age=-1') + ->sendResponse(); + } + exit(); } diff --git a/Controllers/SimfileController.php b/Controllers/SimfileController.php index a5d1a39..9ab78ad 100644 --- a/Controllers/SimfileController.php +++ b/Controllers/SimfileController.php @@ -5,6 +5,7 @@ namespace Controllers; use Exception; use Controllers\IDivineController; use Services\Http\IHttpResponse; +use Services\Http\IHttpRequest; use Services\Uploads\IUploadManager; use Services\IUserSession; use Services\IZipParser; @@ -24,6 +25,7 @@ class SimfileController implements IDivineController private $_simfileRepository; private $_packRepository; private $_fileRepository; + private $_request; private $_response; private $_uploadManager; private $_zipParser; @@ -34,6 +36,7 @@ class SimfileController implements IDivineController public function __construct( IHttpResponse $response, + IHttpRequest $request, IUploadManager $uploadManager, ISimfileRepository $simfileRepository, IPackRepository $packRepository, @@ -45,6 +48,7 @@ class SimfileController implements IDivineController IStatusReporter $statusReporter ) { $this->_response = $response; + $this->_request = $request; $this->_uploadManager = $uploadManager; $this->_simfileRepository = $simfileRepository; $this->_packRepository = $packRepository; @@ -66,9 +70,21 @@ class SimfileController implements IDivineController $file = '../SimfileCache/simfiles.json'; $path = realpath($file); - $this->_response->setHeader('Content-Type', 'application/json') - ->setBody(file_get_contents($path)) - ->sendResponse(); + //Always set incase list changes + $hash = md5_file($path); + $this->_response->setHeader('etag', $hash) + ->setHeader('last-modified', gmdate("D, d M Y H:i:s", filemtime($file)) . " GMT") + ->setHeader('cache-control', 'max-age=-1'); + + if($this->_request->getHeader('HTTP_IF_NONE_MATCH') == $hash) + { + $this->_response->setHeader("HTTP/1.1 304 Not Modified", 'Nice meme!'); + } else { + $this->_response->setHeader('Content-Type', 'application/json') + ->setBody(file_get_contents($path)); + } + + $this->_response->sendResponse(); } public function latestSimfileAction() diff --git a/Services/Http/HttpRequest.php b/Services/Http/HttpRequest.php index 2105cac..cd6afaf 100644 --- a/Services/Http/HttpRequest.php +++ b/Services/Http/HttpRequest.php @@ -185,4 +185,9 @@ class HttpRequest implements IHttpRequest return $path; } + + public function getHeader($header) + { + return isset($_SERVER[$header]) ? $_SERVER[$header] : null; + } } \ No newline at end of file diff --git a/Services/Http/HttpResponse.php b/Services/Http/HttpResponse.php index bc6fd3a..e115d61 100644 --- a/Services/Http/HttpResponse.php +++ b/Services/Http/HttpResponse.php @@ -54,14 +54,14 @@ class HttpResponse implements IHttpResponse foreach($this->_headers as $headerName => $headerValue) { if(!$statusCodeSent) { header( - sprintf('%s:%s', $headerName, $headerValue), + sprintf('%s: %s', $headerName, $headerValue), false, $this->_statusCode); $statusCodeSent = true; } else { header( - sprintf('%s:%s', $headerName, $headerValue)); + sprintf('%s: %s', $headerName, $headerValue)); } } diff --git a/Services/Http/IHttpRequest.php b/Services/Http/IHttpRequest.php index 93d17d3..e88f5d0 100644 --- a/Services/Http/IHttpRequest.php +++ b/Services/Http/IHttpRequest.php @@ -17,6 +17,7 @@ interface IHttpRequest public function delete(); public function cookies(); public function getBody(); + public function getHeader($header); public function getContentType(); public function getHost(); public function getIp(); -- 2.11.0