如何在 Google App Engine 中检测 HTTP 响应大小
我正在用 Java 为 Google App Engine 编写一个应用程序。某个 Servlet 生成的响应可能会超过 32 MB 的限制。在开发服务器上,这似乎不会引起任何问题。在生产服务器上,我需要一种方法将响应拆分为多个请求。
我想知道超出限制时生产服务器上会发生什么。当超出限制时,PrintWriter
是否抛出 IOException
?或者仅当 servlet 终止时才抛出异常?理想情况下,我希望能够在附加每个部分之前计算总响应大小(无需自己计算字节,因为我不知道 HTTP 标头的大小),以便我可以正确清理响应的末尾响应(即附加“}”以完成 JSON 对象)。或者有更好的方法来处理这种情况吗?
I'm writing an app for Google App Engine with Java. A certain servlet produces responses that may go over the 32 megabyte limit. On the development server, this does not seem to cause any issues. On the production server, I'll need a way to split the response over several requests.
I want to know what happens on the production server when the limit is exceeded. Does the PrintWriter
throw an IOException
when the limit is exceeded? Or is an exception only thrown when the servlet terminates? Ideally, I would like to be able to calculate the total response size before appending each section (without counting the bytes myself, because I don't know the size of the HTTP headers), so that I can properly clean up the end of the response (i.e. append "}" to complete the JSON object). Or is there a better way to handle this type of situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尼克是对的,当前响应大小限制为 32MB。
应用程序引擎会缓冲响应数据,因此在代码完成运行之前它不会看到您的响应,因此它无法引发异常或以其他方式发出代码可以捕获的错误信号。相反,它会向客户端返回 500,其正文如下:
HTTP 响应太大:34000134。限制为:33554432。
您可以通过在 http://shell.appspot.com/ :
打印 'x' * 34 * 1000 * 1000
nick's right, the current response size limit is 32MB.
app engine buffers response data, so it doesn't see your response until your code has finished running, so it can't raise an exception or otherwise signal an error that your code can catch. instead, it returns a 500 to the client with a body like this:
HTTP response was too large: 34000134. The limit is: 33554432.
you can try it yourself by running this in http://shell.appspot.com/ :
print 'x' * 34 * 1000 * 1000