// 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();
}
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
private $_simfileRepository;\r
private $_packRepository;\r
private $_fileRepository;\r
+ private $_request;\r
private $_response;\r
private $_uploadManager;\r
private $_zipParser;\r
\r
public function __construct(\r
IHttpResponse $response,\r
+ IHttpRequest $request,\r
IUploadManager $uploadManager,\r
ISimfileRepository $simfileRepository,\r
IPackRepository $packRepository,\r
IStatusReporter $statusReporter\r
) {\r
$this->_response = $response;\r
+ $this->_request = $request;\r
$this->_uploadManager = $uploadManager;\r
$this->_simfileRepository = $simfileRepository;\r
$this->_packRepository = $packRepository;\r
$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
return $path;
}
+
+ public function getHeader($header)
+ {
+ return isset($_SERVER[$header]) ? $_SERVER[$header] : null;
+ }
}
\ No newline at end of file
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));
}
}
public function delete();
public function cookies();
public function getBody();
+ public function getHeader($header);
public function getContentType();
public function getHost();
public function getIp();