[mod_python] returning errors from python handler and ErrorDocument

Graham Dumpleton grahamd at dscpl.com.au
Mon Nov 28 16:40:45 EST 2005

My guess is that once the content handler phase has been executed,
or logically passed, that you have no chance of changing what the return
status is. Thus, since for an ErrorDocument directive it is only applied
after the content handler phase of the first attempt has been passed,
whether that is for a mod_python content handler or an inbuilt static
Apache handler for serving static files, you will not be able to do it.

What you instead need to do is to try and detect the condition of a  
file before the content handler phase is executed. To do that, setup a
PythonFixupHandler. Eg:

   <Directory "D:/website/search">
   PythonFixupHandler check_for_missing_target

This should be done for the search directory. You DO NOT not need to
specify any corresponding SetHandler/AddHandler directive as they
are only relevant to PythonHandler directive and not the other phase

What will happen now is that for any request received, it will be routed
through the function "fixuphandler" within the specified module. In that
module, you could check for the existance of the target. If it doesn't
exist, then create it:

   # check_for_missing_target.py (module)

   def fixuphandler(req):
     if target is missing:
       create target of request
     return apache.OK

With the target now existing, the content handler phase when applied  
possibly be able to find it. I say possibly, as I don't know what you  
serving up and other issues such as Apache's URL matching algorithms
may come into play. You may need to actually adjust req.filename to
undo some of what Apache has done.

Anyway, hope that gives you some ideas to play with.


On 28/11/2005, at 9:27 AM, Martin _ wrote:

> In a related query to this one, is it possible to undo a raised  
> http error? I've not had much luck in sending 404s to a script that  
> then creates the resource asked for and bounces apache back to try  
> again from the start, the 404 sticks despite my best attempts to  
> overwrite it with a 200:
> # httpd.conf
> <Directory "D:/website/search">
>         ErrorDocument 404 /py/404.xml
> </Directory>
> <Directory "D:/website/py">
>         AddHandler mod_python .xml
>         PythonHandler index
> </Directory>
> # D:/website/py/index.py
> def handler(req):
>         if not req.prev:
>                 return apache.HTTP_FORBIDDEN
>         ## create relevant resource here ##
>         # doesn't help # req.prev.state = apache.HTTP_OK
>         # doesn't help # req.state = apache.HTTP_OK
>         req.internal_redirect(req.prev.unparsed_uri)
>         # doesn't help # req.next.state = apache.HTTP_OK
>         # doesn't help # raise apache.SERVER_RETURN, apache.HTTP_OK
>         return apache.OK
> I'm open to complete rethinks here, this doesn't strike me as  
> optimal, as the few requests that don't want existing resources  
> will go through three independant handlers, but for requirements:
> -I want to serve static content, even if it's generated because of  
> that request, as static content, not mod_python created chunked output
> -I want to keep all python stuff in that /py/ dir, rather than  
> scattering scripts all over the tree
> Any wise thoughts gratefully received,
> Martin
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list