Page 1 of 1

FIXED: If-None-Match / ETag caching on non-Apache browsers

Posted: Wed May 31, 2006 1:51 pm
by martin42
This is a standard bandwidth-saving feature in 0.13 in stylesheet.php, to save sending the same stylesheet to the client over and over again.

By default it only works for Apache, but for other webservers that pass the right headers through into the environment, this mod to stylesheet.php will do the job:

Code: Select all

// At the top of stylesheet.php...
function emu_getallheaders() {
   foreach($_SERVER as $name => $value)
       if(substr($name, 0, 5) == 'HTTP_')
           $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))
)))] = str_replace('\\"', '"', $value);
   return $headers;
};

// At the bottom of stylesheet.php...
#Do cache-control stuff
if (function_exists('getallheaders'))
        $headers = getallheaders();
else
        $headers = emu_getallheaders();

$hash = md5($html);

#if browser sent etag and it is the same then reply with 304
if (isset($headers['If-None-Match']) && $headers['If-None-Match'] == '"'.$hash.'"')
{
        header('HTTP/1.1 304 Not Modified');
        exit;
}
else
{
        header('ETag: "'.$hash.'"');
};

echo $css;
Hope that's useful to someone :-)