| 1 | package Plack::Middleware::ConditionalGET; | ||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use parent qw( Plack::Middleware ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use Plack::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 11 | ▶ | sub call { | ||||||||
|
Subroutine
call: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 7 | 11 | my $self = shift; | |||||||||
| 8 | 11 | my $env = shift; | |||||||||
| 9 | |||||||||||
| 10 | 11 | my $res = $self->app->($env); | |||||||||
| 11 | 11 | ▶ | return $res unless $env->{REQUEST_METHOD} =~ /^(GET|HEAD)$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 12 | |||||||||||
| 13 | 10 | ▶ | $self->response_cb($res, sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 14 | 10 | my $res = shift; | |||||||||
| 15 | |||||||||||
| 16 | 10 | my $h = Plack::Util::headers($res->[1]); | |||||||||
| 17 | |||||||||||
| 18 | # check both ETag and If-Modified-Since, and at least one should exist | ||||||||||
| 19 | # and all present headers should match, not either. | ||||||||||
| 20 | 10 | ▶ | my @checks = ( $self->etag_matches($h, $env), $self->not_modified_since($h, $env) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | or return; | ||||||||||
| 22 | |||||||||||
| 23 | 9 | ▶ | unless (grep !$_, @checks) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 5 | $res->[0] = 304; | |||||||||
| 25 | 5 | $h->remove($_) for qw( Content-Type Content-Length Content-Disposition ); | |||||||||
| 26 | 5 | ▶ | if ($res->[2]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 4 | $res->[2] = []; | |||||||||
| 28 | } else { | ||||||||||
| 29 | 4 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 30 | 4 | ▶ | return defined $_[0] ? '' : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 1 | }; | |||||||||
| 32 | } | ||||||||||
| 33 | } | ||||||||||
| 34 | 10 | }); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 2 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | # RFC 2616 14.25 says it's OK and expected to use 'eq' :) | ||||||||||
| 40 | # > Note: When handling an If-Modified-Since header field, some | ||||||||||
| 41 | # > servers will use an exact date comparison function, rather than a | ||||||||||
| 42 | # > less-than function, for deciding whether to send a 304 ... | ||||||||||
| 43 | |||||||||||
| 44 | 10 | ▶ | sub etag_matches { | ||||||||
|
Subroutine
etag_matches: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 45 | 10 | my($self, $h, $env) = @_; | |||||||||
| 46 | 10 | ▶ | return unless $h->exists('ETag'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 6 | $h->get('ETag') eq _value($env->{HTTP_IF_NONE_MATCH}); | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 10 | ▶ | sub not_modified_since { | ||||||||
|
Subroutine
not_modified_since: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 51 | 10 | my($self, $h, $env) = @_; | |||||||||
| 52 | 10 | ▶ | return unless $h->exists('Last-Modified'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 6 | $h->get('Last-Modified') eq _value($env->{HTTP_IF_MODIFIED_SINCE}); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 12 | ▶ | sub _value { | ||||||||
|
Subroutine
_value: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 12 | my $str = shift; | |||||||||
| 58 | # IE sends wrong formatted value(i.e. "Thu, 03 Dec 2009 01:46:32 GMT; length=17936") | ||||||||||
| 59 | 12 | $str =~ s/;.*$//; | |||||||||
| 60 | 12 | return $str; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 2 | 1; | |||||||||
| 64 | |||||||||||
| 65 | __END__ | ||||||||||