/usr/share/perl5/HTTP/Cache/Transparent.pm is in libhttp-cache-transparent-perl 1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | package HTTP::Cache::Transparent;
use strict;
our $VERSION = '1.1';
=head1 NAME
HTTP::Cache::Transparent - Cache the result of http get-requests persistently.
=head1 SYNOPSIS
use LWP::Simple;
use HTTP::Cache::Transparent;
HTTP::Cache::Transparent::init( {
BasePath => '/tmp/cache',
} );
my $data = get( 'http://www.sn.no' );
=head1 DESCRIPTION
An implementation of http get that keeps a local cache of fetched
pages to avoid fetching the same data from the server if it hasn't
been updated. The cache is stored on disk and is thus persistent
between invocations.
Uses the http-headers If-Modified-Since and ETag to let the server
decide if the version in the cache is up-to-date or not.
The cache is implemented by modifying the LWP::UserAgent class to
seamlessly cache the result of all requests that can be cached.
=head1 INITIALIZING THE CACHE
HTTP::Cache::Transparent provides an init-method that sets the
parameters for the cache and overloads a method in LWP::UserAgent
to activate the cache.After init has been called, the normal
LWP-methods (LWP::Simple as well as the more full-fledged
LWP::Request methods) should be used as usual.
=over 4
=cut
use Carp;
use LWP::UserAgent;
use HTTP::Status qw/RC_NOT_MODIFIED RC_OK RC_PARTIAL_CONTENT status_message/;
use Digest::MD5 qw/md5_hex/;
use IO::File;
use File::Copy;
use File::Path;
use Cwd;
# These are the response-headers that we should store in the
# cache-entry and recreate when we return a cached response.
my @cache_headers = qw/Content-Type Content-Encoding
Content-Length Content-Range
Last-Modified/;
my $basepath;
my $maxage;
my $verbose;
my $noupdate;
my $approvecontent;
my $org_simple_request;
=item init
Initialize the HTTP cache. Takes a single parameter which is a
hashref containing named arguments to the object.
HTTP::Cache::Transparent::init( {
# Directory to store the cache in.
BasePath => "/tmp/cache",
# How many hours should items be kept in the cache
# after they were last requested?
# Default is 8*24.
MaxAge => 8*24,
# Print progress-messages to STDERR.
# Default is 0.
Verbose => 1,
# If a request is made for a url that has been requested
# from the server less than NoUpdate seconds ago, the
# response will be generated from the cache without
# contacting the server.
# Default is 0.
NoUpdate => 15*60,
# When a url has been downloaded and the response indicates that
# has been modified compared to the content in the cache,
# the ApproveContent callback is called with the HTTP::Response.
# The callback shall return true if the response shall be used and
# stored in the cache or false if the response shall be discarded
# and the response in the cache used instead.
# This mechanism can be used to work around servers that return errors
# intermittently. The default is to accept all responses.
ApproveContent => sub { return $_[0]->is_success },
} );
The directory where the cache is stored must be writable. It must also only
contain files created by HTTP::Cache::Transparent.
=cut
my $initialized = 0;
sub init {
my( $arg ) = @_;
defined( $arg->{BasePath} )
or croak( "You must specify a BasePath" );
$basepath = $arg->{BasePath};
if( not -d $basepath ) {
eval { mkpath($basepath) };
if ($@) {
print STDERR "$basepath is not a directory and cannot be created: $@\n";
exit 1;
}
}
# Append a trailing slash if it is missing.
$basepath =~ s%([^/])$%$1/%;
$maxage = $arg->{MaxAge} || 8*24;
$verbose = $arg->{Verbose} || 0;
$noupdate = $arg->{NoUpdate} || 0;
$approvecontent = $arg->{ApproveContent} || sub { return 1; };
# Make sure that LWP::Simple does not use its simplified
# get-method that bypasses LWP::UserAgent.
$LWP::Simple::FULL_LWP++;
unless ($initialized++) {
$org_simple_request = \&LWP::UserAgent::simple_request;
no warnings;
*LWP::UserAgent::simple_request = \&_simple_request_cache
}
}
=item Initializing from use-line
An alternative way of initializing HTTP::Cache::Transparent is to supply
parameters in the use-line. This allows you to write
use HTTP::Cache::Transparent ( BasePath => '/tmp/cache' );
which is exactly equivalent to
use HTTP::Cache::Transparent;
HTTP::Cache::Transparent::init( BasePath => '/tmp/cache' );
The advantage to using this method is that you can do
perl -MHTTP::Cache::Transparent=BasePath,/tmp/cache myscript.pl
or even set the environment variable PERL5OPT
PERL5OPT=-MHTTP::Cache::Transparent=BasePath,/tmp/cache
myscript.pl
and have all the http-requests performed by myscript.pl go through the
cache without changing myscript.pl
=back
=cut
sub import {
my( $module, %args ) = @_;
return if (scalar(keys(%args)) == 0);
HTTP::Cache::Transparent::init( \%args );
}
END {
_remove_old_entries();
}
sub _simple_request_cache {
my($self, $r, $content_cb, $read_size_hint) = @_;
my $res;
if( $r->method eq "GET" and
not defined( $r->header( 'If-Modified-Since' ) ) and
not defined( $content_cb ) ) {
print STDERR "Fetching " . $r->uri
if( $verbose );
my $url = $r->uri->as_string;
my $key = $url;
$key .= "\n" . $r->header('Range')
if defined $r->header('Range');
my $filename = $basepath . _urlhash( $url );
my $fh;
my $meta;
if( -s $filename ) {
$fh = new IO::File "< $filename"
or die "Failed to read from $filename";
$meta = _read_meta( $fh );
if( $meta->{Url} eq $url ) {
$meta->{'Range'} = ""
unless defined( $meta->{'Range'} );
# Check that the Range is the same for this request as
# for the one in the cache.
if( (not defined( $r->header( 'Range' ) ) ) or
$r->header( 'Range' ) eq $meta->{'Range'} ) {
$r->header( 'If-Modified-Since', $meta->{'Last-Modified'} )
if exists( $meta->{'Last-Modified'} );
$r->header( 'If-None-Match', $meta->{ETag} )
if( exists( $meta->{ETag} ) );
}
}
else {
warn "Cache collision: $url and $meta->{Url} have the same md5sum";
}
}
if( defined( $meta->{'X-HCT-LastUpdated'} ) and
$noupdate > (time - $meta->{'X-HCT-LastUpdated'} ) ) {
print STDERR " from cache without checking with server.\n"
if $verbose;
$res = HTTP::Response->new( $meta->{Code} );
$res->request($r);
_get_from_cachefile( $filename, $fh, $res, $meta );
$fh->close()
if defined $fh;;
return $res;
}
$res = &$org_simple_request( $self, $r );
if( $res->code == RC_NOT_MODIFIED ) {
print STDERR " from cache.\n"
if( $verbose );
_get_from_cachefile( $filename, $fh, $res, $meta );
$fh->close()
if defined $fh;;
# We need to rewrite the cache-entry to update X-HCT-LastUpdated
_write_cache_entry( $filename, $url, $r, $res );
return $res;
}
elsif( defined( $meta->{'X-HCT-LastUpdated'} )
and not &{$approvecontent}( $res ) ) {
print STDERR " from cache since the response was not approved.\n"
if( $verbose );
_get_from_cachefile( $filename, $fh, $res, $meta );
$fh->close()
if defined $fh;;
# Do NOT update the cache!
return $res;
}
else {
$fh->close()
if defined $fh;;
my $content = $res->content;
$content = "" if not defined $content;
if( defined( $meta->{MD5} ) and
md5_hex( $content ) eq $meta->{MD5} ) {
$res->header( "X-Content-Unchanged", 1 );
print STDERR " unchanged"
if( $verbose );
}
print STDERR " from server.\n"
if( $verbose );
_write_cache_entry( $filename, $url, $r, $res )
if( $res->code == RC_OK or
$res->code == RC_PARTIAL_CONTENT );
}
}
else {
# We won't try to cache this request.
$res = &$org_simple_request( $self, $r,
$content_cb, $read_size_hint );
}
return $res;
}
sub _get_from_cachefile {
my( $filename, $fh, $res, $meta ) = @_;
my $content;
my $buf;
while ( $fh->read( $buf, 1024 ) > 0 ) {
$content .= $buf;
}
$fh->close();
$content = "" if not defined $content;
# Set last-accessed for cache-entry.
my $mtime = time;
utime( $mtime, $mtime, $filename );
# modify response
if( $HTTP::Message::VERSION >= 1.44 ) {
$res->content_ref( \$content );
}
else {
$res->content( $content );
}
# For HTTP::Cache::Transparent earlier than 0.4,
# there is no Code in the cache.
if( defined( $meta->{Code} ) ) {
$res->code( $meta->{Code} );
}
else {
$res->code( RC_OK );
}
$res->message(status_message($res->code) || "Unknown code");
foreach my $h (@cache_headers) {
$res->header( $h, $meta->{$h} )
if defined( $meta->{ $h } );
}
$res->header( "X-Cached", 1 );
$res->header( "X-Content-Unchanged", 1 );
}
# Read metadata and position filehandle at start of data.
sub _read_meta {
my( $fh ) = @_;
my %meta;
my( $key, $value );
do {
my $line = <$fh>;
( $key, $value ) = ($line =~ /(\S+)\s+(.*)[\n\r]*/);
$meta{$key} = $value
if( defined $value );
} while( defined( $value ) );
return \%meta;
}
# Write metadata and position filehandle where data should be written.
sub _write_meta {
my( $fh, $meta ) = @_;
foreach my $key (sort keys( %{$meta} ) ) {
print $fh "$key $meta->{$key}\n";
}
print $fh "\n";
}
sub _write_cache_entry {
my( $filename, $url, $req, $res ) = @_;
my $out_filename = "$filename.tmp$$";
my $fh = new IO::File "> $out_filename"
or die "Failed to write to $out_filename";
my $meta;
$meta->{Url} = $url;
$meta->{ETag} = $res->header('ETag')
if defined( $res->header('ETag') );
my $content = $res->content;
$content = "" if not defined $content;
$meta->{MD5} = md5_hex( $content );
$meta->{Range} = $req->header('Range')
if defined( $req->header('Range') );
$meta->{Code} = $res->code;
$meta->{'X-HCT-LastUpdated'} = time;
foreach my $h (@cache_headers) {
$meta->{$h} = $res->header( $h )
if defined $res->header( $h );
}
_write_meta( $fh, $meta );
print $fh $content;
$fh->close;
move( $out_filename, $filename ) || unlink $out_filename;
}
sub _urlhash {
my( $url ) = @_;
return md5_hex( $url );
}
sub _remove_old_entries {
if( defined( $basepath ) and -d( $basepath ) ) {
my $oldcwd = getcwd();
chdir( $basepath );
my @files = glob("*");
foreach my $file (@files) {
if( $file !~ m%^[0-9a-f]{32}$% ) {
print STDERR "HTTP::Cache::Transparent: Unknown file found in cache directory: $basepath$file\n";
}
else {
my $age = (-M $file);
# The file may have disappeared if another process has cleaned
# the cache.
if( defined($age) && ( $age*24 > $maxage ) ) {
print STDERR "Deleting $file.\n"
if( $verbose );
unlink( $file );
}
}
}
chdir( $oldcwd );
}
}
=head1 INSPECTING CACHE BEHAVIOR
The HTTP::Cache::Transparent inserts two special headers in the
HTTP::Response object. These can be accessed via the
HTTP::Response::header()-method.
=over 4
=item X-Cached
This header is inserted and set to 1 if the response is delivered from
the cache instead of from the server.
=item X-Content-Unchanged
This header is inserted and set to 1 if the content returned is the same
as the content returned the last time this url was fetched. This header
is always inserted and set to 1 when the response is delivered from
the cache.
=back
=head1 LIMITATIONS
This module has a number of limitations that you should be aware of
before using it.
=over 4
=item -
There is no upper limit to how much diskspace the cache requires. The
only limiting mechanism is that data for urls that haven't been requested
in the last MaxAge hours will be removed from the cache the next time
the program exits.
=item -
Currently, only get-requests that store the result in memory (i.e. do
not use the option to have the result stored directly in a file or
delivered via a callback) is cached. I intend to remove this limitation
in a future version.
=item -
The support for Ranges is a bit primitive. It creates a new object in
the cache for each unique combination of url and range. This will work ok
as long as you always request the same range(s) for a url.
=item -
The cache doesn't properly check and store all headers in the HTTP
request and response. Therefore, if you request the same url repeatedly
with different sets of headers (cookies, accept-encoding etc), and these
headers affect the response from the server, the cache may return the
wrong response.
=back
=head1 CACHE FORMAT
The cache is stored on disk as one file per cached object. The filename
is equal to the md5sum of the url and the Range-header if it exists.
The file contains a set of
key/value-pairs with metadata (one entry per line) followed by a blank
line and then the actual data returned by the server.
The last modified date of the cache file is set to the time when the
cache object was last requested by a user.
=head1 AUTHOR
Mattias Holmlund, E<lt>$firstname -at- $lastname -dot- seE<gt>
L<http://www.holmlund.se/mattias/>
=head1 GIT REPOSITORY
A git repository containing the source for this module can be found
via http://git.holmlund.se/
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2004-2007 by Mattias Holmlund
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.4 or,
at your option, any later version of Perl 5 you may have available.
=cut
1;
|