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   90404 use strict;
  4         6  
  4         120  
3 4     4   16 use warnings;
  4         5  
  4         196  
4 4     4   371 use parent qw/Plack::Middleware/;
  4         265  
  4         19  
5 4     4   1642 use Plack::App::File;
  4         11  
  4         133  
6              
7 4     4   18 use Plack::Util::Accessor qw( path root encoding pass_through content_type );
  4         5  
  4         14  
8              
9             sub call {
10 44     44 1 92 my $self = shift;
11 44         65 my $env = shift;
12              
13 44         101 my $res = $self->_handle_static($env);
14 44 100 66     352 if ($res && not ($self->pass_through and $res->[0] == 404)) {
      100        
15 11         106 return $res;
16             }
17              
18 33         118 return $self->app->($env);
19             }
20              
21             sub _handle_static {
22 44     44   80 my($self, $env) = @_;
23              
24 44 50       131 my $path_match = $self->path or return;
25 44         87 my $path = $env->{PATH_INFO};
26              
27 44         87 for ($path) {
28 44 100       292 my $matched = 'CODE' eq ref $path_match ? $path_match->($_, $env) : $_ =~ $path_match;
29 44 100       285 return unless $matched;
30             }
31              
32 13   50     97 $self->{file} ||= Plack::App::File->new({ root => $self->root || '.', encoding => $self->encoding, content_type => $self->content_type });
      66        
33 13         60 local $env->{PATH_INFO} = $path; # rewrite PATH
34 13         78 return $self->{file}->call($env);
35             }
36              
37             1;
38             __END__