Skip to content
Browse files

add support for If-Unmodified-Since

  • Loading branch information...
1 parent f54f322 commit 092157f6fbcb1b863fbf42757db22d76e94df628 @rkh rkh committed
Showing with 24 additions and 1 deletion.
  1. +2 −0 CHANGES
  2. +8 −1 lib/sinatra/base.rb
  3. +14 −0 test/helpers_test.rb
View
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)
View
9 lib/sinatra/base.rb
@@ -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
View
14 test/helpers_test.rb
@@ -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

0 comments on commit 092157f

Please sign in to comment.
Something went wrong with that request. Please try again.