Ticket #215 (new enhancement)

Opened 3 years ago

Last modified 3 years ago

Add GZIP support

Reported by: ecb29 Assigned to: gallir
Priority: high Component: wp-cache
Severity: normal Keywords: gzip cache
Cc:

Description

Here's a diff. See http://elliottback.com/wp/archives/2005/07/12/getting-wp-cache-to-work/ for more info:

24a25
> $meta_pathname = $cache_path . $meta_file;
28,35c29,38
< if( is_file($cache_file) ) {
< 	$cache_rnd = $cache_max_time/8; 
< 	if (($mtime = @filemtime($cache_file)) == false) return;
< 	$cache_time_rnd = $cache_rnd/2 - rand(0, $cache_rnd); // To avoid thrashing when loaded
< 	if ($mtime + $cache_max_time > time() + $cache_time_rnd) {
< 		$meta = new CacheMeta;
< 		if (! ($meta = unserialize(@file_get_contents($cache_path . $meta_file))) ) return;
< 		foreach ($meta->headers as $header) {
---
> if( ($mtime = @filemtime($meta_pathname)) ) {
> 	if ($mtime + $cache_max_time > time() ) {
> 		$meta_obj = new CacheMeta;
> 
> 		// Get META
> 		if (! ($meta_obj = unserialize(@file_get_contents($meta_pathname))) ) 
> 			return;
> 
> 		// Send headers
> 		foreach ($meta_obj->headers as $header) {
38,40c41,50
< 		$log = "\n<!-- Cached page served by WP-Cache -->\n";
< 		if (! ($content_size = @filesize($cache_file)) > 0) return;
< 		if ($meta->dynamic) {
---
> 
> 		// Sanity check
> 		if (! ($content_size = @filesize($cache_file)) > 0 || $mtime < @filemtime($cache_file)) 
> 			return;
> 
> 		// GZIP
>     		$encoding = gzip_accepted();
>     		if (!$encoding){
> 			// Try to get cached file
> 			if ($meta_obj->dynamic) {
43d52
< 			$content_size += strlen($log);
45c54,55
< 			if(!@readfile ($cache_file)) return;
---
> 				if(!@readfile ($cache_file)) 
> 					return;
47c57,83
< 		echo $log;
---
> 		} else {
> 			// Try to get cached file
> 			if ($meta_obj->dynamic) {
> 				ob_start();
> 				include($cache_file);
> 				$contents = ob_get_contents();
> 				ob_end_clean();
> 			} else {
> 				$contents = file_get_contents($cache_file);
> 			}			
> 
>     			$gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00"; // gzip header
>     			$size = strlen($contents);
>    			$crc = crc32($contents);
>     			$gzdata .= gzcompress($contents, 9);
> 			$gzdata = substr($gzdata, 0, strlen($gzdata) - 4); // fix crc bug
>    			$gzdata .= pack("V",$crc) . pack("V", $size);
>     			$gzsize = strlen($gzdata);
> 
> 			Header('Content-Encoding: ' . $encoding);
> 			Header('Vary: Accept-Encoding');
> 			Header('Content-Length: ' . strlen($gzdata));
> 			Header('X-Content-Encoded-By: class.gzip_encode '.$this->_version);
> 
> 			echo $gzdata;
> 		}
> 		
49a86
> 
52a90,117
> /*
>  * http://leknor.com/code/php/class.gzip_encode.0.62.php.txt
>  * 
>  * gzip_accepted() - Test headers for Accept-Encoding: gzip
>  *
>  * Returns: if proper headers aren't found: false
>  *          if proper headers are found: 'gzip' or 'x-gzip'
>  *
>  * Tip: using this function you can test if the class will gzip the output
>  *  without actually compressing it yet, eg:
>  *    if (gzip_encode::gzip_accepted()) {
>  *       echo "Page will be gziped";
>  *    }
>  *  note the double colon syntax, I don't know where it is documented but
>  *  somehow it got in my brain.
>  */
> 
> function gzip_accepted() {
> 	global $HTTP_ACCEPT_ENCODING;
> 	if (strpos($HTTP_ACCEPT_ENCODING, 'gzip') === false) return false;
> 	if (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') === false) {
> 		$encoding = 'gzip';
> 	} else {
> 		$encoding = 'x-gzip';
> 	}
> 
> 	return $encoding;
> }

Change History

11/23/05 01:57:52 changed by yngwin

  • severity changed from major to enhancement.

This actually is an enhancement, not a major bug.

12/21/05 16:55:02 changed by sgrayban

Nice patch there mate :)