blib/lib/LWPx/UserAgent/Cached.pm

filter: Statementstmt 94.5% 86 / 91 Branchbran 59.3% 19 / 32 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 2 totaltotal 73.9% 125 / 169 CC 25 SCAR 29.3
CC
25
Cov
78%
_set_cache
53.6
_get_cache
8.3
_no_cache_header_directives
5.0
CRAP 18.8
↑ Summary
1 package LWPx::UserAgent::Cached;
2  
3 # ABSTRACT: Subclass of LWP::UserAgent that caches HTTP GET requests
4  
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 our $VERSION = '0.011';
9  
10 ## no critic (Bangs::ProhibitCommentedOutCode)
11  
12 #pod =head1 SYNOPSIS
13 #pod
14 #pod use LWPx::UserAgent::Cached;
15 #pod use CHI;
16 #pod
17 #pod my $ua = LWPx::UserAgent::Cached->new(
18 #pod cache => CHI->new(
19 #pod driver => 'File', root_dir => '/tmp/cache', expires_in => '1d',
20 #pod ),
21 #pod );
22 #pod $ua->get('http://www.perl.org/');
23 #pod
24 #pod =head1 DESCRIPTION
25 #pod
26 #pod This module borrows the caching logic from
27 #pod L<C<WWW::Mechanize::Cached>|WWW::Mechanize::Cached> but
28 #pod without inheriting from
29 #pod L<C<WWW::Mechanize>|WWW::Mechanize>; instead it is just
30 #pod a direct subclass of
31 #pod L<C<LWP::UserAgent>|LWP::UserAgent>.
32 #pod
33 #pod =head2 HTTP/1.1 cache operation
34 #pod
35 #pod Full HTTP/1.1 cache compliance is a work in progress. As of version 0.006 we
36 #pod have limited support for HTTP/1.1 C<ETag>/C<If-None-Match> headers, as well as
37 #pod C<no-cache> and C<no-store> C<Cache-Control> directives (both on request and
38 #pod response) and the C<Pragma: no-cache> request header.
39 #pod
40 #pod =head1 SEE ALSO
41 #pod
42 #pod =over
43 #pod
44 #pod =item L<C<LWP::UserAgent>|LWP::UserAgent>
45 #pod
46 #pod Parent of this class.
47 #pod
48 #pod =item L<C<WWW::Mechanize::Cached>|WWW::Mechanize::Cached>
49 #pod
50 #pod Inspiration for this class.
51 #pod
52 #pod =back
53 #pod
54 #pod =cut
55  
56 8 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 8 ▶use HTTP::Status qw(HTTP_OK HTTP_MOVED_PERMANENTLY HTTP_NOT_MODIFIED);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 8 ▶use List::Util 1.33 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 8 ▶use Moo 1.004005;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 8 ▶use Types::Standard qw(Bool HasMethods HashRef InstanceOf Maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 8 extends 'LWP::UserAgent';
63  
64 #pod =attr cache
65 #pod
66 #pod Settable at construction, defaults to using
67 #pod L<C<CHI::Driver::RawMemory>|CHI::Driver::RawMemory> with
68 #pod an instance-specific hash datastore and a namespace with the current
69 #pod package name. You can use your own caching object here as long as it has
70 #pod C<get> and C<set> methods.
71 #pod
72 #pod =cut
73  
74 has cache => (
75 is => 'lazy',
76 isa => HasMethods [qw(get set)],
77 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 3 CHI->new(
79 serializer => 'Sereal',
80 driver => 'RawMemory',
81 datastore => $_[0]->_cache_datastore,
82 namespace => __PACKAGE__,
83 );
84 },
85 8 );
86 has _cache_datastore =>
87 3 ▶ ( is => 'lazy', isa => HashRef, default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
88  
89 #pod =attr is_cached
90 #pod
91 #pod Read-only accessor that indicates if the current request is cached or not.
92 #pod
93 #pod =cut
94  
95 8 has is_cached =>
96 ( is => 'rwp', isa => Maybe [Bool], init_arg => undef, default => undef );
97  
98 #pod =attr cache_undef_content_length
99 #pod
100 #pod Settable at construction or anytime thereafter, indicates whether we should
101 #pod cache content even if the HTTP C<Content-Length> header is missing or
102 #pod undefined. Defaults to false.
103 #pod
104 #pod =cut
105  
106 8 has cache_undef_content_length => ( is => 'rw', isa => Bool, default => 0 );
107  
108 #pod =attr cache_zero_content_length
109 #pod
110 #pod Settable at construction or anytime thereafter, indicates whether we should
111 #pod cache content even if the HTTP C<Content-Length> header is zero. Defaults to
112 #pod false.
113 #pod
114 #pod =cut
115  
116 8 has cache_zero_content_length => ( is => 'rw', isa => Bool, default => 0 );
117  
118 #pod =attr cache_mismatch_content_length
119 #pod
120 #pod Settable at construction or anytime thereafter, indicates whether we should
121 #pod cache content even if the length of the data does not match the HTTP
122 #pod C<Content-Length> header. Defaults to true.
123 #pod
124 #pod =cut
125  
126 8 has cache_mismatch_content_length =>
127 ( is => 'rw', isa => Bool, default => 1 );
128  
129 #pod =attr ref_in_cache_key
130 #pod
131 #pod Settable at construction or anytime thereafter, indicates whether we should
132 #pod store the HTTP referrer in the cache key. Defaults to false.
133 #pod
134 #pod =cut
135  
136 8 has ref_in_cache_key => ( is => 'rw', isa => Bool, default => 0 );
137  
138 #pod =attr positive_cache
139 #pod
140 #pod Settable at construction or anytime thereafter, indicates whether we should
141 #pod only cache positive responses (HTTP response codes from C<200> to C<300>
142 #pod inclusive) or cache everything. Defaults to true.
143 #pod
144 #pod =cut
145  
146 8 has positive_cache => ( is => 'rw', isa => Bool, default => 1 );
147  
148 #pod =attr ignore_headers
149 #pod
150 #pod Settable at construction or anytime thereafter, indicates whether we should
151 #pod ignore C<Cache-Control: no-cache>, C<Cache-Control: no-store>, and
152 #pod C<Pragma: no-cache> HTTP headers when deciding whether to cache a response.
153 #pod Defaults to false.
154 #pod
155 #pod B<Important note:> This option is potentially dangerous, as it ignores the
156 #pod explicit instructions from the server and thus can lead to returning stale
157 #pod content.
158 #pod
159 #pod =cut
160  
161 8 has ignore_headers => ( is => 'rw', isa => Bool, default => 0 );
162  
163 #pod =head1 HANDLERS
164 #pod
165 #pod This module works by adding C<request_send>, C<response_done> and
166 #pod C<response_header> L<handlers|LWP::UserAgent/Handlers>
167 #pod that run on successful HTTP C<GET> requests.
168 #pod If you need to modify or remove these handlers you may use LWP::UserAgent's
169 #pod L<handler-related methods|LWP::UserAgent/Handlers>.
170 #pod
171 #pod =for Pod::Coverage BUILD
172 #pod
173 #pod =cut
174  
175 13 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
176 13 my $self = shift;
177  
178 13 $self->add_handler( request_send => \&_get_cache, ( m_method => 'GET' ) );
179 13 $self->add_handler(
180 response_done => \&_set_cache,
181 ( m_method => 'GET', m_code => 2 ),
182 );
183 13 $self->add_handler(
184 response_header => \&_get_not_modified,
185 ( m_method => 'GET', m_code => HTTP_NOT_MODIFIED ),
186 );
187  
188 13 return;
189 }
190  
191 # load from cache on each GET request
192 73 ▶sub _get_cache {
Subroutine _get_cache: called CC=8 SCAR=21.2
193 73 my ( $request, $self ) = @_;
194 73 $self->_set_is_cached(0);
195  
196 73 my $clone = $request->clone;
197 73 ▶ if ( not $self->ref_in_cache_key ) { $clone->header( Referer => undef ) }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ref_in_cache_key) 73 0 73
198 73 ▶ return if $self->_no_cache_header_directives($request);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_no_cache_header_directives($request) 6 67 73
199  
200 67 ▶ return if not my $response = $self->cache->get( $clone->as_string );
Branch: 1 branch
BranchTrueFalseTotal
unless my $response = $self->cache->get($clone->as_string) 38 29 67
201 return
202 29 ▶ if $response->code < HTTP_OK
Branch: 1 branch
BranchTrueFalseTotal
if $response->code < 200 or $response->code > 301 0 29 29
Condition33%1/3
$response->code < 200 or $response->code > 301
l!l&&r!l&&!r
0029
MC/DC0%0/2
$response->code < 200 or $response->code > 301
$response->code < 200 $response->code > 301
Truth table33%1/3
A or B
A= $response->code < 200B= $response->code > 301
ABresult
1X1
011
000
203 or $response->code > HTTP_MOVED_PERMANENTLY;
204  
205 29 ▶ if ( $response->header('etag') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header("etag")) 9 20 29
206 9 $clone->header( if_none_match => $response->header('etag') );
207 9 $response = $self->request($clone);
208 }
209 29 ▶ return if $self->_no_cache_header_directives($response);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_no_cache_header_directives($response) 0 29 29
210  
211 29 $self->_set_is_cached(1);
212 29 return $response;
213 }
214  
215 9 ▶sub _get_not_modified {
Subroutine _get_not_modified: called CC=1 SCAR=0.0
216 9 my ( $response, $self ) = @_;
217 9 $self->_set_is_cached(0);
218  
219 9 my $request = $response->request->clone;
220 9 $request->remove_header(qw(if_modified_since if_none_match));
221  
222 9 my $cached_response = $self->cache->get( $request->as_string );
223 9 $response->content( $cached_response->decoded_content );
224  
225 3 $self->_set_is_cached(1);
226 3 return;
227 }
228  
229 # save to cache after successful GET
230 44 ▶sub _set_cache {
Subroutine _set_cache: called CC=13 SCAR=39.8
231 44 my ( $response, $self ) = @_;
232 44 ▶ return if not $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 44 44
233  
234 44 ▶ if (not($response->header('client-transfer-encoding')
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->header("client-transfer-encoding") and &any(sub { "chunked" eq $_; } , $response->header("client-transfer-encoding"))) 44 0 44
Condition33%1/3
$response->header("client-transfer-encoding") and &any(sub { "chunked" eq $_; } , $response->header("client-transfer-encoding"))
!ll&&!rl&&r
4400
MC/DC0%0/2
$response->header("client-transfer-encoding") and &any(sub { "chunked" eq $_; } , $response->header("client-transfer-encoding"))
$response->header("client-transfer-encoding") &any(sub { "chunked" eq $_; } , $response->header("client-transfer-encoding"))
Truth table33%1/3
A and B
A= $response->header("client-transfer-encoding")B= &any(sub { "chunked" eq $_; } , $response->header("client-transfer-encoding"))
ABresult
0X0
100
111
235 0 ▶ and any { 'chunked' eq $_ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
236 $response->header('client-transfer-encoding')
237 )
238 )
239 {
240 44 for ( $response->header('size') ) {
241 return
242 0 ▶ if not defined and $self->cache_undef_content_length;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ and $self->cache_undef_content_length 0 0 0
Condition0%0/3
not defined $_ and $self->cache_undef_content_length
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $_ and $self->cache_undef_content_length
not defined $_ $self->cache_undef_content_length
Truth table0%0/3
not(A) and B
A= not defined $_B= $self->cache_undef_content_length
ABresult
0X0
100
111
243 return
244 0 ▶ if 0 == $_
Branch: 1 branch
BranchTrueFalseTotal
if 0 == $_ and not $self->cache_zero_content_length 0 0 0
Condition0%0/3
0 == $_ and not $self->cache_zero_content_length
!ll&&!rl&&r
000
MC/DC0%0/2
0 == $_ and not $self->cache_zero_content_length
0 == $_ not $self->cache_zero_content_length
Truth table0%0/3
A and not(B)
A= 0 == $_B= not $self->cache_zero_content_length
ABresult
0X0
100
111
245 and not $self->cache_zero_content_length;
246 return
247 0 ▶ if $_ != length $response->content
Branch: 1 branch
BranchTrueFalseTotal
if $_ != length $response->content and not $self->cache_mismatch_content_length 0 0 0
Condition0%0/3
$_ != length $response->content and not $self->cache_mismatch_content_length
!ll&&!rl&&r
000
MC/DC0%0/2
$_ != length $response->content and not $self->cache_mismatch_content_length
$_ != length $response->content not $self->cache_mismatch_content_length
Truth table0%0/3
A and not(B)
A= $_ != length $response->contentB= not $self->cache_mismatch_content_length
ABresult
0X0
100
111
248 and not $self->cache_mismatch_content_length;
249 }
250 }
251  
252 44 for my $message ( $response, $response->request ) {
253 84 ▶ return if $self->_no_cache_header_directives($message);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_no_cache_header_directives($message) 10 74 84
254 }
255  
256 34 $response->decode;
257 34 $self->cache->set( $response->request->as_string => $response );
258 34 return;
259 }
260  
261 186 ▶sub _no_cache_header_directives {
Subroutine _no_cache_header_directives: called CC=5 SCAR=16.1
262 186 my ( $self, $message ) = @_;
263 186 ▶ return if $self->ignore_headers;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ignore_headers 0 186 186
264  
265 186 for my $header_name (qw(pragma cache_control)) {
266 368 ▶ if ( my @directives = $message->header($header_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@directives) = $message->header($header_name)) 27 341 368
267 27 ▶ return 1 if any {/\A no- (?: cache | store ) /xms} @directives;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { /\A no- (?: cache | store ) /msx; } , @directives) 16 11 27
268 }
269 }
270 170 return;
271 }
272  
273 #pod =for Pod::Coverage FOREIGNBUILDARGS
274 #pod
275 #pod =cut
276  
277 ## no critic (Subroutines::RequireArgUnpacking)
278 13 ▶sub FOREIGNBUILDARGS {
Subroutine FOREIGNBUILDARGS: called CC=2 SCAR=8.1
Pod undocumented
279 13 shift;
280 0 ▶ return 'HASH' eq ref $_[0] ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $_[0] ? : 0 13 13
281 }
282  
283 8 1;
284  
285 __END__
↑ Summary