| 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
| |||||||||||||||||||||||||||||
| 27 | 8 | ▶ | return if ( Plack::Util::header_exists( $headers, 'ETag' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | return | ||||||||||||||||||||||||||||
| 29 | 7 | ▶ | if ( $self->check_last_modified_header() | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->check_last_modified_header and Plack::Util::header_exists($headers, "Last-Modified")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | my $file_attr = $self->file_etag || [qw/mtime size/]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->file_etag || ['mtime', 'size']
MC/DC0%0/1
$self->file_etag || ['mtime', 'size']
$self->file_etag
Truth table50%1/2
A
A= $self->file_etag
| |||||||||||||||||||||||||||||
| 37 | 1 | my @stats = stat $res->[2]; | |||||||||||||||||||||||||||
| 38 | 1 | ▶ | if ( $stats[9] == time - 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 44 | 0 | $etag .= ( sprintf "%x", $stats[1] ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 1 | ▶ | if ( grep {/mtime/} @$file_attr ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | $etag .= "-" if ( $etag && $etag !~ /-$/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$etag and not $etag =~ /-$/u
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
| |||||||||||||||||||||||||||||
| 48 | 1 | $etag .= ( sprintf "%x", $stats[9] ); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 1 | ▶ | if ( grep {/size/} @$file_attr ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | $etag .= "-" if ( $etag && $etag !~ /-$/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$etag and not $etag =~ /-$/u
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | if ( ref $self->cache_control && ref $self->cache_control eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $self->cache_control and ref $self->cache_control eq "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||