You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Christian Hoermann <0o...@gmail.com> on 2007/06/30 03:56:14 UTC
200 Response on die
Hello everyone,
I'm wondering whether the following is intended behavior. Calling die
inside the mod_perl handler doesn't clear the part of the response
generated to that point. In other words, the HTML error document is
simply appended to anything that has been sent to $r->print()
previously and the whole thing arrives as a 200 Response.
I initially thought that the problem was that the first part of the
response is sent before the call to die(). However, after adding the
sleep statement, I see nothing in the browser for 30 seconds.
## Here's the code for the handler:
package test1;
use strict;
use warnings;
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile => qw(OK);
sub handler {
my $r = shift;
$r->content_type('text/plain');
$r->print("Hello World! \$| is $|.");
sleep(30);
die('just testing');
return Apache2::Const::OK; # or SERVER_ERROR - makes no
difference after die
}
1;
## Here's the response I get back (after 30 secs of nothing):
[chris@localhost ~]$ lynx 'http://localhost/test1'
Hello World! $| is 0.<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>200 OK</title>
</head><body>
<h1>OK</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator,
root@localhost and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>
## And the header, just fyi (also shows version numbers):
[chris@localhost ~]$ lynx -head 'http://localhost/test1'
HTTP/1.1 200 OK
Date: Sat, 30 Jun 2007 01:43:45 GMT
Server: Apache/2.2.4 (Fedora) DAV/2 PHP/5.2.2 mod_python/3.3.1
Python/2.5 mod_s
sl/2.2.4 OpenSSL/0.9.8b mod_apreq2-20051231/2.6.1 mod_perl/2.0.3 Perl/v5.8.8
Connection: close
Content-Type: text/plain; charset=UTF-8
## And a small part from my httpd.conf
<Location /test1>
SetHandler modperl
PerlResponseHandler test1
</Location>
So, is this intended behavior? I know that it can be fixed by storing
everything in a scalar and only having one print statement at the end,
but it seems odd that I don't recall reading anything about this in
any of the documentation. Thank you for reading this.
Re: 200 Response on die
Posted by Christian Hoermann <0o...@gmail.com>.
Hello Clint,
> Yes, it is intended behaviour.
thank you for clarifying this.
> What I do is keep all of my content in a single variable, and once
> pretty much everything that could die has finished, I
> $r->print($content) as the last action by my handler.
> All the previous code is wrapped in an eval, so I can catch any die's
> and send a custom error page instead of $content.
That sounds like a really good way to do it; I was thinking of doing
it like that too. Combining this with a good exception handling module
will allow for some helpful and more specific error pages.
> Also, have a read of these docs:
> http://perl.apache.org/docs/2.0/user/coding/coding.html#Integration_with_Apache_Issues
Thanks for the link, I've had a read of it and most of the other
documentation on the site. It's good that there's quite a bit of
detailed documentation for mod_perl.
Best Regards,
Christian
Re: 200 Response on die
Posted by Clinton Gormley <cl...@traveljury.com>.
Hi Christian
Yes, it is intended behaviour.
Basically, by the time that your script dies, it is too late to send the
correct headers to indicate a server error.
What I do is keep all of my content in a single variable, and once
pretty much everything that could die has finished, I
$r->print($content) as the last action by my handler.
All the previous code is wrapped in an eval, so I can catch any die's
and send a custom error page instead of $content.
Also, have a read of these docs:
http://perl.apache.org/docs/2.0/user/coding/coding.html#Integration_with_Apache_Issues
Clint