File Coverage

blib/lib/Plack/Middleware/Static.pm
Criterion Covered Total %
statement 30 30 100.0
branch 7 8 87.5
condition 8 11 72.7
subroutine 7 7 100.0
pod 1 1 100.0
total 53 57 92.9


line stmt bran cond sub pod time code
1             package Plack::Middleware::Static;
2 4     4   69158 use strict;
  4         17  
  4         120  
3 4     4   18 use warnings;
  4         8  
  4         113  
4 4     4   561 use parent qw/Plack::Middleware/;
  4         301  
  4         18  
5 4     4   1898 use Plack::App::File;
  4         10  
  4         147  
6              
7 4     4   27 use Plack::Util::Accessor qw( path root encoding pass_through content_type );
  4         8  
  4         17  
8              
9             sub call {
10 39     39 1 58 my $self = shift;
11 39         49 my $env = shift;
12              
13 39         85 my $res = $self->_handle_static($env);
14 39 100 66     318 if ($res && not ($self->pass_through and $res->[0] == 404)) {
      100        
15 11         69 return $res;
16             }
17              
18 28         70 return $self->app->($env);
19             }
20              
21             sub _handle_static {
22 39     39   61 my($self, $env) = @_;
23              
24 39 50       86 my $path_match = $self->path or return;
25 39         76 my $path = $env->{PATH_INFO};
26              
27 39         72 for ($path) {
28 39 100       190 my $matched = 'CODE' eq ref $path_match ? $path_match->($_, $env) : $_ =~ $path_match;
29 39 100       200 return unless $matched;
30             }
31              
32 12   50     102 $self->{file} ||= Plack::App::File->new({ root => $self->root || '.', encoding => $self->encoding, content_type => $self->content_type });
      66        
33 12         42 local $env->{PATH_INFO} = $path; # rewrite PATH
34 12         69 return $self->{file}->call($env);
35             }
36              
37             1;
38             __END__