Mojolicious/Plugin/DirectoryServer.pm

filter: Statementstmt 100% 96 / 96 Branchbran 94.7% 36 / 38 Conditioncond 76.6% 23 / 30 MC/DCmcdc 50.0% 9 / 18 Subroutinesub 100% 15 / 15 Podpod 16.6% 1 / 6 totaltotal 88.6% 180 / 203 CC 34 SCAR 18.1
CC
34
Cov
95%
__ANON__
10.0
render_indexes
9.0
locate_index
7.1
CRAP 6.1
↑ Summary iniweb.pm »
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()
l!l&&r!l&&!r
910
MC/DC0%0/2
$args->{'root'} || Cwd::getcwd()
$args->{'root'} Cwd::getcwd()
Truth table66%2/3
A || B
A= $args->{'root'}B= Cwd::getcwd()
ABresult
1X1
011
000
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
l!l
19
MC/DC100%1/1
$args->{"auto_index"} // 1
$args->{"auto_index"}
Truth table100%2/2
A
A= $args->{"auto_index"}
Aresult
11
00
60 10 my $json = $args->{json};
61 10 ▶ my $router = $args->{router} // $app->routes;
Condition66%2/3
$args->{"router"} // $app->routes
l!l&&r!l&&!r
280
MC/DC0%0/2
$args->{"router"} // $app->routes
$args->{"router"} $app->routes
Truth table66%2/3
A // B
A= $args->{"router"}B= $app->routes
ABresult
1X1
011
000
62 10 ▶ $dir_page = $args->{dir_page} if ( $args->{dir_page} );
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"dir_page"} 1 9 10
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
BranchTrueFalseTotal
if (-f $root->to_string) 4 27 31
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
BranchTrueFalseTotal
if ($path =~ m[^\.\.(/|\z)]nu) 1 26 27
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
BranchTrueFalseTotal
if (-f $full_path) { } 12 14 26
elsif (-d $full_path) { } 12 2 14
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
BranchTrueFalseTotal
if ($index and my $index_path = locate_index($index, $full_path)) 1 11 12
Condition66%2/3
$index and my $index_path = locate_index($index, $full_path)
!ll&&!rl&&r
1101
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)
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($path ne "/" and not $c->req->url->path->trailing_slash) 1 10 11
Condition100%3/3
$path ne "/" and not $c->req->url->path->trailing_slash
!ll&&!rl&&r
911
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($auto_index) 9 1 10
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)
l!l
10
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
110 1 ▶ my $dir = shift || Cwd::getcwd;
Condition33%1/3
shift() || Cwd::getcwd()
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || Cwd::getcwd()
shift() Cwd::getcwd()
Truth table33%1/3
A || B
A= shift()B= Cwd::getcwd()
ABresult
1X1
011
000
111 1 my $root = Mojo::Home->new($dir);
112 1 ▶ $index = ( ref $index eq 'ARRAY' ) ? $index : ["$index"];
Branch: 1 branch
BranchTrueFalseTotal
ref $index eq 'ARRAY' ? : 1 0 1
113 1 for (@$index) {
114 1 my $path = $root->rel_file($_);
115 1 ▶ return $path if ( -e $path );
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 1 0 1
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
BranchTrueFalseTotal
if ref $handler eq "CODE" 11 6 17
121 17 ▶ return if $c->tx->res->code;
Branch: 1 branch
BranchTrueFalseTotal
if $c->tx->res->code 11 6 17
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
BranchTrueFalseTotal
$c->req->url->path eq '/' ? : 7 2 9
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
BranchTrueFalseTotal
if ($is_dir) 12 93 105
141 12 $basename .= '/';
142 12 $url->trailing_slash(1);
143 }
144  
145 105 ▶ my $mime_type =
Branch: 1 branch
BranchTrueFalseTotal
$is_dir ? : 12 93 105
Condition100%4/4
$types->type(get_ext($file) || "txt") or "text/plain"
l!l
1677
get_ext($file) || 'txt'
l!l
876
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")
Aresult
11
00
Truth table100%2/2
A
A= get_ext($file)
Aresult
11
00
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
l!l
1041
MC/DC100%1/1
$stat[7] || 0
$stat[7]
Truth table100%2/2
A
A= $stat[7]
Aresult
11
00
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
BranchTrueFalseTotal
if ($json) { } 2 7 9
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
BranchTrueFalseTotal
$_[0] =~ /\.([0-9a-zA-Z]+)$/u or return 87 6 93
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 [])
l!l
90
MC/DC0%0/1
shift() || (return [])
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
179 9 my $dh = DirHandle->new($dir);
180 9 my @children;
181 9 ▶ while ( defined( my $ent = $dh->read ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ent = $dh->read)) 123 9 132
182 123 ▶ next if $ent eq '.' or $ent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $ent eq "." or $ent eq ".." 18 105 123
Condition100%3/3
$ent eq "." or $ent eq ".."
l!l&&r!l&&!r
99105
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 ".."
ABresult
1X1
011
000
183 105 push @children, Encode::decode_utf8($ent);
184 }
185 9 return [ @children ];
186 }
187  
188 10 1;
189  
190 __END__
↑ Summary iniweb.pm »