From f54f322b6576b6d9586a8d188679ecd7813b0c04 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sat, 17 Sep 2011 14:42:38 -0700 Subject: [PATCH] only halt execution in last_modified if status is currently 200 (to confrom with RFC 2616) --- CHANGES | 3 +++ lib/sinatra/base.rb | 2 +- test/helpers_test.rb | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index dc0ef77c..5b869de9 100644 --- a/CHANGES +++ b/CHANGES @@ -82,6 +82,9 @@ * Conditional requests on `etag` helper now work properly for unsafe HTTP methods. (Matthew Schinckel, Konstantin Haase) + * The `last_modified` helper does not stop execution and change the status code + if the status code is something different than 200. (Konstantin Haase) + * `Sinatra::Base.run!` now prints to stderr rather than stdout. (Andrew Armenia) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index cf5a432b..8782b345 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -356,7 +356,7 @@ module Sinatra time = time_for time response['Last-Modified'] = time.httpdate - unless env['HTTP_IF_NONE_MATCH'] + if status == 200 and not env['HTTP_IF_NONE_MATCH'] # compare based on seconds since epoch since = Time.httpdate(env['HTTP_IF_MODIFIED_SINCE']).to_i halt 304 if since >= time.to_i diff --git a/test/helpers_test.rb b/test/helpers_test.rb index 46055dda..566e2d37 100644 --- a/test/helpers_test.rb +++ b/test/helpers_test.rb @@ -858,6 +858,20 @@ class HelpersTest < Test::Unit::TestCase assert ! response['Last-Modified'] end + it 'does not change a status other than 200' do + mock_app do + get '/' do + status 299 + last_modified Time.at(0) + 'ok' + end + end + + get('/', {}, 'HTTP_IF_MODIFIED_SINCE' => 'Sun, 26 Sep 2030 23:43:52 GMT') + assert_status 299 + assert_body 'ok' + end + [Time.now, DateTime.now, Date.today, Time.now.to_i, Struct.new(:to_time).new(Time.now) ].each do |last_modified_time| describe "with #{last_modified_time.class.name}" do