[mod_python] Sending back headers with 4xx responses

Jorey Bump list at joreybump.com
Wed May 31 12:16:13 EDT 2006


Deron Meranda wrote:
> I don't know if this is mod_python or perhaps just an Apache
> limitation, but is it possible to send back specific HTTP
> headers for any 4xx response codes?
> 
> In particular, I'm trying to send a 416 "Requested Range Not
> Satisfiable", as well as a Content-Ranges header, but the
> header is never sent back to the user agent.  As per the
> RFC 2616 spec section 14.16:
> 
>   "A server sending a response with status code 416
>    (Requested range not satisfiable) SHOULD include a
>    Content-Range field with a byte-range-resp-spec of "*".
>    The instance-length specifies the current length of the
>    selected resource."
> 
> Granted, it's a SHOULD rather than a MUST, but is it
> even possible with Apache/mod_python?  BTW, sending
> headers works fine for any 2xx or 3xx reponses.
> 
> Here's a minimal handler which demonstrates this.
> 
> from mod_python import apache
> def handler(req):
>    req.headers_out['Content-Range'] = "*/10000"
>    return apache.HTTP_RANGE_NOT_SATISFIABLE
> 
> Any ideas?

Here is what I do for HTTP_SEE_OTHER (303) redirects, which appears 
similar to what you are trying to do (except I'm using Publisher):

def redirect(req, auth_url):
     """
     Redirect to the authentication page. Use HTTP_SEE_OTHER to coax
     the browser into discarding any form data and requesting the
     page with a GET.

     Arguments:

     req: mod_python apache request object
     auth_url: redirection url

     """
     status = apache.HTTP_SEE_OTHER
     req.headers_out['Location'] = auth_url
     req.status = status
     raise apache.SERVER_RETURN, status

There's a little redundancy to support future modifications I haven't 
implemented yet. Ignore this. It works, and should provide a suitable 
basis. I haven't tested it with 4xx errors, though.



More information about the Mod_python mailing list