| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$expires and $expires =~ /^(A|M)(\d+)$/
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+)$/
| |||||||||||||||||||||||||||||||||
| 30 | 9 | ▶ | my $base = ( $1 eq 'M' ) ? $modified : $access; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | 9 | ▶ | return if ! defined $base; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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(.+)$/
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(.+)$/
| |||||||||||||||||||||||||||||||||
| 35 | 36 | ▶ | my $base = ( $1 eq 'modification' ) ? $modified : $access; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | 36 | ▶ | return if ! defined $base; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 40 | 38 | my $term_sec; | |||||||||||||||||||||||||||||||
| 41 | 38 | (my $sigular_type = lc $type) =~ s/s$//; | |||||||||||||||||||||||||||||||
| 42 | 38 | ▶ | $type = '' if ! defined $type; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 38 | ▶ | Carp::croak "missing type '$type' in '$expires'" if ! exists $term{$sigular_type}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 37 | ▶ | if ( $num !~ m!^\d! ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 79 | 4 | my $type_match = $self->content_type; | |||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | return if ! defined $type_match; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | 4 | ▶ | my @type_match = (ref $type_match && ref $type_match eq 'ARRAY') ? @{$type_match} : ($type_match); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $type_match and ref $type_match eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | # expires works only for successful response | ||||||||||||||||||||||||||||||||
| 84 | 4 | ▶ | return if HTTP::Status::is_error( $res->[0] ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | # if already exists Expires header, do no override | ||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | return if Plack::Util::header_exists($res->[1], 'Expires'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 92 | 3 | my $type_check; | |||||||||||||||||||||||||||||||
| 93 | 3 | for ( @type_match ) { | |||||||||||||||||||||||||||||||
| 94 | 4 | ▶ | if (my $ref = ref $_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 3 | ▶ | if ($ref eq 'Regexp' && $type =~ m!$_!) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$ref eq "Regexp" and $type =~ /$_/
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 =~ /$_/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 106 | 1 | $type_check = 1; | |||||||||||||||||||||||||||||||
| 107 | 1 | last; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | return if ! $type_check; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 3 | my $last_modified; | |||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | if ( $last_modified = Plack::Util::header_get($res->[1], 'Last-Modified') ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||