| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojolicious::Command::static; |
|
2
|
1
|
|
|
1
|
|
615
|
use Mojo::Base 'Mojolicious::Command'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# This command is a copy of Mojolicious::Command::daemon |
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
6632
|
use Mojo::File 'path'; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
60
|
|
|
7
|
1
|
|
|
1
|
|
8
|
use Mojo::Server::Daemon; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
13
|
|
|
8
|
1
|
|
|
1
|
|
46
|
use Mojo::Util qw(decamelize getopt); |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
61
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
10
|
use File::Basename; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
80
|
|
|
11
|
1
|
|
|
1
|
|
625
|
use List::MoreUtils 'uniq'; |
|
|
1
|
|
|
|
|
12485
|
|
|
|
1
|
|
|
|
|
8
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Since this is generally a temporary execution to easily setup a file |
|
14
|
|
|
|
|
|
|
# transfer, allow extremely large files; |
|
15
|
1
|
|
50
|
1
|
|
1000
|
use constant MAX_SIZE => $ENV{STATIC_MAXSIZE} || 10_000_000_000; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
843
|
|
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has description => 'Quickly serve static files'; |
|
20
|
|
|
|
|
|
|
has usage => sub { shift->extract_usage }; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub run { |
|
23
|
0
|
|
|
0
|
1
|
|
my ($self, @args) = @_; |
|
24
|
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
my $daemon = Mojo::Server::Daemon->new(app => Mojolicious->new); |
|
26
|
|
|
|
|
|
|
getopt \@args, |
|
27
|
0
|
|
|
0
|
|
|
'b|backlog=i' => sub { $daemon->backlog($_[1]) }, |
|
28
|
0
|
|
|
0
|
|
|
'c|clients=i' => sub { $daemon->max_clients($_[1]) }, |
|
29
|
0
|
|
|
0
|
|
|
'i|inactivity-timeout=i' => sub { $daemon->inactivity_timeout($_[1]) }, |
|
30
|
|
|
|
|
|
|
'l|listen=s' => \my @listen, |
|
31
|
0
|
|
|
0
|
|
|
'p|proxy' => sub { $daemon->reverse_proxy(1) }, |
|
32
|
0
|
|
|
0
|
|
|
'r|requests=i' => sub { $daemon->max_requests($_[1]) }, |
|
33
|
0
|
|
|
|
|
|
'd|default=s' => \my $default; |
|
34
|
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
push @{$daemon->app->renderer->classes}, __PACKAGE__; |
|
|
0
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
my $app = $daemon->app; |
|
38
|
0
|
|
|
|
|
|
my $config = $app->plugin(Config => {default => {}}); |
|
39
|
|
|
|
|
|
|
$app->log->debug("Adding plugin $_") and |
|
40
|
|
|
|
|
|
|
$app->plugin($_ => $config->{decamelize($_)} || ()) |
|
41
|
0
|
|
0
|
|
|
|
for split /,/, $ENV{MOJOLICIOUS_PLUGINS} // ''; |
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Add all the paths and paths of filenames specified on the command line |
|
44
|
0
|
|
|
|
|
|
$app->static->paths(_static_paths(@args)); |
|
45
|
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
$app->max_request_size(MAX_SIZE); |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Build an index of the available specified files |
|
49
|
0
|
|
|
|
|
|
my @files = _get_files($default, @args); |
|
50
|
0
|
|
|
|
|
|
$app->log->info(sprintf '%d files', $#files+1); |
|
51
|
0
|
0
|
|
|
|
|
if ( $default ) { |
|
52
|
0
|
|
|
|
|
|
$app->log->info("index $default"); |
|
53
|
|
|
|
|
|
|
$app->routes->get('/') |
|
54
|
0
|
|
|
0
|
|
|
->to(cb => sub { shift->reply->static($default) }) |
|
55
|
0
|
|
|
|
|
|
->name('index'); |
|
56
|
|
|
|
|
|
|
} else { |
|
57
|
0
|
|
|
|
|
|
$app->log->info('index directory listing'); |
|
58
|
0
|
|
|
|
|
|
$app->routes->get('/') |
|
59
|
|
|
|
|
|
|
->to(files => \@files) |
|
60
|
|
|
|
|
|
|
->name('index'); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Log requests for static files |
|
64
|
|
|
|
|
|
|
$app->hook(after_static => sub { |
|
65
|
0
|
|
|
0
|
|
|
my $c = shift; |
|
66
|
0
|
|
|
|
|
|
$c->log->info(sprintf 'GET %s', $c->req->url->path); |
|
67
|
0
|
|
|
|
|
|
}); |
|
68
|
|
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
|
$daemon->listen(\@listen) if @listen; |
|
70
|
0
|
|
|
|
|
|
$daemon->run; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _get_files { |
|
74
|
0
|
|
|
0
|
|
|
my @files; |
|
75
|
0
|
0
|
|
|
|
|
foreach my $path ( map { path($_) } grep { $_ && -e $_ } @_ ) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
76
|
0
|
0
|
|
|
|
|
if ( -d $path ) { |
|
77
|
|
|
|
|
|
|
$path->list_tree->each(sub{ |
|
78
|
0
|
|
|
0
|
|
|
push @files, $_->to_rel($path); |
|
79
|
0
|
|
|
|
|
|
}); |
|
80
|
|
|
|
|
|
|
} else { |
|
81
|
0
|
|
|
|
|
|
push @files, $path; |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
} |
|
84
|
0
|
|
|
|
|
|
return @files; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _static_paths { |
|
88
|
0
|
0
|
|
0
|
|
|
[uniq grep { -d $_ } map { -f $_ ? dirname $_ : $_ } @_] |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=encoding utf8 |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 NAME |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Mojolicious::Command::static - Quickly serve static files |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Usage: APPLICATION static [OPTIONS] dir1 dir2 ... file1 file2 ... |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
./myapp.pl static . |
|
104
|
|
|
|
|
|
|
./myapp.pl static -d file2 -l http://*:8080 . |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Options: |
|
107
|
|
|
|
|
|
|
-b, --backlog Listen backlog size, defaults to |
|
108
|
|
|
|
|
|
|
SOMAXCONN |
|
109
|
|
|
|
|
|
|
-c, --clients Maximum number of concurrent |
|
110
|
|
|
|
|
|
|
connections, defaults to 1000 |
|
111
|
|
|
|
|
|
|
-d, --default Default file to respond with (like |
|
112
|
|
|
|
|
|
|
index.html). Defaults to directory |
|
113
|
|
|
|
|
|
|
index listing. |
|
114
|
|
|
|
|
|
|
-h, --help Show this summary of available options |
|
115
|
|
|
|
|
|
|
--home Path to home directory of your |
|
116
|
|
|
|
|
|
|
application, defaults to the value of |
|
117
|
|
|
|
|
|
|
MOJO_HOME or auto-detection |
|
118
|
|
|
|
|
|
|
-i, --inactivity-timeout Inactivity timeout, defaults to the |
|
119
|
|
|
|
|
|
|
value of MOJO_INACTIVITY_TIMEOUT or 15 |
|
120
|
|
|
|
|
|
|
-l, --listen One or more locations you want to |
|
121
|
|
|
|
|
|
|
listen on, defaults to the value of |
|
122
|
|
|
|
|
|
|
MOJO_LISTEN or "http://*:3000" |
|
123
|
|
|
|
|
|
|
-m, --mode Operating mode for your application, |
|
124
|
|
|
|
|
|
|
defaults to the value of |
|
125
|
|
|
|
|
|
|
MOJO_MODE/PLACK_ENV or "development" |
|
126
|
|
|
|
|
|
|
-p, --proxy Activate reverse proxy support, |
|
127
|
|
|
|
|
|
|
defaults to the value of |
|
128
|
|
|
|
|
|
|
MOJO_REVERSE_PROXY |
|
129
|
|
|
|
|
|
|
-r, --requests Maximum number of requests per |
|
130
|
|
|
|
|
|
|
keep-alive connection, defaults to 100 |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
L quickly serves static files |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Serves files from the current directory as well as those specified on the |
|
137
|
|
|
|
|
|
|
command line. If no default file is specified, a directory index will be built. |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
The maximum file size can be specified by the STATIC_MAXSIZE environment |
|
140
|
|
|
|
|
|
|
variable, or 10G by default. |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
L inherits all attributes from |
|
145
|
|
|
|
|
|
|
L and implements the following new ones. |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 description |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
my $description = $static->description; |
|
150
|
|
|
|
|
|
|
$static = $static->description('Foo'); |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Short description of this command, used for the command list. |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 usage |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
my $usage = $static->usage; |
|
157
|
|
|
|
|
|
|
$routes = $static->usage('Foo'); |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Usage information for this command, used for the help screen. |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 METHODS |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
L inherits all methods from |
|
164
|
|
|
|
|
|
|
L and implements the following new ones. |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 run |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
$static->run(@ARGV); |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Run this command. |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
L, L, L. |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
__DATA__ |