Permalink
Browse files

fix for weird last_modified behavior, and duplicative work

fix for issue #180
The api should be passed an object that supports .to_time. If it happens
to be something else, make a best effort to convert it to a time object (such
as if a string is passed in -- which happens in a few of the tests).

Failing conversion, the rescue stanza will result in an http 200
response, which should be a 'safe' thing to do.

Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
  • Loading branch information...
1 parent c6a7e12 commit 43f50165ca8a6bedadd14e21c54437aad12ea6b2 elij committed with rkh Feb 8, 2011
Showing with 14 additions and 4 deletions.
  1. +11 −4 lib/sinatra/base.rb
  2. +3 −0 test/helpers_test.rb
View
@@ -324,10 +324,17 @@ def expires(amount, *values)
# with a '304 Not Modified' response.
def last_modified(time)
return unless time
- time = time.to_time if time.respond_to?(:to_time)
- time = Time.parse time.strftime('%FT%T%:z') if time.respond_to?(:strftime)
- response['Last-Modified'] = time.respond_to?(:httpdate) ? time.httpdate : time.to_s
- halt 304 if Time.httpdate(request.env['HTTP_IF_MODIFIED_SINCE']) >= time
+ if time.respond_to?(:to_time)
+ time = time.to_time
+ else
+ ## make a best effort to convert something else to a time object
+ ## if this fails, this should throw an ArgumentError, then the
+ # rescue will result in an http 200, which should be safe
+ time = Time.parse(time.to_s).to_time
+ end
+ response['Last-Modified'] = time.httpdate
+ # compare based on seconds since epoch
+ halt 304 if Time.httpdate(request.env['HTTP_IF_MODIFIED_SINCE']).to_i >= time.to_i
rescue ArgumentError
end
View
@@ -592,6 +592,9 @@ def send_file_app(opts={})
get '/compare', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'Sun, 26 Sep 2010 23:43:52 GMT' }
assert_equal 200, status
assert_equal 'foo', body
+ get '/compare', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'Sun, 26 Sep 2100 23:43:52 GMT' }
+ assert_equal 304, status
+ assert_equal '', body
end
end

0 comments on commit 43f5016

Please sign in to comment.