views:

168

answers:

2

Response is prepared this way:

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

Response:

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 response status is 200 and not 500?

A: 

Is this a Registry script or a handler?

In a response handler, if you're setting a 4xx or 5xx status you need to return Apache2::Status::DONE rather than Apache2::Status::OK.

From http://perl.apache.org/docs/2.0/user/handlers/intro.html#Stacked%5FHandlers :

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.

Hope that helps - I remember having to spend quite some time looking for this in the docs, I don't think it's mentioned anywhere else!

John O'Rourke
A: 

$r->custom_response(500, $custom_error_message) must be used for this purpose.

Eugene Toropov