blib/lib/Mojolicious/Plugin/CGI.pm

filter: Statementstmt 94.5% 174 / 184 Branchbran 76.5% 75 / 98 Conditioncond 64.2% 45 / 70 MC/DCmcdc 28.2% 11 / 39 Subroutinesub 100% 30 / 30 Podpod 100% 1 / 1 totaltotal 79.6% 336 / 422 CC 80 SCAR 26.8
CC
80
Cov
84%
_emulate_environment
35.6
register
16.1
_run
15.9
CRAP 14.6
↑ Summary
1 package Mojolicious::Plugin::CGI;
2 34 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 34 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 34 ▶use IO::Pipely 'pipely';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use Mojo::Util qw(b64_decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use POSIX 'WNOHANG';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Perl::OSType 'is_os_type';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Socket qw(AF_INET inet_aton);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 34 ▶use constant CHECK_CHILD_INTERVAL => $ENV{CHECK_CHILD_INTERVAL} || 0.01;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'CHECK_CHILD_INTERVAL'} || 0.01
l!l
034
MC/DC0%0/1
$ENV{'CHECK_CHILD_INTERVAL'} || 0.01
$ENV{'CHECK_CHILD_INTERVAL'}
Truth table50%1/2
A
A= $ENV{'CHECK_CHILD_INTERVAL'}
Aresult
11
00
14 34 ▶use constant DEBUG => $ENV{MOJO_PLUGIN_CGI_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
15 34 ▶use constant IS_WINDOWS => is_os_type('Windows');
Subroutine BEGIN: called CC=1 SCAR=0.0
16 34 ▶use constant READ => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 34 ▶use constant WRITE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 34 our $VERSION = '0.40';
20 34 our %ORIGINAL_ENV = %ENV;
21  
22 37 ▶has env => sub { +{%ORIGINAL_ENV} };
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 43 ▶sub register {
Subroutine register: called CC=15 SCAR=27.8
Pod documented
25 43 my ($self, $app, $args) = @_;
26 43 ▶ my $pids = $app->{'mojolicious_plugin_cgi.pids'} ||= {};
Condition100%2/2
$app->{"mojolicious_plugin_cgi.pids"} ||= {}
l!l
637
MC/DC100%1/1
$app->{"mojolicious_plugin_cgi.pids"} ||= {}
$app->{"mojolicious_plugin_cgi.pids"}
Truth table100%2/2
A
A= $app->{"mojolicious_plugin_cgi.pids"}
Aresult
11
00
27  
28 43 ▶ $args = {route => shift @$args, script => shift @$args} if ref $args eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "ARRAY" 16 27 43
29 43 ▶ $args->{env} ||= $self->env;
Condition66%2/3
$args->{"env"} ||= $self->env
l!l&&r!l&&!r
6370
MC/DC0%0/2
$args->{"env"} ||= $self->env
$args->{"env"} $self->env
Truth table66%2/3
A ||= B
A= $args->{"env"}B= $self->env
ABresult
1X1
011
000
30 43 ▶ $args->{run} = delete $args->{script} if ref $args->{script} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args->{"script"} eq "CODE" 0 43 43
31 43 $args->{pids} = $pids;
32  
33 43 ▶ $app->helper('cgi.run' => sub { _run($args, @_) }) unless $app->renderer->helpers->{'cgi.run'};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $app->renderer->helpers->{"cgi.run"} 37 6 43
34 $app->{'mojolicious_plugin_cgi.tid'}
35 43 ▶ ||= Mojo::IOLoop->recurring(CHECK_CHILD_INTERVAL, sub { local ($?, $!); _waitpids($pids); });
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition66%2/3
$app->{"mojolicious_plugin_cgi.tid"} ||= "Mojo::IOLoop"->recurring("0.01", sub { local($?, $!); _waitpids($pids); } )
l!l&&r!l&&!r
6370
MC/DC0%0/2
$app->{"mojolicious_plugin_cgi.tid"} ||= "Mojo::IOLoop"->recurring("0.01", sub { local($?, $!); _waitpids($pids); } )
$app->{"mojolicious_plugin_cgi.tid"} "Mojo::IOLoop"->recurring("0.01", sub { local($?, $!); _waitpids($pids); } )
Truth table66%2/3
A ||= B
A= $app->{"mojolicious_plugin_cgi.tid"}B= "Mojo::IOLoop"->recurring("0.01", sub { local($?, $!); _waitpids($pids); } )
ABresult
1X1
011
000
36  
37 43 ▶ if ($args->{support_semicolon_in_query_string}
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"support_semicolon_in_query_string"} and not $app->{"mojolicious_plugin_cgi.before_dispatch"}++) 2 41 43
Condition66%2/3
$args->{"support_semicolon_in_query_string"} and not $app->{"mojolicious_plugin_cgi.before_dispatch"}++
!ll&&!rl&&r
4102
MC/DC50%1/2
$args->{"support_semicolon_in_query_string"} and not $app->{"mojolicious_plugin_cgi.before_dispatch"}++
$args->{"support_semicolon_in_query_string"} not $app->{"mojolicious_plugin_cgi.before_dispatch"}++
Truth table66%2/3
A and not(B)
A= $args->{"support_semicolon_in_query_string"}B= not $app->{"mojolicious_plugin_cgi.before_dispatch"}++
ABresult
0X0
100
111
38 and !$app->{'mojolicious_plugin_cgi.before_dispatch'}++)
39 {
40 $app->hook(
41 2 ▶ before_dispatch => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 2 $_[0]->stash('cgi.query_string' => $_[0]->req->url->query->to_string);
43 }
44 2 );
45 }
46  
47 43 ▶ return unless $args->{route}; # just register the helper
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"route"} 6 37 43
48 37 ▶ die "Neither 'run', nor 'script' is specified." unless $args->{run} or $args->{script};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"run"} or $args->{"script"} 0 37 37
Condition66%2/3
$args->{"run"} or $args->{"script"}
l!l&&r!l&&!r
2350
MC/DC0%0/2
$args->{"run"} or $args->{"script"}
$args->{"run"} $args->{"script"}
Truth table66%2/3
A or B
A= $args->{"run"}B= $args->{"script"}
ABresult
1X1
011
000
49 $args->{route} = $app->routes->any("$args->{route}/*path_info", {path_info => ''})
50 37 ▶ unless ref $args->{route};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{"route"} 37 0 37
51 37 ▶ $args->{script} = File::Spec->rel2abs($args->{script}) || $args->{script} if $args->{script};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"script"} 35 2 37
Condition33%1/3
'File::Spec'->rel2abs($args->{'script'}) || $args->{'script'}
l!l&&r!l&&!r
3500
MC/DC0%0/2
'File::Spec'->rel2abs($args->{'script'}) || $args->{'script'}
'File::Spec'->rel2abs($args->{'script'}) $args->{'script'}
Truth table33%1/3
A || B
A= 'File::Spec'->rel2abs($args->{'script'})B= $args->{'script'}
ABresult
1X1
011
000
52 37 ▶ $args->{route}->to(cb => sub { _run($args, @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
53 }
54  
55 20 ▶sub _child {
Subroutine _child: called CC=8 SCAR=23.4
56 20 my ($c, $args, $stdin, $stdout, $stderr) = @_;
57 20 ▶ my @STDERR = @$stderr ? ('>&', fileno $stderr->[WRITE]) : ('>>', $args->{errlog});
Branch: 1 branch
BranchTrueFalseTotal
@$stderr ? : 18 2 20
58  
59 20 Mojo::IOLoop->reset;
60 20 warn "[CGI:$args->{name}:$$] <<< (@{[$stdin->slurp]})\n" if DEBUG;
61 0 ▶ open STDIN, '<', $stdin->path or die "STDIN @{[$stdin->path]}: $!" if -s $stdin->path;
Branch: 1 branch
BranchTrueFalseTotal
if -s $stdin->path 1 19 20
Condition60%3/5
-s $stdin->path && (open(STDIN, '<', $stdin->path) || die("STDIN @{[$stdin->path];}: $!"))
!ll
1910
open STDIN, "<", $stdin->path or die "STDIN @{[$stdin->path];}: $!"
l!l
10
MC/DC0%0/2
-s $stdin->path && (open(STDIN, '<', $stdin->path) || die("STDIN @{[$stdin->path];}: $!"))
-s $stdin->path open STDIN, "<", $stdin->path
Truth table33%1/3
A && B
A= -s $stdin->pathB= open STDIN, "<", $stdin->path
ABresult
0X0
100
111
62 20 ▶ open STDERR, $STDERR[0], $STDERR[1] or die "STDERR: @$stderr: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, $STDERR[0], $STDERR[1] or die "STDERR: @${stderr}: $!" 20 0 20
63 20 ▶ open STDOUT, '>&', fileno $stdout->[WRITE] or die "STDOUT: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", fileno $stdout->[1] or die "STDOUT: $!" 20 0 20
64 20 select STDERR;
65 20 $| = 1;
66 20 select STDOUT;
67 20 $| = 1;
68  
69 20 %ENV = _emulate_environment($c, $args);
70 $args->{run} ? $args->{run}->($c) : exec $args->{script}
71 20 ▶ || die "Could not execute $args->{script}: $!";
Branch: 1 branch
BranchTrueFalseTotal
$args->{'run'} ? : 0 20 20
Condition50%1/2
$args->{'script'} || die("Could not execute $args->{'script'}: $!")
l!l
200
MC/DC0%0/1
$args->{'script'} || die("Could not execute $args->{'script'}: $!")
$args->{'script'}
Truth table50%1/2
A
A= $args->{'script'}
Aresult
11
00
72  
73 0 eval { POSIX::_exit($!) } unless IS_WINDOWS;
74 0 eval { CORE::kill KILL => $$ };
75 0 exit $!;
76 }
77  
78 20 ▶sub _emulate_environment {
Subroutine _emulate_environment: called CC=23 SCAR=35.7
79 20 my ($c, $args) = @_;
80 20 my $tx = $c->tx;
81 20 my $req = $tx->req;
82 20 my $headers = $req->headers;
83 20 ▶ my $content_length = $req->content->is_multipart ? $req->body_size : $headers->content_length;
Branch: 1 branch
BranchTrueFalseTotal
$req->content->is_multipart ? : 1 19 20
84 20 my %env_headers = (HTTP_COOKIE => '', HTTP_REFERER => '');
85 20 my ($remote_user, $script_name);
86  
87 20 for my $name (@{$headers->names}) {
88 67 my $key = uc "http_$name";
89 67 $key =~ s!\W!_!g;
90 67 $env_headers{$key} = $headers->header($name);
91 }
92  
93 20 ▶ if (my $userinfo = $c->req->url->to_abs->userinfo) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $userinfo = $c->req->url->to_abs->userinfo) { } 2 18 20
elsif (my $authenticate = $headers->authorization) { } 0 18 18
94 2 ▶ $remote_user = $userinfo =~ /([^:]+)/ ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$userinfo =~ /([^:]+)/u ? : 2 0 2
95 }
96 elsif (my $authenticate = $headers->authorization) {
97 0 ▶ $remote_user = $authenticate =~ /Basic\s+(.*)/ ? b64_decode $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$authenticate =~ /Basic\s+(.*)/u ? : 0 0 0
98 0 ▶ $remote_user = $remote_user =~ /([^:]+)/ ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$remote_user =~ /([^:]+)/u ? : 0 0 0
99 }
100  
101 20 ▶ if ($args->{route}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'route'}) { } 17 3 20
elsif (my $name = $c->stash('script_name')) { } 3 0 3
102 17 $script_name = $c->url_for($args->{route}->name, {path_info => ''})->path->to_string;
103 }
104 elsif (my $name = $c->stash('script_name')) {
105 3 my $name = quotemeta $name;
106 3 ▶ $script_name = $c->req->url->path =~ m!^(.*?/$name)! ? $1 : $c->stash('script_name');
Branch: 1 branch
BranchTrueFalseTotal
$c->req->url->path =~ m[^(.*?/$name)]u ? : 3 0 3
107 }
108  
109 return (
110 20 ▶ %{$args->{env}},
Branch: 2 branches
BranchTrueFalseTotal
$req->is_secure ? : 0 20 20
$req->is_secure ? : 0 20 20
Condition68%15/22
$content_length || 0
l!l
119
$headers->content_type || ''
l!l
119
$c->stash("path_info") // ""
l!l
200
$c->stash('cgi.query_string') || $req->url->query->to_string
l!l&&r!l&&!r
1415
gethostbyaddr(inet_aton($tx->remote_address || '127.0.0.1'), 2) || ''
l!l
200
$tx->remote_address || '127.0.0.1'
l!l
200
$remote_user || ''
l!l
218
$args->{'script'} || ''
l!l
200
$script_name || $args->{'name'}
l!l&&r!l&&!r
2000
$ENV{'USER'} || ''
l!l
020
MC/DC100%1/1
$content_length || 0
$content_length
MC/DC100%1/1
$headers->content_type || ''
$headers->content_type
MC/DC0%0/1
$c->stash("path_info") // ""
$c->stash("path_info")
MC/DC100%2/2
$c->stash('cgi.query_string') || $req->url->query->to_string
$c->stash('cgi.query_string') $req->url->query->to_string
MC/DC0%0/1
gethostbyaddr(inet_aton($tx->remote_address || '127.0.0.1'), 2) || ''
gethostbyaddr(inet_aton($tx->remote_address || '127.0.0.1'), 2)
MC/DC0%0/1
$tx->remote_address || '127.0.0.1'
$tx->remote_address
MC/DC100%1/1
$remote_user || ''
$remote_user
MC/DC0%0/1
$args->{'script'} || ''
$args->{'script'}
MC/DC0%0/2
$script_name || $args->{'name'}
$script_name $args->{'name'}
MC/DC0%0/1
$ENV{'USER'} || ''
$ENV{'USER'}
Truth table100%2/2
A
A= $content_length
Aresult
11
00
Truth table100%2/2
A
A= $headers->content_type
Aresult
11
00
Truth table50%1/2
A
A= $c->stash("path_info")
Aresult
11
00
Truth table100%3/3
A || B
A= $c->stash('cgi.query_string')B= $req->url->query->to_string
ABresult
1X1
011
000
Truth table50%1/2
A
A= gethostbyaddr(inet_aton($tx->remote_address || '127.0.0.1'), 2)
Aresult
11
00
Truth table50%1/2
A
A= $tx->remote_address
Aresult
11
00
Truth table100%2/2
A
A= $remote_user
Aresult
11
00
Truth table50%1/2
A
A= $args->{'script'}
Aresult
11
00
Truth table33%1/3
A || B
A= $script_nameB= $args->{'name'}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $ENV{'USER'}
Aresult
11
00
111 CONTENT_LENGTH => $content_length || 0,
112 CONTENT_TYPE => $headers->content_type || '',
113 GATEWAY_INTERFACE => 'CGI/1.1',
114 HTTPS => $req->is_secure ? 'YES' : 'NO',
115 %env_headers,
116 PATH_INFO => '/' . encode('UTF-8', $c->stash('path_info') // ''),
117 QUERY_STRING => $c->stash('cgi.query_string') || $req->url->query->to_string,
118 REMOTE_ADDR => $tx->remote_address,
119 REMOTE_HOST => gethostbyaddr(inet_aton($tx->remote_address || '127.0.0.1'), AF_INET) || '',
120 REMOTE_PORT => $tx->remote_port,
121 REMOTE_USER => $remote_user || '',
122 REQUEST_METHOD => $req->method,
123 SCRIPT_FILENAME => $args->{script} || '',
124 SCRIPT_NAME => $script_name || $args->{name},
125 20 SERVER_ADMIN => $ENV{USER} || '',
126 SERVER_NAME => hostname,
127 SERVER_PORT => $tx->local_port,
128 SERVER_PROTOCOL => $req->is_secure ? 'HTTPS' : 'HTTP', # TODO: Version is missing
129 SERVER_SOFTWARE => __PACKAGE__,
130 );
131 }
132  
133 56 ▶sub _run {
Subroutine _run: called CC=15 SCAR=27.7
134 56 my ($defaults, $c) = (shift, shift);
135 0 ▶ my $args = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {};
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 9 47 56
@_ > 1 ? : 9 0 9
136 56 ▶ my $before = $args->{before} || $defaults->{before};
Condition66%2/3
$args->{'before'} || $defaults->{'before'}
l!l&&r!l&&!r
0254
MC/DC50%1/2
$args->{'before'} || $defaults->{'before'}
$args->{'before'} $defaults->{'before'}
Truth table66%2/3
A || B
A= $args->{'before'}B= $defaults->{'before'}
ABresult
1X1
011
000
137 56 my $stdin = _stdin($c);
138 56 my @stdout = pipely;
139 56 my ($pid, $log_key, @stderr);
140  
141 56 ▶ $args->{$_} ||= $defaults->{$_} for qw(env errlog route run script);
Condition100%3/3
$args->{$_} ||= $defaults->{$_}
l!l&&r!l&&!r
9155116
MC/DC100%2/2
$args->{$_} ||= $defaults->{$_}
$args->{$_} $defaults->{$_}
Truth table100%3/3
A ||= B
A= $args->{$_}B= $defaults->{$_}
ABresult
1X1
011
000
142 56 ▶ $args->{name} = $args->{run} ? "$args->{run}" : basename $args->{script};
Branch: 1 branch
BranchTrueFalseTotal
$args->{'run'} ? : 4 52 56
143 56 ▶ $c->$before($args) if $before;
Branch: 1 branch
BranchTrueFalseTotal
if $before 2 54 56
144 56 ▶ @stderr = (pipely) unless $args->{errlog};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"errlog"} 51 5 56
145 56 ▶ defined($pid = fork) or die "[CGI:$args->{name}] fork failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined($pid = fork) or die qq[[CGI:$args->{"name"}] fork failed: $!] 56 0 56
146 56 ▶ _child($c, $args, $stdin, \@stdout, \@stderr) unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 20 36 56
147 36 $args->{pids}{$pid} = $args->{name};
148 36 $log_key = "CGI:$args->{name}:$pid";
149 36 ▶ $c->app->log->debug("[$log_key] START @{[$args->{script} || $args->{run}]}");
Condition66%2/3
$args->{'script'} || $args->{'run'}
l!l&&r!l&&!r
3240
MC/DC0%0/2
$args->{'script'} || $args->{'run'}
$args->{'script'} $args->{'run'}
Truth table66%2/3
A || B
A= $args->{'script'}B= $args->{'run'}
ABresult
1X1
011
000
150  
151 36 for my $p (\@stdout, \@stderr) {
152 72 ▶ next unless $p->[READ];
Branch: 1 branch
BranchTrueFalseTotal
unless $p->[0] 3 69 72
153 69 close $p->[WRITE];
154 69 $p->[READ] = Mojo::IOLoop::Stream->new($p->[READ])->timeout(0);
155 69 Mojo::IOLoop->stream($p->[READ]);
156 }
157  
158 36 $c->stash('cgi.pid' => $pid, 'cgi.stdin' => $stdin);
159 36 $c->render_later;
160  
161 36 ▶ $stderr[READ]->on(read => _stderr_cb($c, $log_key)) if $stderr[READ];
Branch: 1 branch
BranchTrueFalseTotal
if $stderr[0] 33 3 36
162 36 $stdout[READ]->on(read => _stdout_cb($c, $log_key));
163 36 ▶ $stdout[READ]->on(close => sub {
Subroutine __ANON__: called CC=8 SCAR=22.5
164 36 my $GUARD = 50;
165 36 warn "[CGI:$args->{name}:$pid] Child closed STDOUT\n" if DEBUG;
166 0 ▶ unlink $stdin->path or die "Could not remove STDIN @{[$stdin->path]}" if -e $stdin->path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $stdin->path 36 0 36
Condition40%2/5
-e $stdin->path && (unlink($stdin->path) || die("Could not remove STDIN @{[$stdin->path];}"))
!ll&&!rl&&r
0360
unlink $stdin->path or die "Could not remove STDIN @{[$stdin->path];}"
l!l
360
MC/DC0%0/2
-e $stdin->path && (unlink($stdin->path) || die("Could not remove STDIN @{[$stdin->path];}"))
-e $stdin->path unlink $stdin->path
Truth table0%0/3
A && B
A= -e $stdin->pathB= unlink $stdin->path
ABresult
0X0
100
111
167 36 local ($?, $!);
168 _waitpids({$pid => $args->{pids}{$pid}})
169 36 ▶ while $args->{pids}{$pid}
Branch: 1 branch
BranchTrueFalseTotal
$args->{"pids"}{$pid} and kill 0, $pid and $GUARD-- and do { _waitpids({$pid, $args->{"pids"}{$pid}}) } 36 36 72
Condition66%4/6
$args->{"pids"}{$pid} and kill 0, $pid and $GUARD--
!ll&&!rl&&r
36036
$args->{"pids"}{$pid} and kill 0, $pid
!ll&&!rl&&r
03636
MC/DC33%1/3
$args->{"pids"}{$pid} and kill 0, $pid and $GUARD--
$args->{"pids"}{$pid} kill 0, $pid $GUARD--
Truth table50%2/4
A and B and C
A= $args->{"pids"}{$pid}B= kill 0, $pidC= $GUARD--
ABCresult
0XX0
10X0
1100
1111
170 and kill 0, $pid
171 and $GUARD--;
172 36 ▶ $defaults->{pids}{$pid} = $args->{pids}{$pid} if kill 0, $pid;
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pid 0 36 36
173 36 ▶ return $c->finish if $c->res->code;
Branch: 1 branch
BranchTrueFalseTotal
if $c->res->code 33 3 36
174 3 return $c->render(text => "Could not run CGI script ($?, $!).\n", status => 500);
175 }
176 36 );
177 }
178  
179 33 ▶sub _stderr_cb {
Subroutine _stderr_cb: called CC=1 SCAR=0.0
180 33 my ($c, $log_key) = @_;
181 33 my $log = $c->app->log;
182 33 my $buf = '';
183  
184 3 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
185 3 my ($stream, $chunk) = @_;
186 3 warn "[$log_key] !!! ($chunk)\n" if DEBUG;
187 3 $buf .= $chunk;
188 3 ▶ $log->warn("[$log_key] $1") while $buf =~ s!^(.+)[\r\n]+$!!m;
Branch: 1 branch
BranchTrueFalseTotal
$buf =~ s/^(.+)[\r\n]+$//mu and do { $log->warn("[$log_key] $1") } 3 3 6
189 33 };
190 }
191  
192 36 ▶sub _stdout_cb {
Subroutine _stdout_cb: called CC=1 SCAR=0.0
193 36 my ($c, $log_key) = @_;
194 36 my $buf = '';
195 36 my $headers;
196  
197 33 ▶ return sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
198 33 my ($stream, $chunk) = @_;
199 33 warn "[$log_key] >>> ($chunk)\n" if DEBUG;
200  
201 # true if HTTP header has been written to client
202 33 ▶ return $c->write($chunk) if $headers;
Branch: 1 branch
BranchTrueFalseTotal
if $headers 0 33 33
203  
204 33 $buf .= $chunk;
205  
206 # false until all headers has been read from the CGI script
207 33 ▶ $buf =~ s/^(.*?\x0a\x0d?\x0a\x0d?)//s or return;
Branch: 1 branch
BranchTrueFalseTotal
$buf =~ s/^(.*?\x0a\x0d?\x0a\x0d?)//su or return 33 0 33
208 33 $headers = $1;
209  
210 33 ▶ if ($headers =~ /^HTTP/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers =~ /^HTTP/u) { } 6 27 33
211 6 ▶ $c->res->code($headers =~ m!^HTTP (\d\d\d)! ? $1 : 200);
Branch: 1 branch
BranchTrueFalseTotal
$headers =~ /^HTTP (\d\d\d)/u ? : 1 5 6
212 6 $c->res->parse($headers);
213 }
214 else {
215 27 ▶ $c->res->code($1) if $headers =~ /^Status: (\d\d\d)/m;
Branch: 1 branch
BranchTrueFalseTotal
if $headers =~ /^Status: (\d\d\d)/mu 2 25 27
216 27 ▶ $c->res->code($headers =~ /Location:/ ? 302 : 200) unless $c->res->code;
Branch: 2 branches
BranchTrueFalseTotal
unless $c->res->code 25 2 27
$headers =~ /Location:/u ? : 1 24 25
217 27 $c->res->parse($c->res->get_start_line_chunk(0) . $headers);
218 }
219 33 ▶ $c->write($buf) if length $buf;
Branch: 1 branch
BranchTrueFalseTotal
if length $buf 31 2 33
220 36 };
221 }
222  
223 56 ▶sub _stdin {
Subroutine _stdin: called CC=3 SCAR=11.0
224 56 my $c = shift;
225 56 my $stdin;
226  
227 56 ▶ if ($c->req->content->is_multipart) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->content->is_multipart) { } 2 54 56
228 2 $stdin = Mojo::Asset::File->new;
229 2 $stdin->add_chunk($c->req->build_body);
230 }
231 else {
232 54 $stdin = $c->req->content->asset;
233 }
234  
235 56 ▶ return $stdin if $stdin->isa('Mojo::Asset::File');
Branch: 1 branch
BranchTrueFalseTotal
if $stdin->isa("Mojo::Asset::File") 2 54 56
236 54 return Mojo::Asset::File->new->add_chunk($stdin->slurp);
237 }
238  
239 4685 ▶sub _waitpids {
Subroutine _waitpids: called CC=4 SCAR=14.1
240 4685 my $pids = shift;
241  
242 4685 for my $pid (keys %$pids) {
243  
244 # no idea why i need to do this, but it seems like waitpid() below return -1 if not
245 36 local $SIG{CHLD} = 'DEFAULT';
246 36 ▶ next unless waitpid $pid, WNOHANG;
Branch: 1 branch
BranchTrueFalseTotal
unless waitpid $pid, 1 0 36 36
247 36 ▶ my $name = delete $pids->{$pid} || 'unknown';
Condition50%1/2
delete $pids->{$pid} || 'unknown'
l!l
360
MC/DC0%0/1
delete $pids->{$pid} || 'unknown'
delete $pids->{$pid}
Truth table50%1/2
A
A= delete $pids->{$pid}
Aresult
11
00
248 36 my ($exit_value, $signal) = ($? >> 8, $? & 127);
249 36 warn "[CGI:$name:$pid] Child exit_value=$exit_value ($signal)\n" if DEBUG;
250 }
251 }
252  
253 34 1;
254  
255 =encoding utf8
256  
257 =head1 NAME
258  
259 Mojolicious::Plugin::CGI - Run CGI script from Mojolicious
260  
261 =head1 VERSION
262  
263 0.40
264  
265 =head1 DESCRIPTION
266  
267 This plugin enables L<Mojolicious> to run Perl CGI scripts. It does so by forking
268 a new process with a modified environment and reads the STDOUT in a non-blocking
269 manner.
270  
271 =head1 SYNOPSIS
272  
273 =head2 Standard usage
274  
275 use Mojolicious::Lite;
276 plugin CGI => [ "/cgi-bin/script" => "/path/to/cgi/script.pl" ];
277  
278 Using the code above is enough to run C<script.pl> when accessing
279 L<http://localhost:3000/cgi-bin/script>.
280  
281 =head2 Complex usage
282  
283 plugin CGI => {
284 # Specify the script and mount point
285 script => "/path/to/cgi/script.pl",
286 route => "/some/route",
287  
288 # %ENV variables visible from inside the CGI script
289 env => {}, # default is \%ENV
290  
291 # Path to where STDERR from cgi script goes
292 errlog => "/path/to/file.log",
293  
294 # The "before" hook is called before script start
295 # It receives a Mojolicious::Controller which can be modified
296 before => sub {
297 my $c = shift;
298 $c->req->url->query->param(a => 123);
299 },
300 };
301  
302 The above contains all the options you can pass on to the plugin.
303  
304 =head2 Helper
305  
306 plugin "CGI";
307  
308 # GET /cgi-bin/some-script.cgi/path/info?x=123
309 get "/cgi-bin/#script_name/*path_info" => {path_info => ''}, sub {
310 my $c = shift;
311 my $name = $c->stash("script_name");
312 $c->cgi->run(script => File::Spec->rel2abs("/path/to/cgi/$name"));
313 };
314  
315 The helper can take most of the arguments that L</register> takes, with the
316 exception of C<support_semicolon_in_query_string>.
317  
318 It is critical that "script_name" and "path_info" is present in
319 L<stash|Mojolicious::Controller/stash>. Whether the values are extracted directly
320 from the path or set manually does not matter.
321  
322 Note that the helper is registered in all of the examples.
323  
324 =head2 Running code refs
325  
326 plugin CGI => {
327 route => "/some/path",
328 run => sub {
329 my $cgi = CGI->new;
330 # ...
331 }
332 };
333  
334 Instead of calling a script, you can run a code block when accessing the route.
335 This is (pretty much) safe, even if the code block modifies global state,
336 since it runs in a separate fork/process.
337  
338 =head2 Support for semicolon in query string
339  
340 plugin CGI => {
341 support_semicolon_in_query_string => 1,
342 ...
343 };
344  
345 The code above needs to be added before other plugins or handlers which use
346 L<Mojo::Message::Request/url>. It will inject a C<before_dispatch>
347 hook which saves the original QUERY_STRING, before it is split on
348 "&" in L<Mojo::Parameters>.
349  
350 =head1 ATTRIBUTES
351  
352 =head2 env
353  
354 Holds a hash ref containing the environment variables that should be
355 used when starting the CGI script. Defaults to C<%ENV> when this module
356 was loaded.
357  
358 This plugin will create a set of environment variables depenendent on the
359 request passed in which is according to the CGI spec. In addition to L</env>,
360 these dynamic variables are set:
361  
362 CONTENT_LENGTH, CONTENT_TYPE, HTTPS, PATH, PATH_INFO, QUERY_STRING,
363 REMOTE_ADDR, REMOTE_HOST, REMOTE_PORT, REMOTE_USER, REQUEST_METHOD,
364 SCRIPT_NAME, SERVER_PORT, SERVER_PROTOCOL.
365  
366 Additional static variables:
367  
368 GATEWAY_INTERFACE = "CGI/1.1"
369 SERVER_ADMIN = $ENV{USER}
370 SCRIPT_FILENAME = Script name given as argument to register.
371 SERVER_NAME = Sys::Hostname::hostname()
372 SERVER_SOFTWARE = "Mojolicious::Plugin::CGI"
373  
374 Plus all headers are exposed. Examples:
375  
376 .----------------------------------------.
377 | Header | Variable |
378 |-----------------|----------------------|
379 | Referer | HTTP_REFERER |
380 | User-Agent | HTTP_USER_AGENT |
381 | X-Forwarded-For | HTTP_X_FORWARDED_FOR |
382 '----------------------------------------'
383  
384 =head2 register
385  
386 $self->register($app, [ $route => $script ]);
387 $self->register($app, %args);
388 $self->register($app, \%args);
389  
390 C<route> and L<path> need to exist as keys in C<%args> unless given as plain
391 arguments.
392  
393 C<$route> can be either a plain path or a route object.
394  
395 =head1 COPYRIGHT AND LICENSE
396  
397 Copyright (C) 2014, Jan Henning Thorsen
398  
399 This program is free software, you can redistribute it and/or modify it under
400 the terms of the Artistic License version 2.0.
401  
402 =head1 AUTHOR
403  
404 Jan Henning Thorsen - C<jhthorsen@cpan.org>
405  
406 =cut
↑ Summary