Debian Codegraph @

README

raw
1 NAME
2 CGI::Compress::Gzip - CGI with automatically compressed output
3
4 LICENSE
5 Copyright 2006-2007 Clotho Advanced Media, Inc., <[email protected]>
6
7 Copyright 2007-2008 Chris Dolan <[email protected]>
8
9 This library is free software; you can redistribute it and/or modify it
10 under the same terms as Perl itself.
11
12 SYNOPSIS
13 use CGI::Compress::Gzip;
14
15 my $cgi = new CGI::Compress::Gzip;
16 print $cgi->header();
17 print "<html> ...";
18
19 See the CAVEATS section below!
20
21 DESCRIPTION
22 CGI::Compress::Gzip extends the CGI module to auto-detect whether the
23 client browser wants compressed output and, if so and if the script
24 chooses HTML output, apply gzip compression on any content header for
25 STDOUT. This module is intended to be a drop-in replacement for CGI.pm.
26
27 Apache mod_perl users may wish to consider the Apache::Compress or
28 Apache::GzipChain modules, which allow more transparent output
29 compression than this module can provide. However, as of this writing
30 those modules are more aggressive about compressing, regardless of
31 Content-Type.
32
33 Headers
34
35 At the time that a header is requested, CGI::Compress::Gzip checks the
36 HTTP_ACCEPT_ENCODING environment variable (passed by Apache). If this
37 variable includes the flag "gzip" and the outgoing mime-type is
38 "text/*", then gzipped output is preferred. [the default mime-type
39 selection of text/* can be changed by subclassing -- see below] The
40 header is altered to add the "Content-Encoding: gzip" flag which
41 indicates that compression is turned on.
42
43 Naturally, it is crucial that the CGI application output nothing before
44 the header is printed. If this is violated, things will go badly.
45
46 Compression
47
48 When the header is created, this module sets up a new filehandle to
49 accept data. STDOUT is redirected through that filehandle. The new
50 filehandle passes data verbatim until it detects the end of the CGI
51 header. At that time, it switches over to Gzip output for the remainder
52 of the CGI run.
53
54 Note that the Zlib library on which this code is ultimately based
55 requires a fileno for the output filehandle. Where the output filehandle
56 is faked (i.e. in mod_perl), we instead use in-memory compression. This
57 is more wasteful of RAM, but it is the only solution I've found (and it
58 is one shared by the Apache::* compression modules).
59
60 Debugging note: if you set $CGI::Compress::Gzip::global_give_reason to a
61 true value, then this module will add an HTTP header entry called
62 X-non-gzip-reason with an explanation of why it chose not to gzip the
63 output stream.
64
65 Buffering
66
67 The Zlib library introduces latencies. In some cases, this module may
68 delay output until the CGI object is garbage collected, presumably at
69 the end of the program. This buffering can be detrimental to long-lived
70 programs which are supposed to have incremental output, causing browser
71 timeouts. To compensate, compression is automatically disabled when
72 autoflush (i.e. the $| variable) is set to true. Future versions may try
73 to enable autoflushing on the Zlib filehandles, if possible [Help
74 wanted].
75
76 CLASS METHODS
77 $pkg->new([CGI ARGS])
78 Create a new object. This resets the environment before creating a
79 CGI.pm object. This should not be called more than once per script
80 run! All arguments are passed to the parent class.
81
82 $pkg->useCompression($boolean)
83 $self->useCompression($boolean)
84 This can be used as a class method or an instance method. The former
85 is included for backward compatibility, and is NOT recommended. As a
86 class method, this changes the default value. As an instance method
87 it affects only the specified instance.
88
89 Turn compression on/off for the target. If turned on, compression
90 will be used only if the prerequisite compression libraries are
91 available and if the client browser requests compression.
92
93 Defaults to on.
94
95 INSTANCE METHODS
96 $self->useFileHandle($filehandle)
97 Manually set the output filehandle. Because of limitations of Zlib,
98 this MUST be a real filehandle (with valid results from fileno())
99 and not a pseudo filehandle like IO::String.
100
101 If this is not set, STDOUT is used.
102
103 $self->isCompressibleType($content_type)
104 Given a MIME type (with possible charset attached), return a boolean
105 indicating if this media type is a good candidate for compression.
106 This implementation is simply:
107
108 return $type =~ /^text\//;
109
110 Subclasses may wish to override this method to apply different
111 criteria.
112
113 $self->header([HEADER ARGS])
114 Overrides the `header()' method in CGI.
115
116 Return a CGI header with the compression flags set properly. Returns
117 an empty string is a header has already been printed.
118
119 This method engages the Gzip output by fiddling with the default
120 output filehandle. All subsequent output via usual Perl print() will
121 be automatically gzipped except for this header (which must go out
122 as plain text).
123
124 Any arguments will be passed on to CGI::header. This method should
125 NOT be called if you don't want your header or STDOUT to be fiddled
126 with.
127
128 $self->DESTROY()
129 Override the CGI destructor so we can close the Gzip output stream,
130 if there is one open.
131
132 CAVEATS
133 Apache::Registry
134
135 Under Apache::Registry, global variables may not go out of scope in
136 time. This may causes timing bugs, since this module makes use of the
137 DESTROY() method. To avoid this issue, make sure your CGI object is
138 stored in a scoped variable.
139
140 # BROKEN CODE
141 use CGI::Compress::Gzip;
142 $q = CGI::Compress::Gzip->new;
143 print $q->header;
144 print "Hello, world\n";
145
146 # WORKAROUND CODE
147 use CGI::Compress::Gzip;
148 do {
149 my $q = CGI::Compress::Gzip->new;
150 print $q->header;
151 print "Hello, world\n";
152 }
153
154 Filehandles
155
156 This module works by changing the default filehandle. It does not change
157 STDOUT at all. As a consequence, your programs should call `print'
158 without a filehandle argument.
159
160 # BROKEN CODE
161 use CGI::Compress::Gzip;
162 my $q = CGI::Compress::Gzip->new;
163 print STDOUT $q->header;
164 print STDOUT "Hello, world\n";
165
166 # WORKAROUND CODE
167 use CGI::Compress::Gzip;
168 my $q = CGI::Compress::Gzip->new;
169 print $q->header;
170 print "Hello, world\n";
171
172 Future versions may steal away STDOUT and replace it with the
173 compression filehandle, but that seemed too risky for this version.
174
175 Header Munging
176
177 When sending compressed output, the HTTP headers must remain
178 uncompressed. So, this module goes to great effort to keep the headers
179 and body separate. That has led to CGI::header() emulation code that is
180 a little brittle. Most potential problems arise because STDOUT gets
181 tweaked as soon as header() is called.
182
183 If you use the CGI.pm header() API as specified in CGI.pm, then all
184 should go well. But if you do anything unusual, this module may break.
185 For example:
186
187 # BROKEN CODE
188 use CGI::Compress::Gzip;
189 my $q = CGI::Compress::Gzip->new;
190 print "Set-Cookie: foo=bar\n" . $q->header;
191 print "Hello, world\n";
192
193 # WORKAROUND 1 (preferred)
194 use CGI::Compress::Gzip;
195 my $q = CGI::Compress::Gzip->new;
196 print $q->header("-Set_Cookie" => "foo=bar");
197 print "Hello, world\n";
198
199 # WORKAROUND 2
200 use CGI::Compress::Gzip;
201 my $q = CGI::Compress::Gzip->new;
202 print "Set-Cookie: foo=bar\n";
203 print $q->header;
204 print "Hello, world\n";
205
206 Future versions could try to parse the header to look for its end rather
207 than insisting that the printed version match the version returned by
208 header(). Patches would be very welcome.
209
210 SEE ALSO
211 CGI::Compress::Gzip depends on CGI and IO::Zlib. Similar functionality
212 is available from mod_gzip, Apache::Compress or Apache::GzipChain,
213 however all of those require changes to the webserver configuration.
214
215 AUTHOR
216 Chris Dolan
217
218 This module was originally developed by me at Clotho Advanced Media Inc.
219 Now I maintain it in my spare time.
220
221 ACKNOWLEDGMENTS
222 Clotho greatly appreciates the assistance and feedback the community has
223 extended to help refine this module.
224
225 Thanks to Rhesa Rozendaal who noticed the -Type omission in v0.17.
226
227 Thanks to Laga Mahesa who did some Windows testing and experimentation.
228
229 Thanks to Slaven Rezic who 1) found several header handling bugs, 2)
230 discovered the Apache::Registry and Filehandle caveats, 3) provided a
231 patch incorporated into v0.17, and 4) persisted with smoke tests that
232 reproduced the envvar problem fixed in v0.23.
233
234 Thanks to Jan Willamowius who found a header handling bug.
235
236 Thanks to Andreas J. Koenig and brian d foy for module naming advice.
237
238 HELP WANTED
239 If you like this module, please help by testing on Windows or in a
240 `FastCGI' environment, since I have neither available for easy testing.
241
242 Personally, I don't use this module much anymore as all of my work is on
243 Catalyst and mod_perl now.
244