File Coverage

lib/Plack/Middleware/DirIndex.pm
Criterion Covered Total %
statement 21 21 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 34 34 100.0


line stmt bran cond sub pod time code
1             package Plack::Middleware::DirIndex;
2             $Plack::Middleware::DirIndex::VERSION = '1.00';
3             # ABSTRACT: Append an index file to request PATH's ending with a /
4              
5 1     1   38628 use parent qw( Plack::Middleware );
  1         1  
  1         5  
6 1     1   50 use Plack::Util::Accessor qw(dir_index);
  1         1  
  1         5  
7 1     1   25 use strict;
  1         5  
  1         20  
8 1     1   3 use warnings;
  1         1  
  1         19  
9 1     1   19 use 5.006;
  1         2  
  1         120  
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             Copyright (c) 2012 Leo Lapworth. All rights reserved.
35             This program is free software; you can redistribute
36             it and/or modify it under the same terms as Perl itself.
37              
38             =cut
39              
40             sub prepare_app {
41 2     2 1 1298 my ($self) = @_;
42              
43 2 100       8 $self->dir_index('index.html') unless $self->dir_index;
44             }
45              
46             sub call {
47 5     5 1 144087 my ( $self, $env ) = @_;
48              
49 5 100       24 if ( $env->{PATH_INFO} =~ m{/$} ) {
50 4         10 $env->{PATH_INFO} .= $self->dir_index();
51             }
52              
53 5         22 return $self->app->($env);
54             }
55              
56             1;