From 05eead98a8d9e894aa6d3f82c1ae4e52f02f5759 Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Tue, 6 Jun 2023 15:18:19 +0100 Subject: [PATCH 1/3] Accept an option to define a callback to render the index page. --- lib/Plack/App/Directory.pm | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/Plack/App/Directory.pm b/lib/Plack/App/Directory.pm index 48443ca09..73069a286 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 'render_cb'; use HTTP::Date; use Plack::MIME; use DirHandle; @@ -38,6 +39,19 @@ table { width:100%%; } PAGE +sub render_index { + my $self = shift; + my ($path, @files) = @_; + + $path = Plack::Util::encode_html($path); + my $files = join "\n", map { + my $f = $_; + sprintf $dir_file, map Plack::Util::encode_html($_), @$f; + } @files; + + return sprintf $dir_page, $path, $path, $files; +} + sub should_handle { my($self, $file) = @_; return -d $file || -f $file; @@ -96,12 +110,11 @@ sub serve_path { push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ]; } - my $path = Plack::Util::encode_html("Index of $env->{PATH_INFO}"); - my $files = join "\n", map { - my $f = $_; - sprintf $dir_file, map Plack::Util::encode_html($_), @$f; - } @files; - my $page = sprintf $dir_page, $path, $path, $files; + my $path = "Index of $env->{PATH_INFO}"; + + my $page = $self->render_cb ? + $self->render_cb->($path, @files) : + $self->render_index($path, @files); return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ]; } From 2076312caa32cd6b1841dbcdfc91b3419945bbdf Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Wed, 7 Jun 2023 16:31:54 +0100 Subject: [PATCH 2/3] Small refactoring --- lib/Plack/App/Directory.pm | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/Plack/App/Directory.pm b/lib/Plack/App/Directory.pm index 73069a286..24ab2e1b5 100644 --- a/lib/Plack/App/Directory.pm +++ b/lib/Plack/App/Directory.pm @@ -43,13 +43,13 @@ sub render_index { my $self = shift; my ($path, @files) = @_; - $path = Plack::Util::encode_html($path); + my $title = Plack::Util::encode_html("Index of $path"); my $files = join "\n", map { my $f = $_; sprintf $dir_file, map Plack::Util::encode_html($_), @$f; } @files; - return sprintf $dir_page, $path, $path, $files; + return sprintf $dir_page, $title, $title, $files; } sub should_handle { @@ -110,11 +110,9 @@ sub serve_path { push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ]; } - my $path = "Index of $env->{PATH_INFO}"; - my $page = $self->render_cb ? - $self->render_cb->($path, @files) : - $self->render_index($path, @files); + $self->render_cb->($env->{PATH_INFO}, @files) : + $self->render_index($env->{PATH_INFO}, @files); return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ]; } From 2b579404f60bddbf4d126c55ad7bfbd42c4a30a4 Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Wed, 7 Jun 2023 16:49:35 +0100 Subject: [PATCH 3/3] Add documentation for render_db --- lib/Plack/App/Directory.pm | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/lib/Plack/App/Directory.pm b/lib/Plack/App/Directory.pm index 24ab2e1b5..32968f7ae 100644 --- a/lib/Plack/App/Directory.pm +++ b/lib/Plack/App/Directory.pm @@ -131,6 +131,21 @@ Plack::App::Directory - Serve static files from document root with directory ind use Plack::App::Directory; my $app = Plack::App::Directory->new({ root => "/path/to/htdocs" })->to_app; +Or + + my $app = Plack::App::Directory->new({ + root => "/path/to/htdocs", + render_cb => \&render, + })->to_app; + + sub render { + my ($path, @files) = @_; + + # Code to render an HTML page + + return $html; + } + =head1 DESCRIPTION This is a static file server PSGI application with directory index a la Apache's mod_autoindex. @@ -143,6 +158,44 @@ This is a static file server PSGI application with directory index a la Apache's Document root directory. Defaults to the current directory. +=item render_cb + +A reference to a subroutine that takes information about a directory and +returns the HTML to display that directory - this is used to override the +default display of the directory. + +The first argument passed to this subroutine is the name of the path that +is being displayed. All other arguments contain information about the +files in the directory. Each of these arguments is a reference to an +array with five elements. These elements are: + +=over 4 + +=item * + +The URL of the file + +=item * + +The name of the file + +=item * + +The size of the file in bytes + +=item * + +The MIME type of the file + +=item * + +The last modified date of the file + +=back + +This subroutine is expected to return a string of HTML which will be +returned to the browser. + =back =head1 AUTHOR