CGI-Compress-Gzip
view release on metacpan or search on metacpan
NAME
CGI::Compress::Gzip - CGI with automatically compressed output
LICENSE
Copyright 2006-2007 Clotho Advanced Media, Inc., <cpan@clotho.com>
Copyright 2007-2008 Chris Dolan <cdolan@cpan.org>
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
SYNOPSIS
use CGI::Compress::Gzip;
my $cgi = new CGI::Compress::Gzip;
print $cgi->header();
print "<html> ...";
See the CAVEATS section below!
DESCRIPTION
CGI::Compress::Gzip extends the CGI module to auto-detect whether the
client browser wants compressed output and, if so and if the script
chooses HTML output, apply gzip compression on any content header for
STDOUT. This module is intended to be a drop-in replacement for CGI.pm.
Apache mod_perl users may wish to consider the Apache::Compress or
Apache::GzipChain modules, which allow more transparent output
compression than this module can provide. However, as of this writing
those modules are more aggressive about compressing, regardless of
Content-Type.
Headers
At the time that a header is requested, CGI::Compress::Gzip checks the
HTTP_ACCEPT_ENCODING environment variable (passed by Apache). If this
variable includes the flag "gzip" and the outgoing mime-type is
"text/*", then gzipped output is preferred. [the default mime-type
selection of text/* can be changed by subclassing -- see below] The
header is altered to add the "Content-Encoding: gzip" flag which
indicates that compression is turned on.
Naturally, it is crucial that the CGI application output nothing before
the header is printed. If this is violated, things will go badly.
Compression
When the header is created, this module sets up a new filehandle to
accept data. STDOUT is redirected through that filehandle. The new
filehandle passes data verbatim until it detects the end of the CGI
header. At that time, it switches over to Gzip output for the remainder
of the CGI run.
Note that the Zlib library on which this code is ultimately based
requires a fileno for the output filehandle. Where the output filehandle
is faked (i.e. in mod_perl), we instead use in-memory compression. This
is more wasteful of RAM, but it is the only solution I've found (and it
is one shared by the Apache::* compression modules).
Debugging note: if you set $CGI::Compress::Gzip::global_give_reason to a
true value, then this module will add an HTTP header entry called
X-non-gzip-reason with an explanation of why it chose not to gzip the
output stream.
Buffering
The Zlib library introduces latencies. In some cases, this module may
delay output until the CGI object is garbage collected, presumably at
the end of the program. This buffering can be detrimental to long-lived
programs which are supposed to have incremental output, causing browser
timeouts. To compensate, compression is automatically disabled when
autoflush (i.e. the $| variable) is set to true. Future versions may try
to enable autoflushing on the Zlib filehandles, if possible [Help
wanted].
CLASS METHODS
$pkg->new([CGI ARGS])
Create a new object. This resets the environment before creating a
CGI.pm object. This should not be called more than once per script
run! All arguments are passed to the parent class.
$pkg->useCompression($boolean)
$self->useCompression($boolean)
This can be used as a class method or an instance method. The former
is included for backward compatibility, and is NOT recommended. As a
class method, this changes the default value. As an instance method
it affects only the specified instance.
( run in 1.503 second using v1.01-cache-2.11-cpan-39bf76dae61 )