Automagical Compression

Add a URI translation handler to automagically compress URLs ending with .gz.

Configuration Entry

PerlTransHandler Apache::AutoGZip

Script III.2.4 Apache::AutoGZip

 package Apache::AutoGZip;
 #File: Apache/AutoGZip.pm
 
 sub handler {
     my $r = shift;

     # don't allow ourselves to be called recursively
     return DECLINED unless $r->is_initial_req;

     # don't do anything for files not ending with .gz
     my $uri = $r->uri;
     return DECLINED unless $uri=~/\.gz$/; 
     my $basename = $`;

     # don't do anything special if the file actually exists
     return DECLINED if -e $r->lookup_uri($uri)->filename;

     # look up information about the file
     my $subr = $r->lookup_uri($basename);
     $r->uri($basename);
     $r->path_info($subr->path_info);
     $r->filename($subr->filename);

     # fix the handler to point to Apache::GZip;
     my $handler = $subr->handler;
     unless ($handler) {
 	$r->handler('perl-script');
 	$r->push_handlers('PerlHandler','Apache::GZip');
     } else {
 	$r->handler($handler);
     }
     return OK;
 }
 
 1;

What it Looks Like

The uncompressed document:
http://localhost/conference/compress_test.html

Under the control of Apache::AutoGzip:
http://localhost/compressed/compress_test.html.gz

<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Mon Aug 17 10:49:23 EDT 1998