Skip to content

Commit

Permalink
add support for If-Unmodified-Since
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed Sep 17, 2011
1 parent f54f322 commit 092157f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 8 additions & 1 deletion lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,19 @@ def last_modified(time)
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

Expand Down
14 changes: 14 additions & 0 deletions test/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,20 @@ def obj.is_a?(thing) 60.is_a?(thing) end
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
Expand Down

0 comments on commit 092157f

Please sign in to comment.