FastCGI/Run.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 157 CC 11 SCAR 42.5
CC
11
Cov
0%
start
132.0
BEGIN
2.0
BEGIN
2.0
CRAP 70.1
1 #
2 #
3 # Copyright (C) 2006-2010 Andrew Speer <andrew@webdyne.org>. All rights
4 # reserved.
5 #
6 # This file is part of WebDyne::Request::FastCGI
7 #
8 # WebDyne is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #
22 #
23 package WebDyne::Request::FastCGI::Run;
24  
25  
26 # Compiler Pragma
27 #
28 use strict qw(vars);
29 use vars qw($VERSION);
30  
31  
32 # External modules
33 #
34 use WebDyne;
35 use WebDyne::Base;
36 use WebDyne::Request::FastCGI;
37 use WebDyne::Request::FastCGI::Constant;
38 use FCGI;
39 use CGI;
40 use HTTP::Status qw(RC_INTERNAL_SERVER_ERROR RC_NOT_FOUND);
41  
42  
43 # Set handler as constant. No longer do this - load hander on per request basis on
44 # information from dir_config.
45 #
46 #use constant WebDyneHandler=>$ENV{'WebDyneHandler'} ? $ENV{'WebDyneHandler'} : 'WebDyne';
47  
48  
49 # Load up whichever handler we are using
50 #
51 #my $handler=WebDyneHandler;
52 #eval ("require $handler") || die("unable to load handler $handler, $@");
53  
54  
55 # Version information
56 #
57 $VERSION='1.002';
58  
59  
60 # Signal handlers (seem broken with lighttp, ignore for now
61 #
62 #$SIG{'USR1'}=sub { exit 0 };
63 #$SIG{'TERM'}=sub { exit 0 };
64 #$SIG{'PIPE'}='IGNORE';
65  
66  
67 # Sig handler
68 #
69 #sub sig {
70 # exit(0) if !$busy;
71 # $exit++;
72 #}
73  
74  
75 # CGI needs to be hacked under some versions of FastCGI so path_info returns correct string, otherwise
76 # CGI->self_url does not return correct value, then forms with explicit action attr stop working
77 #
78 my $path_info_cr=\&CGI::path_info || die('unable to get CGI code ref for path_info');
79 *CGI::path_info=sub {
80 my $path_info=$path_info_cr->(@_);
81 $path_info=~s/^\Q$ENV{'SCRIPT_NAME'}\E//;
82 $path_info;
83 };
84  
85  
86  
87 # Debug
88 #
89 #0 && debug("wdfastcgi handler for pid $$ is $handler, env %s", &Data::Dumper::Dumper(\%ENV));
90  
91  
92 # All done. Positive return
93 #
94 1;
95  
96  
97 #==================================================================================================
98  
99  
100 # Start endless loop
101 #
102 sub start {
103  
104  
105 # Can optionally be passed a socket file name at start
106 #
107 my ($class, $socket_fn, $backlog)=@_;
108 my $socket_fh=$socket_fn && do {
109 $backlog ||= WEBDYNE_FASTCGI_BACKLOG;
110 FCGI::OpenSocket($socket_fn, $backlog) ||
111 die ("could not open socket $socket_fn, $!");
112 };
113
114
115 # Workaround for perlbug 73672
116 #
117 my %env=%ENV;
118 *ENV=\*env;
119  
120  
121 # Cache handler for a location
122 #
123 my ($handler, %handler);
124  
125  
126 # Get FastCGI request object
127 #
128 my $fcgi_r=$socket_fh ?
129 FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket_fh) : FCGI::Request();
130 my ($busy, $exit);
131
132
133 while($busy=($fcgi_r->Accept() >= 0)) {
134  
135  
136 # Create new FastCGI Request object, will pull filename from
137 # environment. goto only invoked if no object created, which
138 # should never happen !
139 #
140 my $r=WebDyne::Request::FastCGI->new() ||
141 goto RENDER_ERROR;
142
143
144 # Get handler
145 #
146 unless ($handler=$handler{my $location=$r->location()}) {
147 my $handler_package=
148 $r->dir_config('WebDyneHandler') || $ENV{'WebDyneHandler'};
149 if ($handler_package) {
150 local $SIG{'__DIE__'};
151 unless (eval("require $handler_package")) {
152 # Didn't load - let Webdyne handle the error.
153 $handler='WebDyne';
154 }
155 else {
156 $handler=$handler{$location}=$handler_package;
157 }
158 }
159 else {
160 $handler=$handler{$location}='WebDyne';
161 }
162 }
163  
164  
165 # Call handler and evaluate results
166 #
167 my $status=eval { $handler->handler($r) } if $handler;
168 if (!defined($status)) {
169 if (($status=$r->status) ne RC_INTERNAL_SERVER_ERROR) {
170 my $error=errdump() || $@; errclr();
171 $r->custom_response(
172 $r->status($status=RC_INTERNAL_SERVER_ERROR),
173 $r->err_html($status, $error)
174 );
175 }
176 }
177 elsif (($status < 0) && !(-f (my $fn=$r->filename()))) {
178 $r->status($status=RC_NOT_FOUND);
179 warn("file $fn not found") if $WEBDYNE_FASTCGI_WARN_ON_ERROR;
180 }
181 elsif ($status < 0) {
182 $r->custom_response(
183 $r->status($status=RC_INTERNAL_SERVER_ERROR),
184 $r->err_html($status, "Unexpected return ($status) from handler $handler")
185 );
186 }
187 0 && debug("handler status was $status");
188  
189  
190 # If status ne Apache::OK invoke error handler
191 #
192 $status && $r->send_error_response();
193  
194  
195 # Done, call destoy manually so cleanup_handlers run
196 #
197 $r->DESTROY;
198  
199  
200 # Signal handlers broken, these never get checked
201 #$busy=0;
202 #last if $exit;
203 next;
204  
205  
206 # Error handler, exceptional cases only (usually handled by
207 # WebDyne handler).
208 #
209 RENDER_ERROR:
210 require CGI;
211 my $error=errdump() || 'unknown error'; errclr();
212 CORE::print
213 sprintf("Status: %s\r\n", RC_INTERNAL_SERVER_ERROR),
214 "Content-Type: text\html\r\n\r\n",
215 WebDyne::Request::FastCGI->err_html(RC_INTERNAL_SERVER_ERROR, CGI->pre($error));
216  
217 };
218  
219 # All done, quit. Commented out due to broken sig handlers
220 #
221 #$fcgi_r->Finish();
222 #exit(0);
223  
224 }
225