setting Expires HTTP response header

Discussion in 'Ruby' started by Mark Volkmann, Jul 16, 2005.

  1. What's the easiest way to set the "Expires" HTTP response header when
    using WEBrick?
    Using the response object passed into do_GET, I think I need to do this.

    res['Expires'] =3D some_string

    The question is how to create some_string with a valid value.
    For example, what if I what to set it to the current date/time plus one hou=
    r.

    --=20
    R. Mark Volkmann
    Partner, Object Computing, Inc.
    Mark Volkmann, Jul 16, 2005
    #1
    1. Advertising

  2. On 7/16/05, Mark Volkmann <> wrote:
    > What's the easiest way to set the "Expires" HTTP response header when
    > using WEBrick?
    > Using the response object passed into do_GET, I think I need to do this.
    >=20
    > res['Expires'] =3D some_string
    >=20
    > The question is how to create some_string with a valid value.
    > For example, what if I what to set it to the current date/time plus one h=

    our.

    I found my own answer.

    require 'date'

    # Get current time in GMT.
    t =3D Time.now.gmtime

    # Add one hour.
    t +=3D 60*60

    # Set "Expires" header to this time, properly formatted.
    res['Expires'] =3D t.strftime("%a, %d %b %Y %H:%M:%S GMT")

    --=20
    R. Mark Volkmann
    Partner, Object Computing, Inc.
    Mark Volkmann, Jul 16, 2005
    #2
    1. Advertising

  3. > I found my own answer.
    >=20
    > require 'date'
    >=20
    > # Get current time in GMT.
    > t =3D Time.now.gmtime
    >=20
    > # Add one hour.
    > t +=3D 60*60
    >=20
    > # Set "Expires" header to this time, properly formatted.
    > res['Expires'] =3D t.strftime("%a, %d %b %Y %H:%M:%S GMT")


    As a pointer for others looking to do the same in Rails, we've wrapped
    it in a method you can call inside any action:

    expires_in 20.minutes

    ..there's also "expires_now". The method is available in beta gems and
    will be a part of the next release.
    --=20
    David Heinemeier Hansson
    http://www.loudthinking.com -- Broadcasting Brain
    http://www.basecamphq.com -- Online project management
    http://www.backpackit.com -- Personal information manager
    http://www.rubyonrails.com -- Web-application framework
    David Heinemeier Hansson, Jul 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Promenade

    Response.Expires and Response.Cache

    Promenade, Dec 5, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    30,696
    Promenade
    Dec 5, 2003
  2. =?Utf-8?B?Q2hyaXM=?=

    Response.Expires

    =?Utf-8?B?Q2hyaXM=?=, Jun 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    701
    =?Utf-8?B?Q2hyaXM=?=
    Jun 18, 2004
  3. Vivek Mehta
    Replies:
    3
    Views:
    8,647
    Vivek Mehta
    Aug 12, 2004
  4. helveticus
    Replies:
    2
    Views:
    1,433
    helveticus
    Sep 15, 2008
  5. helveticus
    Replies:
    0
    Views:
    316
    helveticus
    Sep 14, 2008
Loading...

Share This Page