You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by John Dunlap <jo...@lariat.co> on 2014/01/11 03:36:46 UTC

Returning 200 status from my handler

I'm new to mod_perl within the last year, so I apologize if this is a silly
question.

I notice, in the getting started documentation, that I am supposed to
return Apache2::Const::OK to indicate an HTTP 200 success status but when I
print out the value of this constant I get 0. When I print out other values
of constants from the same package they seem to have numeric values which
directly correspond to their respective HTTP status codes.

I realize that using the constants means that I shouldn't need to know what
their actual values are. However, it strikes me as a extremely bizarre,
especially when returning Apache2::Const::HTTP_OK, which does evaluate to
200, causes Apache2 to response with a canned error response page(similar
to what you see when it returns a 500 error, but it does so with a 200
response code. IE: Apache will return my response body if my handler
returns 0 and one of its default error pages when my handler returns 200.
Why are there conflicting constants in Apache2::Cont which evaluate to
different values when only one of them works correctly? I can return 200
from my handler if I register a custom_response for status 200 but this
just seems wrong to me.

Am I missing something?

Cheers!
-John

Re: Returning 200 status from my handler

Posted by Marius Gavrilescu <ma...@ieval.ro>.
John Dunlap <jo...@lariat.co> writes:

> Am I missing something?

Quoting [0]:

    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.

Apache2::Const::OK tells apache2 that the handler was successful.

Apache2::Const::HTTP_* tell apache2 that the handler failed and the
error code should be HTTP_*.

Thus, returning Apache2::Const::HTTP_OK would tell apache2 that
something went wrong and it should send an error, and the error should
have return code 200 OK.

[0]: https://perl.apache.org/docs/2.0/user/handlers/intro.html#Handler_Return_Values
-- 
Marius Gavrilescu

Re: Returning 200 status from my handler

Posted by John Dunlap <jo...@lariat.co>.
That actually does clear it up for me. Now that you point it out, this is
actually in keeping with established unix conventions. On the shell, a 0
return code has always indicated success while non-zero values indicate an
error. That didn't occur to me(though it probably should have) because I
was building REST endpoints(which are all about HTTP) instead of a shell
script. Your answer does, however, make me feel justified in having
filtered my handler's return code through a hash which maps the HTTP status
code to the appropriate instance of Apache2::Const. That way I can use
numeric statuses without falling out of sync with the defined constants.

Thanks!
John


On Tue, Jan 14, 2014 at 10:34 PM, Perrin Harkins <ph...@gmail.com> wrote:

> Hi John,
>
> > I notice, in the getting started documentation, that I am supposed to
> return
> > Apache2::Const::OK to indicate an HTTP 200 success status
>
> Actually, that's not about sending a HTTP 200 success status.  You
> return Apache2::Const::OK to tell httpd that your handler ran
> correctly.
>
> > When I print out other values of
> > constants from the same package they seem to have numeric values which
> > directly correspond to their respective HTTP status codes.
>
> True, but it's just due to a lack of imagination on the part of the
> httpd developers.  The only constants that intentionally contain valid
> HTTP status codes are the Apache2::Const::HTTP_* ones.
>
> > However, it strikes me as a extremely bizarre,
> > especially when returning Apache2::Const::HTTP_OK, which does evaluate to
> > 200, causes Apache2 to response with a canned error response page
>
> Right because you returned something other than HTTP::Const::OK,
> meaning your handler failed and httpd should sound the alarm.
>
> I hope that clears it up for you.  This is a common confusion, so
> nothing to feel bad about.
>
> - Perrin
>

Re: Returning 200 status from my handler

Posted by Perrin Harkins <ph...@gmail.com>.
Hi John,

> I notice, in the getting started documentation, that I am supposed to return
> Apache2::Const::OK to indicate an HTTP 200 success status

Actually, that's not about sending a HTTP 200 success status.  You
return Apache2::Const::OK to tell httpd that your handler ran
correctly.

> When I print out other values of
> constants from the same package they seem to have numeric values which
> directly correspond to their respective HTTP status codes.

True, but it's just due to a lack of imagination on the part of the
httpd developers.  The only constants that intentionally contain valid
HTTP status codes are the Apache2::Const::HTTP_* ones.

> However, it strikes me as a extremely bizarre,
> especially when returning Apache2::Const::HTTP_OK, which does evaluate to
> 200, causes Apache2 to response with a canned error response page

Right because you returned something other than HTTP::Const::OK,
meaning your handler failed and httpd should sound the alarm.

I hope that clears it up for you.  This is a common confusion, so
nothing to feel bad about.

- Perrin