blib/lib/WWW/Mechanize/Cached.pm

filter: Statementstmt 81.7% 112 / 137 Branchbran 54.5% 36 / 66 Conditioncond 46.6% 7 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 90.0% 18 / 20 Podpod 50.0% 1 / 2 totaltotal 70.5% 175 / 248 SCAR 44.0
CC
41
Cov
71%
_response_cache_ok
62.1
_dwarn
20.0
__ANON__
6.0
CRAP 81.6
↑ Summary
1 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 12 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package WWW::Mechanize::Cached;
5  
6 12 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Moo 1.004005;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use MooX::Types::MooseLike::Base qw(AnyOf Bool Enum Maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 extends 'WWW::Mechanize';
13  
14 12 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Storable qw( nfreeze thaw );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 12 our $VERSION = '1.55';
19  
20 12 has is_cached => ( is => 'rw', isa => Maybe [Bool], default => undef );
21 12 has positive_cache => ( is => 'rw', isa => Bool, default => 1 );
22 12 has ref_in_cache_key => ( is => 'rw', isa => Bool, default => 0 );
23 12 has _verbose_dwarn => ( is => 'rw', isa => Bool, default => 0 );
24 12 has _last_request => ( is => 'rw' );
25  
26 12 has [qw/ cache_undef_content_length cache_zero_content_length /] =>
27 ( is => 'rw', isa => AnyOf [ Bool, Enum ['warn'] ], default => 0 );
28  
29 12 has cache_mismatch_content_length => (
30 is => 'rw',
31 isa => AnyOf [ Bool, Enum ['warn'] ],
32 default => 'warn',
33 );
34  
35 12 has cache => ( is => 'lazy', isa => \&_isa_warn_cache );
36  
37 sub FOREIGNBUILDARGS {
38 22 ▶ my ( $class, %args ) = @_;
Subroutine FOREIGNBUILDARGS: called CC=2 SCAR=6.9
Pod undocumented
39  
40 # WWW::Mechanize/LWP::UserAgent would complain about these
41 22 for my $attribute (
42 qw(
43 is_cached
44 positive_cache
45 ref_in_cach_key
46 _verbose_dwarn
47 cache_undef_content_length
48 cache_zero_content_length
49 cache_mismatch_content_length
50 cache
51 )
52 ) {
53 176 delete $args{$attribute};
54 }
55  
56 22 return %args;
57 }
58  
59 sub _isa_warn_cache {
60 return
61 10 ▶ if 'HASH' ne ref $_[0]
Subroutine _isa_warn_cache: called CC=4 SCAR=14.1
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" ne ref $_[0] and $_[0]->can("get") and $_[0]->can("set") 9 1 10
Condition66%4/6
"HASH" ne ref $_[0] and $_[0]->can("get") and $_[0]->can("set")
!ll&&!rl&&r
109
"HASH" ne ref $_[0] and $_[0]->can("get")
!ll&&!rl&&r
109
MC/DC33%1/3
"HASH" ne ref $_[0] and $_[0]->can("get") and $_[0]->can("set")
"HASH" ne ref $_[0] $_[0]->can("get") $_[0]->can("set")
Truth table50%2/4
A and B and C
A= "HASH" ne ref $_[0]B= $_[0]->can("get")C= $_[0]->can("set")
ABCresult
0XX0
10X0
1100
1111
62 and $_[0]->can('get')
63 and $_[0]->can('set');
64 1 carp 'The cache param must be an initialized cache object';
65 1 $_[0] = undef;
66 }
67  
68 sub _build_cache {
69 2 ▶ my $self = shift;
Subroutine _build_cache: called CC=3 SCAR=16.0
70  
71 return Cache::FileCache->new(
72 {
73 default_expires_in => '1d',
74 namespace => 'www-mechanize-cached',
75 }
76 2 ▶ ) if eval { use_module('Cache::FileCache') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { &use_module("Cache::FileCache") } } 2 0 2
77  
78 return CHI->new(
79 driver => 'File',
80 expires_in => '1d',
81 namespace => 'www-mechanize-cached',
82 0 ▶ ) if eval { use_module('CHI') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { &use_module("CHI") } } 0 0 0
83  
84 0 croak( 'Could not create a default cache.'
85 . 'Please make sure either CHI or Cache::FileCache are installed or configure manually as appropriate'
86 );
87 }
88  
89 around _make_request => sub {
90 59 ▶ my ( $orig, $self, $request ) = splice @_, 0, 3;
Subroutine __ANON__: called CC=6 SCAR=17.9
91 59 my $req = $request;
92  
93 59 $self->is_cached(0);
94  
95 # An odd line to need.
96 # No idea what purpose this serves? OALDERS
97 59 ▶ $self->{proxy} = {} unless defined $self->{proxy}; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"proxy"} 0 59 59
98  
99 # RT #56757
100 59 ▶ if ( !$self->ref_in_cache_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ref_in_cache_key) 59 0 59
101 59 my $clone = $request->clone;
102 59 $clone->header( Referer => undef );
103 59 $req = $clone->as_string;
104 }
105  
106 59 my $response = $self->cache->get($req);
107  
108 59 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 32 27 59
109 32 $response = thaw($response);
110 }
111 59 ▶ if ( $self->_cache_ok($response) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cache_ok($response)) 32 27 59
112 32 $self->is_cached(1);
113 32 $self->_last_request($req);
114 32 return $response;
115 }
116  
117 27 $response = $self->$orig( $request, @_ );
118  
119 # decode strips some important headers.
120 27 my $headers = $response->headers->clone;
121  
122 27 my $should_cache = $self->_response_cache_ok( $response, $headers );
123  
124 # http://rt.cpan.org/Public/Bug/Display.html?id=42693
125 27 $response->decode();
126 27 delete $response->{handlers}; ## no critic
127  
128 27 ▶ if ($should_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_cache) 18 9 27
129 18 $self->_last_request($req);
130 18 $self->cache->set( $req, nfreeze($response) );
131 }
132  
133 27 return $response;
134 12 };
135  
136 sub invalidate_last_request {
137 4 ▶ my $self = shift;
Subroutine invalidate_last_request: called CC=4 SCAR=14.0
Pod documented
138 4 ▶ return unless $self->is_cached;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_cached 2 2 4
139  
140 2 my $request = $self->_last_request;
141 2 ▶ return unless $request;
Branch: 1 branch
BranchTrueFalseTotal
unless $request 0 2 2
142  
143 2 $self->cache->remove($request);
144 2 ▶ return $self->is_cached( $self->cache->get($request) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$self->cache->get($request) ? : 0 2 2
145 }
146  
147 sub _dwarn_filter {
148 return {
149 0 ▶ hide_keys => [
Subroutine _dwarn_filter: not called CC=1 SCAR=6.9
150 qw( _content cookie content set-cookie handlers cookie_jar cache req res page_stack )
151 ]
152 };
153  
154 }
155  
156 sub _dwarn {
157 0 ▶ my $self = shift;
Subroutine _dwarn: not called CC=4 SCAR=30.0
158 0 my $message = shift;
159  
160 0 ▶ return unless my $handler = $self->{onwarn}; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
unless my $handler = $self->{"onwarn"} 0 0 0
161  
162 0 ▶ return if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 0 0 0
163  
164 0 ▶ if ( $self->_verbose_dwarn ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_verbose_dwarn) { } 0 0 0
165 0 my $payload = {
166 self => $self,
167 message => $message,
168 debug => \@_,
169 };
170 0 require Data::Dump;
171 0 return $handler->( Data::Dump::dumpf( $payload, \&_dwarn_filter ) );
172 }
173 else {
174 0 return $handler->($message);
175 }
176 }
177  
178 sub _response_cache_ok {
179 30 ▶ my $self = shift;
Subroutine _response_cache_ok: called CC=20 SCAR=41.3
180 30 my $response = shift;
181 30 my $headers = shift;
182  
183 30 ▶ return 0 if !$response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 30 30
184 30 ▶ return 1 if !$self->positive_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->positive_cache 0 30 30
185  
186 30 ▶ return 0 if $response->code < 200;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code < 200 0 30 30
187 30 ▶ return 0 if $response->code > 301;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code > 301 0 30 30
188  
189 30 my $size;
190 {
191 30 ▶ if ( $headers->header('Client-Transfer-Encoding') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->header("Client-Transfer-Encoding")) 3 27 30
192 3 my @cte = $headers->header('Client-Transfer-Encoding');
193 3 for my $cte (@cte) {
194  
195 # Transfer-Encoding = chunked means document consistency
196 # is independent of Content-Length value,
197 # and that Content-Length can be safely ignored.
198 # Its not obvious how the lower levels represent a
199 # failed chunked-transfer yet.
200 # But its safe to say relying on content-length proves pointless.
201 3 ▶ return 1 if $cte eq 'chunked';
Branch: 1 branch
BranchTrueFalseTotal
if $cte eq "chunked" 2 1 3
202 }
203 }
204  
205 28 $size = $headers->{'content-length'};
206 }
207  
208 28 ▶ if ( not defined $size ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size) 10 18 28
209 10 ▶ if ( $self->cache_undef_content_length . q{} eq q{warn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_undef_content_length . '' eq "warn") 0 10 10
210 0 $self->_dwarn(
211 q[Content-Length header was undefined, not caching]
212 . q[ (E=WWW_MECH_CACHED_CONTENTLENGTH_MISSING)],
213 $headers
214 );
215 0 return 0;
216 }
217 10 ▶ if ( $self->cache_undef_content_length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_undef_content_length == 0) 10 0 10
218 10 return 0;
219 }
220 }
221  
222 18 ▶ if ( defined $size and $size == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size and $size == 0) 0 18 18
Condition33%1/3
defined $size and $size == 0
!ll&&!rl&&r
0180
MC/DC0%0/2
defined $size and $size == 0
defined $size $size == 0
Truth table33%1/3
A and B
A= defined $sizeB= $size == 0
ABresult
0X0
100
111
223 0 ▶ if ( $self->cache_zero_content_length . q{} eq q{warn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_zero_content_length . '' eq "warn") 0 0 0
224 0 $self->_dwarn(
225 q{Content-Length header was 0, not caching}
226 . q{ (E=WWW_MECH_CACHED_CONTENTLENGTH_ZERO)},
227 $headers
228 );
229 0 return 0;
230 }
231 0 ▶ if ( $self->cache_zero_content_length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_zero_content_length == 0) 0 0 0
232 0 return 0;
233 }
234 }
235  
236 18 ▶ if ( defined $size
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size and $size != 0 and $size != length $response->content) 0 18 18
Condition33%2/6
defined $size and $size != 0 and $size != length $response->content
!ll&&!rl&&r
0180
defined $size and $size != 0
!ll&&!rl&&r
0018
MC/DC0%0/3
defined $size and $size != 0 and $size != length $response->content
defined $size $size != 0 $size != length $response->content
Truth table25%1/4
A and B and C
A= defined $sizeB= $size != 0C= $size != length $response->content
ABCresult
0XX0
10X0
1100
1111
237 and $size != 0
238 and $size != length( $response->content ) ) {
239 0 ▶ if ( $self->cache_mismatch_content_length . "" eq "warn" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_mismatch_content_length . '' eq "warn") 0 0 0
240 0 $self->_dwarn(
241 q{Content-Length header did not match contents actual length, not caching}
242 . q{ (E=WWW_MECH_CACHED_CONTENTLENGTH_MISSMATCH)} );
243 0 return 0;
244 }
245 0 ▶ if ( $self->cache_mismatch_content_length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_mismatch_content_length == 0) 0 0 0
246 0 return 0;
247 }
248 }
249  
250 18 return 1;
251 }
252  
253 sub _cache_ok {
254  
255 63 ▶ my $self = shift;
Subroutine _cache_ok: called CC=5 SCAR=16.1
256 63 my $response = shift;
257  
258 63 ▶ return 0 if !$response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 27 36 63
259 36 ▶ return 1 if !$self->positive_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->positive_cache 2 34 36
260  
261 34 ▶ return 0 if $response->code < 200;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code < 200 0 34 34
262 34 ▶ return 0 if $response->code > 301;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code > 301 1 33 34
263  
264 33 return 1;
265 }
266  
267 12 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
268 12 "We miss you, Spoon"; ## no critic
269  
270 # ABSTRACT: Cache response to be polite
271  
272 __END__
↑ Summary