How do I set Expires: header when using send_data

给你一囗甜甜゛ 提交于 2019-12-04 18:35:21

问题


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)?


回答1:


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

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



回答2:


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.




回答3:


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

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


来源:https://stackoverflow.com/questions/3117361/how-do-i-set-expires-header-when-using-send-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!