如何在不关闭连接的情况下在分块的http repsonse中途报告错误? [英] How do I report an error midway through a chunked http repsonse without closing the connection?

查看:176
本文介绍了如何在不关闭连接的情况下在分块的http repsonse中途报告错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTTP服务器,它返回大型主体以响应POST请求(它是一个SOAP服务器)。这些机构通过分块流式传输。如果我在流式传输响应中途遇到错误,如何向客户端报告该错误并仍然保持连接打开?该实现使用专有的HTTP / SOAP堆栈,因此我对HTTP协议级别的答案感兴趣。

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.

推荐答案

一旦服务器具有将状态行(响应的第一行)发送到客户端,您无法再更改响应的状态代码。许多服务器通过在内部缓冲直到缓冲区已满来延迟发送响应。当缓冲区填满时,您仍然可以改变对响应的看法。

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.

如果您的客户端可以访问响应头,您可以使用chunked编码这一事实允许服务器在分块编码的主体之后添加带有标头的预告片。因此,您的服务器遇到错误后,可以优雅地停止发送正文,然后发送预设,将某些标头设置为某个值。然后,您的客户端会将此标头的存在解释为发生错误的迹象。

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.

这篇关于如何在不关闭连接的情况下在分块的http repsonse中途报告错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆