URIFetch.pm

filter: Statementstmt 47.6% 50 / 105 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 75.0% 15 / 20 Podpod 0.0% 0 / 1 totaltotal 38.2% 65 / 170 CC 19 SCAR 49.3
CC
19
Cov
35%
fetch
306.0
new
6.0
BEGIN
2.0
CRAP 137.8
↑ Summary Extension/SimpleRegistration.pm »
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 Net::OpenID::URIFetch - fetch and cache content from HTTP URLs
6  
7 =head1 VERSION
8  
9 version 1.20
10  
11 =head1 DESCRIPTION
12  
13 This is roughly based on Ben Trott's URI::Fetch module, but
14 URI::Fetch doesn't cache enough headers that Yadis can be implemented
15 with it, so this is a lame copy altered to allow Yadis support.
16  
17 Hopefully one day URI::Fetch can be modified to do what we need and
18 this can go away.
19  
20 This module is tailored to the needs of Net::OpenID::Consumer and probably
21 isn't much use outside of it. See URI::Fetch for a more general module.
22  
23 =cut
24  
25 package Net::OpenID::URIFetch;
26 2 $Net::OpenID::URIFetch::VERSION = '1.20';
27 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 ▶use constant URI_OK => 200;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use constant URI_MOVED_PERMANENTLY => 301;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use constant URI_NOT_MODIFIED => 304;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use constant URI_GONE => 410;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 # Fetch a document, either from cache or from a server
39 # URI -- location of document
40 # CONSUMER -- where to find user-agent and cache
41 # CONTENT_HOOK -- applied to freshly-retrieved document
42 # to normalize it into some particular format/structure
43 # PREFIX -- used as part of the cache key, distinguishes
44 # different content formats and must change whenever
45 # CONTENT_HOOK is switched to a new format; this way,
46 # cache entries from a previous run of this server that
47 # are using a different content format will not kill us.
48 0 ▶sub fetch {
Subroutine fetch: not called CC=17 SCAR=57.2
Pod undocumented
49 0 my ($class, $uri, $consumer, $content_hook, $prefix) = @_;
50 0 ▶ $prefix ||= '';
Condition0%0/2
$prefix ||= ""
l!l
00
MC/DC0%0/1
$prefix ||= ""
$prefix
Truth table0%0/2
A
A= $prefix
Aresult
11
00
51  
52 0 ▶ if ($uri eq 'x-xrds-location') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq "x-xrds-location") 0 0 0
53 0 Carp::confess("Buh?");
54 }
55  
56 0 my $ua = $consumer->ua;
57 0 my $cache = $consumer->cache;
58 0 my $ref;
59  
60 0 my $cache_key = "URIFetch:${prefix}:${uri}";
61  
62 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
63 0 ▶ if (my $blob = $cache->get($cache_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $blob = $cache->get($cache_key)) 0 0 0
64 0 $ref = Storable::thaw($blob);
65 }
66 }
67 0 ▶ my $cached_response = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 return Net::OpenID::URIFetch::Response->new(
69 status => 200,
70 content => $ref->{Content},
71 last_modified => $ref->{LastModified},
72 headers => $ref->{Headers},
73 final_uri => $ref->{FinalURI},
74 0 );
75 0 };
76  
77 # We just serve anything from the last 60 seconds right out of the cache,
78 # thus avoiding doing several requests to the same URL when we do
79 # Yadis, then HTML discovery.
80 # TODO: Make this tunable?
81 0 ▶ if ($ref && $ref->{CacheTime} > (time() - 60)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and $ref->{'CacheTime'} > time - 60) { } 0 0 0
Condition0%0/3
$ref and $ref->{"CacheTime"} > time - 60
!ll&&!rl&&r
000
MC/DC0%0/2
$ref and $ref->{"CacheTime"} > time - 60
$ref $ref->{"CacheTime"} > time - 60
Truth table0%0/3
A and B
A= $refB= $ref->{"CacheTime"} > time - 60
ABresult
0X0
100
111
82 0 $consumer->_debug("Cache HIT for $uri");
83 0 return $cached_response->();
84 }
85 else {
86 0 $consumer->_debug("Cache MISS for $uri");
87 }
88  
89 0 my $req = HTTP::Request->new(GET => $uri);
90 0 $req->header('Accept-Encoding', scalar HTTP::Message::decodable());
91 0 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 0 0 0
92 0 ▶ if (my $etag = ($ref->{Headers}->{etag})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $etag = $ref->{"Headers"}{"etag"}) 0 0 0
93 0 $req->header('If-None-Match', $etag);
94 }
95 0 ▶ if (my $ts = $ref->{LastModified}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ts = $ref->{"LastModified"}) 0 0 0
96 0 $req->if_modified_since($ts);
97 }
98 }
99  
100 0 my $res = $ua->request($req);
101  
102 # There are only a few headers that OpenID/Yadis care about
103 0 my @useful_headers = qw(last-modified etag content-type x-yadis-location x-xrds-location);
104  
105 0 my %response_fields;
106  
107 0 ▶ if ($res->code == HTTP::Status::RC_NOT_MODIFIED()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 304) { } 0 0 0
108 0 $consumer->_debug("Server says it's not modified. Serving from cache.");
109 0 return $cached_response->();
110 }
111 else {
112 0 my $final_uri = $res->request->uri->as_string();
113 0 my $final_cache_key = "URIFetch:${prefix}:${final_uri}";
114  
115 0 ▶ my $content = $res->decoded_content # Decode content-encoding and charset
Condition0%0/6
$res->decoded_content || $res->decoded_content('charset', 'none') || $res->content
l!l&&r!l&&!r
000
$res->decoded_content or $res->decoded_content("charset", "none")
l!l&&r!l&&!r
000
MC/DC0%0/3
$res->decoded_content || $res->decoded_content('charset', 'none') || $res->content
$res->decoded_content $res->decoded_content("charset", "none") $res->content
Truth table0%0/4
A or B || C
A= $res->decoded_contentB= $res->decoded_content("charset", "none")C= $res->content
ABCresult
1XX1
01X1
0011
0000
116 || $res->decoded_content(charset => 'none') # Decode content-encoding
117 || $res->content; # Undecoded content
118  
119 0 ▶ if ($content_hook) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_hook) 0 0 0
120 0 $content_hook->(\$content);
121 }
122  
123 0 my $headers = {};
124 0 foreach my $k (@useful_headers) {
125 0 $headers->{$k} = $res->header($k);
126 }
127  
128 0 my $ret = Net::OpenID::URIFetch::Response->new(
129 status => $res->code,
130 last_modified => $res->last_modified,
131 content => $content,
132 headers => $headers,
133 final_uri => $final_uri,
134 );
135  
136 0 ▶ if ($cache && $res->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and $res->code == 200) 0 0 0
Condition0%0/3
$cache and $res->code == 200
!ll&&!rl&&r
000
MC/DC0%0/2
$cache and $res->code == 200
$cache $res->code == 200
Truth table0%0/3
A and B
A= $cacheB= $res->code == 200
ABresult
0X0
100
111
137 0 my $cache_data = {
138 LastModified => $ret->last_modified,
139 Headers => $ret->headers,
140 Content => $ret->content,
141 CacheTime => time(),
142 FinalURI => $final_uri,
143 };
144 0 my $cache_blob = Storable::freeze($cache_data);
145 0 $cache->set($final_cache_key, $cache_blob);
146 0 $cache->set($cache_key, $cache_blob);
147 }
148  
149 0 return $ret;
150 }
151  
152 }
153  
154 package Net::OpenID::URIFetch::Response;
155 2 $Net::OpenID::URIFetch::Response::VERSION = '1.20';
156 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 2 ▶use constant FIELDS => [qw(final_uri status content headers last_modified)];
Subroutine BEGIN: called CC=1 SCAR=0.0
158 2 ▶use fields @{FIELDS()};
Subroutine BEGIN: called CC=1 SCAR=0.0
159 2 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
160  
161 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
162 0 my ($class, %opts) = @_;
163 0 my $self = fields::new($class);
164 0 @{$self}{@{FIELDS()}} = delete @opts{@{FIELDS()}};
165 0 ▶ Carp::croak("Unknown option(s): " . join(", ", keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
166 0 return $self;
167 }
168  
169 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
170 2 foreach my $field_name (@{FIELDS()}) {
171 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
172 10 *{__PACKAGE__ . '::' . $field_name}
173 0 ▶ = sub { return $_[0]->{$field_name}; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
174 }
175 }
176  
177 0 ▶sub header {
Subroutine header: not called CC=1 SCAR=6.9
178 0 return $_[0]->{headers}{lc($_[1])};
179 }
180  
181 2 1;
↑ Summary Extension/SimpleRegistration.pm »