[mod_python] apache.SERVER_RETURN and chunked encoding

Graham Dumpleton graham.dumpleton at gmail.com
Wed Jan 2 21:44:58 EST 2008


On 03/01/2008, Matthew Dennis <mdennis at merfer.net> wrote:
> On 1/2/08, Graham Dumpleton <graham.dumpleton at gmail.com> wrote:
>
> > On 03/01/2008, Matthew Dennis <mdennis at merfer.net> wrote:
> > > I have a handler that streams large amounts of data.  Using chunked
> encoding
> > > in necessary as the total size is not known upfront.  If there is an
> error
> > > during the generation, I raise a apache.SERVER_RETURN (with
> > > HTTP_PARTIAL_RESPONSE I believe).  The problem is that it looks like
> > > mod_python and/or apache just appends some error html to the output
> stream
> > > and continues with the chunked encoding like there was no error.  In
> other
> > > words, some data chunks come from the server, then a chunk with the
> error
> > > html in it, then the last chunk.  Thus, the client has no way of knowing
> > > that the data is incomplete (not all of it was generated) and corrupt
> > > (random html appended onto the end).  Am I just missing something, or is
> > > this a bug?
> >
> > A HTTP status code can only be returned/raised before any data has
> > been sent as that information is at the beginning of the response. If
> > you are trying to raise the HTTP response status after data has been
> > sent then your code is wrong.
> >
> > What do you think HTTP_PARTIAL_RESPONSE is meant to be used for? Maybe
> > you understanding is not complete, or you don't understand how HTTP
> > responses are put together.
> >
> > Graham
> >
>
> Sorry, it was INTERNAL_SERVER_ERROR.  I don't know where I got
> PARTIAL_RESPONSE from, there is no such thing.  There is a PARTIAL_CONTENT
> status, but it's not even an error (it's a 200 status for range requests)
> In any case, I would not expect that raising apache.SERVER_RETURN to
> continue processing the request as if nothing happened.  SERVER_RETURN
> requires a status/error code to be passed in, but I wasn't actually
> expecting the status code to be sent to the client.  I was trying to have
> apache/mod_python signal the client that the generation failed.
>
> Let me approach this from a different direction, as it sounds like my
> understanding of raising SERVER_RETURN is wrong.  I thought raising
> SERVER_RETURN was the correct way to inform apache and/or mod_python that an
> error occurred and that the handler can not continue - is that incorrect?
> I'm starting to assume from the name that my understanding is wrong...
>
> The real question is if a handler is in the middle of sending a chunked
> response, how does it inform apache and/or mod_python that it can not
> continue?

You can't return error status after having started writing data. All
you can do is close the connection and not send any more data.

To do that return apache.OK or raise it using SERVER_RETURN. Ie., from memory:

  raise apache.SERVER_RETURN, apache.OK

If you want some sort of error to be flagged, best you can do is log
something in the Apache error logs using req.log_error().

Graham


More information about the Mod_python mailing list