Forum Moderators: phranque

Message Too Old, No Replies

gzencode returns empty file

Trying to implement a "poor man's mod_gzip"

         

Winnetou

3:47 am on Mar 30, 2008 (gmt 0)

10+ Year Member



My ISP does not allow mod_gzip, so I try to create my own.

I've set up the following rule for .htaccess to route every .html file through the php file:

AddHandler doGZip .html
Action doGZip /gzip.php

This is the essence of the gzip.php file:

$file = $_SERVER["REQUEST_URI"];
$file_contents = file_get_contents($file);

$output = gzencode($file_contents, 9);
header('Vary: Accept-Encoding');
header("Content-Encoding: gzip");
header('Content-Length: ' . strlen($output));

echo $output;

I've stripped out tests for not supporting gzip etc.

However, when I open a page the browser shows an empty page and the HTTP header reads "Content-Length: 0".

When I comment the middle header line (the encoding) the browser shows an encoded file. When I write this file to disk the header of the file is a valid gzip-encoded signature (1F 8B 08).

When I try to immediately gzdecode the output it's empty too:

if( $f = @fopen( "encoded.txt", 'w' ) ) {
fputs( $f, $output );
fclose( $f );
}
if( $f = @fopen( "decoded.txt", 'w' ) ) {
$output = gzdecode($output);
fputs( $f, $output );
fclose( $f );
}

What am I missing? Thanks!

Winnetou

11:37 am on Mar 31, 2008 (gmt 0)

10+ Year Member



Update: The gzdecode() function does not exist in PHP, see [au2.php.net...] which also has a link to a function someone has written.

When I apply the encoded content to this function it successfully decodes the webpage.

But my browser doesn't.

Any clues warmly welcomed. Thank you.

jdMorgan

2:47 pm on Mar 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Where's your Content-Type header generated?

Jim

Winnetou

8:28 am on Apr 1, 2008 (gmt 0)

10+ Year Member



Hi Jim,

I'm using the .htaccess rule to redirect to the php file which does the rest.

As I understand it the gzencode command creates the entire file including the header. I have checked the spec (http://www.gzip.org/zlib/rfc-gzip.html) and compared the first few header bytes which are correct.

The file the browser decodes shows in Firebug "Content-Type text/html", so I assume that's encoded correctly. However, it does also show "Content-Length 0". Closer inspection of the spec reveals that the uncompressed file length should go into the last byte (CRC32 and ISIZE), but that byte is #00 in my tests.

Winnetou

1:21 am on Apr 2, 2008 (gmt 0)

10+ Year Member



Looks like it's an Apache configuration problem.

I just copied the exact same .htaccess and gzip.php to another server installation (at work) and there it works fine.

I'll be comparing configuration files and post the results back.

Winnetou

11:45 am on Apr 18, 2008 (gmt 0)

10+ Year Member



Sorry for the long pause, but I was sick.

I managed to make it work with Apache 2.0 by deleting any further PHP directives after the "echo $output" line. I couldn't figure out why my Apache 2.2 installation at work was more forgiving.