File Coverage

lib/Plack/Middleware/DirIndex.pm
Criterion Covered Total %
statement 21 21 100.0
branch 5 6 83.3
condition 3 3 100.0
subroutine 7 7 100.0
pod 2 2 100.0
total 38 39 97.4


line stmt bran cond sub pod time code
1             package Plack::Middleware::DirIndex;
2             $Plack::Middleware::DirIndex::VERSION = '1.01';
3             # ABSTRACT: Append an index file to request PATH's ending with a /
4              
5 2     2   199205 use parent qw( Plack::Middleware );
  2         5  
  2         8  
6 2     2   96 use Plack::Util::Accessor qw(dir_index root);
  2         3  
  2         10  
7 2     2   79 use strict;
  2         5  
  2         38  
8 2     2   8 use warnings;
  2         2  
  2         35  
9 2     2   27 use 5.006;
  2         4  
10              
11             =head1 NAME
12              
13             Plack::Middleware::DirIndex - Middleware to use with Plack::App::Directory and the like
14              
15             =head1 SYNOPSIS
16              
17             use Plack::Builder;
18             use Plack::App::File;
19             use Plack::Middleware::DirIndex;
20              
21             my $app = Plack::App::File->new({ root => './htdocs/' })->to_app;
22              
23             builder {
24             enable "Plack::Middleware::DirIndex", dir_index => 'index.html';
25             $app;
26             }
27            
28             =head1 DESCRIPTION
29              
30             If $env->{PATH_INFO} ends with a '/' then we will append the dir_index
31             value to it (defaults to index.html)
32              
33             =head1 COPYRIGHT & LICENSE
34              
35             Copyright (c) 2012 Leo Lapworth. All rights reserved.
36             This program is free software; you can redistribute
37             it and/or modify it under the same terms as Perl itself.
38              
39             =cut
40              
41             sub prepare_app {
42 4     4 1 3539 my ($self) = @_;
43              
44 4 50       15 $self->root('.') unless $self->root;
45 4 100       84 $self->dir_index('index.html') unless $self->dir_index;
46             }
47              
48             sub call {
49 10     10 1 67762 my ( $self, $env ) = @_;
50              
51 10 100 100     51 if ( $env->{PATH_INFO} =~ m{/$}
52             and -f $self->root . '/' . $env->{PATH_INFO} . $self->dir_index ) {
53              
54 4         97 $env->{PATH_INFO} .= $self->dir_index();
55             }
56              
57 10         129 return $self->app->($env);
58             }
59              
60             1;