mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
MDL-28709: Performance: Send cache/contenttype headers with 304 responses
This commit is contained in:
parent
4f3632441a
commit
ccc0fff941
@ -56,20 +56,31 @@ if ($rev > -1) {
|
||||
$cacheimage = false;
|
||||
if (file_exists("$candidatelocation/$image.gif")) {
|
||||
$cacheimage = "$candidatelocation/$image.gif";
|
||||
$ext = 'gif';
|
||||
} else if (file_exists("$candidatelocation/$image.png")) {
|
||||
$cacheimage = "$candidatelocation/$image.png";
|
||||
$ext = 'png';
|
||||
} else if (file_exists("$candidatelocation/$image.jpg")) {
|
||||
$cacheimage = "$candidatelocation/$image.jpg";
|
||||
$ext = 'jpg';
|
||||
} else if (file_exists("$candidatelocation/$image.jpeg")) {
|
||||
$cacheimage = "$candidatelocation/$image.jpeg";
|
||||
$ext = 'jpeg';
|
||||
} else if (file_exists("$candidatelocation/$image.ico")) {
|
||||
$cacheimage = "$candidatelocation/$image.ico";
|
||||
$ext = 'ico';
|
||||
}
|
||||
if ($cacheimage) {
|
||||
if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
|
||||
// we do not actually need to verify the etag value because our files
|
||||
// never change in cache because we increment the rev parameter
|
||||
header('HTTP/1.1 304 Not Modified');
|
||||
|
||||
$lifetime = 60*60*24*30; // 30 days
|
||||
$mimetype = get_contenttype_from_ext($ext);
|
||||
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
|
||||
header('Cache-Control: max-age='.$lifetime);
|
||||
header('Content-Type: '.$mimetype);
|
||||
die;
|
||||
}
|
||||
send_cached_image($cacheimage, $rev);
|
||||
@ -125,14 +136,7 @@ function send_cached_image($imagepath, $rev) {
|
||||
$pathinfo = pathinfo($imagepath);
|
||||
$imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
|
||||
|
||||
switch($pathinfo['extension']) {
|
||||
case 'gif' : $mimetype = 'image/gif'; break;
|
||||
case 'png' : $mimetype = 'image/png'; break;
|
||||
case 'jpg' : $mimetype = 'image/jpeg'; break;
|
||||
case 'jpeg' : $mimetype = 'image/jpeg'; break;
|
||||
case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
|
||||
default: $mimetype = 'document/unknown';
|
||||
}
|
||||
$mimetype = get_contenttype_from_ext($pathinfo['extension']);
|
||||
|
||||
header('Etag: '.md5("$rev/$imagepath"));
|
||||
header('Content-Disposition: inline; filename="'.$imagename.'"');
|
||||
@ -154,14 +158,7 @@ function send_uncached_image($imagepath) {
|
||||
$pathinfo = pathinfo($imagepath);
|
||||
$imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
|
||||
|
||||
switch($pathinfo['extension']) {
|
||||
case 'gif' : $mimetype = 'image/gif'; break;
|
||||
case 'png' : $mimetype = 'image/png'; break;
|
||||
case 'jpg' : $mimetype = 'image/jpeg'; break;
|
||||
case 'jpeg' : $mimetype = 'image/jpeg'; break;
|
||||
case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
|
||||
default: $mimetype = 'document/unknown';
|
||||
}
|
||||
$mimetype = get_contenttype_from_ext($pathinfo['extension']);
|
||||
|
||||
header('Content-Disposition: inline; filename="'.$imagename.'"');
|
||||
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
|
||||
@ -178,4 +175,19 @@ function send_uncached_image($imagepath) {
|
||||
function image_not_found() {
|
||||
header('HTTP/1.0 404 not found');
|
||||
die('Image was not found, sorry.');
|
||||
}
|
||||
}
|
||||
|
||||
function get_contenttype_from_ext($ext) {
|
||||
switch ($ext) {
|
||||
case 'gif':
|
||||
return 'image/gif';
|
||||
case 'png':
|
||||
return 'image/png';
|
||||
case 'jpg':
|
||||
case 'jpeg':
|
||||
return 'image/jpeg';
|
||||
case 'ico':
|
||||
return 'image/vnd.microsoft.icon';
|
||||
}
|
||||
return 'document/unknown';
|
||||
}
|
||||
|
@ -53,6 +53,10 @@ if ($rev > -1 and file_exists($candidate)) {
|
||||
// we do not actually need to verify the etag value because our files
|
||||
// never change in cache because we increment the rev parameter
|
||||
header('HTTP/1.1 304 Not Modified');
|
||||
$lifetime = 60*60*24*30; // 30 days
|
||||
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
|
||||
header('Cache-Control: max-age='.$lifetime);
|
||||
header('Content-Type: application/javascript; charset=utf-8');
|
||||
die;
|
||||
}
|
||||
send_cached_js($candidate, $rev);
|
||||
@ -88,12 +92,13 @@ if ($rev > -1) {
|
||||
// parameters to get the best performance.
|
||||
|
||||
function send_cached_js($jspath) {
|
||||
$lifetime = 60*60*24*20;
|
||||
$lifetime = 60*60*24*30; // 30 days
|
||||
|
||||
header('Content-Disposition: inline; filename="javascript.php"');
|
||||
header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
|
||||
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
|
||||
header('Pragma: ');
|
||||
header('Cache-Control: max-age='.$lifetime);
|
||||
header('Accept-Ranges: none');
|
||||
header('Content-Type: application/javascript; charset=utf-8');
|
||||
if (!min_enable_zlib_compression()) {
|
||||
|
@ -57,6 +57,10 @@ if (file_exists($candidatesheet)) {
|
||||
// we do not actually need to verify the etag value because our files
|
||||
// never change in cache because we increment the rev parameter
|
||||
header('HTTP/1.1 304 Not Modified');
|
||||
$lifetime = 60*60*24*30; // 30 days
|
||||
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
|
||||
header('Cache-Control: max-age='.$lifetime);
|
||||
header('Content-Type: text/css; charset=utf-8');
|
||||
die;
|
||||
}
|
||||
send_cached_css($candidatesheet, $rev);
|
||||
@ -116,7 +120,7 @@ function store_css(theme_config $theme, $csspath, $cssfiles) {
|
||||
}
|
||||
|
||||
function send_ie_css($themename, $rev) {
|
||||
$lifetime = 60*60*24*3;
|
||||
$lifetime = 60*60*24*30; // 30 days
|
||||
|
||||
$css = <<<EOF
|
||||
/** Unfortunately IE6/7 does not support more than 4096 selectors in one CSS file, which means we have to use some ugly hacks :-( **/
|
||||
@ -131,6 +135,7 @@ EOF;
|
||||
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
|
||||
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
|
||||
header('Pragma: ');
|
||||
header('Cache-Control: max-age='.$lifetime);
|
||||
header('Accept-Ranges: none');
|
||||
header('Content-Type: text/css; charset=utf-8');
|
||||
header('Content-Length: '.strlen($css));
|
||||
@ -140,12 +145,13 @@ EOF;
|
||||
}
|
||||
|
||||
function send_cached_css($csspath, $rev) {
|
||||
$lifetime = 60*60*24*20;
|
||||
$lifetime = 60*60*24*30; // 30 days
|
||||
|
||||
header('Content-Disposition: inline; filename="styles.php"');
|
||||
header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($csspath)) .' GMT');
|
||||
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
|
||||
header('Pragma: ');
|
||||
header('Cache-Control: max-age='.$lifetime);
|
||||
header('Accept-Ranges: none');
|
||||
header('Content-Type: text/css; charset=utf-8');
|
||||
if (!min_enable_zlib_compression()) {
|
||||
|
@ -45,6 +45,18 @@ if (substr($parts, -3) === '.js') {
|
||||
combo_not_found();
|
||||
}
|
||||
|
||||
// if they are requesting a revision that's not -1, and they have supplied an
|
||||
// If-Modified-Since header, we can send back a 304 Not Modified since the
|
||||
// content never changes (the rev number is increased any time the content changes)
|
||||
if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
|
||||
header('HTTP/1.1 304 Not Modified');
|
||||
$lifetime = 60*60*24*30; // 30 days
|
||||
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
|
||||
header('Cache-Control: max-age='.$lifetime);
|
||||
header('Content-Type: '.$mimetype);
|
||||
die;
|
||||
}
|
||||
|
||||
$parts = explode('&', $parts);
|
||||
$cache = true;
|
||||
|
||||
@ -124,13 +136,13 @@ if ($cache) {
|
||||
* @param string $mimetype
|
||||
*/
|
||||
function combo_send_cached($content, $mimetype) {
|
||||
$lifetime = 60*60*24*300; // 300 days === forever
|
||||
$lifetime = 60*60*24*30; // 30 days
|
||||
|
||||
header('Content-Disposition: inline; filename="combo"');
|
||||
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
|
||||
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
|
||||
header('Pragma: ');
|
||||
header('Cache-Control: max-age=315360000');
|
||||
header('Cache-Control: max-age='.$lifetime);
|
||||
header('Accept-Ranges: none');
|
||||
header('Content-Type: '.$mimetype);
|
||||
if (!min_enable_zlib_compression()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user