setting Expires HTTP response header

M

Mark Volkmann

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.
 
M

Mark Volkmann

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.
 
D

David Heinemeier Hansson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top