blib/lib/Plack/Runner.pm

filter: Statementstmt 33.7% 57 / 169 Branchbran 29.4% 20 / 68 Conditioncond 12.9% 4 / 31 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 30.3% 10 / 33 Podpod 0.0% 0 / 14 totaltotal 27.6% 92 / 333 CC 49 SCAR 32.8
CC
49
Cov
30%
run
110.0
setup
56.0
locate_app
37.9
CRAP 26.6
« blib/lib/Plack/Loader.pm ↑ Summary blib/lib/Plack/Middleware/Lint.pm »
1 package Plack::Runner;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 10 my $class = shift;
10 bless {
11 env => $ENV{PLACK_ENV},
12 10 loader => 'Plack::Loader',
13 includes => [],
14 modules => [],
15 default_middleware => 1,
16 @_,
17 }, $class;
18 }
19  
20 # delay the build process for reloader
21 0 ▶sub build(&;$) {
Subroutine build: not called CC=2 SCAR=17.9
Pod undocumented
22 0 my $block = shift;
23 0 ▶ my $app = shift || sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
shift() || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || sub { }
shift() sub { }
Truth table0%0/3
A || B
A= shift()B= sub { }
ABresult
1X1
011
000
24 0 ▶ return sub { $block->($app->()) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 }
26  
27 10 ▶sub parse_options {
Subroutine parse_options: called CC=7 SCAR=19.8
Pod undocumented
28 10 my $self = shift;
29  
30 10 local @ARGV = @_;
31  
32 # From 'prove': Allow cuddling the paths with -I, -M and -e
33 10 ▶ @ARGV = map { /^(-[IMe])(.+)/ ? ($1,$2) : $_ } @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
/^(-[IMe])(.+)/ ? : 0 32 32
34  
35 10 my($host, $port, $socket, @listen);
36  
37 10 require Getopt::Long;
38 10 my $parser = Getopt::Long::Parser->new(
39 config => [ "no_auto_abbrev", "no_ignore_case", "pass_through" ],
40 );
41  
42 $parser->getoptions(
43 "a|app=s" => \$self->{app},
44 "o|host=s" => \$host,
45 "p|port=i" => \$port,
46 "s|server=s" => \$self->{server},
47 "S|socket=s" => \$socket,
48 'l|listen=s@' => \@listen,
49 'D|daemonize' => \$self->{daemonize},
50 "E|env=s" => \$self->{env},
51 "e=s" => \$self->{eval},
52 'I=s@' => $self->{includes},
53 'M=s@' => $self->{modules},
54 0 ▶ 'r|reload' => sub { $self->{loader} = "Restarter" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 ▶ 'R|Reload=s' => sub { $self->{loader} = "Restarter"; $self->loader->watch(split ",", $_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 'L|loader=s' => \$self->{loader},
57 "access-log=s" => \$self->{access_log},
58 "path=s" => \$self->{path},
59 "h|help" => \$self->{help},
60 "v|version" => \$self->{version},
61 "default-middleware!" => \$self->{default_middleware},
62 10 );
63  
64 10 my(@options, @argv);
65 10 ▶ while (defined(my $arg = shift @ARGV)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $arg = shift @ARGV)) 5 10 15
66 5 ▶ if ($arg =~ s/^--?//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ s/^--?//) { } 4 1 5
67 4 my @v = split '=', $arg, 2;
68 4 $v[0] =~ tr/-/_/;
69 4 ▶ if (@v == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@v == 2) { } 2 2 4
elsif ($v[0] =~ s/^(disable|enable)_//) { } 2 0 2
70 2 push @options, @v;
71 } elsif ($v[0] =~ s/^(disable|enable)_//) {
72 2 push @options, $v[0], $1 eq 'enable';
73 } else {
74 0 push @options, $v[0], shift @ARGV;
75 }
76 } else {
77 1 push @argv, $arg;
78 }
79 }
80  
81 10 push @options, $self->mangle_host_port_socket($host, $port, $socket, @listen);
82 10 ▶ push @options, daemonize => 1 if $self->{daemonize};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"daemonize"} 1 9 10
83  
84 10 $self->{options} = \@options;
85 10 $self->{argv} = \@argv;
86 }
87  
88 0 ▶sub set_options {
Subroutine set_options: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my $self = shift;
90 0 push @{$self->{options}}, @_;
91 }
92  
93 10 ▶sub mangle_host_port_socket {
Subroutine mangle_host_port_socket: called CC=9 SCAR=22.0
Pod undocumented
94 10 my($self, $host, $port, $socket, @listen) = @_;
95  
96 10 for my $listen (reverse @listen) {
97 9 ▶ if ($listen =~ /:\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($listen =~ /:\d+$/) { } 5 4 9
98 5 ($host, $port) = split /:/, $listen, 2;
99 5 ▶ $host = undef if $host eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $host eq "" 2 3 5
100 } else {
101 4 ▶ $socket ||= $listen;
Condition66%2/3
$socket ||= $listen
l!l&&r!l&&!r
130
MC/DC0%0/2
$socket ||= $listen
$socket $listen
Truth table66%2/3
A ||= B
A= $socketB= $listen
ABresult
1X1
011
000
102 }
103 }
104  
105 10 ▶ unless (@listen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@listen) 4 6 10
106 4 ▶ if ($socket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket) { } 1 3 4
107 1 @listen = ($socket);
108 } else {
109 3 ▶ $port ||= 5000;
Condition100%2/2
$port ||= 5000
l!l
12
MC/DC100%1/1
$port ||= 5000
$port
Truth table100%2/2
A
A= $port
Aresult
11
00
110 3 ▶ @listen = ($host ? "$host:$port" : ":$port");
Branch: 1 branch
BranchTrueFalseTotal
$host ? : 1 2 3
111 }
112 }
113  
114 10 return host => $host, port => $port, listen => \@listen, socket => $socket;
115 }
116  
117 0 ▶sub version_cb {
Subroutine version_cb: not called CC=2 SCAR=17.9
Pod undocumented
118 0 my $self = shift;
119 0 ▶ $self->{version_cb} || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 require Plack;
121 0 print "Plack $Plack::VERSION\n";
122 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"version_cb"} or sub { require Plack; print "Plack $Plack::VERSION\n"; } 0 0 0
123 }
124  
125 0 ▶sub setup {
Subroutine setup: not called CC=7 SCAR=40.3
Pod undocumented
126 0 my $self = shift;
127  
128 0 ▶ if ($self->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"help"}) 0 0 0
129 0 require Pod::Usage;
130 0 Pod::Usage::pod2usage(0);
131 }
132  
133 0 ▶ if ($self->{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"version"}) 0 0 0
134 0 $self->version_cb->();
135 0 exit;
136 }
137  
138 0 ▶ if (@{$self->{includes}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"includes"};}) 0 0 0
139 0 require lib;
140 0 lib->import(@{$self->{includes}});
141 }
142  
143 0 ▶ if ($self->{eval}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"eval"}) 0 0 0
144 0 push @{$self->{modules}}, 'Plack::Builder';
145 }
146  
147 0 for (@{$self->{modules}}) {
148 0 my($module, @import) = split /[=,]/;
149 0 ▶ eval "require $module" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $module" or die $@ 0 0 0
150 0 $module->import(@import);
151 }
152 }
153  
154 0 ▶sub locate_app {
Subroutine locate_app: not called CC=7 SCAR=36.3
Pod undocumented
155 0 my($self, @args) = @_;
156  
157 0 ▶ my $psgi = $self->{app} || $args[0];
Condition0%0/3
$self->{'app'} || $args[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'app'} || $args[0]
$self->{'app'} $args[0]
Truth table0%0/3
A || B
A= $self->{'app'}B= $args[0]
ABresult
1X1
011
000
158  
159 0 ▶ if (ref $psgi eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $psgi eq "CODE") 0 0 0
160 0 ▶ return sub { $psgi };
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 }
162  
163 0 ▶ if ($self->{eval}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"eval"}) 0 0 0
164 0 ▶ $self->loader->watch("lib") if -e "lib";
Branch: 1 branch
BranchTrueFalseTotal
if -e "lib" 0 0 0
165 0 ▶ return build {
Subroutine __ANON__: not called CC=3 SCAR=15.4
166 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 0 my $eval = "builder { $self->{eval};";
169 0 ▶ $eval .= "Plack::Util::load_psgi(\$psgi);" if $psgi;
Branch: 1 branch
BranchTrueFalseTotal
if $psgi 0 0 0
170 0 $eval .= "}";
171 0 ▶ eval $eval or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval $eval or die $@ 0 0 0
172 0 };
173 }
174  
175 0 ▶ $psgi ||= "app.psgi";
Condition0%0/2
$psgi ||= "app.psgi"
l!l
00
MC/DC0%0/1
$psgi ||= "app.psgi"
$psgi
Truth table0%0/2
A
A= $psgi
Aresult
11
00
176  
177 0 require File::Basename;
178 0 my $lib = File::Basename::dirname($psgi) . "/lib";
179 0 ▶ $self->loader->watch($lib) if -e $lib;
Branch: 1 branch
BranchTrueFalseTotal
if -e $lib 0 0 0
180 0 $self->loader->watch($psgi);
181 0 ▶ build { Plack::Util::load_psgi $psgi };
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 }
183  
184 0 ▶sub watch {
Subroutine watch: not called CC=2 SCAR=17.9
Pod undocumented
185 0 my($self, @dir) = @_;
186  
187 0 push @{$self->{watch}}, @dir
188 0 ▶ if $self->{loader} eq 'Restarter';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"loader"} eq "Restarter" 0 0 0
189 }
190  
191 0 ▶sub apply_middleware {
Subroutine apply_middleware: not called CC=1 SCAR=6.9
Pod undocumented
192 0 my($self, $app, $class, @args) = @_;
193  
194 0 my $mw_class = Plack::Util::load_class($class, 'Plack::Middleware');
195 0 ▶ build { $mw_class->wrap($_[0], @args) } $app;
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 }
197  
198 0 ▶sub prepare_devel {
Subroutine prepare_devel: not called CC=4 SCAR=30.0
Pod undocumented
199 0 my($self, $app) = @_;
200  
201 0 ▶ if ($self->{default_middleware}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"default_middleware"}) 0 0 0
202 0 $app = $self->apply_middleware($app, 'Lint');
203 0 $app = $self->apply_middleware($app, 'StackTrace');
204 0 ▶ if (!$ENV{GATEWAY_INTERFACE} and !$self->{access_log}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"GATEWAY_INTERFACE"} or $self->{"access_log"}) 0 0 0
Condition0%0/3
$ENV{"GATEWAY_INTERFACE"} or $self->{"access_log"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"GATEWAY_INTERFACE"} or $self->{"access_log"}
$ENV{"GATEWAY_INTERFACE"} $self->{"access_log"}
Truth table0%0/3
A or B
A= $ENV{"GATEWAY_INTERFACE"}B= $self->{"access_log"}
ABresult
1X1
011
000
205 0 $app = $self->apply_middleware($app, 'AccessLog');
206 }
207 }
208  
209 0 ▶ push @{$self->{options}}, server_ready => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
210 0 my($args) = @_;
211 0 ▶ my $name = $args->{server_software} || ref($args); # $args is $server
Condition0%0/3
$args->{'server_software'} || ref $args
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'server_software'} || ref $args
$args->{'server_software'} ref $args
Truth table0%0/3
A || B
A= $args->{'server_software'}B= ref $args
ABresult
1X1
011
000
212 0 ▶ my $host = $args->{host} || 0;
Condition0%0/2
$args->{'host'} || 0
l!l
00
MC/DC0%0/1
$args->{'host'} || 0
$args->{'host'}
Truth table0%0/2
A
A= $args->{'host'}
Aresult
11
00
213 0 ▶ my $proto = $args->{proto} || 'http';
Condition0%0/2
$args->{'proto'} || 'http'
l!l
00
MC/DC0%0/1
$args->{'proto'} || 'http'
$args->{'proto'}
Truth table0%0/2
A
A= $args->{'proto'}
Aresult
11
00
214 0 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
215 0 };
216  
217 0 $app;
218 }
219  
220 0 ▶sub loader {
Subroutine loader: not called CC=2 SCAR=17.9
Pod undocumented
221 0 my $self = shift;
222 0 ▶ $self->{_loader} ||= Plack::Util::load_class($self->{loader}, 'Plack::Loader')->new;
Condition0%0/3
$self->{"_loader"} ||= Plack::Util::load_class($self->{"loader"}, "Plack::Loader")->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_loader"} ||= Plack::Util::load_class($self->{"loader"}, "Plack::Loader")->new
$self->{"_loader"} Plack::Util::load_class($self->{"loader"}, "Plack::Loader")->new
Truth table0%0/3
A ||= B
A= $self->{"_loader"}B= Plack::Util::load_class($self->{"loader"}, "Plack::Loader")->new
ABresult
1X1
011
000
223 }
224  
225 0 ▶sub load_server {
Subroutine load_server: not called CC=2 SCAR=17.9
Pod undocumented
226 0 my($self, $loader) = @_;
227  
228 0 ▶ if ($self->{server}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'server'}) { } 0 0 0
229 0 return $loader->load($self->{server}, @{$self->{options}});
230 } else {
231 0 return $loader->auto(@{$self->{options}});
232 }
233 }
234  
235 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod undocumented
236 0 my $self = shift;
237  
238 0 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
239 0 $self = $self->new;
240 0 $self->parse_options(@_);
241 0 return $self->run;
242 }
243  
244 0 ▶ unless ($self->{options}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"options"}) 0 0 0
245 0 $self->parse_options();
246 }
247  
248 0 ▶ my @args = @_ ? @_ : @{$self->{argv}};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
249  
250 0 $self->setup;
251  
252 0 my $app = $self->locate_app(@args);
253  
254 0 ▶ if ($self->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"path"}) 0 0 0
255 0 require Plack::App::URLMap;
256 0 ▶ $app = build {
Subroutine __ANON__: not called CC=1 SCAR=6.9
257 0 my $urlmap = Plack::App::URLMap->new;
258 0 $urlmap->mount($self->{path} => $_[0]);
259 0 $urlmap->to_app;
260 0 } $app;
261 }
262  
263 0 ▶ $ENV{PLACK_ENV} ||= $self->{env} || 'development';
Condition0%0/5
$ENV{"PLACK_ENV"} ||= $self->{"env"} || "development"
l!l&&r!l&&!r
000
$self->{'env'} || 'development'
l!l
00
MC/DC0%0/2
$ENV{"PLACK_ENV"} ||= $self->{"env"} || "development"
$ENV{"PLACK_ENV"} $self->{'env'}
Truth table0%0/3
A ||= B
A= $ENV{"PLACK_ENV"}B= $self->{'env'}
ABresult
1X1
011
000
264 0 ▶ if ($ENV{PLACK_ENV} eq 'development') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PLACK_ENV"} eq "development") 0 0 0
265 0 $app = $self->prepare_devel($app);
266 }
267  
268 0 ▶ if ($self->{access_log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"access_log"}) 0 0 0
269 open my $logfh, ">>", $self->{access_log}
270 0 ▶ or die "open($self->{access_log}): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $logfh, ">>", $self->{"access_log"} or die qq[open($self->{"access_log"}): $!] 0 0 0
271 0 $logfh->autoflush(1);
272 0 ▶ $app = $self->apply_middleware($app, 'AccessLog', logger => sub { $logfh->print( @_ ) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
273 }
274  
275 0 my $loader = $self->loader;
276 0 $loader->preload_app($app);
277  
278 0 my $server = $self->load_server($loader);
279 0 $loader->run($server);
280 }
281  
282 1 1;
283  
284 __END__
« blib/lib/Plack/Loader.pm ↑ Summary blib/lib/Plack/Middleware/Lint.pm »