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   127547 use strict;
  4         9  
  4         167  
3 4     4   26 use warnings;
  4         7  
  4         263  
4 4     4   551 use parent qw/Plack::Middleware/;
  4         344  
  4         41  
5 4     4   2271 use Plack::App::File;
  4         13  
  4         188  
6              
7 4     4   27 use Plack::Util::Accessor qw( path root encoding pass_through content_type );
  4         7  
  4         17  
8              
9             sub call {
10 39     39 1 64 my $self = shift;
11 39         57 my $env = shift;
12              
13 39         90 my $res = $self->_handle_static($env);
14 39 100 66     340 if ($res && not ($self->pass_through and $res->[0] == 404)) {
      100        
15 11         109 return $res;
16             }
17              
18 28         79 return $self->app->($env);
19             }
20              
21             sub _handle_static {
22 39     39   79 my($self, $env) = @_;
23              
24 39 50       107 my $path_match = $self->path or return;
25 39         84 my $path = $env->{PATH_INFO};
26              
27 39         82 for ($path) {
28 39 100       249 my $matched = 'CODE' eq ref $path_match ? $path_match->($_, $env) : $_ =~ $path_match;
29 39 100       238 return unless $matched;
30             }
31              
32 12   50     106 $self->{file} ||= Plack::App::File->new({ root => $self->root || '.', encoding => $self->encoding, content_type => $self->content_type });
      66        
33 12         56 local $env->{PATH_INFO} = $path; # rewrite PATH
34 12         62 return $self->{file}->call($env);
35             }
36              
37             1;
38             __END__