Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
10 KiB

  1. package CGI::Carp;
  2. =head1 NAME
  3. B<CGI::Carp> - CGI routines for writing to the HTTPD (or other) error log
  4. =head1 SYNOPSIS
  5. use CGI::Carp;
  6. croak "We're outta here!";
  7. confess "It was my fault: $!";
  8. carp "It was your fault!";
  9. warn "I'm confused";
  10. die "I'm dying.\n";
  11. use CGI::Carp qw(cluck);
  12. cluck "I wouldn't do that if I were you";
  13. use CGI::Carp qw(fatalsToBrowser);
  14. die "Fatal error messages are now sent to browser";
  15. =head1 DESCRIPTION
  16. CGI scripts have a nasty habit of leaving warning messages in the error
  17. logs that are neither time stamped nor fully identified. Tracking down
  18. the script that caused the error is a pain. This fixes that. Replace
  19. the usual
  20. use Carp;
  21. with
  22. use CGI::Carp
  23. And the standard warn(), die (), croak(), confess() and carp() calls
  24. will automagically be replaced with functions that write out nicely
  25. time-stamped messages to the HTTP server error log.
  26. For example:
  27. [Fri Nov 17 21:40:43 1995] test.pl: I'm confused at test.pl line 3.
  28. [Fri Nov 17 21:40:43 1995] test.pl: Got an error message: Permission denied.
  29. [Fri Nov 17 21:40:43 1995] test.pl: I'm dying.
  30. =head1 REDIRECTING ERROR MESSAGES
  31. By default, error messages are sent to STDERR. Most HTTPD servers
  32. direct STDERR to the server's error log. Some applications may wish
  33. to keep private error logs, distinct from the server's error log, or
  34. they may wish to direct error messages to STDOUT so that the browser
  35. will receive them.
  36. The C<carpout()> function is provided for this purpose. Since
  37. carpout() is not exported by default, you must import it explicitly by
  38. saying
  39. use CGI::Carp qw(carpout);
  40. The carpout() function requires one argument, which should be a
  41. reference to an open filehandle for writing errors. It should be
  42. called in a C<BEGIN> block at the top of the CGI application so that
  43. compiler errors will be caught. Example:
  44. BEGIN {
  45. use CGI::Carp qw(carpout);
  46. open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or
  47. die("Unable to open mycgi-log: $!\n");
  48. carpout(LOG);
  49. }
  50. carpout() does not handle file locking on the log for you at this point.
  51. The real STDERR is not closed -- it is moved to SAVEERR. Some
  52. servers, when dealing with CGI scripts, close their connection to the
  53. browser when the script closes STDOUT and STDERR. SAVEERR is used to
  54. prevent this from happening prematurely.
  55. You can pass filehandles to carpout() in a variety of ways. The "correct"
  56. way according to Tom Christiansen is to pass a reference to a filehandle
  57. GLOB:
  58. carpout(\*LOG);
  59. This looks weird to mere mortals however, so the following syntaxes are
  60. accepted as well:
  61. carpout(LOG);
  62. carpout(main::LOG);
  63. carpout(main'LOG);
  64. carpout(\LOG);
  65. carpout(\'main::LOG');
  66. ... and so on
  67. FileHandle and other objects work as well.
  68. Use of carpout() is not great for performance, so it is recommended
  69. for debugging purposes or for moderate-use applications. A future
  70. version of this module may delay redirecting STDERR until one of the
  71. CGI::Carp methods is called to prevent the performance hit.
  72. =head1 MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW
  73. If you want to send fatal (die, confess) errors to the browser, ask to
  74. import the special "fatalsToBrowser" subroutine:
  75. use CGI::Carp qw(fatalsToBrowser);
  76. die "Bad error here";
  77. Fatal errors will now be echoed to the browser as well as to the log. CGI::Carp
  78. arranges to send a minimal HTTP header to the browser so that even errors that
  79. occur in the early compile phase will be seen.
  80. Nonfatal errors will still be directed to the log file only (unless redirected
  81. with carpout).
  82. =head2 Changing the default message
  83. By default, the software error message is followed by a note to
  84. contact the Webmaster by e-mail with the time and date of the error.
  85. If this message is not to your liking, you can change it using the
  86. set_message() routine. This is not imported by default; you should
  87. import it on the use() line:
  88. use CGI::Carp qw(fatalsToBrowser set_message);
  89. set_message("It's not a bug, it's a feature!");
  90. You may also pass in a code reference in order to create a custom
  91. error message. At run time, your code will be called with the text
  92. of the error message that caused the script to die. Example:
  93. use CGI::Carp qw(fatalsToBrowser set_message);
  94. BEGIN {
  95. sub handle_errors {
  96. my $msg = shift;
  97. print "<h1>Oh gosh</h1>";
  98. print "Got an error: $msg";
  99. }
  100. set_message(\&handle_errors);
  101. }
  102. In order to correctly intercept compile-time errors, you should call
  103. set_message() from within a BEGIN{} block.
  104. =head1 CHANGE LOG
  105. 1.05 carpout() added and minor corrections by Marc Hedlund
  106. <[email protected]> on 11/26/95.
  107. 1.06 fatalsToBrowser() no longer aborts for fatal errors within
  108. eval() statements.
  109. 1.08 set_message() added and carpout() expanded to allow for FileHandle
  110. objects.
  111. 1.09 set_message() now allows users to pass a code REFERENCE for
  112. really custom error messages. croak and carp are now
  113. exported by default. Thanks to Gunther Birznieks for the
  114. patches.
  115. 1.10 Patch from Chris Dean (ctdean@cogit.com) to allow
  116. module to run correctly under mod_perl.
  117. 1.11 Changed order of &gt; and &lt; escapes.
  118. 1.12 Changed die() on line 217 to CORE::die to avoid B<-w> warning.
  119. 1.13 Added cluck() to make the module orthogonal with Carp.
  120. More mod_perl related fixes.
  121. =head1 AUTHORS
  122. Copyright 1995-1998, Lincoln D. Stein. All rights reserved.
  123. This library is free software; you can redistribute it and/or modify
  124. it under the same terms as Perl itself.
  125. Address bug reports and comments to: lstein@cshl.org
  126. =head1 SEE ALSO
  127. Carp, CGI::Base, CGI::BasePlus, CGI::Request, CGI::MiniSvr, CGI::Form,
  128. CGI::Response
  129. =cut
  130. require 5.000;
  131. use Exporter;
  132. use Carp;
  133. @ISA = qw(Exporter);
  134. @EXPORT = qw(confess croak carp);
  135. @EXPORT_OK = qw(carpout fatalsToBrowser wrap set_message cluck);
  136. $main::SIG{__WARN__}=\&CGI::Carp::warn;
  137. $main::SIG{__DIE__}=\&CGI::Carp::die;
  138. $CGI::Carp::VERSION = '1.13';
  139. $CGI::Carp::CUSTOM_MSG = undef;
  140. # fancy import routine detects and handles 'errorWrap' specially.
  141. sub import {
  142. my $pkg = shift;
  143. my(%routines);
  144. grep($routines{$_}++,@_,@EXPORT);
  145. $WRAP++ if $routines{'fatalsToBrowser'} || $routines{'wrap'};
  146. my($oldlevel) = $Exporter::ExportLevel;
  147. $Exporter::ExportLevel = 1;
  148. Exporter::import($pkg,keys %routines);
  149. $Exporter::ExportLevel = $oldlevel;
  150. }
  151. # These are the originals
  152. sub realwarn { CORE::warn(@_); }
  153. sub realdie { CORE::die(@_); }
  154. sub id {
  155. my $level = shift;
  156. my($pack,$file,$line,$sub) = caller($level);
  157. my($id) = $file=~m|([^/]+)$|;
  158. return ($file,$line,$id);
  159. }
  160. sub stamp {
  161. my $time = scalar(localtime);
  162. my $frame = 0;
  163. my ($id,$pack,$file);
  164. do {
  165. $id = $file;
  166. ($pack,$file) = caller($frame++);
  167. } until !$file;
  168. ($id) = $id=~m|([^/]+)$|;
  169. return "[$time] $id: ";
  170. }
  171. sub warn {
  172. my $message = shift;
  173. my($file,$line,$id) = id(1);
  174. $message .= " at $file line $line.\n" unless $message=~/\n$/;
  175. my $stamp = stamp;
  176. $message=~s/^/$stamp/gm;
  177. realwarn $message;
  178. }
  179. # The mod_perl package Apache::Registry loads CGI programs by calling
  180. # eval, as does PerlEx. These evals don't count when looking at the
  181. # stack backtrace.
  182. sub _longmess {
  183. my $message = Carp::longmess();
  184. my $mod_perl = exists $ENV{MOD_PERL};
  185. my $PerlEx = exists($ENV{'GATEWAY_INTERFACE'}) && $ENV{'GATEWAY_INTERFACE'} =~ /^CGI-PerlEx/;
  186. $message =~ s,eval[^\n]+(Apache/Registry\.pm|\s*PerlEx::Precompiler).*,,s if $mod_perl || $PerlEx;
  187. return( $message );
  188. }
  189. sub die {
  190. my $message = shift;
  191. my $time = scalar(localtime);
  192. my($file,$line,$id) = id(1);
  193. $message .= " at $file line $line." unless $message=~/\n$/;
  194. &fatalsToBrowser($message) if $WRAP && _longmess() !~ /eval [{\']/m;
  195. my $stamp = stamp;
  196. $message=~s/^/$stamp/gm;
  197. realdie $message;
  198. }
  199. sub set_message {
  200. $CGI::Carp::CUSTOM_MSG = shift;
  201. return $CGI::Carp::CUSTOM_MSG;
  202. }
  203. # Avoid generating "subroutine redefined" warnings with the following
  204. # hack:
  205. {
  206. local $^W=0;
  207. eval <<EOF;
  208. sub confess { CGI::Carp::die Carp::longmess \@_; }
  209. sub croak { CGI::Carp::die Carp::shortmess \@_; }
  210. sub carp { CGI::Carp::warn Carp::shortmess \@_; }
  211. sub cluck { CGI::Carp::warn Carp::longmess \@_; }
  212. EOF
  213. ;
  214. }
  215. # We have to be ready to accept a filehandle as a reference
  216. # or a string.
  217. sub carpout {
  218. my($in) = @_;
  219. my($no) = fileno(to_filehandle($in));
  220. realdie("Invalid filehandle $in\n") unless defined $no;
  221. open(SAVEERR, ">&STDERR");
  222. open(STDERR, ">&$no") or
  223. ( print SAVEERR "Unable to redirect STDERR: $!\n" and exit(1) );
  224. }
  225. # headers
  226. sub fatalsToBrowser {
  227. my($msg) = @_;
  228. $msg=~s/&/&amp;/g;
  229. $msg=~s/>/&gt;/g;
  230. $msg=~s/</&lt;/g;
  231. $msg=~s/\"/&quot;/g;
  232. my($wm) = $ENV{SERVER_ADMIN} ?
  233. qq[the webmaster (<a href="mailto:$ENV{SERVER_ADMIN}">$ENV{SERVER_ADMIN}</a>)] :
  234. "this site's webmaster";
  235. my ($outer_message) = <<END;
  236. For help, please send mail to $wm, giving this error message
  237. and the time and date of the error.
  238. END
  239. ;
  240. my $mod_perl = exists $ENV{MOD_PERL};
  241. my $PerlEx = exists($ENV{'GATEWAY_INTERFACE'}) && $ENV{'GATEWAY_INTERFACE'} =~ /^CGI-PerlEx/;
  242. print STDOUT "Content-type: text/html\n\n"
  243. unless $mod_perl || $PerlEx;
  244. if ($CUSTOM_MSG) {
  245. if (ref($CUSTOM_MSG) eq 'CODE') {
  246. &$CUSTOM_MSG($msg); # nicer to perl 5.003 users
  247. return;
  248. } else {
  249. $outer_message = $CUSTOM_MSG;
  250. }
  251. }
  252. my $mess = <<END;
  253. <H1>Software error:</H1>
  254. <CODE>$msg</CODE>
  255. <P>
  256. $outer_message
  257. END
  258. ;
  259. if ($mod_perl) {
  260. my $r = Apache->request;
  261. # If bytes have already been sent, then
  262. # we print the message out directly.
  263. # Otherwise we make a custom error
  264. # handler to produce the doc for us.
  265. if ($r->bytes_sent) {
  266. $r->print($mess);
  267. $r->exit;
  268. } else {
  269. $r->status(500);
  270. $r->custom_response(500,$mess);
  271. }
  272. } else {
  273. print STDOUT $mess;
  274. }
  275. }
  276. # Cut and paste from CGI.pm so that we don't have the overhead of
  277. # always loading the entire CGI module.
  278. sub to_filehandle {
  279. my $thingy = shift;
  280. return undef unless $thingy;
  281. return $thingy if UNIVERSAL::isa($thingy,'GLOB');
  282. return $thingy if UNIVERSAL::isa($thingy,'FileHandle');
  283. if (!ref($thingy)) {
  284. my $caller = 1;
  285. while (my $package = caller($caller++)) {
  286. my($tmp) = $thingy=~/[\':]/ ? $thingy : "$package\:\:$thingy";
  287. return $tmp if defined(fileno($tmp));
  288. }
  289. }
  290. return undef;
  291. }
  292. 1;