tags:

views:

142

answers:

3

I have an HTTP server that returns large bodies in response to POST requests (it is a SOAP server). These bodies are "streamed" via chunking. If I encounter an error midway through streaming the response how can I report that error to the client and still keep the connection open? The implementation uses a proprietary HTTP/SOAP stack so I am interested in answers at the HTTP protocol level.

+1  A: 

Once the server has sent the status line (the very first line of the response) to the client, you can't change the status code of the response anymore. Many servers delay sending the response by buffering it internally until the buffer is full. While the buffer is filling up, you can still change your mind about the response.

If your client has access to the response headers, you could use the fact that chunked encoding allows the server to add a trailer with headers after the chunked-encoded body. So, your server, having encountered the error, could gracefully stop sending the body, and then send a trailer that sets some header to some value. Your client would then interpret the presence of this header as a sign that an error happened.

Alexander
I was hoping that there was some convention for negotiating delivery of an "alternate" status. E.g. shoving it in a final zero size chunk's extensions (after 0; before \r\n\r\n).
Mark Zeren
Yes, this idea has also occurred to me (but only after my original post). See the second paragraph that I added.
Alexander
A: 

you can change the status code as long as response.iscommitted() returns false. (fot HttpServletResponse in java, im sure there exists an equivalent in other languages)

Andreas Petersson
A: 

Also keep in mind that chunked responses can contain "footers" which are just like HTTP headers. After failing, you can send a footer such as:

X-RealStatus: 500 Some bad stuff happened

Or if you succeed:

X-RealStatus: 200 OK
Frank Krueger