File Coverage

blib/lib/Plack/App/DirectoryIndex.pm
Criterion Covered Total %
statement 30 31 96.7
branch 8 10 80.0
condition 6 8 75.0
subroutine 6 6 100.0
pod 0 1 0.0
total 50 56 89.2


line stmt bran cond sub pod time code
1             package Plack::App::DirectoryIndex;
2              
3 1     1   160673 use parent qw[Plack::App::Directory];
  1         2  
  1         8  
4              
5 1     1   140085 use strict;
  1         2  
  1         29  
6 1     1   7 use warnings;
  1         2  
  1         62  
7              
8 1     1   7 use Plack::Util::Accessor qw[dir_index icons pretty];
  1         2  
  1         35  
9 1     1   775 use WebServer::DirIndex;
  1         104077  
  1         452  
10              
11             our $VERSION = '0.2.1';
12              
13             # NOTE: Copied from Plack::App::Directory as that module makes it
14             # impossible to override the HTML.
15              
16             sub serve_path {
17 5     5 0 278686 my $self = shift;
18 5         16 my ($env, $dir) = @_;
19              
20 5   100     22 my $dir_index = $self->dir_index // 'index.html';
21              
22 5 100 66     170 if (-d $dir and $dir_index and -f "$dir$dir_index") {
      66        
23 3         13 $dir .= $dir_index;
24             }
25              
26 5 100       59 if (-f $dir) {
27 3         27 return $self->SUPER::serve_path($env, $dir);
28             }
29            
30 2         10 my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
31            
32 2 50       16 if ($dir_url !~ m{/$}) {
33 0         0 return $self->return_dir_redirect($env);
34             }
35              
36 2         15 my %dir_index_args = (
37             dir => $dir,
38             dir_url => $dir_url,
39             );
40              
41 2 50       11 $dir_index_args{pretty} = $self->pretty if defined $self->pretty;
42 2 100       19 $dir_index_args{icons} = $self->icons if defined $self->icons;
43            
44 2         64 my $di = WebServer::DirIndex->new(%dir_index_args);
45 2         1510 my $page = $di->to_html($env->{PATH_INFO});
46              
47 2         246 return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
48             }
49              
50             1;
51              
52             __END__