使用send_data时如何设置Expires:标头

con*_*nny 12 ruby-on-rails http-caching

我的控制器中有一个方法,它使用send_data,如下所示:

def show
  expires_in 10.hours, :public => true
  send_data my_image_generator, :filename => "image.gif", :type => "image/gif"
end
Run Code Online (Sandbox Code Playgroud)

使用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
Run Code Online (Sandbox Code Playgroud)

我想做的是添加一个标题,Expires: (some exact date)以防止用户代理重新验证.但我不知道如何让send_data设置那个标题?

我想我可以response.headers哈希中明确地设置它,但肯定必须有一个包装器(或其他东西)?

Abd*_*bdo 29

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

response.headers["Expires"] = 1.year.from_now.httpdate
Run Code Online (Sandbox Code Playgroud)


con*_*nny 10

显然似乎没有办法将expires传递给send_data - 相反,你必须自己设置response.headers 并正确处理格式化日期:

response.headers["Expires"] = CGI.rfc1123_date(Time.now + period)
Run Code Online (Sandbox Code Playgroud)

请注意,如果两者都存在max-age,则Cache-Control标头中的指令会覆盖Expires标头.有关更多详细信息,请参阅RFC2616第14.9.3节.