如果部分响应已发送(分块),如何将浏览器发送到错误页面 [英] How to send the browser to an error page if part of the response has been sent (chunked)

查看:84
本文介绍了如果部分响应已发送(分块),如何将浏览器发送到错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是典型的情况:页面被评估,并且有一个缓冲区-一旦缓冲区已满,被评估页面的一部分将发送到浏览器.这使用HTTP 1.1分块编码.

This is typical scenario: a page is evaluated, and there's a buffer - once the buffer is full, the part of the page that is evaluated is sent to the browser. This uses the HTTP 1.1 chunked encoding.

但是,其中一个块中可能发生错误(在第一个已发送之后).在这种情况下:

However, an error can occur in one of the chunks (after the first one is already sent). In that case:

  • 您无法重定向(发送Location标头),因为标头和响应状态已发送
  • 您不能进行服务器端重定向(转发),因为新页面必须在已发送的部分之后呈现-肯定看起来很难看.
  • you can't redirect (send a Location header), because the headers and the response status were already sent
  • you can't do server-side redirect (forward), because the new page will have to be rendered after the part that is already sent - it will look ugly for sure.

那么在这种情况下您应该怎么做?我提出了问题是否可以在分块预告片中发送位置标头,但这是低级http,即使可能(并且可能不受浏览器支持),语言的抽象也可能不允许这样做

So what should you do in this case? I asked a question whether you can send a Location header in the chunked trailer, but this is low-level http and the abstraction of languages may not allow it, even if it is possible (and it is likely not to be supported across browsers)

另一个选择是发送<script>window.href.location="errorPage"</script>从而强制客户端重定向,但这很丑陋.另外,您必须放入</script>来关闭部分页面中所有可能未关闭的<script>标签.

Another option is to send a <script>window.href.location="errorPage"</script> and thus force the client to redirect, but that's ugly. Plus you have to put </script> to close any potential unclosed <script> tag in the partial page.

(我正在标记主要的网络语言/框架,因为这是一个普遍的问题)

(I'm tagging major web languages/frameworks, because this is an universal issue)

推荐答案

由于标头已经发送,因此无法以块编码方式从服务器重定向.执行重定向的唯一方法是从服务器发送<script>标记并执行客户端重定向.出于好奇,您是否正在尝试实现COMET服务器?在这种情况下,与

You cannot redirect from the server in a chunked encoding because the headers have already been sent. The only way to perform a redirect is to send a <script> tag from the server and perform a client side redirect. Just out of curiosity are you trying to implement a COMET server? If this is the case HTML5 WebSockets seem better way (if the browsers you are targeting support them of course) compared to the hidden iframe technique.

这篇关于如果部分响应已发送(分块),如何将浏览器发送到错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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