| 1 | package Plack::Middleware::DirIndex::Htaccess; | ||||||||||||||||||||||||||||
| 2 | 2 | $Plack::Middleware::DirIndex::Htaccess::VERSION = '1.00'; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Check .htaccess file for DirectoryIndex | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use parent qw( Plack::Middleware ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Plack::Util::Accessor qw(dir_index root); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 10 | ▶ | sub check_htaccess { | ||||||||||||||||||||||||||
|
Subroutine
check_htaccess: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 10 | my ($self, $dir) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 10 | my $htaccess_file = "${dir}.htaccess"; | |||||||||||||||||||||||||||
| 16 | 10 | ▶ | return unless (-f $htaccess_file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | open my $fh, '<:encoding(UTF-8)', $htaccess_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | or die "Cannot open $htaccess_file $!"; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | local $/; | |||||||||||||||||||||||||||
| 22 | 2 | my $content = <$fh>; | |||||||||||||||||||||||||||
| 23 | 2 | close $fh; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | my $dir_index; | |||||||||||||||||||||||||||
| 26 | 2 | ▶ | if ($content =~ /^DirectoryIndex\s(.*?)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 2 | $dir_index = $1; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2 | return $dir_index; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | ▶ | $self->root('.') unless $self->root; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | $self->dir_index('index.html') unless $self->dir_index; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 14 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 14 | my ( $self, $env ) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 14 | my $index; | |||||||||||||||||||||||||||
| 44 | 14 | ▶ | if ( $env->{PATH_INFO} =~ m{/$} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 10 | my $dir = $self->root . $env->{PATH_INFO}; | |||||||||||||||||||||||||||
| 46 | 10 | $index = $self->check_htaccess( $dir ); | |||||||||||||||||||||||||||
| 47 | 10 | ▶ | if (!$index && -f $dir . $self->dir_index()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $index and -f $dir . $self->dir_index
MC/DC100%2/2
not $index and -f $dir . $self->dir_index
not $index
-f $dir . $self->dir_index
Truth table100%3/3
not(A) and B
A= not $indexB= -f $dir . $self->dir_index
| |||||||||||||||||||||||||||||
| 48 | 4 | $index = $self->dir_index(); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 14 | ▶ | if ($index) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 6 | $env->{PATH_INFO} .= $index; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 14 | return $self->app->($env); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | 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<DirectoryIndex> directive within | ||||||||||||||||||||||||||||
| 80 | C<.htaccess> files to automatically serve a default page | ||||||||||||||||||||||||||||
| 81 | (e.g., C<about.html> when a user requests a directory URL like C</about/>). | ||||||||||||||||||||||||||||
| 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<DirectoryIndex> 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<DirectoryIndex> directive (or C<.htaccess> file), the module will append the value of <dir_index> (defaults to C<index.html>) if the file exists, like Plack::Middleware::DirIndex. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Keith Carangelo <kcaran@gmail.com> | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||