Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept an option to define a callback to render the index page. #695

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 70 additions & 6 deletions lib/Plack/App/Directory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +39,19 @@ table { width:100%%; }
</body></html>
PAGE

sub render_index {
my $self = shift;
my ($path, @files) = @_;

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, $title, $title, $files;
}

sub should_handle {
my($self, $file) = @_;
return -d $file || -f $file;
Expand Down Expand Up @@ -96,12 +110,9 @@ 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 $page = $self->render_cb ?
$self->render_cb->($env->{PATH_INFO}, @files) :
$self->render_index($env->{PATH_INFO}, @files);

return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
}
Expand All @@ -120,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.
Expand All @@ -132,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
Expand Down