aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--index.php2
-rw-r--r--lib/BridgeAbstract.php23
-rw-r--r--lib/FormatAbstract.php8
3 files changed, 33 insertions, 0 deletions
diff --git a/index.php b/index.php
index 4d59f46d..37cb6700 100644
--- a/index.php
+++ b/index.php
@@ -195,6 +195,7 @@ try {
try {
$bridge->setCache($cache);
$bridge->setCacheTimeout($cache_timeout);
+ $bridge->dieIfNotModified();
$bridge->setDatas($params);
} catch(Error $e) {
http_response_code($e->getCode());
@@ -211,6 +212,7 @@ try {
$format = Format::create($format);
$format->setItems($bridge->getItems());
$format->setExtraInfos($bridge->getExtraInfos());
+ $format->setLastModified($bridge->getCacheTime());
$format->display();
} catch(Error $e) {
http_response_code($e->getCode());
diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php
index fb7456fd..95ef3572 100644
--- a/lib/BridgeAbstract.php
+++ b/lib/BridgeAbstract.php
@@ -292,4 +292,27 @@ abstract class BridgeAbstract implements BridgeInterface {
public function getCacheTimeout(){
return isset($this->cacheTimeout) ? $this->cacheTimeout : static::CACHE_TIMEOUT;
}
+
+ public function getCacheTime(){
+ return !is_null($this->cache) ? $this->cache->getTime() : false;
+ }
+
+ public function dieIfNotModified(){
+ if ((defined('DEBUG') && DEBUG === true)) return; // disabled in debug mode
+
+ $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;
+ if (!$if_modified_since) return; // If-Modified-Since value is required
+
+ $last_modified = $this->getCacheTime();
+ if (!$last_modified) return; // did not detect cache time
+
+ if (time() - $this->getCacheTimeout() > $last_modified) return; // cache timeout
+
+ $last_modified = (gmdate('D, d M Y H:i:s ', $last_modified) . 'GMT');
+
+ if ($if_modified_since == $last_modified) {
+ header('HTTP/1.1 304 Not Modified');
+ die();
+ }
+ }
}
diff --git a/lib/FormatAbstract.php b/lib/FormatAbstract.php
index db8d3dd4..d36b29d4 100644
--- a/lib/FormatAbstract.php
+++ b/lib/FormatAbstract.php
@@ -7,6 +7,7 @@ abstract class FormatAbstract implements FormatInterface {
$contentType,
$charset,
$items,
+ $lastModified,
$extraInfos;
public function setCharset($charset){
@@ -27,11 +28,18 @@ abstract class FormatAbstract implements FormatInterface {
return $this;
}
+ public function setLastModified($lastModified){
+ $this->lastModified = $lastModified;
+ }
+
protected function callContentType(){
header('Content-Type: ' . $this->contentType);
}
public function display(){
+ if ($this->lastModified) {
+ header('Last-Modified: ' . gmdate('D, d M Y H:i:s ', $this->lastModified) . 'GMT');
+ }
echo $this->stringify();
return $this;