You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Michael Peters <mp...@plusthree.com> on 2006/08/12 22:46:25 UTC

status() vs constants

I've got a module which wraps the creation and execution of an object inside of
a few routines. It's up to those routines to set the status(). If the class
can't be found it's a 404, if the object doesn't execute it's a 500, etc.

Now, in my handler sub I want to return the corresponding constant for whatever
status() is set to. Under mod_perl 1.x this worked by just returning the
status() since OK == 200, SERVER_ERROR == 500, etc. But this isn't true under
mod_perl 2 (as far as I can tell). My module needs to work under both, so I need
a better way.

I'd rather not have to check every possible status() value in a huge if/else
tree to return the right constant, but I guess I will if there's no other
choice. Please tell me there's an easier way.

Thanks,
-- 
Michael Peters
Developer
Plus Three, LP


Re: status() vs constants

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
> Now, in my handler sub I want to return the corresponding constant for whatever
> status() is set to. Under mod_perl 1.x this worked by just returning the
> status() since OK == 200, SERVER_ERROR == 500, 

OK is not 200.  OK is 0, HTTP_OK is 200.  'tis always been this way :)

> etc. But this isn't true under
> mod_perl 2 (as far as I can tell). 

mp1 assumed that if you returned 200 (HTTP_OK) you really meant 0 (OK).
 this is no longer true with mp1.  if you do the right thing (return OK,
not HTTP_OK) then both mp1 and mp2 do the right thing.

in general, you should never worry about $r->status - whether you're in
mp1 or mp2, your handler() subroutine should always return either one of
OK, DECLINED, or DONE, or some HTTP _error_ code (like 500, 304, etc).
over in httpd land apache will translate your return value into a
suitable value of $r->status (which isn't always the same as what you
returned, such as when you're in an ErrorDocument it will force
$r->status to whatever the original error status was if your
ErrorDocument returns 500).

HTH

--Geoff

Re: status() vs constants

Posted by Jonathan Vanasco <jo...@2xlp.com>.
That seems to work for me under MP2

My first guess is that it has to do with status renamings...

Instead of setting something to 200 / 500 , why not just set it to  
the name of the status code, then use a Hash to pull out the  
appropriate code for apache1/apache2?

personally i like 'human text' like that, then looking stuff up in a  
hash of values when you need the integer code.  it makes debugging  
way less painful.

Re: status() vs constants

Posted by Perrin Harkins <pe...@elem.com>.
On Sat, 2006-08-12 at 16:46 -0400, Michael Peters wrote:
> Now, in my handler sub I want to return the corresponding constant for whatever
> status() is set to. Under mod_perl 1.x this worked by just returning the
> status() since OK == 200, SERVER_ERROR == 500, etc. But this isn't true under
> mod_perl 2 (as far as I can tell). My module needs to work under both, so I need
> a better way.

The HTTP constants all have names like HTTP_OK rather than OK.  They are
not guaranteed to match the handler return codes, although many of them
do.

> I'd rather not have to check every possible status() value in a huge if/else
> tree to return the right constant

You could make a hash that maps handler response codes to HTTP codes.
Most people don't need to do that because they only use one or two
codes.

- Perrin