| 1 | package Plack::App::Directory; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use parent qw(Plack::App::File); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Plack::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use HTTP::Date; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Plack::MIME; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use DirHandle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Stolen from rack/directory.rb | ||||||||||||||||||||||||||||
| 13 | 1 | 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>"; | |||||||||||||||||||||||||||
| 14 | 1 | my $dir_page = <<PAGE; | |||||||||||||||||||||||||||
| 15 | <html><head> | ||||||||||||||||||||||||||||
| 16 | <title>%s</title> | ||||||||||||||||||||||||||||
| 17 | <meta http-equiv="content-type" content="text/html; charset=utf-8" /> | ||||||||||||||||||||||||||||
| 18 | <style type='text/css'> | ||||||||||||||||||||||||||||
| 19 | table { width:100%%; } | ||||||||||||||||||||||||||||
| 20 | .name { text-align:left; } | ||||||||||||||||||||||||||||
| 21 | .size, .mtime { text-align:right; } | ||||||||||||||||||||||||||||
| 22 | .type { width:11em; } | ||||||||||||||||||||||||||||
| 23 | .mtime { width:15em; } | ||||||||||||||||||||||||||||
| 24 | </style> | ||||||||||||||||||||||||||||
| 25 | </head><body> | ||||||||||||||||||||||||||||
| 26 | <h1>%s</h1> | ||||||||||||||||||||||||||||
| 27 | <hr /> | ||||||||||||||||||||||||||||
| 28 | <table> | ||||||||||||||||||||||||||||
| 29 | <tr> | ||||||||||||||||||||||||||||
| 30 | <th class='name'>Name</th> | ||||||||||||||||||||||||||||
| 31 | <th class='size'>Size</th> | ||||||||||||||||||||||||||||
| 32 | <th class='type'>Type</th> | ||||||||||||||||||||||||||||
| 33 | <th class='mtime'>Last Modified</th> | ||||||||||||||||||||||||||||
| 34 | </tr> | ||||||||||||||||||||||||||||
| 35 | %s | ||||||||||||||||||||||||||||
| 36 | </table> | ||||||||||||||||||||||||||||
| 37 | <hr /> | ||||||||||||||||||||||||||||
| 38 | </body></html> | ||||||||||||||||||||||||||||
| 39 | PAGE | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | sub should_handle { | ||||||||||||||||||||||||||
|
Subroutine
should_handle: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 3 | my($self, $file) = @_; | |||||||||||||||||||||||||||
| 43 | 3 | ▶ | return -d $file || -f $file; | ||||||||||||||||||||||||||
|
Condition66%2/3
-d $file || -f $file
MC/DC0%0/2
-d $file || -f $file
-d $file
-f $file
Truth table66%2/3
A || B
A= -d $fileB= -f $file
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub return_dir_redirect { | ||||||||||||||||||||||||||
|
Subroutine
return_dir_redirect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 0 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 48 | 0 | my $uri = Plack::Request->new($env)->uri; | |||||||||||||||||||||||||||
| 49 | 0 | return [ 301, | |||||||||||||||||||||||||||
| 50 | [ | ||||||||||||||||||||||||||||
| 51 | 'Location' => $uri . '/', | ||||||||||||||||||||||||||||
| 52 | 'Content-Type' => 'text/plain', | ||||||||||||||||||||||||||||
| 53 | 'Content-Length' => 8, | ||||||||||||||||||||||||||||
| 54 | ], | ||||||||||||||||||||||||||||
| 55 | [ 'Redirect' ], | ||||||||||||||||||||||||||||
| 56 | ]; | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | sub serve_path { | ||||||||||||||||||||||||||
|
Subroutine
serve_path: called CC=10 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 3 | my($self, $env, $dir) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | if (-f $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | return $self->SUPER::serve_path($env, $dir); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO}; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | ▶ | if ($dir_url !~ m{/$}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | return $self->return_dir_redirect($env); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | my @files = ([ "../", "Parent Directory", '', '', '' ]); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | my $dh = DirHandle->new($dir); | |||||||||||||||||||||||||||
| 75 | 2 | my @children; | |||||||||||||||||||||||||||
| 76 | 2 | ▶ | while (defined(my $ent = $dh->read)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 10 | ▶ | 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 ".."
| |||||||||||||||||||||||||||||
| 78 | 6 | push @children, $ent; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | for my $basename (sort { $a cmp $b } @children) { | |||||||||||||||||||||||||||
| 82 | 6 | my $file = "$dir/$basename"; | |||||||||||||||||||||||||||
| 83 | 6 | my $url = $dir_url . $basename; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 6 | my $is_dir = -d $file; | |||||||||||||||||||||||||||
| 86 | 6 | my @stat = stat _; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 6 | $url = join '/', map {uri_escape($_)} split m{/}, $url; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 6 | ▶ | if ($is_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $basename .= "/"; | |||||||||||||||||||||||||||
| 92 | 0 | $url .= "/"; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 6 | ▶ | my $mime_type = $is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
"Plack::MIME"->mime_type($file) or "text/plain"
MC/DC100%1/1
"Plack::MIME"->mime_type($file) or "text/plain"
"Plack::MIME"->mime_type($file)
Truth table100%2/2
A
A= "Plack::MIME"->mime_type($file)
| |||||||||||||||||||||||||||||
| 96 | 6 | push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ]; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | my $path = Plack::Util::encode_html("Index of $env->{PATH_INFO}"); | |||||||||||||||||||||||||||
| 100 | my $files = join "\n", map { | ||||||||||||||||||||||||||||
| 101 | 2 | my $f = $_; | |||||||||||||||||||||||||||
| 102 | 8 | sprintf $dir_file, map Plack::Util::encode_html($_), @$f; | |||||||||||||||||||||||||||
| 103 | } @files; | ||||||||||||||||||||||||||||
| 104 | 2 | my $page = sprintf $dir_page, $path, $path, $files; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ]; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | 1; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||