[mod_python] Cookies & Redirects

Jim Dabell jim-mod-python at jimdabell.com
Mon Aug 23 07:06:03 EDT 2004


On Monday 23 August 2004 05:08, Byron Ellacott wrote:
> Jim Dabell wrote:
> > Under what circumstances is having separate sets of headers useful?  Does
>
> def handle_response():
>      set_content_type()
>      set_cache_controls()
>      set_content_length()
>      do_fallible_operation()
>      return apache.OK
>
> ... and if do_fallible_operation() happens to raise an exception,
> assuming it's handled correctly and an appropriate error message is
> generated for the user and a 5xx return code is raised, then the content
> type, length and cache controls that have been set are no longer
> appropriate.

I see what you mean, but surely it's a bad idea to assume success and output 
headers before you try do_fallible_operation()?  Wouldn't it be far better to 
attempt do_fallible_operation() before outputting headers that depend upon 
success?

On the flip side, there are all sorts of situations where it's a pain in the 
neck - caching 410 responses, setting the correct media type/character 
encoding, debugging information, etc.  It's not a good idea to assume headers 
wouldn't apply to error conditions.

It just seems to me that it's an optimisation that only works for poorly 
structured handlers and can get in the way of other people.  If somebody 
outputs headers and then realises the headers are wrong later in the program, 
well then they should either use a better design or take the headers back out 
again before raising an exception.

Is there a list somewhere of which items in headers_out get used in error 
conditions?  The Location header is, but what about others?  A quick skim of 
the source didn't reveal much, but I don't really know my way around it that 
well yet.

-- 
Jim Dabell



More information about the Mod_python mailing list