Implement ETag: header support for cache control

This commit is contained in:
Max Kostikov 2019-04-18 09:50:37 +02:00
parent 1732b2dfe3
commit 7a9093f634

View File

@ -40,7 +40,6 @@ class Photo extends \Zotlabs\Web\Controller {
call_hooks('cache_mode_hook', $cache_mode); call_hooks('cache_mode_hook', $cache_mode);
$observer_xchan = get_observer_hash(); $observer_xchan = get_observer_hash();
$ismodified = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
$cachecontrol = ''; $cachecontrol = '';
if(isset($type)) { if(isset($type)) {
@ -69,8 +68,6 @@ class Photo extends \Zotlabs\Web\Controller {
} }
} }
$modified = time();
$default = z_root() . '/' . $default;
$uid = $person; $uid = $person;
$data = ''; $data = '';
@ -98,12 +95,12 @@ class Photo extends \Zotlabs\Web\Controller {
$default = $d['default']; $default = $d['default'];
$data = $d['data']; $data = $d['data'];
$mimetype = $d['mimetype']; $mimetype = $d['mimetype'];
$modified = time();
} }
if(! $data) { if(! $data) {
$x = z_fetch_url($default,true,0,[ 'novalidate' => true ]); header("Location: " . z_root() . '/' . $default);
$data = ($x['success'] ? $x['body'] : EMPTY_STR); killme();
$mimetype = 'image/png';
} }
$cachecontrol = ', must-revalidate'; $cachecontrol = ', must-revalidate';
@ -212,6 +209,7 @@ class Photo extends \Zotlabs\Web\Controller {
if(! $exists) { if(! $exists) {
http_status_exit(404,'not found'); http_status_exit(404,'not found');
} }
} }
} }
else else
@ -223,7 +221,7 @@ class Photo extends \Zotlabs\Web\Controller {
header_remove('Pragma'); header_remove('Pragma');
if($ismodified === gmdate("D, d M Y H:i:s", $modified) . " GMT") { if($_SERVER['HTTP_IF_NONE_MATCH'] == md5($data) || $_SERVER['HTTP_IF_MODIFIED_SINCE'] === gmdate("D, d M Y H:i:s", $modified) . " GMT") {
header_remove('Expires'); header_remove('Expires');
header_remove('Cache-Control'); header_remove('Cache-Control');
header_remove('Set-Cookie'); header_remove('Set-Cookie');
@ -272,6 +270,7 @@ class Photo extends \Zotlabs\Web\Controller {
header("Content-type: " . $mimetype); header("Content-type: " . $mimetype);
header("Last-Modified: " . gmdate("D, d M Y H:i:s", $modified) . " GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s", $modified) . " GMT");
header("ETag: " . md5($data));
header("Content-Length: " . (isset($filesize) ? $filesize : strlen($data))); header("Content-Length: " . (isset($filesize) ? $filesize : strlen($data)));
// If it's a file resource, stream it. // If it's a file resource, stream it.