How do I report an error midway through a chunked

2019-05-18 07:53发布

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.

标签: http soap
3条回答
萌系小妹纸
2楼-- · 2019-05-18 08:01

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
查看更多
ら.Afraid
3楼-- · 2019-05-18 08:09

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.

查看更多
Juvenile、少年°
4楼-- · 2019-05-18 08:18

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)

查看更多
登录 后发表回答