使用send_data时如何设置Expires:标头
我的控制器中有一个方法,它使用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 exact date)
以防止用户代理重新validation。 但我不知道如何让send_data设置那个标题?
我想我可以在response.headers
哈希中明确地设置它,但肯定必须有一个包装器(或其他东西)?
我遇到了这种语法,我喜欢它:-)
response.headers["Expires"] = 1.year.from_now.httpdate
显然似乎没有办法将过期传递给send_data – 相反,你必须自己在response.headers
设置它并负责适当地格式化日期:
response.headers["Expires"] = CGI.rfc1123_date(Time.now + period)
请注意,如果两者都存在,则Cache-Control
标头中的max-age
指令会覆盖Expires
标头。 有关更多详细信息,请参见RFC2616第14.9.3节 。
你问题中的代码实际上应该适用于最近的Rails:
`expires_in 10.hours, :public => true`