GX/Engine/FCGI.pm

filter: Statementstmt 23.7% 48 / 202 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 14 / 28 Podpod 100% 4 / 4 totaltotal 22.6% 66 / 292 CC 29 SCAR 34.5
CC
29
Cov
19%
process_request_body
110.0
send_response
42.0
process_request_headers
30.0
CRAP 31.4
« GX/View/Error.pm ↑ Summary GX/Engine/Apache2.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Engine/FCGI.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Engine::FCGI;
7  
8 1 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use GX::HTTP::Body::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use GX::HTTP::Body::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use GX::HTTP::Constants qw( CRLF );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use GX::HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 # ----------------------------------------------------------------------------------------------------------------------
18 # Class setup
19 # ----------------------------------------------------------------------------------------------------------------------
20  
21 1 ▶use GX::Class ( extends => 'GX::Engine' );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 build;
24  
25  
26 # ----------------------------------------------------------------------------------------------------------------------
27 # Export mechanism
28 # ----------------------------------------------------------------------------------------------------------------------
29  
30 1 __PACKAGE__->_install_import_method;
31  
32  
33 # ----------------------------------------------------------------------------------------------------------------------
34 # Handlers
35 # ----------------------------------------------------------------------------------------------------------------------
36  
37 0 ▶sub finalize_response_headers :Handler( FinalizeResponseHeaders ) {
Subroutine finalize_response_headers: not called CC=2 SCAR=17.9
Pod documented
38  
39 0 my $self = shift;
40 0 my $context = shift;
41  
42 0 my $response = $context->response;
43  
44 0 for my $cookie ( $response->cookies->all ) {
45 0 $response->headers->add( 'Set-Cookie' => $cookie->as_string );
46 }
47  
48 0 return;
49  
50 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 0 ▶sub process_request_body :Handler( ProcessRequestBody ) {
Subroutine process_request_body: not called CC=10 SCAR=47.0
Pod documented
53  
54 0 my $self = shift;
55 0 my $context = shift;
56  
57 0 my $request = $context->request;
58 0 my $content_length = $request->content_length;
59  
60 0 ▶ if ( ! $content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content_length) 0 0 0
61 0 return;
62 }
63  
64 0 ▶ if ( $content_length !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content_length =~ /^\d+$/) 0 0 0
65 0 $context->send_response( status => 400 );
66 0 return;
67 }
68  
69 0 my $config = $self->_get_config;
70  
71 0 ▶ if ( $content_length > $config->{'max_request_size'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length > $config->{"max_request_size"}) 0 0 0
72 0 $context->send_response( status => 413 );
73 0 return;
74 }
75  
76 0 my $body;
77  
78 0 ▶ if ( $content_length > $config->{'max_request_memory_usage'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length > $config->{'max_request_memory_usage'}) { } 0 0 0
79 0 $body = GX::HTTP::Body::File->new;
80 }
81 else {
82 0 $body = GX::HTTP::Body::Scalar->new;
83 }
84  
85 0 my $body_fh = $body->open( '>' );
86 0 my $input_stream = $context->input_stream;
87 0 my $buffer_size = $config->{'buffer_size'};
88 0 my $bytes_read = 0;
89 0 my $read_callback = $request->read_callback;
90  
91 0 ▶ while ( ( my $bytes = $input_stream->read( my $buffer, $buffer_size ) ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $bytes = $input_stream->read(my $buffer, $buffer_size)) > 0) 0 0 0
92  
93 0 $bytes_read += $bytes;
94  
95 0 ▶ if ( ! $body_fh->print( $buffer ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($body_fh->print($buffer)) 0 0 0
96 0 throw "Cannot write request body";
97 }
98  
99 0 ▶ if ( $bytes_read > $content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read > $content_length) 0 0 0
100 0 last;
101 }
102  
103 0 ▶ if ( $read_callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_callback) 0 0 0
104 0 $read_callback->( $context, $bytes_read, $content_length );
105 }
106  
107 }
108  
109 0 $body_fh->close;
110  
111 0 ▶ if ( $bytes_read != $content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != $content_length) 0 0 0
112 0 $context->send_response( status => 400 );
113 0 return;
114 }
115  
116 0 $request->body( $body );
117  
118 0 return;
119  
120 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 0 ▶sub process_request_headers :Handler( ProcessRequestHeaders ) {
Subroutine process_request_headers: not called CC=5 SCAR=34.0
Pod documented
123  
124 0 my $self = shift;
125 0 my $context = shift;
126  
127 0 my $fcgi = $context->fcgi;
128 0 my $headers = $context->request->headers;
129  
130 # "The server may exclude any headers which it has already processed, such
131 # as Authorization, Content-Type, and Content-Length."
132  
133 0 for ( qw( CONTENT_LENGTH CONTENT_TYPE ) ) {
134 0 ▶ next if ! exists( $fcgi->{$_} ) || exists( $fcgi->{"HTTP_$_"} );
Branch: 1 branch
BranchTrueFalseTotal
if not exists $fcgi->{$_} or exists $$fcgi{"HTTP_$_"} 0 0 0
Condition0%0/3
not exists $fcgi->{$_} or exists $$fcgi{"HTTP_$_"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $fcgi->{$_} or exists $$fcgi{"HTTP_$_"}
not exists $fcgi->{$_} exists $$fcgi{"HTTP_$_"}
Truth table0%0/3
not(A) or B
A= not exists $fcgi->{$_}B= exists $$fcgi{"HTTP_$_"}
ABresult
1X1
011
000
135 0 my $field_name = $_;
136 0 $field_name =~ tr/_/-/;
137 0 $headers->add( $field_name => $fcgi->{$_} );
138 }
139  
140 0 for ( grep { /^HTTP_/ } keys %$fcgi ) {
141 0 my $field_name = substr( $_, 5 );
142 0 $field_name =~ tr/_/-/;
143 0 $headers->add( $field_name => $fcgi->{$_} );
144 }
145  
146 0 return;
147  
148 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 0 ▶sub send_response :Handler( SendResponse ) {
Subroutine send_response: not called CC=6 SCAR=37.4
Pod documented
151  
152 0 my $self = shift;
153 0 my $context = shift;
154  
155 0 my $output_stream = $context->output_stream;
156  
157 0 my $response = $context->response;
158 0 my $headers = $response->headers;
159 0 my $body = $response->body;
160  
161 0 ▶ $headers->set( 'Status' => $response->status // 200 );
Condition0%0/2
$response->status // 200
l!l
00
MC/DC0%0/1
$response->status // 200
$response->status
Truth table0%0/2
A
A= $response->status
Aresult
11
00
162  
163 0 ▶ if ( my $length = $body->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $length = $body->length) { } 0 0 0
164  
165 0 ▶ if ( ! defined $headers->content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $headers->content_length) 0 0 0
166  
167 0 ▶ if ( $length > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 0 0 0
168 0 $headers->content_length( $length );
169 }
170  
171 }
172  
173 0 $output_stream->print( $headers->as_string, CRLF );
174  
175 0 ▶ if ( $context->request->method ne 'HEAD' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->request->method ne "HEAD") 0 0 0
176 0 $body->print_to( $output_stream );
177 }
178  
179 }
180 else {
181 0 $output_stream->print( $headers->as_string, CRLF );
182 }
183  
184 0 $output_stream->flush;
185  
186 0 return;
187  
188 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190  
191 # ----------------------------------------------------------------------------------------------------------------------
192 # Private methods
193 # ----------------------------------------------------------------------------------------------------------------------
194  
195 0 ▶sub _create_fcgi_handler {
Subroutine _create_fcgi_handler: not called CC=2 SCAR=17.9
196  
197 0 my $self = shift;
198 0 my $application = shift;
199  
200 0 my $context_class = ref( $application ) . '::Context';
201 0 my $request_class = ref( $application ) . '::Request';
202 0 my $response_class = ref( $application ) . '::Response';
203  
204 0 my $error_stream_class = __PACKAGE__ . '::ErrorStream';
205 0 my $input_stream_class = __PACKAGE__ . '::InputStream';
206 0 my $output_stream_class = __PACKAGE__ . '::OutputStream';
207  
208 0 ▶ my $handler = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
209  
210 0 my $application = shift->instance;
211 0 my $fcgi = shift;
212  
213 0 local $@;
214  
215 0 eval {
216  
217 0 ▶ my $scheme = ( defined $fcgi->{'HTTPS'} && uc $fcgi->{'HTTPS'} eq 'ON' ) ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
defined $fcgi->{'HTTPS'} && uc $fcgi->{'HTTPS'} eq 'ON' ? : 0 0 0
Condition0%0/3
defined $fcgi->{"HTTPS"} and uc $fcgi->{"HTTPS"} eq "ON"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fcgi->{"HTTPS"} and uc $fcgi->{"HTTPS"} eq "ON"
defined $fcgi->{"HTTPS"} uc $fcgi->{"HTTPS"} eq "ON"
Truth table0%0/3
A and B
A= defined $fcgi->{"HTTPS"}B= uc $fcgi->{"HTTPS"} eq "ON"
ABresult
0X0
100
111
218  
219 0 ▶ ( my $path = $fcgi->{'REQUEST_URI'} // '' ) =~ s/\?.*$//;
Condition0%0/2
$fcgi->{"REQUEST_URI"} // ""
l!l
00
MC/DC0%0/1
$fcgi->{"REQUEST_URI"} // ""
$fcgi->{"REQUEST_URI"}
Truth table0%0/2
A
A= $fcgi->{"REQUEST_URI"}
Aresult
11
00
220  
221 my $context = $context_class->new(
222  
223 input_stream => $input_stream_class->new,
224  
225 output_stream => $output_stream_class->new,
226  
227 error_stream => $error_stream_class->new,
228  
229 request => $request_class->new(
230 host => $fcgi->{'SERVER_NAME'},
231 method => $fcgi->{'REQUEST_METHOD'},
232 path => $path,
233 port => $fcgi->{'SERVER_PORT'},
234 protocol => $fcgi->{'SERVER_PROTOCOL'},
235 query => $fcgi->{'QUERY_STRING'},
236 remote_address => $fcgi->{'REMOTE_ADDR'},
237 scheme => $scheme,
238 0 uri => $fcgi->{'REQUEST_URI'}
239 ),
240  
241 response => $response_class->new(
242 status => 200
243 ),
244  
245 fcgi => $fcgi
246  
247 );
248  
249 0 $application->process( $context );
250  
251 };
252  
253 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
254 0 return $application->engine->_handle_error( $fcgi, $@ );
255 }
256  
257 0 return;
258  
259 0 };
260  
261 0 ▶ if ( $application->mode eq 'development' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($application->mode eq "development") 0 0 0
262  
263 0 my $real_handler = $handler;
264  
265 0 ▶ $handler = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
266 0 ▶ goto &{"$_[0]\::handler"} if eval { $_[0]->reload };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $_[0]->reload } } 0 0 0
267 0 ▶ __PACKAGE__->_handle_reload_error( $_[0], $_[1], $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
268 0 return $real_handler->( @_ );
269 0 };
270  
271 }
272  
273 0 return $handler;
274  
275 }
276  
277 0 ▶sub _deploy {
Subroutine _deploy: not called CC=1 SCAR=6.9
278  
279 0 my $self = shift;
280  
281 0 $self->SUPER::_deploy;
282  
283 0 my $application = $self->application;
284  
285 0 $self->_export_method(
286 ref( $application ),
287 'handler',
288 $self->_create_fcgi_handler( $application )
289 );
290  
291 0 $self->_export_attribute(
292 ref( $application ) . '::Context',
293 {
294 name => 'fcgi',
295 type => 'Scalar',
296 accessor => { type => 'get' }
297 }
298 );
299  
300 0 return;
301  
302 }
303  
304 0 ▶sub _handle_error {
Subroutine _handle_error: not called CC=2 SCAR=17.9
305  
306 0 my $self = shift;
307 0 my $fcgi = shift;
308 0 my $error = shift;
309  
310 0 local $@;
311  
312 0 eval {
313  
314 0 my $message;
315  
316 0 ▶ if ( defined $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) { } 0 0 0
317 0 $message = "$error";
318 0 $message =~ s/\n*$//;
319 0 $message =~ s/\n/\\n/g;
320 }
321 else {
322 0 $message = 'Unknown error';
323 }
324  
325 0 print STDERR "[" . $self->application . "] [error] Uncaught exception: $message\n";
326  
327 };
328  
329 0 return;
330  
331 }
332  
333 0 ▶sub _handle_reload_error {
Subroutine _handle_reload_error: not called CC=2 SCAR=17.9
334  
335 0 my $package = shift;
336 0 my $application_class = shift;
337 0 my $fcgi = shift;
338 0 my $error = shift;
339  
340 0 eval {
341  
342 0 my $message;
343  
344 0 ▶ if ( defined $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) { } 0 0 0
345 0 $message = "$error";
346 0 $message =~ s/\n*$//;
347 0 $message =~ s/\n/\\n/g;
348 }
349 else {
350 0 $message = 'Unknown error';
351 }
352  
353 0 print STDERR "[$application_class] [fatal] $message\n";
354 0 print STDERR "[$application_class] [fatal] Reload failed, exiting ...";
355  
356 0 my $content = $package->_render_reload_error( $error );
357  
358 0 my $headers = GX::HTTP::Headers->new;
359 0 $headers->set( 'Content-Type' => 'text/html; charset=UTF-8' );
360 0 $headers->set( 'Content-Length' => length $content );
361 0 $headers->set( 'Status' => 500 );
362  
363 0 print STDOUT $headers->as_string, CRLF, $content;
364  
365 };
366  
367 0 CORE::exit( 1 );
368  
369 }
370  
371  
372 # ----------------------------------------------------------------------------------------------------------------------
373 # I/O stream adaptors
374 # ----------------------------------------------------------------------------------------------------------------------
375  
376 package GX::Engine::FCGI::InputStream;
377  
378 1 ▶use GX::Class::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
379  
380 1 build;
381  
382 0 ▶sub read {
Subroutine read: not called CC=1 SCAR=6.9
383  
384 0 return read( STDIN, $_[1], $_[2] );
385  
386 }
387  
388  
389 package GX::Engine::FCGI::OutputStream;
390  
391 1 ▶use GX::Class::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
392  
393 1 build;
394  
395 0 ▶sub flush {
Subroutine flush: not called CC=1 SCAR=6.9
396  
397 0 my $fh = select STDOUT;
398  
399 0 my $hot = $|;
400  
401 0 $| = 1;
402  
403 0 print STDOUT '';
404  
405 0 $| = $hot;
406  
407 0 select $fh;
408  
409 0 return 1;
410  
411 }
412  
413 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
414  
415 0 shift;
416  
417 0 return print( STDOUT @_ );
418  
419 }
420  
421  
422 package GX::Engine::FCGI::ErrorStream;
423  
424 1 ▶use GX::Class::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
425  
426 1 build;
427  
428 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
429  
430 0 shift;
431  
432 0 return print( STDERR @_ );
433  
434 }
435  
436  
437 1 1;
438  
439 __END__
« GX/View/Error.pm ↑ Summary GX/Engine/Apache2.pm »