blib/lib/Dancer2/Plugin/ConditionalCaching.pm

filter: Statementstmt 20.9% 31 / 148 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 69.2% 9 / 13 Podpod n/a totaltotal 10.7% 40 / 371 CC 79 SCAR 80.9
CC
79
Cov
10%
__ANON__
4400.0
_cmp_weak
56.0
_instanceof
30.0
CRAP 3246.0
↑ Summary
1 3 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Dancer2::Plugin::ConditionalCaching;
4  
5 # ABSTRACT: RFC7234 Caching
6  
7 3 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use HTTP::Headers::Fancy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use HTTP::Date qw(time2str str2time);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use HTTP::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Math::Round qw(round);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $VERSION = '0.001'; # VERSION
16  
17 0 ▶sub _instanceof {
Subroutine _instanceof: not called CC=5 SCAR=34.0
18 0 my ( $obj, @cls ) = @_;
19 0 ▶ return 0 unless blessed $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($obj) 0 0 0
20 0 foreach my $cls (@cls) {
21 0 ▶ $cls = blessed $cls if blessed $cls;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($cls) 0 0 0
22 0 ▶ return 1 if $obj->isa($cls);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->isa($cls) 0 0 0
23 }
24 0 return 0;
25 }
26  
27 0 ▶sub _cmp_weak {
Subroutine _cmp_weak: not called CC=7 SCAR=40.3
28 0 my ( $a, $b, $w ) = @_;
29 0 ▶ return unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 0 0 0
30 0 ▶ return unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 0 0
31 0 ▶ $w //= 0;
Condition0%0/2
$w //= 0
l!l
00
MC/DC0%0/1
$w //= 0
$w
Truth table0%0/2
A
A= $w
Aresult
11
00
32 0 ▶ if ($w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w) { } 0 0 0
33 0 ▶ if ( ref $b ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $b) { } 0 0 0
34 0 return $a eq $$b;
35 }
36 else {
37 0 return $a eq $b;
38 }
39 }
40 else {
41 0 ▶ return 0 if ref $b;
Branch: 1 branch
BranchTrueFalseTotal
if ref $b 0 0 0
42 0 return $a eq $b;
43 }
44 }
45  
46 17 ▶register caching => sub {
Subroutine __ANON__: called CC=67 SCAR=83.9
47 17 my $dsl = shift;
48 17 my %args = @_;
49  
50 17 my $get_or_head = $dsl->request->method =~ m{^(?:get|head)$}i;
51  
52 0 ▶ my $dry = delete $args{dry} // 0;
Condition0%0/2
delete $args{"dry"} // 0
l!l
00
MC/DC0%0/1
delete $args{"dry"} // 0
delete $args{"dry"}
Truth table0%0/2
A
A= delete $args{"dry"}
Aresult
11
00
53  
54 0 ▶ my $force = delete $args{force} // 0;
Condition0%0/2
delete $args{"force"} // 0
l!l
00
MC/DC0%0/1
delete $args{"force"} // 0
delete $args{"force"}
Truth table0%0/2
A
A= delete $args{"force"}
Aresult
11
00
55 0 ▶ my $throw = delete $args{throw} // 0;
Condition0%0/2
delete $args{"throw"} // 0
l!l
00
MC/DC0%0/1
delete $args{"throw"} // 0
delete $args{"throw"}
Truth table0%0/2
A
A= delete $args{"throw"}
Aresult
11
00
56 0 ▶ my $check = delete $args{check} // 1;
Condition0%0/2
delete $args{"check"} // 1
l!l
00
MC/DC0%0/1
delete $args{"check"} // 1
delete $args{"check"}
Truth table0%0/2
A
A= delete $args{"check"}
Aresult
11
00
57  
58 0 my $etag = delete $args{etag};
59 0 my $weak = !!delete $args{weak};
60  
61 0 my $changed = delete $args{changed};
62 0 my $expires = delete $args{expires};
63  
64 0 ▶ my $no_cache = ( ( delete $args{cache} // 1 ) == 0 );
Condition0%0/2
delete $args{"cache"} // 1
l!l
00
MC/DC0%0/1
delete $args{"cache"} // 1
delete $args{"cache"}
Truth table0%0/2
A
A= delete $args{"cache"}
Aresult
11
00
65 0 ▶ my $no_store = ( ( delete $args{store} // 1 ) == 0 );
Condition0%0/2
delete $args{"store"} // 1
l!l
00
MC/DC0%0/1
delete $args{"store"} // 1
delete $args{"store"}
Truth table0%0/2
A
A= delete $args{"store"}
Aresult
11
00
66  
67 0 ▶ my $private = ( delete $args{private} // 0 );
Condition0%0/2
delete $args{"private"} // 0
l!l
00
MC/DC0%0/1
delete $args{"private"} // 0
delete $args{"private"}
Truth table0%0/2
A
A= delete $args{"private"}
Aresult
11
00
68 0 ▶ my $public = ( delete $args{public} // 0 );
Condition0%0/2
delete $args{"public"} // 0
l!l
00
MC/DC0%0/1
delete $args{"public"} // 0
delete $args{"public"}
Truth table0%0/2
A
A= delete $args{"public"}
Aresult
11
00
69  
70 0 ▶ if ( $private and $public ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($private and $public) 0 0 0
Condition0%0/3
$private and $public
!ll&&!rl&&r
000
MC/DC0%0/2
$private and $public
$private $public
Truth table0%0/3
A and B
A= $privateB= $public
ABresult
0X0
100
111
71 0 warn "Cache-Control: private and public are mutually exclusive";
72 }
73 0 ▶ if ($no_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_cache) 0 0 0
74 0 ▶ if ($no_store) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_store) 0 0 0
75 0 warn "Cache-Control: no-cache and no-store are mutually exclusive";
76 }
77 0 ▶ if ($private) {
Branch: 1 branch
BranchTrueFalseTotal
if ($private) 0 0 0
78 0 warn "Cache-Control: no-cache and private are useless together";
79 }
80 0 ▶ if ($private) {
Branch: 1 branch
BranchTrueFalseTotal
if ($private) 0 0 0
81 0 warn "Cache-Control: no-cache and private are useless together";
82 }
83 }
84  
85 0 my %reqh = HTTP::Headers::Fancy::decode_hash( $dsl->request->headers );
86  
87 0 my %req_cc = HTTP::Headers::Fancy::split_field_hash( $reqh{CacheControl} );
88  
89 0 my ( $age, $fresh );
90  
91 0 ▶ $age = time - $changed if defined $changed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $changed 0 0 0
92 0 ▶ $fresh = $expires - time if defined $expires;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires 0 0 0
93  
94 0 ▶ if ( defined $age and $age < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $age and $age < 0) 0 0 0
Condition0%0/3
defined $age and $age < 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $age and $age < 0
defined $age $age < 0
Truth table0%0/3
A and B
A= defined $ageB= $age < 0
ABresult
0X0
100
111
95 0 warn("Age: 'changed' points to a timestamp in the future");
96 0 $age = 0;
97 }
98  
99 0 my %resp_cc = (
100 ( NoCache => undef ) x !!$no_cache,
101 ( NoStore => undef ) x !!$no_store,
102 ( Private => undef ) x !!$private,
103 ( Public => undef ) x !!$public,
104 MustRevalidate => undef,
105 NoTransform => undef,
106 );
107  
108 0 ▶ if ($check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check) 0 0 0
109 0 ▶ if ( $req_cc{MaxAge} and defined $age ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_cc{"MaxAge"} and defined $age) 0 0 0
Condition0%0/3
$req_cc{"MaxAge"} and defined $age
!ll&&!rl&&r
000
MC/DC0%0/2
$req_cc{"MaxAge"} and defined $age
$req_cc{"MaxAge"} defined $age
Truth table0%0/3
A and B
A= $req_cc{"MaxAge"}B= defined $age
ABresult
0X0
100
111
110 0 ▶ if ( $age > $req_cc{MaxAge} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($age > $req_cc{"MaxAge"}) 0 0 0
111 0 $force = 1;
112 }
113 }
114 0 ▶ if ( $req_cc{MinFresh} and defined $fresh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_cc{"MinFresh"} and defined $fresh) 0 0 0
Condition0%0/3
$req_cc{"MinFresh"} and defined $fresh
!ll&&!rl&&r
000
MC/DC0%0/2
$req_cc{"MinFresh"} and defined $fresh
$req_cc{"MinFresh"} defined $fresh
Truth table0%0/3
A and B
A= $req_cc{"MinFresh"}B= defined $fresh
ABresult
0X0
100
111
115 0 ▶ if ( $fresh < $req_cc{MinFresh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fresh < $req_cc{"MinFresh"}) 0 0 0
116 0 $force = 1;
117 }
118 }
119 }
120  
121 0 ▶ my $builder = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
122 0 ▶ return unless defined $args{builder};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"builder"} 0 0 0
123 0 my $sub;
124 0 ▶ if ( ref $args{builder} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'builder'} eq 'CODE') { } 0 0 0
125 0 $sub = delete $args{builder};
126 }
127 else {
128 0 my $data = delete $args{builder};
129 0 ▶ $sub = sub { return $data };
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 }
131 0 my %subargs = ( %req_cc, Force => $force, );
132 0 return $sub->(%subargs);
133 0 };
134  
135 0 my %resph;
136  
137 0 ▶ if ( keys %resp_cc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %resp_cc) 0 0 0
138 0 $resph{CacheControl} = HTTP::Headers::Fancy::build_field_hash(%resp_cc);
139 }
140 0 ▶ if ( defined $age ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $age) 0 0 0
141 0 $resph{Age} = round($age);
142 }
143 0 ▶ if ( defined $expires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expires) 0 0 0
144 0 $resph{Expires} = time2str( round($expires) );
145 }
146 0 ▶ if ( defined $changed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $changed) 0 0 0
147 0 $resph{LastModified} = time2str( round($changed) );
148 }
149 0 ▶ if ( defined $etag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $etag) 0 0 0
150 0 ▶ if ($weak) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak) { } 0 0 0
151 0 $resph{Etag} = 'W/"' . $etag . '"';
152 }
153 else {
154 0 $resph{Etag} = '"' . $etag . '"';
155 }
156 }
157  
158 0 ▶ if ( $get_or_head and !$dry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($get_or_head and not $dry) 0 0 0
Condition0%0/3
$get_or_head and not $dry
!ll&&!rl&&r
000
MC/DC0%0/2
$get_or_head and not $dry
$get_or_head not $dry
Truth table0%0/3
A and not(B)
A= $get_or_headB= not $dry
ABresult
0X0
100
111
159 0 $dsl->response->header( HTTP::Headers::Fancy::encode_hash(%resph) );
160 }
161  
162 0 ▶ unless ($check) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($check) 0 0 0
163 0 return $builder->();
164 }
165  
166 0 ▶ my $if_match = ( exists $reqh{IfMatch} and defined $reqh{IfMatch} );
Condition0%0/3
exists $reqh{'IfMatch'} && defined $reqh{'IfMatch'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $reqh{'IfMatch'} && defined $reqh{'IfMatch'}
exists $reqh{'IfMatch'} defined $reqh{'IfMatch'}
Truth table0%0/3
A && B
A= exists $reqh{'IfMatch'}B= defined $reqh{'IfMatch'}
ABresult
0X0
100
111
167 my $if_match_any =
168 0 ▶ ( $if_match and ( $reqh{IfMatch} =~ qr{^ \s* \* \s* $}xsi ) );
Condition0%0/3
$if_match && $reqh{'IfMatch'} =~ qr/^ \s* \* \s* $/six
!ll&&!rl&&r
000
MC/DC0%0/2
$if_match && $reqh{'IfMatch'} =~ qr/^ \s* \* \s* $/six
$if_match $reqh{'IfMatch'} =~ qr/^ \s* \* \s* $/six
Truth table0%0/3
A && B
A= $if_matchB= $reqh{'IfMatch'} =~ qr/^ \s* \* \s* $/six
ABresult
0X0
100
111
169 my @if_match =
170 $if_match_any
171 ? ()
172 0 ▶ : HTTP::Headers::Fancy::split_field_list( $reqh{IfMatch} );
Branch: 1 branch
BranchTrueFalseTotal
$if_match_any ? : 0 0 0
173  
174 my $if_none_match =
175 0 ▶ ( exists $reqh{IfNoneMatch} and defined $reqh{IfNoneMatch} );
Condition0%0/3
exists $reqh{'IfNoneMatch'} && defined $reqh{'IfNoneMatch'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $reqh{'IfNoneMatch'} && defined $reqh{'IfNoneMatch'}
exists $reqh{'IfNoneMatch'} defined $reqh{'IfNoneMatch'}
Truth table0%0/3
A && B
A= exists $reqh{'IfNoneMatch'}B= defined $reqh{'IfNoneMatch'}
ABresult
0X0
100
111
176 my $if_none_match_any =
177 0 ▶ ( $if_none_match and ( $reqh{IfNoneMatch} =~ qr{^ \s* \* \s* $}xsi ) );
Condition0%0/3
$if_none_match && $reqh{'IfNoneMatch'} =~ qr/^ \s* \* \s* $/six
!ll&&!rl&&r
000
MC/DC0%0/2
$if_none_match && $reqh{'IfNoneMatch'} =~ qr/^ \s* \* \s* $/six
$if_none_match $reqh{'IfNoneMatch'} =~ qr/^ \s* \* \s* $/six
Truth table0%0/3
A && B
A= $if_none_matchB= $reqh{'IfNoneMatch'} =~ qr/^ \s* \* \s* $/six
ABresult
0X0
100
111
178 my @if_none_match =
179 $if_none_match_any
180 ? ()
181 0 ▶ : HTTP::Headers::Fancy::split_field_list( $reqh{IfNoneMatch} );
Branch: 1 branch
BranchTrueFalseTotal
$if_none_match_any ? : 0 0 0
182  
183 0 my $if_modified_since = str2time( $reqh{IfModifiedSince} );
184 0 my $if_unmodified_since = str2time( $reqh{IfUnmodifiedSince} );
185  
186 0 ▶ if ($if_match) {
Branch: 2 branches
BranchTrueFalseTotal
if ($if_match) { } 0 0 0
elsif ($if_unmodified_since and defined $changed) { } 0 0 0
187 0 ▶ my $xa = ( !!$if_match_any and !!$etag );
Condition0%0/3
!$if_match_any || !$etag
l!l&&r!l&&!r
000
MC/DC0%0/2
!$if_match_any || !$etag
!$if_match_any !$etag
Truth table0%0/3
not(A) || not(B)
A= !$if_match_anyB= !$etag
ABresult
1X1
011
000
188 0 my $xb = scalar grep { _cmp_weak( $etag, $_, $weak ) } @if_match;
189 0 ▶ unless ( $xa or $xb ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xa or $xb) 0 0 0
Condition0%0/3
$xa or $xb
l!l&&r!l&&!r
000
MC/DC0%0/2
$xa or $xb
$xa $xb
Truth table0%0/3
A or B
A= $xaB= $xb
ABresult
1X1
011
000
190 0 ▶ HTTP::Exception->throw(412) if $throw;
Branch: 1 branch
BranchTrueFalseTotal
if $throw 0 0 0
191 0 ▶ return 412 if $dry;
Branch: 1 branch
BranchTrueFalseTotal
if $dry 0 0 0
192 0 $dsl->send_error( 'Precondition Failed', 412 );
193 }
194 }
195 ▶ elsif ( $if_unmodified_since and defined $changed ) {
Condition0%0/3
$if_unmodified_since and defined $changed
!ll&&!rl&&r
000
MC/DC0%0/2
$if_unmodified_since and defined $changed
$if_unmodified_since defined $changed
Truth table0%0/3
A and B
A= $if_unmodified_sinceB= defined $changed
ABresult
0X0
100
111
196 0 ▶ unless ( $if_unmodified_since > $changed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($if_unmodified_since > $changed) 0 0 0
197 0 ▶ HTTP::Exception->throw(412) if $throw;
Branch: 1 branch
BranchTrueFalseTotal
if $throw 0 0 0
198 0 ▶ return 412 if $dry;
Branch: 1 branch
BranchTrueFalseTotal
if $dry 0 0 0
199 0 $dsl->send_error( 'Precondition Failed', 412 );
200 }
201 }
202 0 ▶ if ($if_none_match) {
Branch: 2 branches
BranchTrueFalseTotal
if ($if_none_match) { } 0 0 0
elsif ($get_or_head and $if_modified_since and defined $changed) { } 0 0 0
203 0 ▶ my $xa = ( !!$if_none_match_any and !!$etag );
Condition0%0/3
!$if_none_match_any || !$etag
l!l&&r!l&&!r
000
MC/DC0%0/2
!$if_none_match_any || !$etag
!$if_none_match_any !$etag
Truth table0%0/3
not(A) || not(B)
A= !$if_none_match_anyB= !$etag
ABresult
1X1
011
000
204 0 my $xb = scalar grep { _cmp_weak( $etag, $_, $weak ) } @if_none_match;
205 0 ▶ if ( $xa or $xb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xa or $xb) 0 0 0
Condition0%0/3
$xa or $xb
l!l&&r!l&&!r
000
MC/DC0%0/2
$xa or $xb
$xa $xb
Truth table0%0/3
A or B
A= $xaB= $xb
ABresult
1X1
011
000
206 0 ▶ if ($get_or_head) {
Branch: 1 branch
BranchTrueFalseTotal
if ($get_or_head) { } 0 0 0
207 0 ▶ HTTP::Exception->throw(304) if $throw;
Branch: 1 branch
BranchTrueFalseTotal
if $throw 0 0 0
208 0 ▶ return 304 if $dry;
Branch: 1 branch
BranchTrueFalseTotal
if $dry 0 0 0
209 0 $dsl->send_error( 'Not Modfied', 304 );
210 }
211 else {
212 0 ▶ HTTP::Exception->throw(412) if $throw;
Branch: 1 branch
BranchTrueFalseTotal
if $throw 0 0 0
213 0 ▶ return 412 if $dry;
Branch: 1 branch
BranchTrueFalseTotal
if $dry 0 0 0
214 0 $dsl->send_error( 'Precondition Failed', 412 );
215 }
216 }
217 }
218 ▶ elsif ( $get_or_head and $if_modified_since and defined $changed ) {
Condition0%0/6
$get_or_head and $if_modified_since and defined $changed
!ll&&!rl&&r
000
$get_or_head and $if_modified_since
!ll&&!rl&&r
000
MC/DC0%0/3
$get_or_head and $if_modified_since and defined $changed
$get_or_head $if_modified_since defined $changed
Truth table0%0/4
A and B and C
A= $get_or_headB= $if_modified_sinceC= defined $changed
ABCresult
0XX0
10X0
1100
1111
219 0 ▶ if ( $if_modified_since > $changed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($if_modified_since > $changed) 0 0 0
220 0 ▶ HTTP::Exception->throw(304) if $throw;
Branch: 1 branch
BranchTrueFalseTotal
if $throw 0 0 0
221 0 ▶ return 304 if $dry;
Branch: 1 branch
BranchTrueFalseTotal
if $dry 0 0 0
222 0 $dsl->send_error( 'Not Modfied', 304 );
223 }
224 }
225  
226 0 return $builder->();
227 3 };
228  
229 3 register_plugin;
230  
231 3 1;
232  
233 __END__
↑ Summary