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   58110 use strict;
  4         17  
  4         107  
3 4     4   17 use warnings;
  4         7  
  4         100  
4 4     4   373 use parent qw/Plack::Middleware/;
  4         343  
  4         17  
5 4     4   1550 use Plack::App::File;
  4         10  
  4         134  
6              
7 4     4   21 use Plack::Util::Accessor qw( path root encoding pass_through content_type );
  4         6  
  4         17  
8              
9             sub call {
10 39     39 1 54 my $self = shift;
11 39         45 my $env = shift;
12              
13 39         101 my $res = $self->_handle_static($env);
14 39 100 66     302 if ($res && not ($self->pass_through and $res->[0] == 404)) {
      100        
15 11         72 return $res;
16             }
17              
18 28         84 return $self->app->($env);
19             }
20              
21             sub _handle_static {
22 39     39   67 my($self, $env) = @_;
23              
24 39 50       81 my $path_match = $self->path or return;
25 39         66 my $path = $env->{PATH_INFO};
26              
27 39         59 for ($path) {
28 39 100       175 my $matched = 'CODE' eq ref $path_match ? $path_match->($_, $env) : $_ =~ $path_match;
29 39 100       177 return unless $matched;
30             }
31              
32 12   50     65 $self->{file} ||= Plack::App::File->new({ root => $self->root || '.', encoding => $self->encoding, content_type => $self->content_type });
      66        
33 12         45 local $env->{PATH_INFO} = $path; # rewrite PATH
34 12         48 return $self->{file}->call($env);
35             }
36              
37             1;
38             __END__