From 95f2f6ee3ee7a2f8b1e4fe6350bb8589605c95ac Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Thu, 19 Nov 2020 16:15:16 +0000 Subject: [PATCH] Add support for a dir_index option --- lib/Plack/App/Directory.pm | 5 +++++ t/Plack-Middleware/directory.t | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/Plack/App/Directory.pm b/lib/Plack/App/Directory.pm index 48443ca09..1b0cbf745 100644 --- a/lib/Plack/App/Directory.pm +++ b/lib/Plack/App/Directory.pm @@ -3,6 +3,7 @@ use parent qw(Plack::App::File); use strict; use warnings; use Plack::Util; +use Plack::Util::Accessor 'dir_index'; use HTTP::Date; use Plack::MIME; use DirHandle; @@ -69,6 +70,10 @@ sub serve_path { return $self->return_dir_redirect($env); } + if ($self->dir_index and -f $dir . $self->dir_index) { + return $self->SUPER::serve_path($env, $dir . $self->dir_index); + } + my @files = ([ "../", "Parent Directory", '', '', '' ]); my $dh = DirHandle->new($dir); diff --git a/t/Plack-Middleware/directory.t b/t/Plack-Middleware/directory.t index a2bfb3302..cd402f4cc 100644 --- a/t/Plack-Middleware/directory.t +++ b/t/Plack-Middleware/directory.t @@ -56,4 +56,26 @@ my %test = ( test_psgi %test; +$handler = Plack::App::Directory->new({ root => 'share', dir_index => 'index.html' }); + +%test = ( + client => sub { + my $cb = shift; + + open my $fh, ">", "share/index.html" or die $!; + print $fh "\n"; + close $fh; + + my $res = $cb->(GET "/"); + is $res->code, 200; + is $res->content, "\n"; + + unlink "share/index.html"; + + }, + app => $handler, +); + +test_psgi %test; + done_testing;