| 1 | package URI::Fetch; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | $URI::Fetch::VERSION = '0.15'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use base qw( Class::ErrorHandler ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use URI::Fetch::Response; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | our $HAS_ZLIB; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | $HAS_ZLIB = eval "use Compress::Zlib (); 1;"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use constant URI_OK => 200; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use constant URI_MOVED_PERMANENTLY => 301; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use constant URI_NOT_MODIFIED => 304; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use constant URI_GONE => 410; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=31 SCAR=69.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | my($uri, %param) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # get user parameters | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $cache = delete $param{Cache}; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $ua = delete $param{UserAgent}; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | my $p_etag = delete $param{ETag}; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my $p_lastmod = delete $param{LastModified}; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $content_hook = delete $param{ContentAlterHook}; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $p_no_net = delete $param{NoNetwork}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my $p_cache_grep = delete $param{CacheEntryGrep}; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | my $freeze = delete $param{Freeze}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | my $thaw = delete $param{Thaw}; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my $force = delete $param{ForceResponse}; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | croak("Unknown parameters: " . join(", ", keys %param)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | if %param; | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $ref; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ($cache) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | unless ($freeze && $thaw) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$freeze and $thaw
MC/DC0%0/2
$freeze and $thaw
$freeze
$thaw
Truth table0%0/3
A and B
A= $freezeB= $thaw
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | require Storable; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $thaw = \&Storable::thaw; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $freeze = \&Storable::freeze; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if (my $blob = $cache->get($uri)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $ref = $thaw->($blob); | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # NoNetwork support (see pod docs below for logic clarification) | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ($p_no_net) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | croak("Invalid NoNetworkValue (negative)") if $p_no_net < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ($ref && ($p_no_net == 1 || $ref->{CacheTime} > time() - $p_no_net)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$ref and $p_no_net == 1 || $ref->{"CacheTime"} > time - $p_no_net
$p_no_net == 1 or $ref->{"CacheTime"} > time - $p_no_net
MC/DC0%0/3
$ref and $p_no_net == 1 || $ref->{"CacheTime"} > time - $p_no_net
$ref
$p_no_net == 1
$ref->{"CacheTime"} > time - $p_no_net
Truth table0%0/4
A and B or C
A= $refB= $p_no_net == 1C= $ref->{"CacheTime"} > time - $p_no_net
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $fetch = URI::Fetch::Response->new; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $fetch->status(URI_OK); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $fetch->content($ref->{Content}); | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $fetch->etag($ref->{ETag}); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $fetch->last_modified($ref->{LastModified}); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $fetch->content_type($ref->{ContentType}); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | return $fetch; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | return undef if $p_no_net == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $ua ||= do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$ua ||= do {
my $ua = "LWP::UserAgent"->new;
$ua->agent(join("/", $class, $class->VERSION));
$ua->env_proxy;
$ua
}
MC/DC0%0/2
$ua ||= do {
my $ua = "LWP::UserAgent"->new;
$ua->agent(join("/", $class, $class->VERSION));
$ua->env_proxy;
$ua
}
$ua
do {
my $ua = "LWP::UserAgent"->new;
$ua->agent(join("/", $class, $class->VERSION));
$ua->env_proxy;
$ua
}
Truth table0%0/3
A ||= B
A= $uaB= do {
my $ua = "LWP::UserAgent"->new;
$ua->agent(join("/", $class, $class->VERSION));
$ua->env_proxy;
$ua
}
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $ua->agent(join '/', $class, $class->VERSION); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $ua->env_proxy; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $ua; | |||||||||||||||||||||||||||||||||||||||||
| 75 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $req = HTTP::Request->new(GET => $uri); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($HAS_ZLIB) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $req->header('Accept-Encoding', 'gzip'); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if (my $etag = ($p_etag || $ref->{ETag})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$p_etag || $ref->{'ETag'}
MC/DC0%0/2
$p_etag || $ref->{'ETag'}
$p_etag
$ref->{'ETag'}
Truth table0%0/3
A || B
A= $p_etagB= $ref->{'ETag'}
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $req->header('If-None-Match', $etag); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if (my $ts = ($p_lastmod || $ref->{LastModified})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$p_lastmod || $ref->{'LastModified'}
MC/DC0%0/2
$p_lastmod || $ref->{'LastModified'}
$p_lastmod
$ref->{'LastModified'}
Truth table0%0/3
A || B
A= $p_lastmodB= $ref->{'LastModified'}
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $req->if_modified_since($ts); | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $res = $ua->request($req); | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $fetch = URI::Fetch::Response->new; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $fetch->uri($uri); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $fetch->http_status($res->code); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $fetch->http_response($res); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $fetch->content_type($res->header('Content-Type')); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ($res->previous && $res->previous->code == HTTP::Status::RC_MOVED_PERMANENTLY()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition0%0/3
$res->previous and $res->previous->code == 301
MC/DC0%0/2
$res->previous and $res->previous->code == 301
$res->previous
$res->previous->code == 301
Truth table0%0/3
A and B
A= $res->previousB= $res->previous->code == 301
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $fetch->status(URI_MOVED_PERMANENTLY); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $fetch->uri($res->previous->header('Location')); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } elsif ($res->code == HTTP::Status::RC_GONE()) { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $fetch->status(URI_GONE); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $fetch->uri(undef); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | return $fetch; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } elsif ($res->code == HTTP::Status::RC_NOT_MODIFIED()) { | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $fetch->status(URI_NOT_MODIFIED); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $fetch->content($ref->{Content}); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $fetch->etag($ref->{ETag}); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $fetch->last_modified($ref->{LastModified}); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $fetch->content_type($ref->{ContentType}); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | return $fetch; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } elsif (!$res->is_success) { | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | return $force ? $fetch : $class->error($res->message); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $fetch->status(URI_OK); | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $fetch->last_modified($res->last_modified); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $fetch->etag($res->header('ETag')); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my $content = $res->content; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($res->content_encoding && $res->content_encoding eq 'gzip') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res->content_encoding and $res->content_encoding eq "gzip"
MC/DC0%0/2
$res->content_encoding and $res->content_encoding eq "gzip"
$res->content_encoding
$res->content_encoding eq "gzip"
Truth table0%0/3
A and B
A= $res->content_encodingB= $res->content_encoding eq "gzip"
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $content = Compress::Zlib::memGunzip($content); | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # let caller-defined transform hook modify the result that'll be | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # cached. perhaps the caller only wants the <head> section of | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # HTML, or wants to change the content to a parsed datastructure | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # already serialized with Storable. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ($content_hook) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | croak("ContentAlterHook is not a subref") unless ref $content_hook eq "CODE"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | $content_hook->(\$content); | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $fetch->content($content); | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # cache by default, if there's a cache. but let callers cancel | ||||||||||||||||||||||||||||||||||||||||||
| 133 | # the cache action by defining a cache grep hook | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ($cache && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$cache and $p_cache_grep ? &$p_cache_grep($fetch) : 1
MC/DC0%0/2
$cache and $p_cache_grep ? &$p_cache_grep($fetch) : 1
$cache
$p_cache_grep ? &$p_cache_grep($fetch) : 1
Truth table0%0/3
A and B
A= $cacheB= $p_cache_grep ? &$p_cache_grep($fetch) : 1
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | ($p_cache_grep ? $p_cache_grep->($fetch) : 1)) { | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $cache->set($uri, $freeze->({ | |||||||||||||||||||||||||||||||||||||||||
| 138 | ETag => $fetch->etag, | ||||||||||||||||||||||||||||||||||||||||||
| 139 | LastModified => $fetch->last_modified, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | Content => $fetch->content, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | CacheTime => time(), | ||||||||||||||||||||||||||||||||||||||||||
| 142 | ContentType => $fetch->content_type, | ||||||||||||||||||||||||||||||||||||||||||
| 143 | })); | ||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | $fetch; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||||||||||||