diff options
author | 2018-08-26 00:00:38 +0500 | |
---|---|---|
committer | 2018-08-25 20:00:38 +0100 | |
commit | 422c125d8e25b9ac5209580b1976fb5d75dab8f8 (patch) | |
tree | 9c44229dd95609992c0a95333294f7230034da13 /lib/BridgeAbstract.php | |
parent | 059656c3707cfeaf3ef8e12dd77444569c343635 (diff) | |
download | rss-bridge-422c125d8e25b9ac5209580b1976fb5d75dab8f8.tar.gz rss-bridge-422c125d8e25b9ac5209580b1976fb5d75dab8f8.tar.zst rss-bridge-422c125d8e25b9ac5209580b1976fb5d75dab8f8.zip |
[core] Returning 304 http code when returning cached data (#793)
Diffstat (limited to 'lib/BridgeAbstract.php')
-rw-r--r-- | lib/BridgeAbstract.php | 23 |
1 files changed, 23 insertions, 0 deletions
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(); + } + } } |