Permalink
Browse files

Use date comparison instead of string comparison for last_modified he…

…lper. Fixes #98.
  • Loading branch information...
1 parent b2a73d5 commit 616576ce53ed2691a5b790fa90d4ea148288e51e @rkh rkh committed Oct 19, 2010
Showing with 16 additions and 7 deletions.
  1. +3 −7 lib/sinatra/base.rb
  2. +13 −0 test/helpers_test.rb
View
@@ -312,13 +312,9 @@ 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)
- time = time.httpdate if time.respond_to?(:httpdate)
- response['Last-Modified'] = time.to_s
- begin
- halt 304 if time <= Time.httpdate(request.env['HTTP_IF_MODIFIED_SINCE']).httpdate
- rescue ArgumentError
- end
- time
+ response['Last-Modified'] = time.respond_to?(:httpdate) ? time.httpdate : time.to_s
+ halt 304 if Time.httpdate(request.env['HTTP_IF_MODIFIED_SINCE']) >= time
+ rescue ArgumentError
end
# Set the response entity tag (HTTP 'ETag' header) and halt if conditional
View
@@ -522,6 +522,19 @@ def send_file_app(opts={})
assert_equal 200, status
assert_equal 'Boo!', body
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
context "when the resource has been modified on the exact If-Modified-Since header date" do

0 comments on commit 616576c

Please sign in to comment.