URIFetch.pm

filter: Statementstmt 38.0% 35 / 92 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 65.0% 13 / 20 Podpod 0.0% 0 / 1 totaltotal 31.5% 48 / 152 CC 17 SCAR 49.8
CC
17
Cov
28%
fetch
306.0
new
2.0
final_uri
2.0
CRAP 145.2
↑ 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.030099_004
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 BEGIN {
27 2 ▶ $Net::OpenID::URIFetch::VERSION = '1.030099_004';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 }
29  
30 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 2 our $HAS_ZLIB;
37 BEGIN {
38 2 ▶ $HAS_ZLIB = eval "use Compress::Zlib (); 1;";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
39 }
40  
41 2 ▶use constant URI_OK => 200;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use constant URI_MOVED_PERMANENTLY => 301;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use constant URI_NOT_MODIFIED => 304;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use constant URI_GONE => 410;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 sub fetch {
47 0 ▶ my ($class, $uri, $consumer, $content_hook) = @_;
Subroutine fetch: not called CC=17 SCAR=57.2
Pod undocumented
48  
49 0 ▶ if ($uri eq 'x-xrds-location') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq "x-xrds-location") 0 0 0
50 0 Carp::confess("Buh?");
51 }
52  
53 0 my $ua = $consumer->ua;
54 0 my $cache = $consumer->cache;
55 0 my $ref;
56  
57 # By prefixing the cache key, we can ensure we won't
58 # get left-over cache items from older versions of Consumer
59 # that used URI::Fetch.
60 0 my $cache_key = 'URIFetch:'.$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  
68 # We just serve anything from the last 60 seconds right out of the cache,
69 # thus avoiding doing several requests to the same URL when we do
70 # Yadis, then HTML discovery.
71 # TODO: Make this tunable?
72 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
73 0 $consumer->_debug("Cache HIT for $uri");
74 return Net::OpenID::URIFetch::Response->new(
75 status => 200,
76 content => $ref->{Content},
77 headers => $ref->{Headers},
78 final_uri => $ref->{FinalURI},
79 0 );
80 }
81 else {
82 0 $consumer->_debug("Cache MISS for $uri");
83 }
84  
85 0 my $req = HTTP::Request->new(GET => $uri);
86 0 ▶ if ($HAS_ZLIB) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::OpenID::URIFetch::HAS_ZLIB) 0 0 0
87 0 $req->header('Accept-Encoding', 'gzip');
88 }
89 0 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 0 0 0
90 0 ▶ if (my $etag = ($ref->{Headers}->{etag})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $etag = $ref->{"Headers"}{"etag"}) 0 0 0
91 0 $req->header('If-None-Match', $etag);
92 }
93 0 ▶ if (my $ts = ($ref->{Headers}->{'last-modified'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ts = $ref->{"Headers"}{"last-modified"}) 0 0 0
94 0 $req->if_modified_since($ts);
95 }
96 }
97  
98 0 my $res = $ua->request($req);
99  
100 # There are only a few headers that OpenID/Yadis care about
101 0 my @useful_headers = qw(last-modified etag content-type x-yadis-location x-xrds-location);
102  
103 0 my %response_fields;
104  
105 0 ▶ if ($res->code == HTTP::Status::RC_NOT_MODIFIED()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 304) { } 0 0 0
106 0 $consumer->_debug("Server says it's not modified. Serving from cache.");
107 return Net::OpenID::URIFetch::Response->new(
108 status => 200,
109 content => $ref->{Content},
110 headers => $ref->{Headers},
111 final_uri => $ref->{FinalURI},
112 0 );
113 }
114 else {
115 0 my $content = $res->content;
116 0 my $final_uri = $res->request->uri->as_string();
117 0 my $final_cache_key = "URIFetch:".$final_uri;
118  
119 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
120 0 $content = Compress::Zlib::memGunzip($content);
121 }
122  
123 0 ▶ if ($content_hook) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_hook) 0 0 0
124 0 $content_hook->(\$content);
125 }
126  
127 0 my $headers = {};
128 0 foreach my $k (@useful_headers) {
129 0 $headers->{$k} = $res->header($k);
130 }
131  
132 0 my $ret = Net::OpenID::URIFetch::Response->new(
133 status => $res->code,
134 content => $content,
135 headers => $headers,
136 final_uri => $final_uri,
137 );
138  
139 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
140 0 my $cache_data = {
141 Headers => $ret->headers,
142 Content => $ret->content,
143 CacheTime => time(),
144 FinalURI => $final_uri,
145 };
146 0 my $cache_blob = Storable::freeze($cache_data);
147 0 $cache->set($final_cache_key, $cache_blob);
148 0 $cache->set($cache_key, $cache_blob);
149 }
150  
151 0 return $ret;
152 }
153  
154 }
155  
156 package Net::OpenID::URIFetch::Response;
157 BEGIN {
158 2 ▶ $Net::OpenID::URIFetch::Response::VERSION = '1.030099_004';
Subroutine BEGIN: called CC=1 SCAR=0.0
159 }
160  
161 sub new {
162 0 ▶ my ($class, %opts) = @_;
Subroutine new: not called CC=1 SCAR=6.9
163  
164 0 my $self = {};
165 0 $self->{final_uri} = delete($opts{final_uri});
166 0 $self->{status} = delete($opts{status});
167 0 $self->{content} = delete($opts{content});
168 0 $self->{headers} = delete($opts{headers});
169  
170 0 return bless $self, $class;
171 }
172  
173 sub final_uri {
174 0 ▶ return $_[0]->{final_uri};
Subroutine final_uri: not called CC=1 SCAR=6.9
175 }
176  
177 sub status {
178 0 ▶ return $_[0]->{status};
Subroutine status: not called CC=1 SCAR=6.9
179 }
180  
181 sub content {
182 0 ▶ return $_[0]->{content};
Subroutine content: not called CC=1 SCAR=6.9
183 }
184  
185 sub headers {
186 0 ▶ return $_[0]->{headers};
Subroutine headers: not called CC=1 SCAR=6.9
187 }
188  
189 sub header {
190 0 ▶ return $_[0]->{headers}{lc($_[1])};
Subroutine header: not called CC=1 SCAR=6.9
191 }
192  
193 2 1;
↑ Summary Extension/SimpleRegistration.pm »