Apache / 2.2.9, mod_perl / 2.0.4: status_line doesn't seem to work
The answer is prepared as follows:
my $r = Apache2::RequestUtil->request;
$r->status_line('500 Internal Server Error');
$r->send_cgi_header("Content-Type: text/html; charset=UTF-8\n\n");
print 'Custom error message';
Request:
GET /test_page HTTP/1.1
Host: www.xxx.xxx
Answer:
HTTP/1.1 200 OK
Date: XXXXXXXXXX
Server: Apache/xxxxxxxx
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
44
Custom error message
0
Why is the response status 200 and not 500?
Is this a registry script or a handler?
In a response handler, if you are setting the status to 4xx or 5xx, you need to return Apache2::Status::DONE
, not Apache2::Status::OK
.
From http://perl.apache.org/docs/2.0/user/handlers/intro.html#Stacked_Handlers :
HTTP handlers can also be returned by Apache2 :: Const :: DONE, which tells Apache to stop the normal HTTP request loop and speed up the PerlLogHandler followed by PerlCleanupHandler. HTTP handlers can return any HTTP status, which, similarly to Apache2 :: Const :: DONE, will cause the request cycle to be interrupted, and will also be interpreted as an error. So 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 on its own.
Hope this helps - I remember having to search for this in the docs for a long time, I don't think it was mentioned anywhere else!
source to share