Skip to content
Browse files

Merge pull request #3479 from arvida/ensure-date-header-on-expires-in

Ensure Date header on expires_in
  • Loading branch information...
2 parents 663c9f3 + 2a7230a commit 2f689d462d8e94365724c5575b14ab997e11bb43 @josevalim josevalim committed Feb 18, 2012
View
3 actionpack/lib/action_controller/metal/conditional_get.rb
@@ -115,6 +115,8 @@ def stale?(record_or_options, additional_options = {})
#
# This method will overwrite an existing Cache-Control header.
# See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for more possibilities.
+ #
+ # The method will also ensure a HTTP Date header for client compatibility.
def expires_in(seconds, options = {}) #:doc:
response.cache_control.merge!(
:max_age => seconds,
@@ -124,6 +126,7 @@ def expires_in(seconds, options = {}) #:doc:
options.delete(:private)
response.cache_control[:extras] = options.map {|k,v| "#{k}=#{v}"}
+ response.date = Time.now unless response.date?
end
# Sets a HTTP 1.1 Cache-Control header of <tt>no-cache</tt> so no caching should occur by the browser or
View
14 actionpack/lib/action_dispatch/http/cache.rb
@@ -60,6 +60,20 @@ def last_modified=(utc_time)
headers[LAST_MODIFIED] = utc_time.httpdate
end
+ def date
+ if date_header = headers['Date']
+ Time.httpdate(date_header)
+ end
+ end
+
+ def date?
+ headers.include?('Date')
+ end
+
+ def date=(utc_time)
+ headers['Date'] = utc_time.httpdate
+ end
+
def etag=(etag)
key = ActiveSupport::Cache.expand_cache_key(etag)
@etag = self[ETAG] = %("#{Digest::MD5.hexdigest(key)}")
View
7 actionpack/test/controller/render_test.rb
@@ -1433,6 +1433,13 @@ def test_expires_now
get :conditional_hello_with_expires_now
assert_equal "no-cache", @response.headers["Cache-Control"]
end
+
+ def test_date_header_when_expires_in
+ time = Time.mktime(2011,10,30)
+ Time.stubs(:now).returns(time)
+ get :conditional_hello_with_expires_in
+ assert_equal Time.now.httpdate, @response.headers["Date"]
+ end
end
class LastModifiedRenderTest < ActionController::TestCase

0 comments on commit 2f689d4

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