| 1 | package Plack::Middleware::DirListing; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Display a directory listing if no default index html page | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use parent qw( Plack::Middleware ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use DirHandle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Plack::MIME; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Plack::Util::Accessor qw( root ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | sub dir_html { | ||||||||||||||||||||||||||
|
Subroutine
dir_html: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | state $html = do { | |||||||||||||||||||||||||||
| 20 | 2 | local $/ = undef; | |||||||||||||||||||||||||||
| 21 | 2 | my $data_string = <DATA>; | |||||||||||||||||||||||||||
| 22 | 2 | close DATA; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | $data_string =~ s/%(?!s)/%%/g; | |||||||||||||||||||||||||||
| 25 | 2 | $data_string; | |||||||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 5 | return $html; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 26 | ▶ | sub file_html { | ||||||||||||||||||||||||||
|
Subroutine
file_html: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 26 | return <<FILE; | |||||||||||||||||||||||||||
| 33 | <tr> | ||||||||||||||||||||||||||||
| 34 | <td class="%s"></td> | ||||||||||||||||||||||||||||
| 35 | <td class="name"><a href="%s">%s</a></td> | ||||||||||||||||||||||||||||
| 36 | <td class="mtime">%s</td> | ||||||||||||||||||||||||||||
| 37 | <td class="size">%s</td> | ||||||||||||||||||||||||||||
| 38 | <td class="type">%s</td> | ||||||||||||||||||||||||||||
| 39 | </tr> | ||||||||||||||||||||||||||||
| 40 | FILE | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 21 | ▶ | sub last_modified { | ||||||||||||||||||||||||||
|
Subroutine
last_modified: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 21 | my ($self, $date) = @_; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 21 | return Time::Piece->new( $date )->strftime( "%d-%b-%Y %H:%M" ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 5 | ▶ | sub sort_order { | ||||||||||||||||||||||||||
|
Subroutine
sort_order: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 5 | my ($self, $env, $page) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 5 | ▶ | if (my ($field, $order) = $env->{ QUERY_STRING } =~ /C=(\w);O=(\w)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | my $invert = ($order eq 'A') ? 'D' : 'A'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 3 | $page =~ s/C=$field;O=$order/C=$field;O=$invert/; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 5 | return $page; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | my %col_sort = ( | ||||||||||||||||||||||||||||
| 61 | 12 | ▶ | 'NA' => sub { $a->[2] cmp $b->[2] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 9 | ▶ | 'ND' => sub { $b->[2] cmp $a->[2] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | 'MA' => sub { $a->[6] <=> $b->[6] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | 'MD' => sub { $b->[6] <=> $a->[6] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | 'SA' => sub { $a->[4] <=> $b->[4] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 66 | 6 | ▶ | 'SD' => sub { $b->[4] <=> $a->[4] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | 'DA' => sub { $a->[5] cmp $b->[5] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | 'DD' => sub { $b->[5] cmp $a->[5] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 69 | 2 | ); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 21 | ▶ | sub filetype_class { | ||||||||||||||||||||||||||
|
Subroutine
filetype_class: called CC=5 SCAR=17.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 21 | my ($self, $filetype) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 21 | ▶ | return 'ft_directory' if ($filetype eq 'directory'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 21 | ▶ | return 'ft_image' if ($filetype =~ /^image/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 21 | ▶ | return 'ft_pdf' if ($filetype =~ /pdf$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 21 | ▶ | return 'ft_html' if ($filetype =~ /html$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 21 | return ''; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 5 | ▶ | sub read_dir { | ||||||||||||||||||||||||||
|
Subroutine
read_dir: called CC=9 SCAR=22.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 5 | my ($self, $env, $dir) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 5 | my @files; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 5 | my $dh = DirHandle->new($dir); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 5 | ▶ | while (defined(my $ent = $dh->read)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 31 | ▶ | next if $ent eq '.' or $ent eq '..'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ent eq "." or $ent eq ".."
MC/DC100%2/2
$ent eq "." or $ent eq ".."
$ent eq "."
$ent eq ".."
Truth table100%3/3
A or B
A= $ent eq "."B= $ent eq ".."
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 21 | my $file = "$dir/$ent"; | |||||||||||||||||||||||||||
| 94 | 21 | my $url = $env->{PATH_INFO} . $ent; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 21 | my $is_dir = -d $file; | |||||||||||||||||||||||||||
| 97 | 21 | my @stat = stat _; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 21 | $url = join '/', map {uri_escape($_)} split m{/}, $url; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 21 | ▶ | if ($is_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | $ent .= "/"; | |||||||||||||||||||||||||||
| 103 | 0 | $url .= "/"; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 21 | ▶ | my $mime_type = $is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
"Plack::MIME"->mime_type($file) or "text/plain"
MC/DC0%0/1
"Plack::MIME"->mime_type($file) or "text/plain"
"Plack::MIME"->mime_type($file)
Truth table50%1/2
A
A= "Plack::MIME"->mime_type($file)
| |||||||||||||||||||||||||||||
| 107 | 21 | my $filetype_class = $self->filetype_class( $mime_type ); | |||||||||||||||||||||||||||
| 108 | 21 | push @files, [ $filetype_class, $url, $ent, $self->last_modified( $stat[9] ), $stat[7], $mime_type, $stat[9] ]; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 5 | my ($field, $order) = $env->{ QUERY_STRING } =~ /C=(\w);O=(\w)/; | |||||||||||||||||||||||||||
| 112 | 5 | ▶ | $field ||= 'N'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$field ||= "N"
MC/DC100%1/1
$field ||= "N"
$field
Truth table100%2/2
A
A= $field
| |||||||||||||||||||||||||||||
| 113 | 5 | ▶ | $order ||= 'A'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$order ||= "A"
MC/DC100%1/1
$order ||= "A"
$order
Truth table100%2/2
A
A= $order
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 5 | @files = sort { &{ $col_sort{ "$field$order" } } } @files; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 5 | return [ [ 'ft_parent', "../", "Parent Directory", '', '', '', 0], @files ]; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 2 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | ▶ | $self->root('.') unless $self->root; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # NOTE: Copied from Plack::App::Directory as that module makes it | ||||||||||||||||||||||||||||
| 127 | # impossible to override the HTML. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 5 | ▶ | sub serve_path { | ||||||||||||||||||||||||||
|
Subroutine
serve_path: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | 5 | my ($env, $dir) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 5 | my $files = $self->read_dir( $env, $dir ); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 5 | my $path = Plack::Util::encode_html("Index of $env->{PATH_INFO}"); | |||||||||||||||||||||||||||
| 136 | my $files_html = join "\n", map { | ||||||||||||||||||||||||||||
| 137 | 26 | my $f = $_; | |||||||||||||||||||||||||||
| 138 | 26 | sprintf $self->file_html, map Plack::Util::encode_html($_), @{ $f }[ 0..5 ]; | |||||||||||||||||||||||||||
| 139 | 5 | } @{ $files }; | |||||||||||||||||||||||||||
| 140 | 5 | my $page = sprintf $self->dir_html, $path, $path, $files_html, $env->{ HTTP_HOST }; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 5 | $page = $self->sort_order( $env, $page ); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 5 | return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ]; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 7 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 7 | my ( $self, $env ) = @_; | |||||||||||||||||||||||||||
| 149 | 7 | my $req = Plack::Request->new( $env ); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 7 | my $dir = $self->root . $req->path_info(); | |||||||||||||||||||||||||||
| 152 | 7 | ▶ | if (-d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 7 | ▶ | if (substr( $dir, -1 ) eq '/') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 5 | return $self->serve_path( $env, $dir ); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | else { | ||||||||||||||||||||||||||||
| 157 | 2 | my $uri = $req->uri(); | |||||||||||||||||||||||||||
| 158 | 2 | $uri->path( $uri->path . '/' ); | |||||||||||||||||||||||||||
| 159 | 2 | my $res = $req->new_response(301); # new Plack::Response | |||||||||||||||||||||||||||
| 160 | 2 | $res->headers([ | |||||||||||||||||||||||||||
| 161 | 'Location' => $uri, | ||||||||||||||||||||||||||||
| 162 | 'Content-Type' => 'text/html; charset=UTF-8', | ||||||||||||||||||||||||||||
| 163 | 'Cache-Control' => 'must-revalidate, max-age=3600' | ||||||||||||||||||||||||||||
| 164 | ]); | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | my $uhe = encode_entities($uri); | |||||||||||||||||||||||||||
| 167 | 2 | $res->body( <<REDIRECT_BODY ); | |||||||||||||||||||||||||||
| 168 | <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>301 Moved Permanently</title></head><body><h1>Moved Permanently</h1><p>The document has moved <a href="$uhe">here</a>.</p></body></html> | ||||||||||||||||||||||||||||
| 169 | REDIRECT_BODY | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 2 | return $res->finalize; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | return $self->app->($env); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 NAME | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Plack::Middleware::DirListing - Display a listing of a directory in html | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | use Plack::Builder; | ||||||||||||||||||||||||||||
| 185 | use Plack::App::File; | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | my $app = Plack::App::File->new({ root => '.' })->to_app; | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | builder { | ||||||||||||||||||||||||||||
| 190 | enable "DirListing", root => '.'; | ||||||||||||||||||||||||||||
| 191 | $app; | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | This Plack middleware provides the same functionality as L<Plack::App::Directory>, but *only* serves the directory listing if the URL points to a directory. It does not try to serve any files. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | It also strives to have a cleaner UI that more closely matches a prettified version of the Apache web server's output. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | This modules does not attempt to find a default html file for the directory. If desired, include L<Plack::Middleware::DirIndex::Htaccess> or L<Plack::Middleware::DirIndex> before this module. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =over 4 | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =item root | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Document root directory. Defaults to the current directory. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =back | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | Keith Carangelo <kcaran@gmail.com> | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 2 | 1; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | __DATA__ | ||||||||||||||||||||||||||||