blib/lib/Plack/Middleware/ETag.pm

filter: Statementstmt 96.4% 55 / 57 Branchbran 70.8% 17 / 24 Conditioncond 50.0% 7 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 79.8% 91 / 114 CC 18 SCAR 24.5
CC
18
Cov
83%
__ANON__
18.5
_set_cache_control
4.0
call
1.0
CRAP 11.6
↑ Summary
1 package Plack::Middleware::ETag;
2  
3 # ABSTRACT: Adds automatically an ETag header.
4  
5 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Digest::SHA 5.96;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 use Plack::Util::Accessor
11 3 ▶ qw( file_etag cache_control check_last_modified_header);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use parent qw/Plack::Middleware/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $VERSION = '0.06';
16  
17 8 ▶sub call {
Subroutine call: called CC=1 SCAR=0.1
Pod documented
18 8 my $self = shift;
19 8 my $res = $self->app->(@_);
20  
21 $self->response_cb(
22 $res,
23 8 ▶ sub {
Subroutine __ANON__: called CC=15 SCAR=29.2
24 8 my $res = shift;
25 8 my $headers = $res->[1];
26 8 ▶ return if ( !defined $res->[2] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res->[2] 0 8 8
27 8 ▶ return if ( Plack::Util::header_exists( $headers, 'ETag' ) );
Branch: 1 branch
BranchTrueFalseTotal
if Plack::Util::header_exists($headers, "ETag") 1 7 8
28 return
29 7 ▶ if ( $self->check_last_modified_header()
Branch: 1 branch
BranchTrueFalseTotal
if $self->check_last_modified_header and Plack::Util::header_exists($headers, "Last-Modified") 1 6 7
Condition66%2/3
$self->check_last_modified_header and Plack::Util::header_exists($headers, "Last-Modified")
!ll&&!rl&&r
601
MC/DC50%1/2
$self->check_last_modified_header and Plack::Util::header_exists($headers, "Last-Modified")
$self->check_last_modified_header Plack::Util::header_exists($headers, "Last-Modified")
Truth table66%2/3
A and B
A= $self->check_last_modified_headerB= Plack::Util::header_exists($headers, "Last-Modified")
ABresult
0X0
100
111
30 && Plack::Util::header_exists( $headers, 'Last-Modified' ) );
31  
32 6 my $etag;
33  
34 6 ▶ if ( Plack::Util::is_real_fh( $res->[2] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Plack::Util::is_real_fh($res->[2])) { } 1 5 6
35  
36 1 ▶ my $file_attr = $self->file_etag || [qw/mtime size/];
Condition50%1/2
$self->file_etag || ['mtime', 'size']
l!l
01
MC/DC0%0/1
$self->file_etag || ['mtime', 'size']
$self->file_etag
Truth table50%1/2
A
A= $self->file_etag
Aresult
11
00
37 1 my @stats = stat $res->[2];
38 1 ▶ if ( $stats[9] == time - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stats[9] == time - 1) 0 1 1
39 # if the file was modified less than one second before the request
40 # it may be modified in a near future, so we return a weak etag
41 0 $etag = "W/";
42 }
43 1 ▶ if ( grep {/inode/} @$file_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/inode/u;} @$file_attr) 0 1 1
44 0 $etag .= ( sprintf "%x", $stats[1] );
45 }
46 1 ▶ if ( grep {/mtime/} @$file_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/mtime/u;} @$file_attr) 1 0 1
47 1 ▶ $etag .= "-" if ( $etag && $etag !~ /-$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $etag and not $etag =~ /-$/u 0 1 1
Condition33%1/3
$etag and not $etag =~ /-$/u
!ll&&!rl&&r
100
MC/DC0%0/2
$etag and not $etag =~ /-$/u
$etag not $etag =~ /-$/u
Truth table33%1/3
A and not(B)
A= $etagB= not $etag =~ /-$/u
ABresult
0X0
100
111
48 1 $etag .= ( sprintf "%x", $stats[9] );
49 }
50 1 ▶ if ( grep {/size/} @$file_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/size/u;} @$file_attr) 1 0 1
51 1 ▶ $etag .= "-" if ( $etag && $etag !~ /-$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $etag and not $etag =~ /-$/u 1 0 1
Condition33%1/3
$etag and not $etag =~ /-$/u
!ll&&!rl&&r
001
MC/DC0%0/2
$etag and not $etag =~ /-$/u
$etag not $etag =~ /-$/u
Truth table33%1/3
A and not(B)
A= $etagB= not $etag =~ /-$/u
ABresult
0X0
100
111
52 1 $etag .= ( sprintf "%x", $stats[7] );
53 }
54 } else {
55 5 my $sha = Digest::SHA->new;
56 5 $sha->add( @{ $res->[2] } );
57 5 $etag = $sha->hexdigest;
58 }
59 6 Plack::Util::header_set( $headers, 'ETag', $etag );
60 6 $self->_set_cache_control($headers);
61 6 return;
62 }
63 8 );
64 }
65  
66 6 ▶sub _set_cache_control {
Subroutine _set_cache_control: called CC=4 SCAR=13.9
67 6 my ( $self, $headers ) = @_;
68 6 ▶ return unless $self->cache_control;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cache_control 4 2 6
69  
70 2 ▶ if ( ref $self->cache_control && ref $self->cache_control eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->cache_control and ref $self->cache_control eq 'ARRAY') { } 1 1 2
Condition66%2/3
ref $self->cache_control and ref $self->cache_control eq "ARRAY"
!ll&&!rl&&r
101
MC/DC50%1/2
ref $self->cache_control and ref $self->cache_control eq "ARRAY"
ref $self->cache_control ref $self->cache_control eq "ARRAY"
Truth table66%2/3
A and B
A= ref $self->cache_controlB= ref $self->cache_control eq "ARRAY"
ABresult
0X0
100
111
71 Plack::Util::header_set( $headers, 'Cache-Control',
72 1 join( ', ', @{ $self->cache_control } ) );
73 } else {
74 1 Plack::Util::header_set( $headers, 'Cache-Control',
75 'must-revalidate' );
76 }
77 }
78  
79 3 1;
80  
81 __END__
↑ Summary