Skip to content
Snippets Groups Projects
Commit ca2b464b authored by Chris Smith's avatar Chris Smith
Browse files

Add capability to send pre-compressed js & css files if the browser can accept them

- save a gzipped version of js & css files at the same time as the uncompressed version is cache
- basic content negotiation to send the compressed files
- uses sendfile (for compressed or uncompressed versions) if config indicates its available

darcs-hash:20090118184354-f07c6-66c5b465ab147d83de409708bab2c47d1dafcf8d.gz
parent 6106ad89
No related branches found
No related tags found
No related merge requests found
...@@ -578,6 +578,9 @@ function http_conditionalRequest($timestamp){ ...@@ -578,6 +578,9 @@ function http_conditionalRequest($timestamp){
exit; exit;
} }
/**
* common routines for handling communication with the client (browser)
*/
function http_sendfile($file) { function http_sendfile($file) {
global $conf; global $conf;
...@@ -596,4 +599,23 @@ function http_sendfile($file) { ...@@ -596,4 +599,23 @@ function http_sendfile($file) {
return false; return false;
} }
function http_accepts_gzip() {
return !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
}
function http_accepts_deflate() {
return !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'deflate') !== false);
}
/**
* return true if there exists a gzip version of the uncompressed file (samepath/samefilename.sameext.gz)
* created after the uncompressed file
*/
function http_gzip_valid($uncompressed_file) {
$gzip = $uncompressed_file.'.gz';
if (filemtime($gzip) < filemtime($uncompressed_file)) { // filemtime returns false (0) if file doesn't exist
return copy($uncompressed_file, 'compress.zlib://'.$gzip);
}
return true;
}
//Setup VIM: ex: et ts=2 enc=utf-8 : //Setup VIM: ex: et ts=2 enc=utf-8 :
...@@ -92,7 +92,20 @@ function css_out(){ ...@@ -92,7 +92,20 @@ function css_out(){
if(css_cacheok($cache,array_keys($files),$tplinc)){ if(css_cacheok($cache,array_keys($files),$tplinc)){
http_conditionalRequest(filemtime($cache)); http_conditionalRequest(filemtime($cache));
if($conf['allowdebug']) header("X-CacheUsed: $cache"); if($conf['allowdebug']) header("X-CacheUsed: $cache");
readfile($cache);
// finally send output
if (http_accepts_gzip() && http_gzip_valid($cache)) {
header('Vary: Accept-Encoding');
header('Content-Encoding: gzip');
if (!http_sendfile($cache.'.gz')) readfile($cache.".gz");
# } else if (http_accepts_deflate()) {
# header('Vary: Accept-Encoding');
# header('Content-Encoding: deflate');
# readfile($cache.".zip");
} else {
if (!http_sendfile($cache)) readfile($cache);
}
return; return;
} else { } else {
http_conditionalRequest(time()); http_conditionalRequest(time());
...@@ -124,9 +137,20 @@ function css_out(){ ...@@ -124,9 +137,20 @@ function css_out(){
// save cache file // save cache file
io_saveFile($cache,$css); io_saveFile($cache,$css);
copy($cache,"compress.zlib://$cache.gz");
// finally send output // finally send output
print $css; if (http_accepts_gzip()) {
header('Vary: Accept-Encoding');
header('Content-Encoding: gzip');
print gzencode($css,9,FORCE_GZIP);
# } else if (http_accepts_deflate()) {
# header('Vary: Accept-Encoding');
# header('Content-Encoding: deflate');
# print gzencode($css,9,FORCE_DEFLATE);
} else {
print $css;
}
} }
/** /**
......
...@@ -64,7 +64,20 @@ function js_out(){ ...@@ -64,7 +64,20 @@ function js_out(){
if(js_cacheok($cache,array_merge($files,$plugins))){ if(js_cacheok($cache,array_merge($files,$plugins))){
http_conditionalRequest(filemtime($cache)); http_conditionalRequest(filemtime($cache));
if($conf['allowdebug']) header("X-CacheUsed: $cache"); if($conf['allowdebug']) header("X-CacheUsed: $cache");
readfile($cache);
// finally send output
if (http_accepts_gzip() && http_gzip_valid($cache)) {
header('Vary: Accept-Encoding');
header('Content-Encoding: gzip');
if (!http_sendfile($cache.'.gz')) readfile($cache.".gz");
# } else if (http_accepts_deflate()) {
# header('Vary: Accept-Encoding');
# header('Content-Encoding: deflate');
# readfile($cache.".zip");
} else {
if (!http_sendfile($cache)) readfile($cache);
}
return; return;
} else { } else {
http_conditionalRequest(time()); http_conditionalRequest(time());
...@@ -147,9 +160,20 @@ function js_out(){ ...@@ -147,9 +160,20 @@ function js_out(){
// save cache file // save cache file
io_saveFile($cache,$js); io_saveFile($cache,$js);
copy($cache,"compress.zlib://$cache.gz");
// finally send output // finally send output
print $js; if (http_accepts_gzip()) {
header('Vary: Accept-Encoding');
header('Content-Encoding: gzip');
print gzencode($js,9,FORCE_GZIP);
# } else if (http_accepts_deflate()) {
# header('Vary: Accept-Encoding');
# header('Content-Encoding: deflate');
# print gzencode($js,9,FORCE_DEFLATE);
} else {
print $js;
}
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment