Use date comparison instead of string comparison for last_modified helper. Fixes #98.

This commit is contained in:
Konstantin Haase 2010-10-19 08:26:32 +02:00
parent b2a73d565d
commit 616576ce53
2 changed files with 16 additions and 7 deletions

View File

@ -312,13 +312,9 @@ module Sinatra
return unless time return unless time
time = time.to_time if time.respond_to?(:to_time) time = time.to_time if time.respond_to?(:to_time)
time = Time.parse time.strftime('%FT%T%:z') if time.respond_to?(:strftime) time = Time.parse time.strftime('%FT%T%:z') if time.respond_to?(:strftime)
time = time.httpdate if time.respond_to?(:httpdate) response['Last-Modified'] = time.respond_to?(:httpdate) ? time.httpdate : time.to_s
response['Last-Modified'] = time.to_s halt 304 if Time.httpdate(request.env['HTTP_IF_MODIFIED_SINCE']) >= time
begin rescue ArgumentError
halt 304 if time <= Time.httpdate(request.env['HTTP_IF_MODIFIED_SINCE']).httpdate
rescue ArgumentError
end
time
end end
# Set the response entity tag (HTTP 'ETag' header) and halt if conditional # Set the response entity tag (HTTP 'ETag' header) and halt if conditional

View File

@ -522,6 +522,19 @@ class HelpersTest < Test::Unit::TestCase
assert_equal 200, status assert_equal 200, status
assert_equal 'Boo!', body assert_equal 'Boo!', body
end end
it 'does not rely on string comparison' do
mock_app do
get '/compare' do
last_modified "Mon, 18 Oct 2010 20:57:11 GMT"
"foo"
end
end
get '/compare', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'Sun, 26 Sep 2010 23:43:52 GMT' }
assert_equal 200, status
assert_equal 'foo', body
end
end end
context "when the resource has been modified on the exact If-Modified-Since header date" do context "when the resource has been modified on the exact If-Modified-Since header date" do