blib/lib/Plack/Middleware/Expires.pm

filter: Statementstmt 97.7% 85 / 87 Branchbran 76.9% 40 / 52 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 12 / 12 Podpod 66.6% 2 / 3 totaltotal 87.9% 153 / 174 CC 32 SCAR 25.6
CC
32
Cov
89%
__ANON__
18.6
calc_expires
15.2
prepare_app
2.0
CRAP 12.9
↑ Summary
1 package Plack::Middleware::Expires;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use parent qw/Plack::Middleware/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Plack::Util::Accessor qw( content_type expires );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use HTTP::Status qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.06';
13  
14 46 ▶sub calc_expires {
Subroutine calc_expires: called CC=15 SCAR=27.2
Pod undocumented
15 46 my ( $expires, $modified, $access ) = @_;
16 46 ▶ $access = time if ! defined $access;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $access 0 46 46
17  
18 46 my %term = (
19 year => 60*60*24*365,
20 month => 60*60*24*31,
21 week => 60*60*24*7,
22 day => 60*60*24,
23 hour => 60*60,
24 minute => 60,
25 second => 1
26 );
27  
28 46 my $expires_sec;
29 46 ▶ if ( $expires && $expires =~ m!^(A|M)(\d+)$! ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expires and $expires =~ /^(A|M)(\d+)$/) { } 9 37 46
elsif ($expires and $expires =~ /^(access|now|modification)\s(?:plus)\s(.+)$/) { } 36 1 37
Condition66%2/3
$expires and $expires =~ /^(A|M)(\d+)$/
!ll&&!rl&&r
0379
MC/DC50%1/2
$expires and $expires =~ /^(A|M)(\d+)$/
$expires $expires =~ /^(A|M)(\d+)$/
Truth table66%2/3
A and B
A= $expiresB= $expires =~ /^(A|M)(\d+)$/
ABresult
0X0
100
111
30 9 ▶ my $base = ( $1 eq 'M' ) ? $modified : $access;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'M' ? : 1 8 9
31 9 ▶ return if ! defined $base;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base 0 9 9
32 9 $expires_sec = $base + $2;
33 }
34 ▶ elsif ( $expires && $expires =~ m!^(access|now|modification)\s(?:plus)\s(.+)$! ) {
Condition66%2/3
$expires and $expires =~ /^(access|now|modification)\s(?:plus)\s(.+)$/
!ll&&!rl&&r
0136
MC/DC50%1/2
$expires and $expires =~ /^(access|now|modification)\s(?:plus)\s(.+)$/
$expires $expires =~ /^(access|now|modification)\s(?:plus)\s(.+)$/
Truth table66%2/3
A and B
A= $expiresB= $expires =~ /^(access|now|modification)\s(?:plus)\s(.+)$/
ABresult
0X0
100
111
35 36 ▶ my $base = ( $1 eq 'modification' ) ? $modified : $access;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'modification' ? : 16 20 36
36 36 ▶ return if ! defined $base;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base 1 35 36
37 35 my @datetime = split /\s+/,$2;
38 35 $expires_sec = $base;
39 35 ▶ while ( my ($num, $type) = splice( @datetime, 0, 2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@datetime, 0, 2)) 38 33 71
40 38 my $term_sec;
41 38 (my $sigular_type = lc $type) =~ s/s$//;
42 38 ▶ $type = '' if ! defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 38 38
43 38 ▶ Carp::croak "missing type '$type' in '$expires'" if ! exists $term{$sigular_type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $term{$sigular_type} 1 37 38
44  
45 37 ▶ if ( $num !~ m!^\d! ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num =~ /^\d/) 1 36 37
46 1 Carp::croak "numeric value expected '$num' in '$expires'";
47 }
48 3 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 36 $num = int( $num + 0 );
50  
51 36 $expires_sec += $term{$sigular_type} * $num;
52 }
53 }
54 else {
55 1 Carp::croak("unkown expires format: '$expires'");
56 }
57 42 ▶ $expires_sec = 2147483647 if $expires_sec > 2147483647; #year 2039
Branch: 1 branch
BranchTrueFalseTotal
if $expires_sec > 2147483647 1 41 42
58 42 return $expires_sec;
59 }
60  
61 5 ▶sub prepare_app {
Subroutine prepare_app: called CC=2 SCAR=7.1
Pod documented
62 5 my $self = shift;
63 5 ▶ if ( my $expires = $self->expires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expires = $self->expires) 5 0 5
64 # test run for configuration check
65 5 calc_expires( $expires, time, time );
66 }
67 }
68  
69 4 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
70 4 my($self, $env) = @_;
71 4 my $req_time = time;
72  
73 4 my $res = $self->app->($env);
74  
75 4 ▶ $self->response_cb($res, sub {
Subroutine __ANON__: called CC=17 SCAR=29.2
76 4 my $res = shift;
77  
78 4 ▶ return if ! $self->expires;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->expires 0 4 4
79 4 my $type_match = $self->content_type;
80 4 ▶ return if ! defined $type_match;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type_match 0 4 4
81 4 ▶ my @type_match = (ref $type_match && ref $type_match eq 'ARRAY') ? @{$type_match} : ($type_match);
Branch: 1 branch
BranchTrueFalseTotal
ref $type_match && ref $type_match eq 'ARRAY' ? : 1 3 4
Condition66%2/3
ref $type_match and ref $type_match eq "ARRAY"
!ll&&!rl&&r
031
MC/DC50%1/2
ref $type_match and ref $type_match eq "ARRAY"
ref $type_match ref $type_match eq "ARRAY"
Truth table66%2/3
A and B
A= ref $type_matchB= ref $type_match eq "ARRAY"
ABresult
0X0
100
111
82  
83 # expires works only for successful response
84 4 ▶ return if HTTP::Status::is_error( $res->[0] );
Branch: 1 branch
BranchTrueFalseTotal
if &HTTP::Status::is_error($res->[0]) 0 4 4
85  
86 # if already exists Expires header, do no override
87 4 ▶ return if Plack::Util::header_exists($res->[1], 'Expires');
Branch: 1 branch
BranchTrueFalseTotal
if Plack::Util::header_exists($res->[1], "Expires") 1 3 4
88  
89 #content_type check
90 3 my $type = Plack::Util::header_get($res->[1], 'Content-Type');
91 3 ▶ return if ! defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 3 3
92 3 my $type_check;
93 3 for ( @type_match ) {
94 4 ▶ if (my $ref = ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $_) { } 3 1 4
95 3 ▶ if ($ref eq 'Regexp' && $type =~ m!$_!) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'Regexp' and $type =~ /$_/) { } 1 2 3
elsif ($ref eq 'CODE') { } 1 1 2
Condition100%3/3
$ref eq "Regexp" and $type =~ /$_/
!ll&&!rl&&r
111
MC/DC100%2/2
$ref eq "Regexp" and $type =~ /$_/
$ref eq "Regexp" $type =~ /$_/
Truth table100%3/3
A and B
A= $ref eq "Regexp"B= $type =~ /$_/
ABresult
0X0
100
111
96 1 $type_check = 1;
97 1 last;
98 }
99 elsif ($ref eq 'CODE') {
100 1 $type_check = $_->($env);
101 1 last;
102 }
103 }
104 else {
105 1 ▶ if ( lc $type eq lc $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $type eq lc $_) 1 0 1
106 1 $type_check = 1;
107 1 last;
108 }
109 }
110 }
111 3 ▶ return if ! $type_check;
Branch: 1 branch
BranchTrueFalseTotal
unless $type_check 0 3 3
112  
113 3 my $last_modified;
114 3 ▶ if ( $last_modified = Plack::Util::header_get($res->[1], 'Last-Modified') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_modified = Plack::Util::header_get($res->[1], "Last-Modified")) 0 3 3
115 0 $last_modified = HTTP::Date::str2time( $last_modified );
116 }
117
118 # calurate
119 3 my $expires_sec = calc_expires( $self->expires, $last_modified, $req_time );
120 3 Plack::Util::header_set( $res->[1], 'Expires', HTTP::Date::time2str( $expires_sec ) );
121 3 ▶ if ( my $cc = Plack::Util::header_get($res->[1], 'Cache-Control') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cc = Plack::Util::header_get($res->[1], 'Cache-Control')) { } 0 3 3
122 0 $cc .= sprintf "max-age=%d", $expires_sec - $req_time;
123 }
124 else {
125 3 Plack::Util::header_set( $res->[1], 'Cache-Control', sprintf("max-age=%d", $expires_sec - $req_time) );
126 }
127 4 });
128  
129 }
130  
131  
132 3 1;
133 __END__
↑ Summary