Fetch.pm

filter: Statementstmt 35.5% 42 / 118 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 93.3% 14 / 15 Podpod 100% 1 / 1 totaltotal 25.9% 57 / 220 CC 31 SCAR 65.3
CC
31
Cov
22%
fetch
992.0
BEGIN
1.0
BEGIN
1.0
CRAP 683.7
↑ Summary Fetch/Response.pm »
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
BranchTrueFalseTotal
if %param 0 0 0
40 if %param;
41  
42 0 my $ref;
43 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
44 0 ▶ unless ($freeze && $thaw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($freeze and $thaw) 0 0 0
Condition0%0/3
$freeze and $thaw
!ll&&!rl&&r
000
MC/DC0%0/2
$freeze and $thaw
$freeze $thaw
Truth table0%0/3
A and B
A= $freezeB= $thaw
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (my $blob = $cache->get($uri)) 0 0 0
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
BranchTrueFalseTotal
if ($p_no_net) 0 0 0
56 0 ▶ croak("Invalid NoNetworkValue (negative)") if $p_no_net < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p_no_net < 0 0 0 0
57 0 ▶ if ($ref && ($p_no_net == 1 || $ref->{CacheTime} > time() - $p_no_net)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and $p_no_net == 1 || $ref->{"CacheTime"} > time - $p_no_net) 0 0 0
Condition0%0/6
$ref and $p_no_net == 1 || $ref->{"CacheTime"} > time - $p_no_net
!ll&&!rl&&r
000
$p_no_net == 1 or $ref->{"CacheTime"} > time - $p_no_net
l!l&&r!l&&!r
000
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
ABCresult
0XX0
1000
11X1
1011
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
BranchTrueFalseTotal
if $p_no_net == 1 0 0 0
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 }
l!l&&r!l&&!r
000
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 }
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($URI::Fetch::HAS_ZLIB) 0 0 0
79 0 $req->header('Accept-Encoding', 'gzip');
80 }
81 0 ▶ if (my $etag = ($p_etag || $ref->{ETag})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $etag = $p_etag || $ref->{"ETag"}) 0 0 0
Condition0%0/3
$p_etag || $ref->{'ETag'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$p_etag || $ref->{'ETag'}
$p_etag $ref->{'ETag'}
Truth table0%0/3
A || B
A= $p_etagB= $ref->{'ETag'}
ABresult
1X1
011
000
82 0 $req->header('If-None-Match', $etag);
83 }
84 0 ▶ if (my $ts = ($p_lastmod || $ref->{LastModified})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ts = $p_lastmod || $ref->{"LastModified"}) 0 0 0
Condition0%0/3
$p_lastmod || $ref->{'LastModified'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$p_lastmod || $ref->{'LastModified'}
$p_lastmod $ref->{'LastModified'}
Truth table0%0/3
A || B
A= $p_lastmodB= $ref->{'LastModified'}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($res->previous and $res->previous->code == 301) { } 0 0 0
elsif ($res->code == 410) { } 0 0 0
elsif ($res->code == 304) { } 0 0 0
elsif (not $res->is_success) { } 0 0 0
Condition0%0/3
$res->previous and $res->previous->code == 301
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$force ? : 0 0 0
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
BranchTrueFalseTotal
if ($res->content_encoding and $res->content_encoding eq "gzip") 0 0 0
Condition0%0/3
$res->content_encoding and $res->content_encoding eq "gzip"
!ll&&!rl&&r
000
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($content_hook) 0 0 0
126 0 ▶ croak("ContentAlterHook is not a subref") unless ref $content_hook eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $content_hook eq "CODE" 0 0 0
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
BranchTrueFalseTotal
if ($cache and $p_cache_grep ? &$p_cache_grep($fetch) : 1) 0 0 0
$p_cache_grep ? : 0 0 0
Condition0%0/3
$cache and $p_cache_grep ? &$p_cache_grep($fetch) : 1
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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__
↑ Summary Fetch/Response.pm »