File Coverage

lib/Plack/Middleware/DirIndex/Htaccess.pm
Criterion Covered Total %
statement 41 41 100.0
branch 13 16 81.2
condition 3 3 100.0
subroutine 9 9 100.0
pod 2 3 66.6
total 68 72 94.4


line stmt bran cond sub pod time code
1             package Plack::Middleware::DirIndex::Htaccess;
2             $Plack::Middleware::DirIndex::Htaccess::VERSION = '1.00';
3              
4             # ABSTRACT: Check .htaccess file for DirectoryIndex
5              
6 2     2   871757 use parent qw( Plack::Middleware );
  2         5  
  2         18  
7 2     2   2315 use Plack::Util::Accessor qw(dir_index root);
  2         5  
  2         9  
8 2     2   118 use strict;
  2         6  
  2         49  
9 2     2   11 use warnings;
  2         3  
  2         99  
10 2     2   42 use 5.006;
  2         9  
11              
12             sub check_htaccess {
13 10     10 0 20 my ($self, $dir) = @_;
14              
15 10         17 my $htaccess_file = "${dir}.htaccess";
16 10 100       316 return unless (-f $htaccess_file);
17              
18 2 50   2   1692 open my $fh, '<:encoding(UTF-8)', $htaccess_file
  2         35  
  2         14  
  2         95  
19             or die "Cannot open $htaccess_file $!";
20              
21 2         2311 local $/;
22 2         81 my $content = <$fh>;
23 2         86 close $fh;
24              
25 2         5 my $dir_index;
26 2 50       28 if ($content =~ /^DirectoryIndex\s(.*?)$/) {
27 2         6 $dir_index = $1;
28             }
29              
30 2         28 return $dir_index;
31             }
32              
33             sub prepare_app {
34 4     4 1 734912 my ($self) = @_;
35              
36 4 50       18 $self->root('.') unless $self->root;
37 4 100       114 $self->dir_index('index.html') unless $self->dir_index;
38             }
39              
40             sub call {
41 14     14 1 94988 my ( $self, $env ) = @_;
42              
43 14         25 my $index;
44 14 100       73 if ( $env->{PATH_INFO} =~ m{/$} ) {
45 10         32 my $dir = $self->root . $env->{PATH_INFO};
46 10         68 $index = $self->check_htaccess( $dir );
47 10 100 100     51 if (!$index && -f $dir . $self->dir_index()) {
48 4         112 $index = $self->dir_index();
49             }
50             }
51              
52 14 100       177 if ($index) {
53 6         18 $env->{PATH_INFO} .= $index;
54             }
55              
56 14         58 return $self->app->($env);
57             }
58              
59             1;
60              
61             =head1 NAME
62              
63             Plack::Middleware::DirIndex::Htaccess - Serve default html files for directory URLS. Includes checking of .htaccess file for DirectoryIndex.
64              
65             =head1 SYNOPSIS
66              
67             use Plack::Builder;
68             use Plack::App::Directory;
69              
70             my $app = Plack::App::Directory->new()->to_app;
71              
72             builder {
73             enable "DirIndex::Htaccess", root => '.', dir_index => 'index.html';
74             $app;
75             };
76              
77             =head1 DESCRIPTION
78              
79             The Apache web server uses the C directive within
80             C<.htaccess> files to automatically serve a default page
81             (e.g., C when a user requests a directory URL like C).
82              
83             This Plack middleware provides a lightweight simulation of
84             Apache's DirectoryIndex functionality for your local Plack environment.
85             By reading the C from your existing C<.htaccess> files,
86             it ensures that requests to directories resolve to the correct default
87             file, just as they would on the live server.
88              
89             If there is no c directive (or C<.htaccess> file), the module will append the value of (defaults to C) if the file exists, like Plack::Middleware::DirIndex.
90              
91             =head1 AUTHOR
92              
93             Keith Carangelo
94              
95             =cut