add support for If-Unmodified-Since
This commit is contained in:
parent
f54f322b65
commit
092157f6fb
2
CHANGES
2
CHANGES
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue