|
#!/usr/bin/perl -w # # gunz.cgi --crb3 03jul05/23Jan07/16mar07/03apr07 # # Action application/x-gzip /cgi-bin/gunz.cgi # # take over management of *.gz filetype from Apache, # so as to be able to emit ungzipped html pages. # this only works for lightly browsed LAN servers, but then, # a Net-facing server shouldn't have pages compressed # anyway. this is for a local server where downloads are # stored, where the ~50% space savings in gzipping HTML # matters to disk space. # # An issue: shtml processing, including server-side includes, # won't happen because Apache never sees the ungzipped # extension. Design decision: any gzipped pages with that # filetype are presumed to be downloaded that way, already fully # merged by the server which made them available for download. # # One problem surfaced: Apache can only tell if the script # worked or not by the exit-code, where 0 provokes a '200 Ok' # and nonzero provokes a '500 Server Error', leaving us with no # way to directly signal a 404 condition. # We get around that by redirecting to a nonexistent page if # the requested file isn't found, thus triggering a 404 anyway. # Apparently, Apache picks up the redirect for handling. This # isn't directly covered in RFC2616. # # --crb3 15Mar07: - add content-length for method-0 # - path /bin/cat properly for method-0 # - fix quotes (NOT tiks...duhhh) for method-0 # --crb3 16Mar07: - fix 404 handling (redirect to nonexistent page) # (doesn't fix 503: bad permissions) # --crb3 03Apr07: - short 'n' sweet. exec zcat or cat. this means # giving up on trying to put the ungzipped filename # in the address bar. # %contypes = ( 'default' => [ 'application/x-gzip', 0 ], # mimetype and method for all others 'tar' => [ 'application/x-tar-gz',0 ], # 0: dump out verbatim 'txt' => [ 'text/plain', 1 ], 'org' => [ 'text/plain', 1 ], # archived Maildir email 'log' => [ 'text/plain', 1 ], 'htm' => [ 'text/html', 1 ], # 1: gunzip as lines 'html' => [ 'text/html', 1 ], # 'shtml' => [ 'text/html', 1 ], # 'gif' => [ 'image/gif', 2 ], # 2: gunzip as blob 'jpg' => [ 'image/jpeg', 2 ], 'xpm' => [ 'image/xpm', 2 ], 'png' => [ 'image/png', 2 ], ); $gfile=$ENV{PATH_TRANSLATED}; # # 404 problem resolved: redirect to a # known-missing page thus provoking a 404. # --crb3 16Mar07 # unless(-e $gfile){ print "Location: /RepointToNonexistentPage-TriggersA404.html\n", "Content-Type: none\n\n"; exit(0); } ($ftype=$gfile) =~ s/\.gz$//; $ftype =~ s/^.+\.//; $uri = $ENV{REQUEST_URI}; # # figure out what kind it is, and ship it. # foreach $key (keys %contypes){ if($ftype eq $key){ $mime = $contypes{$key}->[0]; $method=$contypes{$key}->[1]; last; } } unless(defined($method)){ $mime = $contypes{default}->[0]; $method=$contypes{default}->[1]; } $uri =~ s/\.gz$// if $method; print "Content-Location: $uri\n"; if($method==0){ my $len = (-s $gfile); print "Content-Length: $len\n"; } print "Content-Type: $mime\n\n"; if($method==0){ # bulk shipment exec("/bin/cat \"$gfile\""); }elsif($method==1){ # gunzip as lines exec("/bin/zcat \"$gfile\""); }elsif($method==2){ # gunzip to blob exec("/bin/zcat \"$gfile\""); }else{ warn "bad method: $method\n"; # goes in error_log } |
Grab a gzipped copy here |
| Syntax highlighting using Syntax::Highlight::Engine::Kate |