在App Engine上的Servlet响应中会覆盖与缓存相关的HTTP标头 [英] Cache-related HTTP headers are overridden in Servlet response on App Engine

查看:103
本文介绍了在App Engine上的Servlet响应中会覆盖与缓存相关的HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



servlet会正确设置HTTP标头值,以指示图像应该被缓存。但是,App Engine会覆盖这些头部,导致图像不被缓存。



请注意,相同的代码在以前工作过,但现在一段时间不起作用。 / p>

App引擎文档指出,如果 Cache-Control Expires Vary 标题由servlet设置,它们将保持不变:
https://developers.google.com/appengine/docs/java/runtime#Responses



这是我的示例代码:

  response.setContentType(image / jpeg); 
//response.setDateHeader(Expires,new Date()。getTime()+ 60L * 24 * 60 * 60 * 1000); // 60天缓存时间
//response.addHeader(Cache-Control,public,max-age = 5184000); // 5_184_000秒= 60天缓存时间
response.addHeader(Cache-Control,public,max-age = 90000); // 90_000秒= 25小时缓存时间
response.getOutputStream()。write(data); // Data是一个包含JPEG图像数据的字节数组



检查HTTP请求响应,响应包含以下标题:

  HTTP / 1.1 200 OK 
status:200 OK
版本:HTTP / 1.1
cache-control:public,max-age = 90000
cache -control:no-cache,must-revalidate
content-length:6777
content-type:image / jpeg
date:Sat,05 Jan 2013 14:11:47 GMT
过期:1990年1月1日星期五00:00:00 GMT
编译指示:无缓存
服务器:Google Frontend

如您所见,App Engine会插入 cache-control expires pragma 标题禁用缓存。请注意,这是由于请求包含 cookie 标头。但是也有
的文档 https://developers.google.com / appengine / docs / java / runtime#Responses
声明在这种情况下(当cookie被设置时),App引擎将配置缓存为私有,所以浏览器仍然可以缓存它,但不是中间的代理服务器。
我甚至没有添加cookies,但Google Analytics代码(我在页面上启用了Google Analytics)。



我做错了什么?

编辑:
进一步的调查显示,cookie被添加到请求中,因为我使用Google身份验证,并且如果用户使用他/她的Google帐户登录,则可以理解地添加与用户相关的Cookie。如果没有用户登录,则缓存不会被覆盖。所以我的进一步问题是:当用户使用Google帐户登录时,有没有办法缓存servlet提供的图像?



编辑,解决方案:
如果应用的管理员用户是客户端,则Google App Engine仅禁用缓存。在这种情况下,App Engine会自动插入仅用于管理员的标头,例如请求的估计成本。这是私人信息,所以它是可以理解的缓存被禁用。

解决方案

根据文档,只有覆盖登录用户是管理员。你有没有检查是否是这种情况?


I have a servlet on App engine which serves images.

The servlet sets HTTP header values properly to indicate that the images should be cached. But App Engine overrides these headers which results in the images not being cached.

Note that the same code worked before, but it doesn't work for some time now.

The App engine documentation states that if Cache-Control, Expires and Vary headers are set by the servlet, they will be left unmodified: https://developers.google.com/appengine/docs/java/runtime#Responses

This is my sample code:

response.setContentType( "image/jpeg" );
//response.setDateHeader( "Expires", new Date().getTime() + 60L*24*60*60*1000 ); // 60 days cache time
//response.addHeader( "Cache-Control", "public, max-age=5184000" ); // 5_184_000 sec = 60 days cache time
response.addHeader( "Cache-Control", "public, max-age=90000" ); // 90_000 sec = 25 hours cache time
response.getOutputStream().write( data ); // Data is a byte array containing the JPEG image data

(I've tried putting all in that is commented out.)

Examining the HTTP request-response, the response contains the following headers:

HTTP/1.1 200 OK
status: 200 OK
version: HTTP/1.1
cache-control: public, max-age=90000
cache-control: no-cache, must-revalidate
content-length: 6777
content-type: image/jpeg
date: Sat, 05 Jan 2013 14:11:47 GMT
expires: Fri, 01 Jan 1990 00:00:00 GMT
pragma: no-cache
server: Google Frontend

As you can see, App Engine inserts cache-control, expires and pragma headers disabling caching. Note that this is due to the request has a cookie header. But also the documentation at https://developers.google.com/appengine/docs/java/runtime#Responses states that in this case (when cookie is set), App engine will configure caching to be private so browsers will still be able to cache it but not intermediate proxy servers. The cookies are not even added by me but Google Analytics code (I've enabled Google Analytics on the page).

What am I doing wrong? How can I achieve proper caching of the servlet response?

EDIT: Further investigation showed that cookies are added to requests because I use Google Authentication, and if a user is logged in using his/her Google account, user related cookies are added, understandably. If no user is logged in, the caching is not overridden. So my further question is: Is there a way to cache servlet-served images when the user is logged in with a Google account?

EDIT, SOLUTION: Google App Engine only disables caching if an admin user of the app is the client. In this case, App Engine automatically inserts headers intended only for the admin, for example the estimated cost of the request. This is private info so it is understandable caching is disabled.

解决方案

According to the docs, the headers should only be overwritten if the logged-in user is an administrator. Did you check if this is the case?

这篇关于在App Engine上的Servlet响应中会覆盖与缓存相关的HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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