| 1 | package YATT::Lite::WebMVC0::SiteApp::FCGI; | ||||||||||||||||||||||||||||
| 2 | # -*- coding: utf-8 -*- | ||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use FCGI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use YATT::Lite::WebMVC0::SiteApp; # To make lint happy, this is required. | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use YATT::Lite::WebMVC0::SiteApp::CGI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package YATT::Lite::WebMVC0::SiteApp; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | ######################################## | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # FastCGI support with auto_exit, based on PSGI mode. | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | # Many parts are stolen from Plack::Handler::FCGI. | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | ######################################## | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub _prepare_config_for_fcgi { | ||||||||||||||||||||||||||
|
Subroutine
_prepare_config_for_fcgi: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 23 | 0 | (my MY $self, my ($opts)) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | $self->{is_psgi} = 1; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # In suexec fcgi, $0 will not be absolute path. | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | $self->{progname} //= do { | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"progname"} //= do {
if (-r (my $fn = delete $opts->{"progname"} || $0)) {
$self->rel2abs($fn);
}
else {
croak("progname is empty!");
}
}
MC/DC0%0/2
$self->{"progname"} //= do {
if (-r (my $fn = delete $opts->{"progname"} || $0)) {
$self->rel2abs($fn);
}
else {
croak("progname is empty!");
}
}
$self->{"progname"}
do {
if (-r (my $fn = delete $opts->{"progname"} || $0)) {
$self->rel2abs($fn);
}
else {
croak("progname is empty!");
}
}
Truth table0%0/3
A //= B
A= $self->{"progname"}B= do {
if (-r (my $fn = delete $opts->{"progname"} || $0)) {
$self->rel2abs($fn);
}
else {
croak("progname is empty!");
}
}
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | if (-r (my $fn = delete $opts->{progname} || $0)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
delete $opts->{'progname'} || $0
MC/DC0%0/2
delete $opts->{'progname'} || $0
delete $opts->{'progname'}
$0
Truth table0%0/3
A || B
A= delete $opts->{'progname'}B= $0
| |||||||||||||||||||||||||||||
| 30 | 0 | $self->rel2abs($fn); | |||||||||||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||||||||||
| 32 | 0 | croak "progname is empty!"; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ((my $dn = $self->{progname}) =~ s{/html/cgi-bin/[^/]+$}{}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | $dn = YATT::Lite::Util::normalize_fs_path($dn); | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | $self->{app_root} //= $dn; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"app_root"} //= $dn
MC/DC0%0/2
$self->{"app_root"} //= $dn
$self->{"app_root"}
$dn
Truth table0%0/3
A //= B
A= $self->{"app_root"}B= $dn
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $self->{doc_root} //= "$dn/html"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"doc_root"} //= "$dn/html"
MC/DC0%0/1
$self->{"doc_root"} //= "$dn/html"
$self->{"doc_root"}
Truth table0%0/2
A
A= $self->{"doc_root"}
| |||||||||||||||||||||||||||||
| 40 | 0 | push @{$self->{_tmpldirs}}, $self->{doc_root} | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | unless $self->{_tmpldirs} and @{$self->{_tmpldirs}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"_tmpldirs"} and @{$self->{"_tmpldirs"};}
MC/DC0%0/2
$self->{"_tmpldirs"} and @{$self->{"_tmpldirs"};}
$self->{"_tmpldirs"}
@{$self->{"_tmpldirs"};}
Truth table0%0/3
A and B
A= $self->{"_tmpldirs"}B= @{$self->{"_tmpldirs"};}
| |||||||||||||||||||||||||||||
| 42 | #print STDERR "Now:", terse_dump($self->{app_root}, $self->{doc_root} | ||||||||||||||||||||||||||||
| 43 | # , $self->{_tmpldirs}), "\n"; | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # callas_fcgi() and runas_fcgi() is designed for single process app. | ||||||||||||||||||||||||||||
| 48 | # If you want psgi.multiprocess, use Plack's own FCGI instead. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||
| 51 | # Check timestamp for $self->{progname} and exit when modified. | ||||||||||||||||||||||||||||
| 52 | # (Outer processmanager is responsible to restart). | ||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub _callas_fcgi { | ||||||||||||||||||||||||||
|
Subroutine
_callas_fcgi: not called CC=22 SCAR=62.3
| |||||||||||||||||||||||||||||
| 55 | 0 | (my MY $self, my $app, my $fhset, my Env $init_env, my ($args, %opts)) = @_; | |||||||||||||||||||||||||||
| 56 | # $fhset is either stdout or [\*STDIN, \*STDOUT, \*STDERR]. | ||||||||||||||||||||||||||||
| 57 | # $init_env is just discarded. | ||||||||||||||||||||||||||||
| 58 | # $args = \@ARGV | ||||||||||||||||||||||||||||
| 59 | # %opts is fcgi specific options. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | $self->_prepare_config_for_fcgi(\%opts); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | my $dir = dirname($self->{progname}); | |||||||||||||||||||||||||||
| 64 | 0 | my $age = -M $self->{progname}; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | my ($stdin, $stdout, $stderr) = ref $fhset eq 'ARRAY' ? @$fhset | ||||||||||||||||||||||||||||
| 67 | : (\*STDIN, $fhset | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | , ((delete $opts{isolate_stderr}) // 1) ? \*STDERR : $fhset); | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
delete $opts{"isolate_stderr"} // 1
MC/DC0%0/1
delete $opts{"isolate_stderr"} // 1
delete $opts{"isolate_stderr"}
Truth table0%0/2
A
A= delete $opts{"isolate_stderr"}
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $sock = do { | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (my $sockfile = delete $opts{listen}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | unless (-e (my $d = dirname($sockfile))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | require File::Path; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | File::Path::make_path($d) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | or die "Can't mkdir $d: $!"; | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | FCGI::OpenSocket($sockfile, 100) | ||||||||||||||||||||||||||
|
Condition0%0/2
&FCGI::OpenSocket($sockfile, 100) || die("Can't open FCGI socket '${sockfile}': $!")
MC/DC0%0/1
&FCGI::OpenSocket($sockfile, 100) || die("Can't open FCGI socket '${sockfile}': $!")
&FCGI::OpenSocket($sockfile, 100)
Truth table0%0/2
A
A= &FCGI::OpenSocket($sockfile, 100)
| |||||||||||||||||||||||||||||
| 78 | or die "Can't open FCGI socket '$sockfile': $!"; | ||||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||
| 80 | 0 | 0; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | my %env; | |||||||||||||||||||||||||||
| 85 | my $request = FCGI::Request | ||||||||||||||||||||||||||||
| 86 | ($stdin, $stdout, $stderr | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | , \%env, $sock, $opts{nointr} ? 0 :&FCGI::FAIL_ACCEPT_ON_INTR); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (keys %opts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | croak "Unknown options: ".join(", ", sort keys %opts); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | local $self->{at_done} = sub {die \"DONE"}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 94 | 0 | local $SIG{PIPE} = 'IGNORE'; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | while ($request->Accept >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | my Env $env = $self->psgi_fcgi_newenv(\%env, $stdin, $stderr); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (-e "$dir/.htdebug_env") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | $self->printenv($stdout, $env); | |||||||||||||||||||||||||||
| 100 | 0 | next; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | my $res; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | if (my $err = catch { $res = $app->($env) }) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | # XXX: Should I do error specific things? | ||||||||||||||||||||||||||||
| 106 | 0 | $res = $err; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | unless (defined $res) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 110 | 0 | die "Empty response"; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | elsif (ref $res eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 113 | 0 | $self->fcgi_handle_response($res); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | elsif (ref $res eq 'CODE') { | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $res->(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 117 | 0 | $self->fcgi_handle_response($_[0]); | |||||||||||||||||||||||||||
| 118 | 0 | }); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | ▶ | elsif (not ref $res or UNIVERSAL::can($res, 'message')) { | |||||||||||||||||||||||||||
|
Condition0%0/3
not ref $res or UNIVERSAL::can($res, "message")
MC/DC0%0/2
not ref $res or UNIVERSAL::can($res, "message")
not ref $res
UNIVERSAL::can($res, "message")
Truth table0%0/3
not(A) or B
A= not ref $resB= UNIVERSAL::can($res, "message")
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | print $stderr $res if $$stderr ne $stdout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($self->is_debug_allowed($env)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $self->cgi_process_error($res, undef, $stdout, $env); | |||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||
| 125 | 0 | $self->cgi_process_error("Application error", undef, $stdout, $env); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||
| 129 | 0 | die "Bad response $res"; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | $request->Finish; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # Exit if bootscript is modified. | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | last if -e $self->{progname} and -M $self->{progname} < $age; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $self->{"progname"} and -M $self->{"progname"} < $age
MC/DC0%0/2
-e $self->{"progname"} and -M $self->{"progname"} < $age
-e $self->{"progname"}
-M $self->{"progname"} < $age
Truth table0%0/3
A and B
A= -e $self->{"progname"}B= -M $self->{"progname"} < $age
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub _runas_fcgi { | ||||||||||||||||||||||||||
|
Subroutine
_runas_fcgi: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 140 | 0 | (my MY $self, my $fhset, my Env $init_env, my ($args, %opts)) = @_; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # This will be called again in _callas_fcgi, but it will not harm. | ||||||||||||||||||||||||||||
| 143 | 0 | $self->_prepare_config_for_fcgi(\%opts); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | $self->prepare_app; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | $self->_callas_fcgi($self->to_app, $fhset, $init_env, $args, %opts); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | *psgi_fcgi_newenv = *psgi_cgi_newenv; | |||||||||||||||||||||||||||
| 151 | 1 | *psgi_fcgi_newenv = *psgi_cgi_newenv; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub fcgi_handle_response { | ||||||||||||||||||||||||||
|
Subroutine
fcgi_handle_response: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | 0 | my ($self, $res) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | require HTTP::Status; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | *STDOUT->autoflush(1); | |||||||||||||||||||||||||||
| 159 | 0 | binmode STDOUT; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | my $hdrs; | |||||||||||||||||||||||||||
| 162 | 0 | my $message = HTTP::Status::status_message($res->[0]); | |||||||||||||||||||||||||||
| 163 | 0 | $hdrs = "Status: $res->[0] $message\015\012"; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my $headers = $res->[1]; | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | while (my ($k, $v) = splice @$headers, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | $hdrs .= "$k: $v\015\012"; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 0 | $hdrs .= "\015\012"; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | print STDOUT $hdrs; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my $cb = sub { print STDOUT $_[0] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 174 | 0 | my $body = $res->[2]; | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | if (defined $body) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | Plack::Util::foreach($body, $cb); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | else { | ||||||||||||||||||||||||||||
| 179 | return Plack::Util::inline_object | ||||||||||||||||||||||||||||
| 180 | (write => $cb, | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | close => sub { }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1 | &YATT::Lite::Breakpoint::break_load_dispatcher_fcgi; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 1 | 1; | |||||||||||||||||||||||||||