/usr/share/perl5/Plack/App/Directory.pm is in libplack-perl 1.0042-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | package Plack::App::Directory;
use parent qw(Plack::App::File);
use strict;
use warnings;
use Plack::Util;
use HTTP::Date;
use Plack::MIME;
use DirHandle;
use URI::Escape;
use Plack::Request;
# Stolen from rack/directory.rb
my $dir_file = "<tr><td class='name'><a href='%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>";
my $dir_page = <<PAGE;
<html><head>
<title>%s</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type='text/css'>
table { width:100%%; }
.name { text-align:left; }
.size, .mtime { text-align:right; }
.type { width:11em; }
.mtime { width:15em; }
</style>
</head><body>
<h1>%s</h1>
<hr />
<table>
<tr>
<th class='name'>Name</th>
<th class='size'>Size</th>
<th class='type'>Type</th>
<th class='mtime'>Last Modified</th>
</tr>
%s
</table>
<hr />
</body></html>
PAGE
sub should_handle {
my($self, $file) = @_;
return -d $file || -f $file;
}
sub return_dir_redirect {
my ($self, $env) = @_;
my $uri = Plack::Request->new($env)->uri;
return [ 301,
[
'Location' => $uri . '/',
'Content-Type' => 'text/plain',
'Content-Length' => 8,
],
[ 'Redirect' ],
];
}
sub serve_path {
my($self, $env, $dir, $fullpath) = @_;
if (-f $dir) {
return $self->SUPER::serve_path($env, $dir, $fullpath);
}
my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
if ($dir_url !~ m{/$}) {
return $self->return_dir_redirect($env);
}
my @files = ([ "../", "Parent Directory", '', '', '' ]);
my $dh = DirHandle->new($dir);
my @children;
while (defined(my $ent = $dh->read)) {
next if $ent eq '.' or $ent eq '..';
push @children, $ent;
}
for my $basename (sort { $a cmp $b } @children) {
my $file = "$dir/$basename";
my $url = $dir_url . $basename;
my $is_dir = -d $file;
my @stat = stat _;
$url = join '/', map {uri_escape($_)} split m{/}, $url;
if ($is_dir) {
$basename .= "/";
$url .= "/";
}
my $mime_type = $is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' );
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;
return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
}
1;
__END__
=head1 NAME
Plack::App::Directory - Serve static files from document root with directory index
=head1 SYNOPSIS
# app.psgi
use Plack::App::Directory;
my $app = Plack::App::Directory->new({ root => "/path/to/htdocs" })->to_app;
=head1 DESCRIPTION
This is a static file server PSGI application with directory index a la Apache's mod_autoindex.
=head1 CONFIGURATION
=over 4
=item root
Document root directory. Defaults to the current directory.
=back
=head1 AUTHOR
Tatsuhiko Miyagawa
=head1 SEE ALSO
L<Plack::App::File>
=cut
|