blib/lib/MKDoc/Apache_Cache.pm untested

filter: Statementstmt 0.0% 0 / 171 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 76 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 349 CC 8 SCAR 15.2
CC
8
Cov
0%
handler
12.0
_do_cached
12.0
_make_cache_friendly
12.0
CRAP 4.6
1 package MKDoc::Apache_Cache::Capture;
2 use base qw /Apache::RegistryNG Apache/;
3 use bytes;
4  
5 sub new
6 {
7 my ($class, $r) = @_;
8 $r ||= Apache->request();
9
10 tie *STDOUT, $class, $r;
11 return tied *STDOUT;
12 }
13  
14  
15 sub print
16 {
17 my $self = shift;
18 $self->{_data} ||= '';
19 $self->{_data} .= join ('', @_);
20 }
21  
22  
23 sub data
24 {
25 my $self = shift;
26 return $self->{_data};
27 }
28  
29  
30 sub TIEHANDLE
31 {
32 my ($class, $r) = @_;
33 return bless { r => $r, _r => $r, _data => undef }, $class;
34 }
35  
36  
37 sub PRINT
38 {
39 shift->print (@_);
40 }
41  
42  
43 package MKDoc::Apache_Cache;
44 use base qw /Apache::RegistryNG/;
45 use strict;
46 use warnings;
47 use Apache;
48 use Apache::Constants;
49 use MKDoc::Control_List;
50 use Cache::FileCache;
51 use File::Spec;
52 use vars qw /$Request/;
53 use CGI;
54 use Compress::Zlib;
55 use Digest::MD5;
56  
57 our $VERSION = '0.71';
58  
59  
60 sub handler ($$)
61 {
62 my ($class, $r) = (@_ >= 2) ? (shift, shift) : (__PACKAGE__, shift);
63
64 # Makes $MKDoc::Apache_Cache::Request available
65 local $Request = $r;
66
67 my @args = do {
68 no warnings;
69 no strict;
70 ( $class->_control_list_process() );
71 };
72  
73 my ($ret, $data) = $class->_do_cached (@args);
74  
75 # bug:
76 # 200 at /usr/local/lib/perl5/site_perl/5.8.0/MKDoc/Apache_Cache.pm line 66.
77 # Status: 404 Not Found
78 # Content-Type: text/html
79 # =======================
80 # this is the bugfix
81 $data ||= '';
82 my ($n_ret) = $data =~ /Status\:\s+(\d+)/;
83 $n_ret ||= $ret;
84  
85 # if the client doesn't support gzip, fix the headers,
86 # ungzip && send.
87 $ENV{HTTP_ACCEPT_ENCODING} ||= '';
88 lc $ENV{'HTTP_ACCEPT_ENCODING'} !~ /gzip/ and do {
89  
90 my ($headers, $body) = split /\r?\n\r?\n/, $data, 2;
91 $body ||= '';
92 $headers = join "\r\n",
93 grep !/content-length\:/i,
94 grep !/content-encoding\:/i,
95 grep !/vary\:/i,
96 split /\r?\n/, $headers;
97 $body = Compress::Zlib::memGunzip ($body);
98 $headers .= "\r\nContent-Length: " . length ($body) if ($body);
99 $data = $headers . "\r\n\r\n" . $body;
100 };
101  
102 $ENV{REQUEST_METHOD} =~ /HEAD/i and do {
103 $data =~ s/\r?\ncontent-length\:.*//i;
104 $data =~ s/\r?\netag\:.*//i;
105 };
106  
107 $r->print ($data);
108 return $n_ret;
109 }
110  
111  
112 sub _do_cached
113 {
114 my $class = shift;
115
116 my $timeout = shift || return $class->_do_request();
117 my $identifier = shift || $class->_default_identifier();
118  
119 $timeout = _expiration_time ($timeout);
120
121 my $cache_obj = $class->_cache_object();
122 my $cached = $cache_obj->get ($identifier) || do {
123  
124 my ($ret, $data) = $class->_do_request();
125  
126 # don't cache unless the return code is 200
127 return ($ret, $data) unless ($ret == 200);
128  
129 # cookies usually mean non cacheable content
130 return ($ret, $data) if ($data =~ /\nSet-Cookie:.+/);
131  
132 # add expires: header to be stored in the cached file
133 my $expires = _http_date ($timeout + time());
134 $data =~ s/\r?\n\r?\n/\r\nExpires: $expires\r\n\r\n/;
135  
136 $cache_obj->set ($identifier, "$ret\n$data", $timeout);
137 "$ret\n$data";
138 };
139
140 return split /\n/, $cached, 2;
141 }
142  
143  
144 sub _do_request
145 {
146 my $class = shift;
147 my $fake_r = MKDoc::Apache_Cache::Capture->new ($Request);
148 my $ret = $class->SUPER::handler ($fake_r);
149 return ($ret, _make_cache_friendly ($fake_r->data()));
150 }
151  
152  
153 sub _default_identifier
154 {
155 my $class = shift;
156 return "$ENV{REQUEST_METHOD}:" . CGI->new()->self_url();
157 }
158  
159  
160 sub _control_list_process
161 {
162 my $class = shift;
163 my $key = 'MKDoc_Apache_Cache_CONFIG';
164 my $file = Apache->request->dir_config ($key) || $ENV{$key} || '/etc/mkdoc-apache-cache.conf';
165 -e $file && -f $file || do {
166 warn "Cannot stat $file - skipping";
167 return ();
168 };
169
170 my $ctrl = new MKDoc::Control_List ( file => $file );
171 return $ctrl->process();
172 }
173  
174  
175 sub _cache_object
176 {
177 my $class = shift;
178 my %args = ();
179
180 $class->_cache_object_option ('namespace', \%args);
181 $class->_cache_object_option ('default_expires_in', \%args);
182 $class->_cache_object_option ('auto_purge_interval', \%args);
183 $class->_cache_object_option ('auto_purge_on_set', \%args);
184 $class->_cache_object_option ('auto_purge_on_get', \%args);
185 $class->_cache_object_option ('cache_root', \%args);
186 $class->_cache_object_option ('cache_depth', \%args);
187 $class->_cache_object_option ('directory_umask', \%args);
188
189 return new Cache::FileCache ( \%args );
190 }
191  
192  
193 sub _cache_object_option
194 {
195 my $self = shift;
196 my $opt = shift;
197 my $args = shift;
198 my $key = 'MKDoc_Apache_Cache_' . uc ($opt);
199 my $val = Apache->request->dir_config ($key) || $ENV{$key};
200 $key eq 'MKDoc_Apache_Cache_CACHE_ROOT' and do { $val ||= File::Spec->tmpdir() };
201  
202 defined $val and do { $args->{$opt} = $val };
203 }
204  
205  
206 # borrowed from http://www.mnot.net/cgi_buffer/
207 # --------------------------------------------------------------------------------
208 # (c) 2000 Copyright Mark Nottingham
209 # <mnot@pobox.com>
210 #
211 # This software may be freely distributed, modified and used,
212 # provided that this copyright notice remain intact.
213 #
214 # This software is provided 'as is' without warranty of any kind.
215 #
216 # Note from JM: This has been heavily modified from the original, which uses
217 # deprecated libs, doesn't compile under 'use strict', and doesn't care about
218 # unicode.
219 sub _make_cache_friendly
220 {
221 my $buf = shift;
222 $buf ||= '';
223 my ($headers, $body) = split /\r?\n\r?\n/, $buf, 2;
224 $headers ||= '';
225 $body ||= '';
226 my @o = ();
227  
228 # Figure out some kind of content_type
229 my ($content_type) = grep /^content-type\:/i, split (/\r?\n/, $headers);
230 $content_type ||= 'application/octet-stream';
231  
232 # Gzip body if content type probably needs gzipping
233
234 # Vary: Accept-Encoding is here to tell proxies to keep a separate
235 # cache for every different Accept-Encoding that is being sent.
236 $content_type !~ /zip/ and $content_type =~ /(text|xml)/ and do {
237 $body = Compress::Zlib::memGzip ($body);
238 push @o, "Content-Encoding: gzip";
239 push @o, "Vary: Accept-Encoding";
240 };
241  
242 # Compute ETag
243 push @o, "ETag: " . Digest::MD5::md5_hex ($body) if ($body);
244  
245 # Compute Content-Length
246 push @o, "Content-Length: " . length ($body) if ($body);
247  
248 push @o, $headers;
249 push @o, "";
250 push @o, $body;
251 return join "\r\n", @o;
252 }
253  
254  
255 # borrowed from http://search.cpan.org/src/RSE/lcwa-1.0.0/lib/lwp/lib/HTTP/Date.pm
256 # --------------------------------------------------------------------------------
257 our @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
258 our @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
259  
260 sub _http_date (;$)
261 {
262 my $time = shift;
263 $time = time unless defined $time;
264 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($time);
265 sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",
266 $DoW[$wday],
267 $mday, $MoY[$mon], $year+1900,
268 $hour, $min, $sec);
269 }
270 # --------------------------------------------------------------------------------
271  
272  
273 # borrowed / modded from Cache::BaseCache
274 # --------------------------------------------------------------------------------
275 our $EXPIRES_NOW = 'now';
276 our $EXPIRES_NEVER = 'never';
277 our %Expiration_Units = ( map(($_, 1), qw(s second seconds sec)),
278 map(($_, 60), qw(m minute minutes min)),
279 map(($_, 60*60), qw(h hour hours)),
280 map(($_, 60*60*24), qw(d day days)),
281 map(($_, 60*60*24*7), qw(w week weeks)),
282 map(($_, 60*60*24*30), qw(M month months)),
283 map(($_, 60*60*24*365), qw(y year years)) );
284 sub _expiration_time
285 {
286 my ($p_expires_in) = @_;
287 uc ($p_expires_in) eq uc ($EXPIRES_NOW) and return 0;
288 uc ($p_expires_in) eq uc ($EXPIRES_NEVER) and return;
289 $p_expires_in =~ /^\s*([+-]?(?:\d+|\d*\.\d*))\s*$/ and return $p_expires_in;
290 $p_expires_in =~ /^\s*([+-]?(?:\d+|\d*\.\d*))\s*(\w*)\s*$/ and
291 exists $Expiration_Units{$2} and
292 return $Expiration_Units{$2} * $1;
293
294 return 0;
295 }
296 # --------------------------------------------------------------------------------
297  
298  
299 1;
300  
301  
302 __END__