Skip to content

Commit

Permalink
fix for weird last_modified behavior, and duplicative work
Browse files Browse the repository at this point in the history
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
elij authored and rkh committed Feb 17, 2011
1 parent c6a7e12 commit 43f5016
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/sinatra/base.rb
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions test/helpers_test.rb
Expand Up @@ -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

Expand Down

0 comments on commit 43f5016

Please sign in to comment.