From f73c6bad2cc3017a9c21298560d688e8be3531da Mon Sep 17 00:00:00 2001 From: Tony Levi Date: Thu, 11 Aug 2011 14:04:36 +0930 Subject: [PATCH] MDL-28708: Send 304 Not Modified response when browser If-Modified-Since header is after file mtime --- lib/filelib.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/filelib.php b/lib/filelib.php index 5cc33d0e213..8524e3d5e17 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -1728,6 +1728,21 @@ function send_file($path, $filename, $lifetime = 'default' , $filter=0, $pathiss //try to disable automatic sid rewrite in cookieless mode @ini_set("session.use_trans_sid", "false"); + if ($lifetime > 0 && !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { + // get unixtime of request header; clip extra junk off first + $since = strtotime(preg_replace('/;.*$/','',$_SERVER["HTTP_IF_MODIFIED_SINCE"])); + if ($since && $since >= $lastmodified) { + header('HTTP/1.1 304 Not Modified'); + header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); + header('Cache-Control: max-age='.$lifetime); + header('Content-Type: '.$mimetype); + if ($dontdie) { + return; + } + die; + } + } + //do not put '@' before the next header to detect incorrect moodle configurations, //error should be better than "weird" empty lines for admins/users header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT'); @@ -1928,6 +1943,21 @@ function send_stored_file($stored_file, $lifetime=86400 , $filter=0, $forcedownl //try to disable automatic sid rewrite in cookieless mode @ini_set("session.use_trans_sid", "false"); + if ($lifetime > 0 && !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { + // get unixtime of request header; clip extra junk off first + $since = strtotime(preg_replace('/;.*$/','',$_SERVER["HTTP_IF_MODIFIED_SINCE"])); + if ($since && $since >= $lastmodified) { + header('HTTP/1.1 304 Not Modified'); + header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); + header('Cache-Control: max-age='.$lifetime); + header('Content-Type: '.$mimetype); + if ($dontdie) { + return; + } + die; + } + } + //do not put '@' before the next header to detect incorrect moodle configurations, //error should be better than "weird" empty lines for admins/users //TODO: should we remove all those @ before the header()? Are all of the values supported on all servers? -- 2.43.0