使用 send_data 时如何设置 Expires: header
我的控制器中有一个方法,它使用这样的 send_data :
def show
expires_in 10.hours, :public => true
send_data my_image_generator, :filename => "image.gif", :type => "image/gif"
end
使用 expires_in 会导致像这样发送标头:
HTTP/1.1 200 OK
Connection: close
Date: Fri, 25 Jun 2010 10:41:22 GMT
ETag: "885d75258e9306c46a5dbfe3de44e581"
Content-Transfer-Encoding: binary
X-Runtime: 143
Content-Type: image/gif
Content-Disposition: inline; filename="image.gif"
Content-Length: 1277
Cache-Control: max-age=36000, public
我想做的是添加一个像 Expires: (some certain date)
这样的标头来保留用户代理免于重新验证。但我不知道如何让 send_data 设置该标头?
我想我可以在response.headers
哈希中显式设置它,但肯定必须有一个包装器(或其他东西)?
I have a method in my controller which uses send_data like this:
def show
expires_in 10.hours, :public => true
send_data my_image_generator, :filename => "image.gif", :type => "image/gif"
end
Using expires_in results in headers being sent like this:
HTTP/1.1 200 OK
Connection: close
Date: Fri, 25 Jun 2010 10:41:22 GMT
ETag: "885d75258e9306c46a5dbfe3de44e581"
Content-Transfer-Encoding: binary
X-Runtime: 143
Content-Type: image/gif
Content-Disposition: inline; filename="image.gif"
Content-Length: 1277
Cache-Control: max-age=36000, public
What I would like to do is add an header like Expires: (some exact date)
to keep the user agent from revalidating. But I don't see how to make send_data set that header?
I guess I could set it explicitly in the response.headers
hash, but surely there must be a wrapper for that (or something)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了这种语法并且我喜欢它:-)
I came across this syntax and I like it :-)
显然,似乎没有办法将 expires 传递给 send_data - 相反,您必须在
response.headers
中自行设置它并适当地格式化日期:请注意 <
Cache-Control
标头中的 code>max-age 指令会覆盖Expires
标头(如果两者都存在)。有关更多详细信息,请参阅 RFC2616 第 14.9.3 节。Apparently there seems to be no way to pass expires to send_data - instead you must set it yourself in
response.headers
and take care of formatting the date appropriately:Note that the
max-age
directive in theCache-Control
header overrides theExpires
header if both are present. See RFC2616 Section 14.9.3 for more details.你问题中的代码实际上应该可以在最近的 Rails 上运行:
The code in your question should actually work on recent Rails: