[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

setting Expires HTTP response header

Mark Volkmann

7/16/2005 6:15:00 PM

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'] = 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 hour.

--
R. Mark Volkmann
Partner, Object Computing, Inc.


2 Answers

Mark Volkmann

7/16/2005 9:19:00 PM

0

On 7/16/05, Mark Volkmann <r.mark.volkmann@gmail.com> 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.
>
> res['Expires'] = 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 hour.

I found my own answer.

require 'date'

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

# Add one hour.
t += 60*60

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

--
R. Mark Volkmann
Partner, Object Computing, Inc.


David Heinemeier Hansson

7/17/2005 2:36:00 PM

0

> I found my own answer.
>
> require 'date'
>
> # Get current time in GMT.
> t = Time.now.gmtime
>
> # Add one hour.
> t += 60*60
>
> # Set "Expires" header to this time, properly formatted.
> res['Expires'] = 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.
--
David Heinemeier Hansson
http://www.loudth... -- Broadcasting Brain
http://www.base... -- Online project management
http://www.back... -- Personal information manager
http://www.rubyo... -- Web-application framework