mod_perl error returning 202 Apache2::Const::HTTP_ACCEPTED

443 views Asked by At

I am trying to build a simple asynchronous web service with Apache and mod_perl. But every time I try to return HTTP status 202 (Accepted), I get an error.

Below a very simple example (non-asynchronous):

package MyHandler;

use Apache2::Const '-compile' => qw 'OK HTTP_ACCEPTED HTTP_OK';
use Apache2::RequestRec;
use CGI;

sub handler {
        my $r = shift;
        print "Hallo";
        $r->content_type('text/plain');
        $r->status(Apache2::Const::HTTP_ACCEPTED);
        return Apache2::Const::HTTP_ACCEPTED;
}

1;

I get the error

calling the handler in my browser on localhost, I get the output but also an error:

Hallo
Accepted

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.

I also get an error with Apache2::Const::HTTP_OK, the only one that works without error is Apache2::Const::OK.

My apache error log makes no mention of this error.

2

There are 2 answers

2
Len Jaffe On

Try NOT printing anything BEFORE you set your content-type header.

0
Dondi Michael Stroma On

With mod_perl2 you do not return HTTP status codes (this is why it is necessary to use $r->status() so set the HTTP status code.

Instead, you return a value depending on what you want the server to do. The most common would be Apache2::Const::OK. This tells the server your handler has finished successfully. This constant, if I recall correctly, has an integer value of 0, not 200.

Returning an HTTP status code from a mod_perl handler will be interpreted as an error.

https://perl.apache.org/docs/2.0/user/handlers/intro.html#Handler_Return_Values

Different handler groups are supposed to return different values.

Make sure that you always explicitly return a wanted value and don't rely on the result of last expression to be used as the return value -- things will change in the future and you won't know why things aren't working anymore.

The only value that can be returned by all handlers is Apache2::Const::OK, which tells Apache that the handler has successfully finished its execution.

Apache2::Const::DECLINED is another return value that indicates success, but it's only relevant for phases of type RUN_FIRST.

HTTP handlers may also return Apache2::Const::DONE which tells Apache to stop the normal HTTP request cycle and fast forward to the PerlLogHandler, followed by PerlCleanupHandler. HTTP handlers may return any HTTP status, which similarly to Apache2::Const::DONE will cause an abort of the request cycle, by also will be interpreted as an error. Therefore you don't want to return Apache2::Const::HTTP_OK from your HTTP response handler, but Apache2::Const::OK and Apache will send the 200 OK status by itself.