使用 send_data 时如何设置 Expires: header

发布于 2024-09-06 20:17:26 字数 742 浏览 1 评论 0原文

我的控制器中有一个方法,它使用这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玻璃人 2024-09-13 20:17:26

我遇到了这种语法并且我喜欢它:-)

response.headers["Expires"] = 1.year.from_now.httpdate

I came across this syntax and I like it :-)

response.headers["Expires"] = 1.year.from_now.httpdate
聚集的泪 2024-09-13 20:17:26

显然,似乎没有办法将 expires 传递给 send_data - 相反,您必须在 response.headers 中自行设置它并适当地格式化日期:

response.headers["Expires"] = CGI.rfc1123_date(Time.now + period)

请注意 < 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:

response.headers["Expires"] = CGI.rfc1123_date(Time.now + period)

Note that the max-age directive in the Cache-Control header overrides the Expires header if both are present. See RFC2616 Section 14.9.3 for more details.

胡渣熟男 2024-09-13 20:17:26

你问题中的代码实际上应该可以在最近的 Rails 上运行:

`expires_in 10.hours, :public => true`

The code in your question should actually work on recent Rails:

`expires_in 10.hours, :public => true`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文