[cgiapp] Best practices for returning 404/file-not-found pages inside and outside of mod_perl

Mark Stosberg mark at summersault.com
Fri Apr 11 16:58:07 EDT 2008


In the past, the way I returned 404 page through CGI::App was something 
like this:

  return $self->error(title => 'Page not found');

I learned that is too late to return a real 404 error running under CGI, 
so the page comes back with a 200 status code, which isn't quite right.

More recently, I learned that with mod_perl, I learned that I can get 
the system to return a true 404, so I updated the logic to do that when 
possible:

  if (exists $ENV{MOD_PERL}) {
    $self->header_add( -status => 404 );
    return '';
  }
  else {
     return $self->error(title => 'Page not found')
  }

However, I don't think I'm doing the ideal think in mod_perl, because it 
behaves strangely in some cases. Two specific cases:

If I use GET on the command line, instead of 404, I'll get back this:
"500 EOF when chunk header expected"

Unless I fallback to HTTP 1.0:

PERL_LWP_USE_HTTP_10=1 GET ...

But for some reason, setting this environment variable was not working 
for with Test::WWW::Mechanize.

More troubling is the behavior I see in the browser: The first time I 
access the script that would through this 404 in mod_perl, it works. 
Then for attempts 2 through 6 return internal server errors complaining 
about "can't locate modules". Starting on load 7, the pages are returned 
   reliably with the 404 error. WTF?

The approach of CGI::Application::Dispatch is to also return the "404" 
code, but also returns the body content along with it. In my case, I'm 
hoping to trigger the internal ErrorDocument 404 page instead of 
re-inventingt that wheel.

What am I missing?

Thanks!

    Mark







More information about the cgiapp mailing list