File Coverage

blib/lib/PAB3/CGI.pm
Criterion Covered Total %
statement 32 351 9.1
branch 8 190 4.2
condition 6 99 6.0
subroutine 9 29 31.0
pod 12 17 70.5
total 67 686 9.7


line stmt bran cond sub pod time code
1             package PAB3::CGI;
2             # =============================================================================
3             # Perl Application Builder
4             # Module: PAB3::CGI
5             # Use "perldoc PAB3::CGI" for documenation
6             # =============================================================================
7              
8 2         314 use vars qw(
9             $VERSION %HEAD $FIRSTRUN
10             %_GET %_POST %_REQUEST %_COOKIE %_FILES
11             $HeaderDone $Logger @CleanupHandler
12             $MPartBufferSize $MaxBoundary $UploadFileDir $SaveToFile $RequestMaxData
13             $TempDir
14 2     2   1482 );
  2         4  
15              
16 2     2   11 use Carp ();
  2         3  
  2         27  
17 2     2   1964 use Time::HiRes ();
  2         4284  
  2         63  
18              
19 2     2   15 use strict;
  2         2  
  2         61  
20 2     2   8 no strict 'refs';
  2         4  
  2         961  
21              
22             our @EXPORT_VAR = qw(
23             %_GET %_POST %_REQUEST %_FILES %_COOKIE
24             );
25             our @EXPORT_SUB = qw(
26             &header &redirect &setcookie &print_r &print_var
27             &encode_uri &decode_uri &encode_uri_component &decode_uri_component
28             );
29             our @EXPORT_OK = ( @EXPORT_SUB, @EXPORT_VAR );
30             our @EXPORT = @EXPORT_VAR; # export variables by default
31             our %EXPORT_TAGS = (
32             # 'all' => \@EXPORT_OK,
33             'default' => \@EXPORT_OK,
34             # 'var' => \@EXPORT_VAR,
35             );
36             require Exporter;
37             *import = \&Exporter::import;
38              
39             BEGIN {
40 2     2   5 $VERSION = '2.0.1';
41 2         7 *print_r = \&print_var;
42 2         5 $GLOBAL::MPREQ = undef;
43 2         4 $GLOBAL::MODPERL = 0;
44 2 50 33     14 $GLOBAL::MODPERL = 2 if exists $ENV{'MOD_PERL_API_VERSION'}
45             && $ENV{'MOD_PERL_API_VERSION'} == 2;
46 2 0 33     28 $GLOBAL::MODPERL = 1 if ! $GLOBAL::MODPERL && exists $ENV{'MOD_PERL'}
      33        
      33        
47             && $Apache::VERSION > 1 && $Apache::VERSION < 1.99;
48 2 50 33     24 if( $GLOBAL::MODPERL == 2 ) {
    50 33        
    50          
    50          
49 0         0 require mod_perl2;
50 0         0 require Apache2::Module;
51 0         0 require Apache2::ServerUtil;
52 0         0 require Apache2::RequestUtil;
53 0         0 require APR::Pool;
54 0         0 require APR::Table;
55 0         0 require PAB3::CGI::Request;
56             }
57             elsif( $GLOBAL::MODPERL == 1 ) {
58 0         0 require Apache;
59 0         0 require Apache::Log;
60 0         0 require PAB3::CGI::Request;
61             }
62             elsif( exists $ENV{'GATEWAY_INTERFACE'}
63             && $ENV{'GATEWAY_INTERFACE'} eq 'CGI-PerlEx'
64             ) {
65 0         0 require PAB3::CGI::Request;
66             }
67             elsif( exists $ENV{'CONTENT_TYPE'}
68             && index( lc( $ENV{'CONTENT_TYPE'} ), 'multipart/form-data' ) >= 0
69             ) {
70 0         0 require PAB3::CGI::Request;
71             }
72             else {
73 2         3785 require PAB3::CGI::RequestStd;
74             }
75 2 50       11 if( $^O eq 'MSWin32' ) {
76 0 0       0 $TempDir = $ENV{'TEMP'}
77             ? ( $ENV{'TEMP'} . "\\" )
78             # CSIDL_WINDOWS (0x0024)
79             : ( &Win32::GetFolderPath( 0x0024 ) . "\\Temp\\" )
80             ;
81             }
82             else {
83 2         6 $TempDir = '/tmp/';
84             }
85 2         116 $FIRSTRUN = 1;
86             }
87              
88             END {
89 2 50   2   434 if( ! $GLOBAL::MODPERL ) {
90 2         9 &cleanup();
91             }
92             }
93              
94 2     2   1088 use PAB3::Output::CGI ();
  2         6  
  2         10410  
95              
96             1;
97              
98             sub _import {
99 0     0     my $pkg = shift;
100 0           my $callpkg = caller();
101 0 0 0       if( $_[0] and $pkg eq __PACKAGE__ and $_[0] eq 'import' ) {
      0        
102 0           *{$callpkg . '::import'} = \&import;
  0            
103 0           return;
104             }
105             # export symbols
106 0           foreach( @_ ) {
107 0 0         if( $_ eq ':default' ) {
108 0           *{$callpkg . '::' . $_} = \%{$pkg . '::' . $_} foreach @EXPORT_SUB;
  0            
  0            
109             }
110             }
111 0           *{$callpkg . '::' . $_} = \%{$pkg . '::' . $_} foreach @EXPORT_VAR;
  0            
  0            
112             }
113              
114             sub cleanup {
115 2 50   2 1   return if $FIRSTRUN;
116 0 0         if( %_FILES ) {
117 0           foreach( keys %_FILES ) {
118 0 0         unless( $_FILES{$_}->{'tmp_name'} ) {
119 0           next;
120             }
121 0           unlink( split( "\0", $_FILES{$_}->{'tmp_name'} ) );
122             }
123             }
124 0           undef %_GET;
125 0           undef %_POST;
126 0           undef %_REQUEST;
127 0           undef %_FILES;
128 0           undef %_COOKIE;
129 0           undef $HeaderDone;
130 0           undef %HEAD;
131 0           print ''; # untie stdout
132 0           $FIRSTRUN = 1;
133 0           my( $handler, $h, $ref );
134 0           foreach $h( @CleanupHandler ) {
135 0 0         if( ref( $h ) eq 'ARRAY' ) {
136 0           $handler = shift @$h;
137             }
138             else {
139 0           $handler = $h;
140 0           $h = [];
141             }
142 0 0         if( ( $ref = ref( $handler ) ) ) {
143 0 0         if( $ref eq 'CODE' ) {
144 0           eval{
145 0           local( $SIG{'__DIE__'}, $SIG{'__WARN__'} );
146 0           $handler->( @$h );
147             };
148             }
149             }
150             else {
151 0           eval{
152 0           local( $SIG{'__DIE__'}, $SIG{'__WARN__'} );
153 0           &{$handler}( @$h );
  0            
154             };
155             }
156             }
157 0           undef @CleanupHandler;
158 0 0         if( $PAB3::Statistic::VERSION ) {
159 0   0       &PAB3::Statistic::send(
      0        
160             'CSN|' . ( $GLOBAL::MPREQ || $$ )
161             . '|' . time
162             . '|' . µtime()
163             . '|' . ( $GLOBAL::STATUS || ( $GLOBAL::MPREQ ? $GLOBAL::MPREQ->status : 200 ) )
164             );
165             }
166 0           undef $GLOBAL::MPREQ;
167             }
168              
169             sub cleanup_register {
170 0     0 1   push @CleanupHandler, [ @_ ];
171             }
172              
173             sub setenv {
174 0 0 0 0 1   if( $ENV{'SCRIPT_FILENAME'}
    0          
175             && $ENV{'SCRIPT_FILENAME'} =~ /^(.+[\\\/])(.+?)$/
176             ) {
177 0           $ENV{'SCRIPT_PATH'} = $1;
178 0           $ENV{'SCRIPT'} = $2;
179             }
180             elsif( $0 =~ /^(.+[\\\/])(.+?)$/ ) {
181 0           $ENV{'SCRIPT_PATH'} = $1;
182 0           $ENV{'SCRIPT'} = $2;
183             }
184             else {
185 0           $ENV{'SCRIPT_PATH'} = '';
186 0           $ENV{'SCRIPT'} = $0;
187             }
188 0           my $hua = lc( $ENV{'HTTP_USER_AGENT'} );
189 0 0         if( index( $hua, 'win' ) >= 0 ) {
    0          
    0          
    0          
190 0           $ENV{'REMOTE_OS'} = 'windows'
191             }
192             elsif( index( $hua, 'linux' ) >= 0 ) {
193 0           $ENV{'REMOTE_OS'} = 'linux';
194             }
195             elsif( index( $hua, 'ppc' ) >= 0 ) {
196 0           $ENV{'REMOTE_OS'} = 'macos';
197             }
198             elsif( index( $hua, 'freebsd' ) >= 0 ) {
199 0           $ENV{'REMOTE_OS'} = 'freebsd';
200             }
201             else {
202 0           $ENV{'REMOTE_OS'} = 'unknown';
203             }
204             }
205              
206             sub set {
207 0     0 0   my( $index, $len );
208 0           $len = $#_ + 1;
209 0           for( $index = 0; $index < $len; $index += 2 ) {
210 0 0         if( $_[ $index ] eq 'request_max_size' ) {
    0          
    0          
    0          
    0          
    0          
    0          
211 0           $RequestMaxData = $_[ $index + 1 ];
212             }
213             elsif( $_[ $index ] eq 'mpart_buffer_size' ) {
214 0           $MPartBufferSize = $_[ $index + 1 ];
215             }
216             elsif( $_[ $index ] eq 'max_boundary' ) {
217 0           $MaxBoundary = $_[ $index + 1 ];
218             }
219             elsif( $_[ $index ] eq 'temp_dir' ) {
220 0           $UploadFileDir = $_[ $index + 1 ];
221             }
222             elsif( $_[ $index ] eq 'save_to_file' ) {
223 0           $SaveToFile = $_[ $index + 1 ];
224             }
225             elsif( $_[ $index ] eq 'logger' ) {
226 0           $Logger = $_[ $index + 1 ];
227             }
228             elsif( $_[ $index ] eq 'request' ) {
229 0           $GLOBAL::MPREQ = $_[ $index + 1 ];
230             }
231             else {
232             # &Carp::carp( 'Unknown parameter ' . $_[ $index ] );
233             }
234             }
235             }
236              
237             sub init {
238 0 0   0 1   &cleanup() if ! $FIRSTRUN;
239 0           $UploadFileDir = $TempDir;
240 0           $RequestMaxData = 131072;
241 0           $MPartBufferSize = 8192;
242 0           $MaxBoundary = 10;
243 0           $SaveToFile = 1;
244 0           $Logger = undef;
245 0           $GLOBAL::MPREQ = undef;
246 0           &set( @_ );
247 0 0         if( $FIRSTRUN ) {
248 0           $FIRSTRUN = 0;
249 0 0         if( $GLOBAL::MODPERL ) {
250 0 0         if( $GLOBAL::MODPERL == 2 ) {
    0          
251 0   0       $GLOBAL::MPREQ ||= Apache2::RequestUtil->request();
252 0           $GLOBAL::MPREQ->pool->cleanup_register( \&cleanup );
253 0 0         if( $GLOBAL::MPREQ->handler() eq 'modperl' ) {
254 0           tie *STDIN, $GLOBAL::MPREQ;
255             }
256             }
257             elsif( $GLOBAL::MODPERL == 1 ) {
258 0   0       $GLOBAL::MPREQ ||= Apache->request();
259 0           $GLOBAL::MPREQ->register_cleanup( \&cleanup );
260             }
261 0 0         if( $PAB3::Statistic::VERSION ) {
262 0           my $r = $GLOBAL::MPREQ;
263 0           my $s = $r->server();
264 0 0         my $s2 = $GLOBAL::MODPERL == 2
265             ? Apache2::ServerUtil->server()
266             : $r->server()
267             ;
268 0           my $c = $r->connection();
269 0   0       &PAB3::Statistic::send(
      0        
270             'ISN|' . $r
271             . '|' . time
272             . '|' . µtime()
273             . '|' . $s->server_hostname
274             . '|' . ( $s->port || $s2->port )
275             . '|' . $s->is_virtual
276             . '|' . $r->document_root
277             . '|' . $r->uri
278             . '|' . ( $c->remote_host || $c->remote_ip )
279             . '|' . $GLOBAL::MODPERL
280             );
281             }
282             }
283             else {
284 0           my $iru = index( $ENV{'REQUEST_URI'}, '?' );
285 0 0         if( $PAB3::Statistic::VERSION ) {
286 0 0         &PAB3::Statistic::send(
287             'ISN|' . $$
288             . '|' . time
289             . '|' . µtime()
290             . '|' . $ENV{'SERVER_NAME'}
291             . '|' . $ENV{'SERVER_PORT'}
292             . '|' . '2'
293             . '|' . $ENV{'DOCUMENT_ROOT'}
294             . '|' . ( $iru > 0 ? substr( $ENV{'REQUEST_URI'}, 0, $iru ) : $ENV{'REQUEST_URI'} )
295             . '|' . $ENV{'REMOTE_ADDR'}
296             . '|' . '0'
297             );
298             }
299             }
300 0           %HEAD = ();
301 0           $HeaderDone = 0;
302 0           tie *STDOUT, 'PAB3::Output::CGI';
303 0           $SIG{'__DIE__'} = \&_die_handler;
304 0           $SIG{'__WARN__'} = \&_warn_handler;
305 0           &_parse_cookie();
306 0           &_parse_request();
307             }
308 0           return 1;
309             }
310              
311             sub setcookie {
312 0     0 1   my( $name, $value, $expire, $path, $domain, $secure ) = @_;
313 0 0         unless( $name ) {
314 0           &Carp::croak(
315             'Usage: setcookie( $name [, $value [, $expire [, $path [, $domain'
316             . ' [, $secure ]]]]] )'
317             );
318             }
319 0 0         if( $HeaderDone ) {
320 0           &Carp::carp(
321             'CGI Headers already sent at '
322             . $HeaderDone->[1] . ':' . $HeaderDone->[2]
323             );
324 0           return 0;
325             }
326 0 0         if( $domain ) {
327 0           my $suffix = substr( $domain, rindex( $domain, '.' ) + 1 );
328 0           my $len = length( $suffix );
329 0 0 0       if( $suffix !~ /\d{$len}|com|net|org/i && $domain !~ /^\./ ) {
330 0           $domain = '.' . $domain;
331             }
332             }
333 0 0 0       if( defined $expire && $expire > 0 ) {
334 0           my @t = split( / +/, gmtime( $expire ) );
335 0           push @t, split( /:/, $t[3] );
336 0           $expire = $t[0] . ', ' . $t[2] . '-' . $t[1] . '-' .
337             $t[4] . ' ' . $t[5] . ':' . $t[6] . ':' . $t[7] .
338             ' GMT';
339             }
340 0 0         if( $value ) {
341 0           $value =~ s/([^0-9A-z]{1})/"%".unpack("H2",$1)/ge;
  0            
342             }
343 0           $name =~ s/([^0-9A-z]{1})/"%".unpack("H2",$1)/ge;
  0            
344 0 0         &header(
    0          
    0          
    0          
    0          
    0          
345             'Set-Cookie: ' . $name . '='
346             . ( defined $value ? '"' . $value . '";' : ';' )
347             . ( defined $expire ? ' Expires="' . $expire . '";' : '' )
348             . ( $domain ? ' Domain="' . $domain . '";' : '' )
349             . ( $path ? ' Path="' . $path . '";' : '' )
350             . ( $secure ? ' Secure="1";' : '' )
351             . ' Version="1";'
352             . "\n\r"
353             ) or return 0;
354 0           return 1;
355             }
356              
357             sub redirect {
358 0     0 1   my( $location, $params, $internal ) = @_;
359 0 0         if( ! $location ) {
360 0           &Carp::croak(
361             'Usage: &PAB3::CGI::redirect( $location [, \%params [, $internal ] ] )'
362             );
363             }
364 0 0 0       if( defined $params && ref( $params ) eq 'HASH' ) {
365 0           my( $index );
366 0 0 0       if( $location && index( $location, '?' ) >= 0 ) {
367 0           $location .= '&';
368 0           $index = 1;
369             }
370             else {
371 0           $location .= '?';
372 0           $index = 0;
373             }
374 0           foreach( keys %$params ) {
375 0 0         $location .= '&' if $index ++ > 0;
376 0           $location .= $_ . '=' . &encode_uri_component( $params->{$_} );
377             }
378             }
379 0           &header( 'Status: 302 Moved' );
380 0 0 0       &header(
381             $internal && $GLOBAL::MPREQ
382             ? 'intredir: ' . $location
383             : 'Location: ' . $location
384             );
385 0           print '';
386 0           return 302;
387             }
388              
389             sub header {
390             # my( $header, $replace ) = @_;
391 0     0 1   my( $key, $val, $k );
392 0 0         if( $HeaderDone ) {
393 0           &Carp::carp(
394             'CGI Headers already sent at '
395             . $HeaderDone->[1] . ':' . $HeaderDone->[2]
396             );
397             }
398 0 0         if( $_[0] =~ m!^HTTP/\d+\.\d+\s+(\d+\s*.*)!i ) {
399 0           &header( "Status: $1", $_[1] );
400             }
401 0 0         ( $key, $val ) = $_[0] =~ m!^\s*([\w\-\_]+)\s*?\:\s*(.+)! or return;
402 0           $k = lc( $key );
403 0 0 0       if( ! defined $_[1] || $_[1] || ! defined $HEAD{$k} ) {
    0 0        
404 0           $HEAD{$k} = $val;
405             }
406             elsif( defined $HEAD{$k} ) {
407 0 0         $HEAD{$k} = [ $HEAD{$k} ] if ! ref( $HEAD{$k} );
408 0           push @{ $HEAD{$k} }, $val;
  0            
409             }
410             }
411              
412             sub print_hash {
413 0     0 0   my( $hashname, $ref_table, $level ) = @_;
414 0           my( $r_hash, $r, $k );
415 0   0       $ref_table ||= [];
416 0 0         if( $hashname =~ /HASH\(0x\w+\)/ ) {
417 0           $r_hash = $hashname;
418             }
419             else {
420 0           return;
421             }
422 0           print $r_hash;
423 0 0 0       if( $ref_table->{$r_hash} && $ref_table->{$r_hash} <= $level ) {
424 0           print " [recursive loop]\n";
425 0           return;
426             }
427 0           print "\n", " " x $level, "(\n";
428 0           $ref_table->{$r_hash} = $level + 1;
429 0           foreach $k( sort { lc( $a ) cmp lc( $b ) } keys %{ $r_hash } ) {
  0            
  0            
430 0           print " " x ( $level + 1 ) . "[$k] => ";
431 0           $r = ref( $r_hash->{$k} );
432 0 0 0       if( $r && index( $r_hash->{$k}, 'ARRAY(' ) >= 0 ) {
    0 0        
433 0           &print_array( $r_hash->{$k}, $ref_table, $level + 1 );
434             }
435             elsif( $r && index( $r_hash->{$k}, 'HASH(' ) >= 0 ) {
436 0           &print_hash( $r_hash->{$k}, $ref_table, $level + 1 );
437             }
438             else {
439 0 0         print ( ! defined $r_hash->{$k} ? '(null)' : $r_hash->{ $k } );
440 0           print "\n";
441             }
442             }
443 0           print " " x $level, ")\n";
444             }
445              
446             sub print_array {
447 0     0 0   my( $arrayname, $ref_table, $level ) = @_;
448 0           my( $r_array, $r, $v, $i );
449 0   0       $ref_table ||= {};
450 0   0       $level ||= 0;
451 0 0         if( $arrayname =~ /ARRAY\(0x\w+\)/ ) {
452 0           $r_array = $arrayname;
453             }
454             else {
455 0           return;
456             }
457 0           print $r_array;
458 0 0 0       if( $ref_table->{$r_array} && $ref_table->{$r_array} <= $level ) {
459 0           print " [recursive loop]\n";
460 0           return;
461             }
462 0           print "\n", " " x $level, "(\n";
463 0           $ref_table->{$r_array} = $level + 1;
464 0           $i = 0;
465 0           foreach $v( @{ $r_array } ) {
  0            
466 0           $r = ref( $v );
467 0           print " " x ( $level + 1 ) . "[$i] => ";
468 0 0 0       if( $r && index( $v, 'ARRAY(' ) >= 0 ) {
    0 0        
469 0           &print_array( $v, $ref_table, $level + 1 );
470             }
471             elsif( $r && index( $v, 'HASH(' ) >= 0 ) {
472 0           &print_hash( $v, $ref_table, $level + 1 );
473             }
474             else {
475 0 0         print "" . ( ! defined $v ? '(null)' : $v ) . "\n";
476             }
477 0           $i ++;
478             }
479 0           print " " x $level, ")\n";
480             }
481              
482             sub print_var {
483 0     0 1   my( $v, $r, $ref_table );
484 0           $ref_table = {};
485 0           print "
\n"; 
486 0           foreach $v( @_ ) {
487 0           $r = ref( $v );
488 0 0 0       if( $r && index( $v, 'ARRAY(' ) >= 0 ) {
    0 0        
    0 0        
489 0           &print_array( $v, $ref_table, 0 );
490             }
491             elsif( $r && index( $v, 'HASH(' ) >= 0 ) {
492 0           &print_hash( $v, $ref_table, 0 );
493             }
494             elsif( $r && index( $v, 'SCALAR(' ) >= 0 ) {
495 0           print $$v, "\n";
496             }
497             else {
498 0           print $v, "\n";
499             }
500             }
501 0           print "\n";
502             }
503              
504             sub print_code {
505 0     0 0   my( $content, $filename ) = @_;
506 0           my( $t, $l, $p );
507 0 0         return if ! defined $content;
508 0           $content =~ s/\r//go;
509 0           $content =~ s/
510 0           $content =~ s/>/>/go;
511             #$content =~ s/ / /go;
512 0           print "\n"; \n" if $filename; \n";
513 0 0         print "
$filename
514 0           print "
\n"; 
515 0           $p = 1;
516 0           foreach $l( split( /\n/, $content ) ) {
517 0           print $p . "\t" . $l . "\n";
518 0           $p ++;
519             }
520 0           print "
521 0           print "
\n";
522             }
523              
524             sub encode_uri($) {
525 0 0   0 1   my $s = $_[0] or return $_[0];
526 0           $s =~ s/([^A-Za-z0-9\-_.!~*\'()\,\/\?\:\@\&\=\+\$]{1})/sprintf('%%%02X',ord($1))/ge;
  0            
527 0           return $s;
528             }
529              
530             sub decode_uri($) {
531 0 0   0 1   my $s = $_[0] or return $_[0];
532 0           $s =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
  0            
533 0           return $s;
534             }
535              
536             sub encode_uri_component($) {
537 0 0   0 1   my $s = $_[0] or return $_[0];
538 0           $s =~ s/([^A-Za-z0-9\-_.!~*\'()]{1})/sprintf('%%%02X',ord($1))/ge;
  0            
539 0           return $s;
540             }
541              
542             sub decode_uri_component($) {
543 0 0   0 1   my $s = $_[0] or return $_[0];
544 0           $s =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
  0            
545 0           return $s;
546             }
547              
548             sub microtime {
549 0     0 0   my( $sec, $usec ) = &Time::HiRes::gettimeofday();
550 0           return $sec + $usec / 1000000;
551             }
552              
553             sub _parse_cookie {
554 0     0     my( $key, $val, $i, @in, $iv );
555 0           %_COOKIE = ();
556 0 0         return 1 unless defined $ENV{'HTTP_COOKIE'};
557 0           @in = split( /; */, $ENV{'HTTP_COOKIE'} );
558 0           for $i( 0 .. $#in ) {
559 0           $iv = index( $in[$i], '=' );
560 0 0         if( $iv > 0 ) {
561 0           $key = substr( $in[$i], 0, $iv );
562 0           $val = substr( $in[$i], $iv + 1 );
563 0           $key =~ tr/+/ /;
564 0           $key =~ s/%([A-Fa-f0-9]{2})/chr(hex($1))/ge;
  0            
565 0 0         if( $val ) {
566 0           $val =~ s!\"!!gso;
567             #$val =~ s/^\"(.+)\"$/$1/;
568 0           $val =~ tr/+/ /;
569 0           $val =~ s/%([A-Fa-f0-9]{2})/chr(hex($1))/ge;
  0            
570             }
571 0 0         $_COOKIE{ $key } = defined $_COOKIE{ $key } ? "\0" . $val : $val;
572             }
573             else {
574 0 0         $_COOKIE{ $in[$i] } .= defined $_COOKIE{ $in[$i] } ? "\0" : "";
575             }
576             }
577 0           return 1;
578             }
579              
580             sub _die_handler {
581 0     0     my $str = shift;
582 0           my( @c, $step );
583 0 0         if( $str =~ /(.+) at (.+) line (.+)$/s ) {
584 0           print "
\nFatal:\n"
585             . "

$1

\n"
586             . 'at ' . $2 . ' line ' . $3 . ''
587             . "
\n"
588             ;
589             }
590             else {
591 0           print "
\nFatal:\n"
592             . '

' . $str . "


\n"
593             ;
594             }
595 0           @c = caller();
596 0           print "
    \n";
597 0           print '
  • '
  • 598             . '' . $c[0] . ' raised the exception'
    599             . ' at ' . $c[1] . ' line ' . $c[2] . ''
    600             . "\n"
    601             ;
    602 0           $step = 1;
    603 0           while( @c = caller( $step ) ) {
    604 0           print '
  • '
  • 605             . '' . $c[0] . ' called ' . $c[3] . ''
    606             . ' at ' . $c[1] . ' line ' . $c[2] . ''
    607             . "\n"
    608             ;
    609 0           $step ++;
    610             }
    611 0           print "\n";
    612 0           print "
    \n";
    613 0           my $s = $str;
    614 0           $s =~ s!\n+$!!;
    615 0 0         if( $Logger ) {
    616 0           $Logger->error( $s );
    617             }
    618 0 0         if( $GLOBAL::MPREQ ) {
    619 0           $GLOBAL::MPREQ->log()->error( $s );
    620             #$GLOBAL::MPREQ->status( 500 );
    621 0           $GLOBAL::STATUS = 500;
    622 0 0         Apache::exit() if $GLOBAL::MODPERL == 1;
    623             }
    624             else {
    625 0           print STDERR '[error] Perl: ' . $str;
    626             }
    627             # return 500;
    628 0           exit( 0 );
    629             }
    630              
    631             sub _warn_handler {
    632 0     0     my $str = shift;
    633 0 0         if( $str =~ /(.+) at (.+) line (.+)$/s ) {
    634 0           print "
    \nWarning: $1\n"
    635             . 'at ' . $2 . ' line ' . $3 . ''
    636             . "\n
    \n"
    637             ;
    638             }
    639             else {
    640 0           print "
    \nWarning: $str

    \n
    \n";
    641             }
    642 0           my $s = $str;
    643 0           $s =~ s!\n+$!!;
    644 0 0         if( $Logger ) {
    645 0           $Logger->warn( $s );
    646             }
    647 0 0         if( $GLOBAL::MPREQ ) {
    648 0           $GLOBAL::MPREQ->log()->warn( $s );
    649             }
    650             else {
    651 0           print STDERR '[warn] Perl: ' . $str;
    652             }
    653             }
    654              
    655              
    656             __END__