| 1 | package PAGI::App::Directory; | ||||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::App::Directory::VERSION = '0.002002'; | |||||||||||||||||||||||||||||
| 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 Future::AsyncAwait; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use parent 'PAGI::App::File'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Cwd qw(realpath); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||
| 13 | PAGI::App::Directory - Serve files with directory listing | ||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | use PAGI::App::Directory; | ||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | my $app = PAGI::App::Directory->new( | ||||||||||||||||||||||||||||||
| 20 | root => '/var/www/files', | ||||||||||||||||||||||||||||||
| 21 | )->to_app; | ||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||
| 25 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 26 | 3 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 3 | my $self = $class->SUPER::new(%args); | |||||||||||||||||||||||||||||
| 29 | 3 | ▶ | $self->{show_hidden} = $args{show_hidden} // 0; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"show_hidden"} // 0
MC/DC0%0/1
$args{"show_hidden"} // 0
$args{"show_hidden"}
Truth table50%1/2
A
A= $args{"show_hidden"}
| |||||||||||||||||||||||||||||||
| 30 | # Cache realpath of root for symlink escape detection | ||||||||||||||||||||||||||||||
| 31 | 3 | ▶ | $self->{real_root} = realpath($self->{root}) // $self->{root}; | ||||||||||||||||||||||||||||
|
Condition33%1/3
realpath($self->{"root"}) // $self->{"root"}
MC/DC0%0/2
realpath($self->{"root"}) // $self->{"root"}
realpath($self->{"root"})
$self->{"root"}
Truth table33%1/3
A // B
A= realpath($self->{"root"})B= $self->{"root"}
| |||||||||||||||||||||||||||||||
| 32 | 3 | return $self; | |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | # HTML escape to prevent XSS | ||||||||||||||||||||||||||||||
| 36 | 11 | ▶ | sub _html_escape { | ||||||||||||||||||||||||||||
|
Subroutine
_html_escape: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 37 | 11 | my $str = shift; | |||||||||||||||||||||||||||||
| 38 | 11 | ▶ | return '' unless defined $str; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 39 | 10 | $str =~ s/&/&/g; | |||||||||||||||||||||||||||||
| 40 | 10 | $str =~ s/</</g; | |||||||||||||||||||||||||||||
| 41 | 10 | $str =~ s/>/>/g; | |||||||||||||||||||||||||||||
| 42 | 10 | $str =~ s/"/"/g; | |||||||||||||||||||||||||||||
| 43 | 10 | $str =~ s/'/'/g; | |||||||||||||||||||||||||||||
| 44 | 10 | return $str; | |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | # URL encode for href attributes | ||||||||||||||||||||||||||||||
| 48 | 5 | ▶ | sub _url_encode { | ||||||||||||||||||||||||||||
|
Subroutine
_url_encode: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 49 | 5 | my $str = shift; | |||||||||||||||||||||||||||||
| 50 | 5 | ▶ | return '' unless defined $str; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 51 | 4 | $str =~ s/([^A-Za-z0-9\-_.~\/])/sprintf("%%%02X", ord($1))/ge; | |||||||||||||||||||||||||||||
| 52 | 4 | return $str; | |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | 3 | ▶ | sub to_app { | ||||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 56 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||
| 58 | 3 | my $parent_app = $self->SUPER::to_app(); | |||||||||||||||||||||||||||||
| 59 | 3 | my $root = $self->{root}; | |||||||||||||||||||||||||||||
| 60 | 3 | my $real_root = $self->{real_root}; | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | return async sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=23.3
| |||||||||||||||||||||||||||||||
| 63 | 3 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||
| 64 | 3 | ▶ | die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'http'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | my $path = $scope->{path} // '/'; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"path"} // "/"
MC/DC0%0/1
$scope->{"path"} // "/"
$scope->{"path"}
Truth table50%1/2
A
A= $scope->{"path"}
| |||||||||||||||||||||||||||||||
| 67 | 3 | $path =~ s{^/+}{}; | |||||||||||||||||||||||||||||
| 68 | 3 | my $dir_path = File::Spec->catdir($root, $path); | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||
| 70 | # Symlink escape check: ensure resolved path is within root | ||||||||||||||||||||||||||||||
| 71 | 3 | my $real_dir = realpath($dir_path); | |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | if (!$real_dir || index($real_dir, $real_root) != 0) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $real_dir or index($real_dir, $real_root) != 0
MC/DC50%1/2
not $real_dir or index($real_dir, $real_root) != 0
not $real_dir
index($real_dir, $real_root) != 0
Truth table66%2/3
not(A) or B
A= not $real_dirB= index($real_dir, $real_root) != 0
| |||||||||||||||||||||||||||||||
| 73 | 2 | await $self->_send_error($send, 403, 'Forbidden'); | |||||||||||||||||||||||||||||
| 74 | 2 | return; | |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | # If it's a directory without index file, show listing | ||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | if (-d $dir_path) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 79 | 1 | my $has_index = 0; | |||||||||||||||||||||||||||||
| 80 | 1 | for my $index (@{$self->{index}}) { | |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | if (-f File::Spec->catfile($dir_path, $index)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | 0 | $has_index = 1; | |||||||||||||||||||||||||||||
| 83 | 0 | last; | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | 1 | ▶ | unless ($has_index) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 88 | 1 | await $self->_send_listing($send, $scope, $dir_path, $path); | |||||||||||||||||||||||||||||
| 89 | 1 | return; | |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | # Fall back to parent file serving | ||||||||||||||||||||||||||||||
| 94 | 0 | await $parent_app->($scope, $receive, $send); | |||||||||||||||||||||||||||||
| 95 | 3 | }; | |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | async sub _send_listing { | ||||||||||||||||||||||||||||
|
Subroutine
_send_listing: called CC=18 SCAR=32.5
| |||||||||||||||||||||||||||||||
| 99 | 1 | my ($self, $send, $scope, $dir_path, $rel_path) = @_; | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | opendir my $dh, $dir_path or do { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 102 | 0 | await $self->_send_error($send, 403, 'Forbidden'); | |||||||||||||||||||||||||||||
| 103 | 0 | return; | |||||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | 1 | my @entries; | |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | while (my $entry = readdir $dh) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 108 | 3 | ▶ | next if $entry eq '.'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | next if !$self->{show_hidden} && $entry =~ /^\./; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->{"show_hidden"} and $entry =~ /^\./
MC/DC50%1/2
not $self->{"show_hidden"} and $entry =~ /^\./
not $self->{"show_hidden"}
$entry =~ /^\./
Truth table66%2/3
not(A) and B
A= not $self->{"show_hidden"}B= $entry =~ /^\./
| |||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||
| 111 | 1 | my $full_path = File::Spec->catfile($dir_path, $entry); | |||||||||||||||||||||||||||||
| 112 | 1 | my @stat = stat($full_path); | |||||||||||||||||||||||||||||
| 113 | 1 | ▶ | push @entries, { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$stat[7] // 0
$stat[9] // 0
MC/DC0%0/1
$stat[7] // 0
$stat[7]
MC/DC0%0/1
$stat[9] // 0
$stat[9]
Truth table50%1/2
A
A= $stat[7]
Truth table50%1/2
A
A= $stat[9]
| |||||||||||||||||||||||||||||||
| 114 | name => $entry, | ||||||||||||||||||||||||||||||
| 115 | is_dir => -d $full_path ? 1 : 0, | ||||||||||||||||||||||||||||||
| 116 | size => $stat[7] // 0, | ||||||||||||||||||||||||||||||
| 117 | mtime => $stat[9] // 0, | ||||||||||||||||||||||||||||||
| 118 | }; | ||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||
| 120 | 1 | closedir $dh; | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | # Sort directories first, then by name | ||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | @entries = sort { $b->{is_dir} <=> $a->{is_dir} || $a->{name} cmp $b->{name} } @entries; | ||||||||||||||||||||||||||||
|
Condition0%0/3
$PAGI::App::Directory::b->{'is_dir'} <=> $PAGI::App::Directory::a->{'is_dir'} || $PAGI::App::Directory::a->{'name'} cmp $PAGI::App::Directory::b->{'name'}
MC/DC0%0/2
$PAGI::App::Directory::b->{'is_dir'} <=> $PAGI::App::Directory::a->{'is_dir'} || $PAGI::App::Directory::a->{'name'} cmp $PAGI::App::Directory::b->{'name'}
$PAGI::App::Directory::b->{'is_dir'} <=> $PAGI::App::Directory::a->{'is_dir'}
$PAGI::App::Directory::a->{'name'} cmp $PAGI::App::Directory::b->{'name'}
Truth table0%0/3
A || B
A= $PAGI::App::Directory::b->{'is_dir'} <=> $PAGI::App::Directory::a->{'is_dir'}B= $PAGI::App::Directory::a->{'name'} cmp $PAGI::App::Directory::b->{'name'}
| |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | # Check Accept header for JSON | ||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | my $accept = $self->_get_header($scope, 'accept') // ''; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_get_header($scope, "accept") // ""
MC/DC0%0/1
$self->_get_header($scope, "accept") // ""
$self->_get_header($scope, "accept")
Truth table50%1/2
A
A= $self->_get_header($scope, "accept")
| |||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | if ($accept =~ m{application/json}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 128 | 0 | my $json = JSON::MaybeXS::encode_json(\@entries); | |||||||||||||||||||||||||||||
| 129 | 0 | await $send->({ | |||||||||||||||||||||||||||||
| 130 | type => 'http.response.start', | ||||||||||||||||||||||||||||||
| 131 | status => 200, | ||||||||||||||||||||||||||||||
| 132 | headers => [['content-type', 'application/json'], ['content-length', length($json)]], | ||||||||||||||||||||||||||||||
| 133 | }); | ||||||||||||||||||||||||||||||
| 134 | 0 | await $send->({ type => 'http.response.body', body => $json, more => 0 }); | |||||||||||||||||||||||||||||
| 135 | 0 | return; | |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||
| 138 | # HTML listing | ||||||||||||||||||||||||||||||
| 139 | 1 | ▶ | my $base_path = $rel_path eq '' ? '/' : "/$rel_path"; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 140 | 1 | $base_path =~ s{/+$}{}; | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||
| 142 | # Escape base_path for safe HTML output | ||||||||||||||||||||||||||||||
| 143 | 1 | my $escaped_path = _html_escape($base_path); | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | 1 | my $html = "<!DOCTYPE html><html><head><title>Index of $escaped_path/</title>"; | |||||||||||||||||||||||||||||
| 146 | 1 | $html .= '<style>body{font-family:sans-serif;margin:20px}table{border-collapse:collapse}'; | |||||||||||||||||||||||||||||
| 147 | 1 | $html .= 'th,td{padding:8px 16px;text-align:left;border-bottom:1px solid #ddd}'; | |||||||||||||||||||||||||||||
| 148 | 1 | $html .= 'a{text-decoration:none;color:#0066cc}a:hover{text-decoration:underline}</style></head>'; | |||||||||||||||||||||||||||||
| 149 | 1 | $html .= "<body><h1>Index of $escaped_path/</h1><table><tr><th>Name</th><th>Size</th></tr>"; | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | 1 | ▶ | if ($rel_path ne '') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 152 | 1 | $html .= '<tr><td><a href="../">..</a></td><td>-</td></tr>'; | |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||
| 155 | 1 | for my $entry (@entries) { | |||||||||||||||||||||||||||||
| 156 | 1 | my $name = $entry->{name}; | |||||||||||||||||||||||||||||
| 157 | 1 | ▶ | my $display = $entry->{is_dir} ? "$name/" : $name; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 158 | 1 | ▶ | my $href = "$name" . ($entry->{is_dir} ? '/' : ''); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 159 | 1 | ▶ | my $size = $entry->{is_dir} ? '-' : _format_size($entry->{size}); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||
| 161 | # Escape all user-controlled values to prevent XSS | ||||||||||||||||||||||||||||||
| 162 | 1 | my $escaped_display = _html_escape($display); | |||||||||||||||||||||||||||||
| 163 | 1 | my $escaped_href = _html_escape(_url_encode($href)); | |||||||||||||||||||||||||||||
| 164 | 1 | $html .= qq{<tr><td><a href="$escaped_href">$escaped_display</a></td><td>$size</td></tr>}; | |||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||
| 167 | 1 | $html .= '</table></body></html>'; | |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||
| 169 | 1 | await $send->({ | |||||||||||||||||||||||||||||
| 170 | type => 'http.response.start', | ||||||||||||||||||||||||||||||
| 171 | status => 200, | ||||||||||||||||||||||||||||||
| 172 | headers => [['content-type', 'text/html'], ['content-length', length($html)]], | ||||||||||||||||||||||||||||||
| 173 | }); | ||||||||||||||||||||||||||||||
| 174 | 1 | await $send->({ type => 'http.response.body', body => $html, more => 0 }); | |||||||||||||||||||||||||||||
| 175 | 1 | } | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | sub _format_size { | ||||||||||||||||||||||||||||
|
Subroutine
_format_size: called CC=4 SCAR=25.0
| |||||||||||||||||||||||||||||||
| 178 | 1 | my $size = shift; | |||||||||||||||||||||||||||||
| 179 | 1 | ▶ | return '0' if $size == 0; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 180 | 0 | my @units = qw(B KB MB GB); | |||||||||||||||||||||||||||||
| 181 | 0 | my $i = 0; | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | while ($size >= 1024 && $i < $#units) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$size >= 1024 and $i < $#units
MC/DC0%0/2
$size >= 1024 and $i < $#units
$size >= 1024
$i < $#units
Truth table0%0/3
A and B
A= $size >= 1024B= $i < $#units
| |||||||||||||||||||||||||||||||
| 183 | 0 | $size /= 1024; | |||||||||||||||||||||||||||||
| 184 | 0 | $i++; | |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||
| 186 | 0 | return sprintf("%.1f %s", $size, $units[$i]); | |||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||
| 189 | 1 | 1; | |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||
| 191 | __END__ | ||||||||||||||||||||||||||||||