HTTP caching stuff, nice memes,.
authorCameron Ball <cameron@getapproved.com.au>
Fri, 19 Dec 2014 04:56:44 +0000 (12:56 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 19 Dec 2014 04:56:44 +0000 (12:56 +0800)
Controllers/FileController.php
Controllers/SimfileController.php
Services/Http/HttpRequest.php
Services/Http/HttpResponse.php
Services/Http/IHttpRequest.php

index 22b000f..bd0c9e6 100644 (file)
@@ -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();
     }
     
index a5d1a39..9ab78ad 100644 (file)
@@ -5,6 +5,7 @@ namespace Controllers;
 use Exception;\r
 use Controllers\IDivineController;\r
 use Services\Http\IHttpResponse;\r
+use Services\Http\IHttpRequest;\r
 use Services\Uploads\IUploadManager;\r
 use Services\IUserSession;\r
 use Services\IZipParser;\r
@@ -24,6 +25,7 @@ class SimfileController implements IDivineController
     private $_simfileRepository;\r
     private $_packRepository;\r
     private $_fileRepository;\r
+    private $_request;\r
     private $_response;\r
     private $_uploadManager;\r
     private $_zipParser;\r
@@ -34,6 +36,7 @@ class SimfileController implements IDivineController
     \r
     public function __construct(\r
         IHttpResponse $response,\r
+        IHttpRequest $request,\r
         IUploadManager $uploadManager,\r
         ISimfileRepository $simfileRepository,\r
         IPackRepository $packRepository,\r
@@ -45,6 +48,7 @@ class SimfileController implements IDivineController
         IStatusReporter $statusReporter\r
     ) {\r
         $this->_response = $response;\r
+        $this->_request = $request;\r
         $this->_uploadManager = $uploadManager;\r
         $this->_simfileRepository = $simfileRepository;\r
         $this->_packRepository = $packRepository;\r
@@ -66,9 +70,21 @@ class SimfileController implements IDivineController
         $file = '../SimfileCache/simfiles.json';\r
         $path = realpath($file);\r
         \r
-        $this->_response->setHeader('Content-Type', 'application/json')\r
-                        ->setBody(file_get_contents($path))\r
-                        ->sendResponse();\r
+        //Always set incase list changes\r
+        $hash = md5_file($path);\r
+        $this->_response->setHeader('etag', $hash)\r
+                        ->setHeader('last-modified', gmdate("D, d M Y H:i:s", filemtime($file)) . " GMT")\r
+                        ->setHeader('cache-control', 'max-age=-1');\r
+        \r
+        if($this->_request->getHeader('HTTP_IF_NONE_MATCH') == $hash)\r
+        {\r
+            $this->_response->setHeader("HTTP/1.1 304 Not Modified", 'Nice meme!');\r
+        } else {\r
+            $this->_response->setHeader('Content-Type', 'application/json')\r
+                            ->setBody(file_get_contents($path));\r
+        }\r
+        \r
+        $this->_response->sendResponse();\r
     }\r
     \r
     public function latestSimfileAction()\r
index 2105cac..cd6afaf 100644 (file)
@@ -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
index bc6fd3a..e115d61 100644 (file)
@@ -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));
             }
         }
         
index 93d17d3..e88f5d0 100644 (file)
@@ -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();