CGI/Carp.pm

filter: Statementstmt 97.5% 156 / 160 Branchbran 80.2% 77 / 96 Conditioncond 50.0% 16 / 32 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 100% 25 / 25 Podpod 0.0% 0 / 15 totaltotal 80.1% 279 / 348 CC 59 SCAR 22.1
CC
59
Cov
86%
fatalsToBrowser
20.2
die
13.4
to_filehandle
8.1
CRAP 9.1
« CGI/Push.pm ↑ Summary CGI/Cookie.pm »
1 package CGI::Carp;
2  
3 3 my $appease_cpants_kwalitee = q/
4 use strict;
5 use warnings;
6 #/;
7  
8 =head1 NAME
9  
10 B<CGI::Carp> - CGI routines for writing to the HTTPD (or other) error log
11  
12 =head1 SYNOPSIS
13  
14 use CGI::Carp;
15  
16 croak "We're outta here!";
17 confess "It was my fault: $!";
18 carp "It was your fault!";
19 warn "I'm confused";
20 die "I'm dying.\n";
21  
22 use CGI::Carp qw(cluck);
23 cluck "I wouldn't do that if I were you";
24  
25 use CGI::Carp qw(fatalsToBrowser);
26 die "Fatal error messages are now sent to browser";
27  
28 =head1 DESCRIPTION
29  
30 CGI scripts have a nasty habit of leaving warning messages in the error
31 logs that are neither time stamped nor fully identified. Tracking down
32 the script that caused the error is a pain. This fixes that. Replace
33 the usual
34  
35 use Carp;
36  
37 with
38  
39 use CGI::Carp
40  
41 The standard warn(), die (), croak(), confess() and carp() calls will
42 be replaced with functions that write time-stamped messages to the
43 HTTP server error log.
44  
45 For example:
46  
47 [Fri Nov 17 21:40:43 1995] test.pl: I'm confused at test.pl line 3.
48 [Fri Nov 17 21:40:43 1995] test.pl: Got an error message: Permission denied.
49 [Fri Nov 17 21:40:43 1995] test.pl: I'm dying.
50  
51 =head1 REDIRECTING ERROR MESSAGES
52  
53 By default, error messages are sent to STDERR. Most HTTPD servers
54 direct STDERR to the server's error log. Some applications may wish
55 to keep private error logs, distinct from the server's error log, or
56 they may wish to direct error messages to STDOUT so that the browser
57 will receive them.
58  
59 The C<carpout()> function is provided for this purpose. Since
60 carpout() is not exported by default, you must import it explicitly by
61 saying
62  
63 use CGI::Carp qw(carpout);
64  
65 The carpout() function requires one argument, a reference to an open
66 filehandle for writing errors. It should be called in a C<BEGIN>
67 block at the top of the CGI application so that compiler errors will
68 be caught. Example:
69  
70 BEGIN {
71 use CGI::Carp qw(carpout);
72 open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or
73 die("Unable to open mycgi-log: $!\n");
74 carpout(LOG);
75 }
76  
77 carpout() does not handle file locking on the log for you at this
78 point. Also, note that carpout() does not work with in-memory file
79 handles, although a patch would be welcome to address that.
80  
81 The real STDERR is not closed -- it is moved to CGI::Carp::SAVEERR.
82 Some servers, when dealing with CGI scripts, close their connection to
83 the browser when the script closes STDOUT and STDERR.
84 CGI::Carp::SAVEERR is there to prevent this from happening
85 prematurely.
86  
87 You can pass filehandles to carpout() in a variety of ways. The "correct"
88 way according to Tom Christiansen is to pass a reference to a filehandle
89 GLOB:
90  
91 carpout(\*LOG);
92  
93 This looks weird to mere mortals however, so the following syntaxes are
94 accepted as well:
95  
96 carpout(LOG);
97 carpout(main::LOG);
98 carpout(main'LOG);
99 carpout(\LOG);
100 carpout(\'main::LOG');
101  
102 ... and so on
103  
104 FileHandle and other objects work as well.
105  
106 Use of carpout() is not great for performance, so it is recommended
107 for debugging purposes or for moderate-use applications. A future
108 version of this module may delay redirecting STDERR until one of the
109 CGI::Carp methods is called to prevent the performance hit.
110  
111 =head1 MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW
112  
113 If you want to send fatal (die, confess) errors to the browser, import
114 the special "fatalsToBrowser" subroutine:
115  
116 use CGI::Carp qw(fatalsToBrowser);
117 die "Bad error here";
118  
119 Fatal errors will now be echoed to the browser as well as to the log.
120 CGI::Carp arranges to send a minimal HTTP header to the browser so
121 that even errors that occur in the early compile phase will be seen.
122 Nonfatal errors will still be directed to the log file only (unless
123 redirected with carpout).
124  
125 Note that fatalsToBrowser may B<not> work well with mod_perl version 2.0
126 and higher.
127  
128 =head2 Changing the default message
129  
130 By default, the software error message is followed by a note to
131 contact the Webmaster by e-mail with the time and date of the error.
132 If this message is not to your liking, you can change it using the
133 set_message() routine. This is not imported by default; you should
134 import it on the use() line:
135  
136 use CGI::Carp qw(fatalsToBrowser set_message);
137 set_message("It's not a bug, it's a feature!");
138  
139 You may also pass in a code reference in order to create a custom
140 error message. At run time, your code will be called with the text
141 of the error message that caused the script to die. Example:
142  
143 use CGI::Carp qw(fatalsToBrowser set_message);
144 BEGIN {
145 sub handle_errors {
146 my $msg = shift;
147 print "<h1>Oh gosh</h1>";
148 print "<p>Got an error: $msg</p>";
149 }
150 set_message(\&handle_errors);
151 }
152  
153 In order to correctly intercept compile-time errors, you should call
154 set_message() from within a BEGIN{} block.
155  
156 =head1 DOING MORE THAN PRINTING A MESSAGE IN THE EVENT OF PERL ERRORS
157  
158 If fatalsToBrowser in conjunction with set_message does not provide
159 you with all of the functionality you need, you can go one step
160 further by specifying a function to be executed any time a script
161 calls "die", has a syntax error, or dies unexpectedly at runtime
162 with a line like "undef->explode();".
163  
164 use CGI::Carp qw(set_die_handler);
165 BEGIN {
166 sub handle_errors {
167 my $msg = shift;
168 print "content-type: text/html\n\n";
169 print "<h1>Oh gosh</h1>";
170 print "<p>Got an error: $msg</p>";
171  
172 #proceed to send an email to a system administrator,
173 #write a detailed message to the browser and/or a log,
174 #etc....
175 }
176 set_die_handler(\&handle_errors);
177 }
178  
179 Notice that if you use set_die_handler(), you must handle sending
180 HTML headers to the browser yourself if you are printing a message.
181  
182 If you use set_die_handler(), you will most likely interfere with
183 the behavior of fatalsToBrowser, so you must use this or that, not
184 both.
185  
186 Using set_die_handler() sets SIG{__DIE__} (as does fatalsToBrowser),
187 and there is only one SIG{__DIE__}. This means that if you are
188 attempting to set SIG{__DIE__} yourself, you may interfere with
189 this module's functionality, or this module may interfere with
190 your module's functionality.
191  
192 =head1 SUPPRESSING PERL ERRORS APPEARING IN THE BROWSER WINDOW
193  
194 A problem sometimes encountered when using fatalsToBrowser is
195 when a C<die()> is done inside an C<eval> body or expression.
196 Even though the
197 fatalsToBrower support takes precautions to avoid this,
198 you still may get the error message printed to STDOUT.
199 This may have some undesirable effects when the purpose of doing the
200 eval is to determine which of several algorithms is to be used.
201  
202 By setting C<$CGI::Carp::TO_BROWSER> to 0 you can suppress printing
203 the C<die> messages but without all of the complexity of using
204 C<set_die_handler>. You can localize this effect to inside C<eval>
205 bodies if this is desirable: For example:
206  
207 eval {
208 local $CGI::Carp::TO_BROWSER = 0;
209 die "Fatal error messages not sent browser"
210 }
211 # $@ will contain error message
212  
213  
214 =head1 MAKING WARNINGS APPEAR AS HTML COMMENTS
215  
216 It is also possible to make non-fatal errors appear as HTML comments
217 embedded in the output of your program. To enable this feature,
218 export the new "warningsToBrowser" subroutine. Since sending warnings
219 to the browser before the HTTP headers have been sent would cause an
220 error, any warnings are stored in an internal buffer until you call
221 the warningsToBrowser() subroutine with a true argument:
222  
223 use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
224 use CGI qw(:standard);
225 print header();
226 warningsToBrowser(1);
227  
228 You may also give a false argument to warningsToBrowser() to prevent
229 warnings from being sent to the browser while you are printing some
230 content where HTML comments are not allowed:
231  
232 warningsToBrowser(0); # disable warnings
233 print "<script type=\"text/javascript\"><!--\n";
234 print_some_javascript_code();
235 print "//--></script>\n";
236 warningsToBrowser(1); # re-enable warnings
237  
238 Note: In this respect warningsToBrowser() differs fundamentally from
239 fatalsToBrowser(), which you should never call yourself!
240  
241 In persistent environments (mod_perl, FastCGI, PSGI) the buffered warnings
242 and the warningsToBrowser() setting are reset whenever
243 C<CGI::initialize_globals()> is called, as it is between requests by mod_perl
244 and CGI::Fast. Prior to v5.00 they were not, so warnings from one request
245 could be emitted into the output of a later request. If you use CGI::Carp in
246 a persistent environment without CGI.pm call
247 C<CGI::Carp::_reset_globals()> at the start of each request.
248  
249 =head1 OVERRIDING THE NAME OF THE PROGRAM
250  
251 CGI::Carp includes the name of the program that generated the error or
252 warning in the messages written to the log and the browser window.
253 Sometimes, Perl can get confused about what the actual name of the
254 executed program was. In these cases, you can override the program
255 name that CGI::Carp will use for all messages.
256  
257 The quick way to do that is to tell CGI::Carp the name of the program
258 in its use statement. You can do that by adding
259 "name=cgi_carp_log_name" to your "use" statement. For example:
260  
261 use CGI::Carp qw(name=cgi_carp_log_name);
262  
263 . If you want to change the program name partway through the program,
264 you can use the C<set_progname()> function instead. It is not
265 exported by default, you must import it explicitly by saying
266  
267 use CGI::Carp qw(set_progname);
268  
269 Once you've done that, you can change the logged name of the program
270 at any time by calling
271  
272 set_progname(new_program_name);
273  
274 You can set the program back to the default by calling
275  
276 set_progname(undef);
277  
278 Note that this override doesn't happen until after the program has
279 compiled, so any compile-time errors will still show up with the
280 non-overridden program name
281  
282 =head1 TURNING OFF TIMESTAMPS IN MESSAGES
283  
284 If your web server automatically adds a timestamp to each log line,
285 you may not need CGI::Carp to add its own. You can disable timestamping
286 by importing "noTimestamp":
287  
288 use CGI::Carp qw(noTimestamp);
289  
290 Alternatively you can set C<$CGI::Carp::NO_TIMESTAMP> to 1.
291  
292 Note that the name of the program is still automatically included in
293 the message.
294  
295 =head1 GETTING THE FULL PATH OF THE SCRIPT IN MESSAGES
296  
297 Set C<$CGI::Carp::FULL_PATH> to 1.
298  
299 =head1 AUTHOR INFORMATION
300  
301 The CGI.pm distribution is copyright 1995-2007, Lincoln D. Stein. It is
302 distributed under the Artistic License 2.0. It is currently
303 maintained by Lee Johnson with help from many contributors.
304  
305 Address bug reports and comments to: https://github.com/leejo/CGI.pm/issues
306  
307 The original bug tracker can be found at: https://rt.cpan.org/Public/Dist/Display.html?Queue=CGI.pm
308  
309 When sending bug reports, please provide the version of CGI.pm, the version of
310 Perl, the name and version of your Web server, and the name and version of the
311 operating system you are using. If the problem is even remotely browser
312 dependent, please provide information about the affected browsers as well.
313  
314 =head1 SEE ALSO
315  
316 L<Carp>, L<CGI::Base>, L<CGI::BasePlus>, L<CGI::Request>,
317 L<CGI::MiniSvr>, L<CGI::Form>, L<CGI::Response>.
318  
319 =cut
320  
321 3 require 5.008001;
322 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
323 #use Carp;
324 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
325 3 require Carp;
326 3 *CORE::GLOBAL::die = \&CGI::Carp::die;
327 }
328  
329 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
330  
331 3 @ISA = qw(Exporter);
332 3 @EXPORT = qw(confess croak carp);
333 3 @EXPORT_OK = qw(carpout fatalsToBrowser warningsToBrowser wrap noTimestamp set_message set_die_handler set_progname cluck ^name= die);
334  
335 3 $main::SIG{__WARN__}=\&CGI::Carp::warn;
336  
337 3 $CGI::Carp::VERSION = '5.02';
338 3 $CGI::Carp::CUSTOM_MSG = undef;
339 3 $CGI::Carp::DIE_HANDLER = undef;
340 3 $CGI::Carp::TO_BROWSER = 1;
341 3 $CGI::Carp::NO_TIMESTAMP= 0;
342 3 $CGI::Carp::FULL_PATH = 0;
343  
344 # fancy import routine detects and handles 'errorWrap' specially.
345 5 ▶sub import {
Subroutine import: called CC=7 SCAR=19.7
346 5 my $pkg = shift;
347 5 my(%routines);
348 my(@name);
349 5 ▶ if (@name=grep(/^name=/,@_))
Branch: 1 branch
BranchTrueFalseTotal
if (@name = grep(/^name=/, @_)) 1 4 5
350 {
351 1 my($n) = (split(/=/,$name[0]))[1];
352 1 set_progname($n);
353 1 @_=grep(!/^name=/,@_);
354 }
355  
356 5 grep($routines{$_}++,@_,@EXPORT);
357 5 ▶ $WRAP++ if $routines{'fatalsToBrowser'} || $routines{'wrap'};
Branch: 1 branch
BranchTrueFalseTotal
if $routines{"fatalsToBrowser"} or $routines{"wrap"} 0 5 5
Condition33%1/3
$routines{"fatalsToBrowser"} or $routines{"wrap"}
l!l&&r!l&&!r
005
MC/DC0%0/2
$routines{"fatalsToBrowser"} or $routines{"wrap"}
$routines{"fatalsToBrowser"} $routines{"wrap"}
Truth table33%1/3
A or B
A= $routines{"fatalsToBrowser"}B= $routines{"wrap"}
ABresult
1X1
011
000
358 5 ▶ $WARN++ if $routines{'warningsToBrowser'};
Branch: 1 branch
BranchTrueFalseTotal
if $routines{"warningsToBrowser"} 1 4 5
359 5 my($oldlevel) = $Exporter::ExportLevel;
360 5 $Exporter::ExportLevel = 1;
361 5 Exporter::import($pkg,keys %routines);
362 5 $Exporter::ExportLevel = $oldlevel;
363 5 ▶ $main::SIG{__DIE__} =\&CGI::Carp::die if $routines{'fatalsToBrowser'};
Branch: 1 branch
BranchTrueFalseTotal
if $routines{"fatalsToBrowser"} 0 5 5
364 5 ▶ $CGI::Carp::NO_TIMESTAMP = 1 if $routines{'noTimestamp'};
Branch: 1 branch
BranchTrueFalseTotal
if $routines{"noTimestamp"} 1 4 5
365 }
366  
367 # These are the originals
368 8 ▶sub realwarn { CORE::warn(@_); }
Subroutine realwarn: called CC=1 SCAR=0.0
Pod undocumented
369 12 ▶sub realdie { CORE::die(@_); }
Subroutine realdie: called CC=1 SCAR=0.0
370  
371 36 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
372 36 my $level = shift;
373 36 my($pack,$file,$line,$sub) = caller($level);
374 36 my($dev,$dirs,$id) = File::Spec->splitpath($file);
375 36 return ($file,$line,$id);
376 }
377  
378 24 ▶sub stamp {
Subroutine stamp: called CC=5 SCAR=16.1
Pod undocumented
379 24 my $frame = 0;
380 24 my ($id,$pack,$file,$dev,$dirs);
381 24 ▶ if (defined($CGI::Carp::PROGNAME)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PROGNAME) { } 0 24 24
382 0 $id = $CGI::Carp::PROGNAME;
383 } else {
384 24 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
not $file or do { do { $id = $file; () = caller $frame++ } } 24 62 86
385 86 $id = $file;
386 86 ($pack,$file) = caller($frame++);
387 } until !$file;
388 }
389 24 ▶ if (! $CGI::Carp::FULL_PATH) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($FULL_PATH) 23 1 24
390 23 ($dev,$dirs,$id) = File::Spec->splitpath($id);
391 }
392 24 ▶ return "$id: " if $CGI::Carp::NO_TIMESTAMP;
Branch: 1 branch
BranchTrueFalseTotal
if $NO_TIMESTAMP 2 22 24
393 22 my $time = scalar(localtime);
394 22 return "[$time] $id: ";
395 }
396  
397 3 ▶sub set_progname {
Subroutine set_progname: called CC=1 SCAR=0.0
Pod undocumented
398 3 $CGI::Carp::PROGNAME = shift;
399 3 return $CGI::Carp::PROGNAME;
400 }
401  
402  
403 12 ▶sub warn {
Subroutine warn: called CC=3 SCAR=11.0
404 12 my $message = shift;
405 12 my($file,$line,$id) = id(1);
406 12 ▶ $message .= " at $file line $line.\n" unless $message=~/\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ /\n$/ 5 7 12
407 12 ▶ _warn($message) if $WARN;
Branch: 1 branch
BranchTrueFalseTotal
if $WARN 9 3 12
408 12 my $stamp = stamp;
409 12 $message=~s/^/$stamp/gm;
410 12 realwarn $message;
411 }
412  
413 14 ▶sub _warn {
Subroutine _warn: called CC=2 SCAR=6.9
414 14 my $msg = shift;
415 14 ▶ if ($EMIT_WARNINGS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($EMIT_WARNINGS) { } 5 9 14
416 # We need to mangle the message a bit to make it a valid HTML
417 # comment. This is done by substituting similar-looking ISO
418 # 8859-1 characters for <, > and -. This is a hack.
419 5 $msg =~ tr/<>-/\253\273\255/;
420 5 chomp $msg;
421 5 print STDOUT "<!-- warning: $msg -->\n";
422 } else {
423 9 push @WARNINGS, $msg;
424 }
425 }
426  
427  
428 # The mod_perl package Apache::Registry loads CGI programs by calling
429 # eval. These evals don't count when looking at the stack backtrace.
430 10 ▶sub _longmess {
Subroutine _longmess: called CC=2 SCAR=7.1
431 10 my $message = Carp::longmess();
432 $message =~ s,eval[^\n]+(ModPerl|Apache)/(?:Registry|Dispatch)\w*\.pm.*,,s
433 10 ▶ if exists $ENV{MOD_PERL};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"MOD_PERL"} 0 10 10
434 10 return $message;
435 }
436  
437 23 ▶sub ineval {
Subroutine ineval: called CC=3 SCAR=11.2
Pod undocumented
438 23 ▶ (exists $ENV{MOD_PERL} ? 0 : $^S) || _longmess() =~ /eval [\{\']/m
Branch: 2 branches
BranchTrueFalseTotal
exists $ENV{"MOD_PERL"} ? 0 : $^S or _longmess() =~ /eval [\{\']/m 13 10 23
exists $ENV{'MOD_PERL'} ? : 0 23 23
439 }
440  
441 21 ▶sub die {
Subroutine die: called CC=13 SCAR=25.9
442 # if no argument is passed, propagate $@ like
443 # the real die
444 21 ▶ my ($arg,@rest) = @_ ? @_
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 18 3 21
$@ ? : 1 2 3
445 : $@ ? "$@\t...propagated"
446 : "Died"
447 ;
448  
449 21 ▶ &$DIE_HANDLER($arg,@rest) if $DIE_HANDLER;
Branch: 1 branch
BranchTrueFalseTotal
if $DIE_HANDLER 0 21 21
450  
451 # the "$arg" is done on purpose!
452 # if called as die( $object, 'string' ),
453 # all is stringified, just like with
454 # the real 'die'
455 21 ▶ $arg = join '' => "$arg", @rest if @rest;
Branch: 1 branch
BranchTrueFalseTotal
if @rest 2 19 21
456  
457 21 my($file,$line,$id) = id(1);
458  
459 21 ▶ $arg .= " at $file line $line.\n" unless ref $arg or $arg=~/\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg or $arg =~ /\n$/ 11 10 21
Condition66%2/3
ref $arg or $arg =~ /\n$/
l!l&&r!l&&!r
10011
MC/DC50%1/2
ref $arg or $arg =~ /\n$/
ref $arg $arg =~ /\n$/
Truth table66%2/3
A or B
A= ref $argB= $arg =~ /\n$/
ABresult
1X1
011
000
460  
461 21 ▶ realdie $arg if ineval();
Branch: 1 branch
BranchTrueFalseTotal
if ineval() 12 9 21
462 9 ▶ &fatalsToBrowser($arg) if ($WRAP and $CGI::Carp::TO_BROWSER);
Branch: 1 branch
BranchTrueFalseTotal
if $WRAP and $TO_BROWSER 8 1 9
Condition66%2/3
$WRAP and $TO_BROWSER
!ll&&!rl&&r
108
MC/DC50%1/2
$WRAP and $TO_BROWSER
$WRAP $TO_BROWSER
Truth table66%2/3
A and B
A= $WRAPB= $TO_BROWSER
ABresult
0X0
100
111
463  
464 9 ▶ $arg=~s/^/ stamp() /gme if $arg =~ /\n$/ or not exists $ENV{MOD_PERL};
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\n$/ or not exists $ENV{"MOD_PERL"} 9 0 9
Condition66%2/3
$arg =~ /\n$/ or not exists $ENV{"MOD_PERL"}
l!l&&r!l&&!r
450
MC/DC0%0/2
$arg =~ /\n$/ or not exists $ENV{"MOD_PERL"}
$arg =~ /\n$/ not exists $ENV{"MOD_PERL"}
Truth table66%2/3
A or not(B)
A= $arg =~ /\n$/B= not exists $ENV{"MOD_PERL"}
ABresult
1X1
011
000
465  
466 9 ▶ $arg .= "\n" unless $arg =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /\n$/ 5 4 9
467  
468 9 realdie $arg;
469 }
470  
471 5 ▶sub set_message {
Subroutine set_message: called CC=1 SCAR=0.0
Pod undocumented
472 5 $CGI::Carp::CUSTOM_MSG = shift;
473 5 return $CGI::Carp::CUSTOM_MSG;
474 }
475  
476 1 ▶sub set_die_handler {
Subroutine set_die_handler: called CC=1 SCAR=0.0
Pod undocumented
477  
478 1 my ($handler) = shift;
479
480 #setting SIG{__DIE__} here is necessary to catch runtime
481 #errors which are not called by literally saying "die",
482 #such as the line "undef->explode();". however, doing this
483 #will interfere with fatalsToBrowser, which also sets
484 #SIG{__DIE__} in the import() function above (or the
485 #import() function above may interfere with this). for
486 #this reason, you should choose to either set the die
487 #handler here, or use fatalsToBrowser, not both.
488 1 $main::SIG{__DIE__} = $handler;
489
490 1 $CGI::Carp::DIE_HANDLER = $handler;
491
492 1 return $CGI::Carp::DIE_HANDLER;
493 }
494  
495 1 ▶sub confess { CGI::Carp::die Carp::longmess @_; }
Subroutine confess: called CC=1 SCAR=0.0
Pod undocumented
496 1 ▶sub croak { CGI::Carp::die Carp::shortmess @_; }
Subroutine croak: called CC=1 SCAR=0.0
Pod undocumented
497 1 ▶sub carp { CGI::Carp::warn Carp::shortmess @_; }
Subroutine carp: called CC=1 SCAR=0.0
Pod undocumented
498 1 ▶sub cluck { CGI::Carp::warn Carp::longmess @_; }
Subroutine cluck: called CC=1 SCAR=0.0
Pod undocumented
499  
500 # We have to be ready to accept a filehandle as a reference
501 # or a string.
502 1 ▶sub carpout {
Subroutine carpout: called CC=4 SCAR=16.6
Pod undocumented
503 1 my($in) = @_;
504 1 my($no) = fileno(to_filehandle($in));
505 1 ▶ realdie("Invalid filehandle $in\n") unless defined $no;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $no 0 1 1
506
507 1 open(SAVEERR, ">&STDERR");
508 1 ▶ open(STDERR, ">&$no") or
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&$no" or print SAVEERR "Unable to redirect >&${no}: $!\n" and exit 1 1 0 1
Condition20%1/5
open(STDERR, ">&$no") || print(SAVEERR "Unable to redirect >&${no}: $!\n") && exit 1
l!l&&r!l&&!r
100
print SAVEERR "Unable to redirect >&${no}: $!\n" and exit 1
!ll
00
MC/DC0%0/2
open(STDERR, ">&$no") || print(SAVEERR "Unable to redirect >&${no}: $!\n") && exit 1
open(STDERR, ">&$no") print SAVEERR "Unable to redirect >&${no}: $!\n"
Truth table33%1/3
A || B
A= open(STDERR, ">&$no")B= print SAVEERR "Unable to redirect >&${no}: $!\n"
ABresult
1X1
011
000
509 ( print SAVEERR "Unable to redirect >&$no: $!\n" and exit(1) );
510 }
511  
512 18 ▶sub warningsToBrowser {
Subroutine warningsToBrowser: called CC=4 SCAR=13.9
Pod undocumented
513 18 ▶ $EMIT_WARNINGS = @_ ? shift : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 18 0 18
514 18 ▶ _warn(shift @WARNINGS) while $EMIT_WARNINGS and @WARNINGS;
Branch: 1 branch
BranchTrueFalseTotal
$EMIT_WARNINGS and @WARNINGS and do { _warn(shift @WARNINGS) } 5 18 23
Condition100%3/3
$EMIT_WARNINGS and @WARNINGS
!ll&&!rl&&r
1175
MC/DC100%2/2
$EMIT_WARNINGS and @WARNINGS
$EMIT_WARNINGS @WARNINGS
Truth table100%3/3
A and B
A= $EMIT_WARNINGSB= @WARNINGS
ABresult
0X0
100
111
515 }
516  
517 # Reset per-request state. This is called by CGI::initialize_globals() (as
518 # run between requests by mod_perl, CGI::Fast, etc) so that warnings from
519 # one request are never emitted into another request's output, and so that
520 # warnings aren't printed before the headers of the next request.
521 7 ▶sub _reset_globals {
Subroutine _reset_globals: called CC=1 SCAR=0.0
522 7 @WARNINGS = ();
523 7 $EMIT_WARNINGS = 0;
524 }
525  
526 # headers
527 15 ▶sub fatalsToBrowser {
Subroutine fatalsToBrowser: called CC=18 SCAR=30.0
Pod undocumented
528 15 my $msg = shift;
529  
530 15 ▶ $msg = "$msg" if ref $msg;
Branch: 1 branch
BranchTrueFalseTotal
if ref $msg 5 10 15
531  
532 15 $msg=~s/&/&amp;/g;
533 15 $msg=~s/>/&gt;/g;
534 15 $msg=~s/</&lt;/g;
535 15 $msg=~s/"/&quot;/g;
536  
537 my($wm) = $ENV{SERVER_ADMIN} ?
538 15 ▶ qq[the webmaster (<a href="mailto:$ENV{SERVER_ADMIN}">$ENV{SERVER_ADMIN}</a>)] :
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'SERVER_ADMIN'} ? : 3 12 15
539 "this site's webmaster";
540 15 my ($outer_message) = <<END;
541 For help, please send mail to $wm, giving this error message
542 and the time and date of the error.
543 END
544 ;
545 15 my $mod_perl = exists $ENV{MOD_PERL};
546  
547 15 ▶ if ($CUSTOM_MSG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CUSTOM_MSG) 2 13 15
548 2 ▶ if (ref($CUSTOM_MSG) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $CUSTOM_MSG eq 'CODE') { } 1 1 2
549 1 ▶ print STDOUT "Content-type: text/html\n\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $mod_perl 1 0 1
550 unless $mod_perl;
551 1 eval {
552 1 &$CUSTOM_MSG($msg); # nicer to perl 5.003 users
553 };
554 0 ▶ if ($@) { print STDERR qq(error while executing the error handler: $@); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
555  
556 1 return;
557 } else {
558 1 $outer_message = $CUSTOM_MSG;
559 }
560 }
561  
562 14 my $mess = <<END;
563 <h1>Software error:</h1>
564 <pre>$msg</pre>
565 <p>
566 $outer_message
567 </p>
568 END
569 ;
570  
571 14 ▶ if ($mod_perl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod_perl) { } 2 12 14
572 2 my $r;
573 2 ▶ if ($ENV{MOD_PERL_API_VERSION} && $ENV{MOD_PERL_API_VERSION} == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'MOD_PERL_API_VERSION'} and $ENV{'MOD_PERL_API_VERSION'} == 2) { } 1 1 2
Condition66%2/3
$ENV{"MOD_PERL_API_VERSION"} and $ENV{"MOD_PERL_API_VERSION"} == 2
!ll&&!rl&&r
011
MC/DC50%1/2
$ENV{"MOD_PERL_API_VERSION"} and $ENV{"MOD_PERL_API_VERSION"} == 2
$ENV{"MOD_PERL_API_VERSION"} $ENV{"MOD_PERL_API_VERSION"} == 2
Truth table66%2/3
A and B
A= $ENV{"MOD_PERL_API_VERSION"}B= $ENV{"MOD_PERL_API_VERSION"} == 2
ABresult
0X0
100
111
574 1 $mod_perl = 2;
575 1 require Apache2::RequestRec;
576 1 require Apache2::RequestIO;
577 1 require Apache2::RequestUtil;
578 1 require APR::Pool;
579 1 require ModPerl::Util;
580 1 require Apache2::Response;
581 1 $r = Apache2::RequestUtil->request;
582 }
583 else {
584 1 $r = Apache->request;
585 }
586 # If bytes have already been sent, then
587 # we print the message out directly.
588 # Otherwise we make a custom error
589 # handler to produce the doc for us.
590 2 ▶ if ($r->bytes_sent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->bytes_sent) { } 1 1 2
591 1 $r->print($mess);
592 1 ▶ $mod_perl == 2 ? ModPerl::Util::exit(0) : $r->exit;
Branch: 1 branch
BranchTrueFalseTotal
$mod_perl == 2 ? : 1 0 1
593 } else {
594 # MSIE won't display a custom 500 response unless it is >512 bytes!
595 1 ▶ if (defined($ENV{HTTP_USER_AGENT}) && $ENV{HTTP_USER_AGENT} =~ /MSIE/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"HTTP_USER_AGENT"} and $ENV{"HTTP_USER_AGENT"} =~ /MSIE/) 1 0 1
Condition33%1/3
defined $ENV{"HTTP_USER_AGENT"} and $ENV{"HTTP_USER_AGENT"} =~ /MSIE/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $ENV{"HTTP_USER_AGENT"} and $ENV{"HTTP_USER_AGENT"} =~ /MSIE/
defined $ENV{"HTTP_USER_AGENT"} $ENV{"HTTP_USER_AGENT"} =~ /MSIE/
Truth table33%1/3
A and B
A= defined $ENV{"HTTP_USER_AGENT"}B= $ENV{"HTTP_USER_AGENT"} =~ /MSIE/
ABresult
0X0
100
111
596 1 $mess = "<!-- " . (' ' x 513) . " -->\n$mess";
597 }
598 1 $r->custom_response(500,$mess);
599 }
600 } else {
601 12 my $bytes_written = eval{tell STDOUT};
602 12 ▶ if (defined $bytes_written && $bytes_written > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes_written and $bytes_written > 0) { } 0 12 12
Condition33%1/3
defined $bytes_written and $bytes_written > 0
!ll&&!rl&&r
1200
MC/DC0%0/2
defined $bytes_written and $bytes_written > 0
defined $bytes_written $bytes_written > 0
Truth table33%1/3
A and B
A= defined $bytes_writtenB= $bytes_written > 0
ABresult
0X0
100
111
603 0 print STDOUT $mess;
604 }
605 else {
606 12 print STDOUT "Status: 500\n";
607 12 print STDOUT "Content-type: text/html\n\n";
608 # MSIE won't display a custom 500 response unless it is >512 bytes!
609 12 ▶ if (defined($ENV{HTTP_USER_AGENT}) && $ENV{HTTP_USER_AGENT} =~ /MSIE/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"HTTP_USER_AGENT"} and $ENV{"HTTP_USER_AGENT"} =~ /MSIE/) 0 12 12
Condition33%1/3
defined $ENV{"HTTP_USER_AGENT"} and $ENV{"HTTP_USER_AGENT"} =~ /MSIE/
!ll&&!rl&&r
1200
MC/DC0%0/2
defined $ENV{"HTTP_USER_AGENT"} and $ENV{"HTTP_USER_AGENT"} =~ /MSIE/
defined $ENV{"HTTP_USER_AGENT"} $ENV{"HTTP_USER_AGENT"} =~ /MSIE/
Truth table33%1/3
A and B
A= defined $ENV{"HTTP_USER_AGENT"}B= $ENV{"HTTP_USER_AGENT"} =~ /MSIE/
ABresult
0X0
100
111
610 0 $mess = "<!-- " . (' ' x 513) . " -->\n$mess";
611 }
612 12 print STDOUT $mess;
613 }
614 }
615  
616 14 warningsToBrowser(1); # emit warnings before dying
617 }
618  
619 # Cut and paste from CGI.pm so that we don't have the overhead of
620 # always loading the entire CGI module.
621 6 ▶sub to_filehandle {
Subroutine to_filehandle: called CC=8 SCAR=20.9
Pod undocumented
622 6 my $thingy = shift;
623 6 ▶ return undef unless $thingy;
Branch: 1 branch
BranchTrueFalseTotal
unless $thingy 0 6 6
624 6 ▶ return $thingy if UNIVERSAL::isa($thingy,'GLOB');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($thingy, "GLOB") 3 3 6
625 3 ▶ return $thingy if UNIVERSAL::isa($thingy,'FileHandle');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($thingy, "FileHandle") 0 3 3
626 3 ▶ if (!ref($thingy)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $thingy) 3 0 3
627 3 my $caller = 1;
628 3 ▶ while (my $package = caller($caller++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $package = caller $caller++) 3 1 4
629 3 ▶ my($tmp) = $thingy=~/[\':]/ ? $thingy : "$package\:\:$thingy";
Branch: 1 branch
BranchTrueFalseTotal
$thingy =~ /[\':]/ ? : 2 1 3
630 3 ▶ return $tmp if defined(fileno($tmp));
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $tmp 2 1 3
631 }
632 }
633 1 return undef;
634 }
635  
636 3 1;
« CGI/Push.pm ↑ Summary CGI/Cookie.pm »