File Coverage

blib/lib/Sledge/Plugin/IfModifiedSince.pm
Criterion Covered Total %
statement 23 42 54.7
branch 0 12 0.0
condition n/a
subroutine 6 10 60.0
pod 3 3 100.0
total 32 67 47.7


line stmt bran cond sub pod time code
1             package Sledge::Plugin::IfModifiedSince;
2              
3 1     1   24304 use strict;
  1         2  
  1         41  
4 1     1   5 use vars qw($VERSION);
  1         2  
  1         60  
5             $VERSION = '0.05';
6              
7 1     1   433013 use HTTP::Date qw(str2time time2str);
  1         5220  
  1         76  
8              
9 1     1   9 use constant NOT_MODIFIED => 304;
  1         2  
  1         75  
10              
11             sub import {
12 1     1   13 my $class = shift;
13 1         3 my $pkg = caller;
14 1     1   6 no strict 'refs';
  1         2  
  1         413  
15 1         2 *{"$pkg\::not_modified"} = \¬_modified;
  1         8  
16 1         2 *{"$pkg\::if_modified_since"} = \&if_modified_since;
  1         8  
17 1         2 *{"$pkg\::set_last_modified"} = \&set_last_modified;
  1         17  
18             }
19              
20             sub not_modified {
21 0     0 1   my $self=shift;
22 0           $self->r->status(NOT_MODIFIED);
23 0           $self->send_http_header;
24 0           $self->finished(1);
25             }
26              
27             sub if_modified_since {
28 0     0 1   my ($self, $thing) = @_;
29 0 0         return 1 unless $thing;
30 0           my $mtime = _get_epoch($thing);
31 0           my $if_modified_since_epoch = str2time($ENV{'HTTP_IF_MODIFIED_SINCE'});
32 0 0         return 1 unless $if_modified_since_epoch;
33 0 0         return ( $mtime > $if_modified_since_epoch ) ? return 1 : 0;
34              
35             }
36              
37             sub set_last_modified {
38 0     0 1   my ($self, $thing) = @_;
39 0           my $mtime = _get_epoch($thing);
40 0 0         $self->r->header_out('Last-Modified' => time2str($mtime))
41             if $mtime;
42             }
43              
44             sub _get_epoch {
45 0     0     my $thing = shift;
46 0 0         if ( $thing =~ /^\d+$/ ) { # treat as epoch
    0          
47 0           return $thing;
48             } elsif ( -e $thing ) { # treat as path
49 0           return (stat($thing))[9];
50             } else {
51 0           warn "Argument must be a epochtime or filepath\n";
52 0           return;
53             }
54             }
55              
56             1;
57             __END__