代码之家  ›  专栏  ›  技术社区  ›  conny scipilot

使用send\ u data时如何设置Expires:header

  •  12
  • conny scipilot  · 技术社区  · 16 年前

    我的控制器中有一个方法,它使用如下的send\u数据:

    def show
      expires_in 10.hours, :public => true
      send_data my_image_generator, :filename => "image.gif", :type => "image/gif"
    end
    

    在中使用expires\u会导致发送如下标头:

    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) 防止用户代理重新验证。但我不知道如何使发送数据集的标题?

    我想我 response.headers 散列,但肯定有一个包装(或什么)?

    3 回复  |  直到 16 年前
        1
  •  29
  •   Abdo    14 年前

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

    response.headers["Expires"] = 1.year.from_now.httpdate
    
        2
  •  10
  •   Thiago Figueiro John Mee    11 年前

    response.headers 注意适当设置日期格式:

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

    请注意 max-age Cache-Control 标题覆盖 Expires 标题(如果两者都存在)。看到了吗 RFC2616 Section 14.9.3 更多细节。

        3
  •  3
  •   mahemoff    10 年前

    问题中的代码实际上应该适用于最新的Rails:

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