| 1 | 10 | ▶ | use v5.32; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | package Mojolicious::Plugin::DirectoryServer; | ||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | 10 | our $VERSION = '1.007'; | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Cwd (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Encode (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use DirHandle; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Mojo::Path; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use Mojo::Base qw( Mojolicious::Plugin -signatures ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use Mojo::JSON qw(encode_json); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use Mojolicious::Types; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | # Stolen from Plack::App::Direcotry | ||||||||||||||||||||||||||||||
| 16 | 10 | my $dir_page = <<'PAGE'; | |||||||||||||||||||||||||||||
| 17 | <html><head> | ||||||||||||||||||||||||||||||
| 18 | <title>Index of <%= $cur_path %></title> | ||||||||||||||||||||||||||||||
| 19 | <meta http-equiv="content-type" content="text/html; charset=utf-8" /> | ||||||||||||||||||||||||||||||
| 20 | <style type='text/css'> | ||||||||||||||||||||||||||||||
| 21 | table { margin: 0; border-collapse: collapse } | ||||||||||||||||||||||||||||||
| 22 | .name { text-align:left; padding-left: 1em; } | ||||||||||||||||||||||||||||||
| 23 | .size, .mtime { text-align:right; padding-left: 1em; } | ||||||||||||||||||||||||||||||
| 24 | .type { text-align:left; padding-left: 1em;} | ||||||||||||||||||||||||||||||
| 25 | .mtime { text-align:left; padding-left: 1em; } | ||||||||||||||||||||||||||||||
| 26 | </style> | ||||||||||||||||||||||||||||||
| 27 | <link rel="stylesheet" href="/index.css"> | ||||||||||||||||||||||||||||||
| 28 | </head><body> | ||||||||||||||||||||||||||||||
| 29 | <h1>Index of <%= $cur_path %></h1> | ||||||||||||||||||||||||||||||
| 30 | <hr id="top-rule"/> | ||||||||||||||||||||||||||||||
| 31 | <table id="listing"> | ||||||||||||||||||||||||||||||
| 32 | <tr id="listing-header" class="<%= $cur_path %>"> | ||||||||||||||||||||||||||||||
| 33 | <th class="name">Name</th> | ||||||||||||||||||||||||||||||
| 34 | <th class="size">Size</th> | ||||||||||||||||||||||||||||||
| 35 | <th class="type">Type</th> | ||||||||||||||||||||||||||||||
| 36 | <th class="mtime">Last Modified</th> | ||||||||||||||||||||||||||||||
| 37 | </tr> | ||||||||||||||||||||||||||||||
| 38 | % for my $file (@$files) { my $css_type = $file->{type} =~ s/;.*//r; $css_type =~ s|/|-|g; | ||||||||||||||||||||||||||||||
| 39 | <tr class="listing-row <%= $css_type %>-type" id="<%= $file->{name} =~ s|/\z||r %>"> | ||||||||||||||||||||||||||||||
| 40 | <td class="name"><a href='<%= $file->{url} %>'><%== $file->{name} %></a></td> | ||||||||||||||||||||||||||||||
| 41 | <td class="size"><%= $file->{size} %></td> | ||||||||||||||||||||||||||||||
| 42 | <td class="type"><%= $file->{type} %></td> | ||||||||||||||||||||||||||||||
| 43 | <td class="mtime"><%= $file->{mtime} %></td> | ||||||||||||||||||||||||||||||
| 44 | </tr> | ||||||||||||||||||||||||||||||
| 45 | % } | ||||||||||||||||||||||||||||||
| 46 | </table> | ||||||||||||||||||||||||||||||
| 47 | <hr id="bottom-rule"/> | ||||||||||||||||||||||||||||||
| 48 | </body></html> | ||||||||||||||||||||||||||||||
| 49 | PAGE | ||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||
| 51 | 10 | my $types = Mojolicious::Types->new; | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||
| 53 | 10 | ▶ | sub register { | ||||||||||||||||||||||||||||
|
Subroutine
register: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 54 | 10 | my ( $self, $app, $args ) = @_; | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||
| 56 | 10 | ▶ | my $root = Mojo::Home->new( $args->{root} || Cwd::getcwd ); | ||||||||||||||||||||||||||||
|
Condition66%2/3
$args->{'root'} || Cwd::getcwd()
MC/DC0%0/2
$args->{'root'} || Cwd::getcwd()
$args->{'root'}
Cwd::getcwd()
Truth table66%2/3
A || B
A= $args->{'root'}B= Cwd::getcwd()
| |||||||||||||||||||||||||||||||
| 57 | 10 | my $handler = $args->{handler}; | |||||||||||||||||||||||||||||
| 58 | 10 | my $index = $args->{dir_index}; | |||||||||||||||||||||||||||||
| 59 | 10 | ▶ | my $auto_index = $args->{auto_index} // 1; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"auto_index"} // 1
MC/DC100%1/1
$args->{"auto_index"} // 1
$args->{"auto_index"}
Truth table100%2/2
A
A= $args->{"auto_index"}
| |||||||||||||||||||||||||||||||
| 60 | 10 | my $json = $args->{json}; | |||||||||||||||||||||||||||||
| 61 | 10 | ▶ | my $router = $args->{router} // $app->routes; | ||||||||||||||||||||||||||||
|
Condition66%2/3
$args->{"router"} // $app->routes
MC/DC0%0/2
$args->{"router"} // $app->routes
$args->{"router"}
$app->routes
Truth table66%2/3
A // B
A= $args->{"router"}B= $app->routes
| |||||||||||||||||||||||||||||||
| 62 | 10 | ▶ | $dir_page = $args->{dir_page} if ( $args->{dir_page} ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | $router->get( | ||||||||||||||||||||||||||||||
| 65 | 31 | ▶ | '/*filepath' => { filepath => '/' } => sub ($c) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||
| 66 | 31 | ▶ | if ( -f $root->to_string ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 67 | 4 | return render_file( $c, $root, $handler ); | |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||
| 70 | 27 | my $path = Mojo::Path | |||||||||||||||||||||||||||||
| 71 | ->new( Mojo::Util::url_unescape( $c->param('filepath') ) ) | ||||||||||||||||||||||||||||||
| 72 | ->canonicalize; | ||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | 27 | ▶ | if ( $path =~ m"^\Q..\E(/|\z)"n ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 75 | 1 | $c->reply->not_found; | |||||||||||||||||||||||||||||
| 76 | 1 | return; | |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||
| 79 | 26 | my $full_path = $root->child($path); | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 26 | ▶ | if ( -f $full_path ) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 82 | 12 | render_file( $c, $full_path, $handler ); | |||||||||||||||||||||||||||||
| 83 | 12 | return; | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||
| 85 | elsif ( -d $full_path ) { | ||||||||||||||||||||||||||||||
| 86 | 12 | ▶ | if ( $index && ( my $index_path = locate_index( $index, $full_path ) ) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$index and my $index_path = locate_index($index, $full_path)
MC/DC50%1/2
$index and my $index_path = locate_index($index, $full_path)
$index
my $index_path = locate_index($index, $full_path)
Truth table66%2/3
A and B
A= $indexB= my $index_path = locate_index($index, $full_path)
| |||||||||||||||||||||||||||||||
| 87 | 1 | return render_file( $c, $index_path, $handler ); | |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | 11 | ▶ | if ( $path ne '/' && ! $c->req->url->path->trailing_slash ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$path ne "/" and not $c->req->url->path->trailing_slash
MC/DC100%2/2
$path ne "/" and not $c->req->url->path->trailing_slash
$path ne "/"
not $c->req->url->path->trailing_slash
Truth table100%3/3
A and not(B)
A= $path ne "/"B= not $c->req->url->path->trailing_slash
| |||||||||||||||||||||||||||||||
| 91 | 1 | $c->redirect_to($c->req->url->path->trailing_slash(1)); | |||||||||||||||||||||||||||||
| 92 | 1 | return; | |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | 10 | ▶ | if( $auto_index ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 96 | 9 | render_indexes( $c, $full_path, $json ); | |||||||||||||||||||||||||||||
| 97 | 9 | return; | |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | 3 | return $c->reply->not_found; | |||||||||||||||||||||||||||||
| 102 | }, | ||||||||||||||||||||||||||||||
| 103 | 10 | ); | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||
| 105 | 10 | return $app; | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | sub locate_index { | ||||||||||||||||||||||||||||
|
Subroutine
locate_index: called CC=6 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 109 | 1 | ▶ | my $index = shift || return; | ||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | my $dir = shift || Cwd::getcwd; | ||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || Cwd::getcwd()
MC/DC0%0/2
shift() || Cwd::getcwd()
shift()
Cwd::getcwd()
Truth table33%1/3
A || B
A= shift()B= Cwd::getcwd()
| |||||||||||||||||||||||||||||||
| 111 | 1 | my $root = Mojo::Home->new($dir); | |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | $index = ( ref $index eq 'ARRAY' ) ? $index : ["$index"]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 113 | 1 | for (@$index) { | |||||||||||||||||||||||||||||
| 114 | 1 | my $path = $root->rel_file($_); | |||||||||||||||||||||||||||||
| 115 | 1 | ▶ | return $path if ( -e $path ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||
| 119 | 17 | ▶ | sub render_file ($c, $path, $handler) { | ||||||||||||||||||||||||||||
|
Subroutine
render_file: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 120 | 17 | ▶ | $handler->( $c, $path ) if ref $handler eq 'CODE'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 121 | 17 | ▶ | return if $c->tx->res->code; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 122 | 6 | $c->reply->file($path); | |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | 9 | ▶ | sub render_indexes ($c, $dir, $json) { | ||||||||||||||||||||||||||||
|
Subroutine
render_indexes: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 126 | 9 | ▶ | my @files = | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 127 | ( $c->req->url->path eq '/' ) | ||||||||||||||||||||||||||||||
| 128 | ? () | ||||||||||||||||||||||||||||||
| 129 | : ( { url => '../', name => 'Parent Directory', size => '', type => '', mtime => '' } ); | ||||||||||||||||||||||||||||||
| 130 | 9 | my $children = list_files($dir); | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||
| 132 | 9 | my $cur_path = Encode::decode_utf8( Mojo::Util::url_unescape( $c->req->url->path ) ); | |||||||||||||||||||||||||||||
| 133 | 9 | for my $basename ( sort { $a cmp $b } @$children ) { | |||||||||||||||||||||||||||||
| 134 | 105 | my $file = "$dir/$basename"; | |||||||||||||||||||||||||||||
| 135 | 105 | my $url = Mojo::Path->new($cur_path)->trailing_slash(0); | |||||||||||||||||||||||||||||
| 136 | 105 | push @{ $url->parts }, $basename; | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||
| 138 | 105 | my $is_dir = -d $file; | |||||||||||||||||||||||||||||
| 139 | 105 | my @stat = stat _; | |||||||||||||||||||||||||||||
| 140 | 105 | ▶ | if ($is_dir) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 141 | 12 | $basename .= '/'; | |||||||||||||||||||||||||||||
| 142 | 12 | $url->trailing_slash(1); | |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | 105 | ▶ | my $mime_type = | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
$types->type(get_ext($file) || "txt") or "text/plain"
get_ext($file) || 'txt'
MC/DC100%1/1
$types->type(get_ext($file) || "txt") or "text/plain"
$types->type(get_ext($file) || "txt")
MC/DC100%1/1
get_ext($file) || 'txt'
get_ext($file)
Truth table100%2/2
A
A= $types->type(get_ext($file) || "txt")
Truth table100%2/2
A
A= get_ext($file)
| |||||||||||||||||||||||||||||||
| 146 | $is_dir | ||||||||||||||||||||||||||||||
| 147 | ? 'directory' | ||||||||||||||||||||||||||||||
| 148 | : ( $types->type( get_ext($file) || 'txt' ) || 'text/plain' ); | ||||||||||||||||||||||||||||||
| 149 | 105 | my $mtime = Mojo::Date->new( $stat[9] )->to_string(); | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | 105 | ▶ | push @files, { | ||||||||||||||||||||||||||||
|
Condition100%2/2
$stat[7] || 0
MC/DC100%1/1
$stat[7] || 0
$stat[7]
Truth table100%2/2
A
A= $stat[7]
| |||||||||||||||||||||||||||||||
| 152 | url => $url, | ||||||||||||||||||||||||||||||
| 153 | name => $basename, | ||||||||||||||||||||||||||||||
| 154 | size => $stat[7] || 0, | ||||||||||||||||||||||||||||||
| 155 | type => $mime_type, | ||||||||||||||||||||||||||||||
| 156 | mtime => $mtime, | ||||||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||
| 160 | 9 | my $any = { inline => $dir_page, files => \@files, cur_path => $cur_path }; | |||||||||||||||||||||||||||||
| 161 | 9 | ▶ | if ($json) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 162 | 2 | $c->respond_to( | |||||||||||||||||||||||||||||
| 163 | json => { json => encode_json(\@files) }, | ||||||||||||||||||||||||||||||
| 164 | any => $any, | ||||||||||||||||||||||||||||||
| 165 | ); | ||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||
| 167 | else { | ||||||||||||||||||||||||||||||
| 168 | 7 | $c->render( %$any ); | |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||
| 172 | 93 | ▶ | sub get_ext { | ||||||||||||||||||||||||||||
|
Subroutine
get_ext: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 173 | 93 | ▶ | $_[0] =~ /\.([0-9a-zA-Z]+)$/ || return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 174 | 87 | return lc $1; | |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||
| 177 | 9 | ▶ | sub list_files { | ||||||||||||||||||||||||||||
|
Subroutine
list_files: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 178 | 9 | ▶ | my $dir = shift || return []; | ||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return [])
MC/DC0%0/1
shift() || (return [])
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||
| 179 | 9 | my $dh = DirHandle->new($dir); | |||||||||||||||||||||||||||||
| 180 | 9 | my @children; | |||||||||||||||||||||||||||||
| 181 | 9 | ▶ | while ( defined( my $ent = $dh->read ) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 182 | 123 | ▶ | 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 ".."
| |||||||||||||||||||||||||||||||
| 183 | 105 | push @children, Encode::decode_utf8($ent); | |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||
| 185 | 9 | return [ @children ]; | |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||
| 188 | 10 | 1; | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||||