Leaked source code of windows server 2003
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.

421 lines
12 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 CGI::Carp::SAVEERR. Some
  52. servers, when dealing with CGI scripts, close their connection to the
  53. browser when the script closes STDOUT and STDERR. CGI::Carp::SAVEERR is there 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 MAKING WARNINGS APPEAR AS HTML COMMENTS
  105. It is now also possible to make non-fatal errors appear as HTML
  106. comments embedded in the output of your program. To enable this
  107. feature, export the new "warningsToBrowser" subroutine. Since sending
  108. warnings to the browser before the HTTP headers have been sent would
  109. cause an error, any warnings are stored in an internal buffer until
  110. you call the warningsToBrowser() subroutine with a true argument:
  111. use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
  112. use CGI qw(:standard);
  113. print header();
  114. warningsToBrowser(1);
  115. You may also give a false argument to warningsToBrowser() to prevent
  116. warnings from being sent to the browser while you are printing some
  117. content where HTML comments are not allowed:
  118. warningsToBrowser(0); # disable warnings
  119. print "<SCRIPT type=javascript><!--\n";
  120. print_some_javascript_code();
  121. print "//--></SCRIPT>\n";
  122. warningsToBrowser(1); # re-enable warnings
  123. Note: In this respect warningsToBrowser() differs fundamentally from
  124. fatalsToBrowser(), which you should never call yourself!
  125. =head1 CHANGE LOG
  126. 1.05 carpout() added and minor corrections by Marc Hedlund
  127. <[email protected]> on 11/26/95.
  128. 1.06 fatalsToBrowser() no longer aborts for fatal errors within
  129. eval() statements.
  130. 1.08 set_message() added and carpout() expanded to allow for FileHandle
  131. objects.
  132. 1.09 set_message() now allows users to pass a code REFERENCE for
  133. really custom error messages. croak and carp are now
  134. exported by default. Thanks to Gunther Birznieks for the
  135. patches.
  136. 1.10 Patch from Chris Dean (ctdean@cogit.com) to allow
  137. module to run correctly under mod_perl.
  138. 1.11 Changed order of &gt; and &lt; escapes.
  139. 1.12 Changed die() on line 217 to CORE::die to avoid B<-w> warning.
  140. 1.13 Added cluck() to make the module orthogonal with Carp.
  141. More mod_perl related fixes.
  142. 1.20 Patch from Ilmari Karonen (perl@itz.pp.sci.fi): Added
  143. warningsToBrowser(). Replaced <CODE> tags with <PRE> in
  144. fatalsToBrowser() output.
  145. =head1 AUTHORS
  146. Copyright 1995-1998, Lincoln D. Stein. All rights reserved.
  147. This library is free software; you can redistribute it and/or modify
  148. it under the same terms as Perl itself.
  149. Address bug reports and comments to: lstein@cshl.org
  150. =head1 SEE ALSO
  151. Carp, CGI::Base, CGI::BasePlus, CGI::Request, CGI::MiniSvr, CGI::Form,
  152. CGI::Response
  153. =cut
  154. require 5.000;
  155. use Exporter;
  156. use Carp;
  157. @ISA = qw(Exporter);
  158. @EXPORT = qw(confess croak carp);
  159. @EXPORT_OK = qw(carpout fatalsToBrowser warningsToBrowser wrap set_message cluck);
  160. $main::SIG{__WARN__}=\&CGI::Carp::warn;
  161. $main::SIG{__DIE__}=\&CGI::Carp::die;
  162. $CGI::Carp::VERSION = '1.20';
  163. $CGI::Carp::CUSTOM_MSG = undef;
  164. # fancy import routine detects and handles 'errorWrap' specially.
  165. sub import {
  166. my $pkg = shift;
  167. my(%routines);
  168. grep($routines{$_}++,@_,@EXPORT);
  169. $WRAP++ if $routines{'fatalsToBrowser'} || $routines{'wrap'};
  170. $WARN++ if $routines{'warningsToBrowser'};
  171. my($oldlevel) = $Exporter::ExportLevel;
  172. $Exporter::ExportLevel = 1;
  173. Exporter::import($pkg,keys %routines);
  174. $Exporter::ExportLevel = $oldlevel;
  175. }
  176. # These are the originals
  177. sub realwarn { CORE::warn(@_); }
  178. sub realdie { CORE::die(@_); }
  179. sub id {
  180. my $level = shift;
  181. my($pack,$file,$line,$sub) = caller($level);
  182. my($id) = $file=~m|([^/]+)$|;
  183. return ($file,$line,$id);
  184. }
  185. sub stamp {
  186. my $time = scalar(localtime);
  187. my $frame = 0;
  188. my ($id,$pack,$file);
  189. do {
  190. $id = $file;
  191. ($pack,$file) = caller($frame++);
  192. } until !$file;
  193. ($id) = $id=~m|([^/]+)$|;
  194. return "[$time] $id: ";
  195. }
  196. sub warn {
  197. my $message = shift;
  198. my($file,$line,$id) = id(1);
  199. $message .= " at $file line $line.\n" unless $message=~/\n$/;
  200. _warn($message) if $WARN;
  201. my $stamp = stamp;
  202. $message=~s/^/$stamp/gm;
  203. realwarn $message;
  204. }
  205. sub _warn {
  206. my $msg = shift;
  207. if ($EMIT_WARNINGS) {
  208. # We need to mangle the message a bit to make it a valid HTML
  209. # comment. This is done by substituting similar-looking ISO
  210. # 8859-1 characters for <, > and -. This is a hack.
  211. $msg =~ tr/<>-/\253\273\255/;
  212. chomp $msg;
  213. print STDOUT "<!-- warning: $msg -->\n";
  214. } else {
  215. push @WARNINGS, $msg;
  216. }
  217. }
  218. sub ineval { _longmess() =~ /eval [\{\']/m }
  219. # The mod_perl package Apache::Registry loads CGI programs by calling
  220. # eval. These evals don't count when looking at the stack backtrace.
  221. sub _longmess {
  222. my $message = Carp::longmess();
  223. my $mod_perl = exists $ENV{MOD_PERL};
  224. $message =~ s,eval[^\n]+Apache/Registry\.pm.*,,s if $mod_perl;
  225. return $message;
  226. }
  227. sub die {
  228. realdie @_ if ineval;
  229. my ($message) = @_;
  230. my $time = scalar(localtime);
  231. my($file,$line,$id) = id(1);
  232. $message .= " at $file line $line." unless $message=~/\n$/;
  233. &fatalsToBrowser($message) if $WRAP;
  234. my $stamp = stamp;
  235. $message=~s/^/$stamp/gm;
  236. realdie $message;
  237. }
  238. sub set_message {
  239. $CGI::Carp::CUSTOM_MSG = shift;
  240. return $CGI::Carp::CUSTOM_MSG;
  241. }
  242. # Avoid generating "subroutine redefined" warnings with the following
  243. # hack:
  244. {
  245. local $^W=0;
  246. eval <<EOF;
  247. sub confess { CGI::Carp::die Carp::longmess \@_; }
  248. sub croak { CGI::Carp::die Carp::shortmess \@_; }
  249. sub carp { CGI::Carp::warn Carp::shortmess \@_; }
  250. sub cluck { CGI::Carp::warn Carp::longmess \@_; }
  251. EOF
  252. ;
  253. }
  254. # We have to be ready to accept a filehandle as a reference
  255. # or a string.
  256. sub carpout {
  257. my($in) = @_;
  258. my($no) = fileno(to_filehandle($in));
  259. realdie("Invalid filehandle $in\n") unless defined $no;
  260. open(SAVEERR, ">&STDERR");
  261. open(STDERR, ">&$no") or
  262. ( print SAVEERR "Unable to redirect STDERR: $!\n" and exit(1) );
  263. }
  264. sub warningsToBrowser {
  265. $EMIT_WARNINGS = @_ ? shift : 1;
  266. _warn(shift @WARNINGS) while $EMIT_WARNINGS and @WARNINGS;
  267. }
  268. # headers
  269. sub fatalsToBrowser {
  270. my($msg) = @_;
  271. $msg=~s/&/&amp;/g;
  272. $msg=~s/>/&gt;/g;
  273. $msg=~s/</&lt;/g;
  274. $msg=~s/\"/&quot;/g;
  275. my($wm) = $ENV{SERVER_ADMIN} ?
  276. qq[the webmaster (<a href="mailto:$ENV{SERVER_ADMIN}">$ENV{SERVER_ADMIN}</a>)] :
  277. "this site's webmaster";
  278. my ($outer_message) = <<END;
  279. For help, please send mail to $wm, giving this error message
  280. and the time and date of the error.
  281. END
  282. ;
  283. my $mod_perl = exists $ENV{MOD_PERL};
  284. print STDOUT "Content-type: text/html\n\n"
  285. unless $mod_perl;
  286. warningsToBrowser(1); # emit warnings before dying
  287. if ($CUSTOM_MSG) {
  288. if (ref($CUSTOM_MSG) eq 'CODE') {
  289. &$CUSTOM_MSG($msg); # nicer to perl 5.003 users
  290. return;
  291. } else {
  292. $outer_message = $CUSTOM_MSG;
  293. }
  294. }
  295. my $mess = <<END;
  296. <H1>Software error:</H1>
  297. <PRE>$msg</PRE>
  298. <P>
  299. $outer_message
  300. END
  301. ;
  302. if ($mod_perl && (my $r = Apache->request)) {
  303. # If bytes have already been sent, then
  304. # we print the message out directly.
  305. # Otherwise we make a custom error
  306. # handler to produce the doc for us.
  307. if ($r->bytes_sent) {
  308. $r->print($mess);
  309. $r->exit;
  310. } else {
  311. $r->status(500);
  312. $r->custom_response(500,$mess);
  313. }
  314. } else {
  315. print STDOUT $mess;
  316. }
  317. }
  318. # Cut and paste from CGI.pm so that we don't have the overhead of
  319. # always loading the entire CGI module.
  320. sub to_filehandle {
  321. my $thingy = shift;
  322. return undef unless $thingy;
  323. return $thingy if UNIVERSAL::isa($thingy,'GLOB');
  324. return $thingy if UNIVERSAL::isa($thingy,'FileHandle');
  325. if (!ref($thingy)) {
  326. my $caller = 1;
  327. while (my $package = caller($caller++)) {
  328. my($tmp) = $thingy=~/[\':]/ ? $thingy : "$package\:\:$thingy";
  329. return $tmp if defined(fileno($tmp));
  330. }
  331. }
  332. return undef;
  333. }
  334. 1;