add support for If-Unmodified-Since

This commit is contained in:
Konstantin Haase 2011-09-17 15:11:32 -07:00
parent f54f322b65
commit 092157f6fb
3 changed files with 24 additions and 1 deletions

View File

@ -85,6 +85,8 @@
* The `last_modified` helper does not stop execution and change the status code
if the status code is something different than 200. (Konstantin Haase)
* Added support for If-Unmodified-Since header. (Konstantin Haase)
* `Sinatra::Base.run!` now prints to stderr rather than stdout. (Andrew
Armenia)

View File

@ -355,12 +355,19 @@ module Sinatra
return unless time
time = time_for time
response['Last-Modified'] = time.httpdate
return if env['HTTP_IF_NONE_MATCH']
if status == 200 and not env['HTTP_IF_NONE_MATCH']
if status == 200 and env['HTTP_IF_MODIFIED_SINCE']
# compare based on seconds since epoch
since = Time.httpdate(env['HTTP_IF_MODIFIED_SINCE']).to_i
halt 304 if since >= time.to_i
end
if (success? or status == 412) and env['HTTP_IF_UNMODIFIED_SINCE']
# compare based on seconds since epoch
since = Time.httpdate(env['HTTP_IF_UNMODIFIED_SINCE']).to_i
halt 412 if since < time.to_i
end
rescue ArgumentError
end

View File

@ -969,6 +969,20 @@ class HelpersTest < Test::Unit::TestCase
assert_equal '', body
end
end
context "If-Unmodified-Since" do
it 'results in 200 if resource has not been modified' do
get '/', {}, { 'HTTP_IF_UNMODIFIED_SINCE' => 'Sun, 26 Sep 2030 23:43:52 GMT' }
assert_equal 200, status
assert_equal 'Boo!', body
end
it 'results in 412 if resource has been modified' do
get '/', {}, { 'HTTP_IF_UNMODIFIED_SINCE' => Time.at(0).httpdate }
assert_equal 412, status
assert_equal '', body
end
end
end
end
end