diff options
-rw-r--r-- | actionpack/lib/action_dispatch/http/cache.rb | 42 | ||||
-rw-r--r-- | actionpack/test/controller/render_test.rb | 12 |
2 files changed, 44 insertions, 10 deletions
diff --git a/actionpack/lib/action_dispatch/http/cache.rb b/actionpack/lib/action_dispatch/http/cache.rb index 5ee4c044ea..0b895e7860 100644 --- a/actionpack/lib/action_dispatch/http/cache.rb +++ b/actionpack/lib/action_dispatch/http/cache.rb @@ -84,17 +84,29 @@ module ActionDispatch LAST_MODIFIED = "Last-Modified".freeze ETAG = "ETag".freeze CACHE_CONTROL = "Cache-Control".freeze + SPESHUL_KEYS = %w[extras no-cache max-age public must-revalidate] + + def cache_control_headers + cache_control = {} + if cc = self[CACHE_CONTROL] + cc.delete(' ').split(',').each do |segment| + directive, argument = segment.split('=', 2) + case directive + when *SPESHUL_KEYS + key = directive.tr('-', '_') + cache_control[key.to_sym] = argument || true + else + cache_control[:extras] ||= [] + cache_control[:extras] << segment + end + end + end + cache_control + end def prepare_cache_control! - @cache_control = {} + @cache_control = cache_control_headers @etag = self[ETAG] - - if cache_control = self[CACHE_CONTROL] - cache_control.split(/,\s*/).each do |segment| - first, last = segment.split("=") - @cache_control[first.to_sym] = last || true - end - end end def handle_conditional_get! @@ -110,14 +122,24 @@ module ActionDispatch MUST_REVALIDATE = "must-revalidate".freeze def set_conditional_cache_control! - return if self[CACHE_CONTROL].present? + control = {} + cc_headers = cache_control_headers + if extras = cc_headers.delete(:extras) + @cache_control[:extras] ||= [] + @cache_control[:extras] += extras + @cache_control[:extras].uniq! + end - control = @cache_control + control.merge! cc_headers + control.merge! @cache_control if control.empty? headers[CACHE_CONTROL] = DEFAULT_CACHE_CONTROL elsif control[:no_cache] headers[CACHE_CONTROL] = NO_CACHE + if control[:extras] + headers[CACHE_CONTROL] += ", #{control[:extras].join(', ')}" + end else extras = control[:extras] max_age = control[:max_age] diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 3d58c02338..e9cfda45a9 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -116,6 +116,12 @@ class TestController < ActionController::Base render :action => 'hello_world' end + def conditional_hello_with_cache_control_headers + response.headers['Cache-Control'] = 'no-transform' + expires_now + render :action => 'hello_world' + end + def conditional_hello_with_bangs render :action => 'hello_world' end @@ -1512,6 +1518,12 @@ class ExpiresInRenderTest < ActionController::TestCase assert_equal "no-cache", @response.headers["Cache-Control"] end + def test_expires_now + get :conditional_hello_with_cache_control_headers + assert_match /no-cache/, @response.headers["Cache-Control"] + assert_match /no-transform/, @response.headers["Cache-Control"] + end + def test_date_header_when_expires_in time = Time.mktime(2011,10,30) Time.stubs(:now).returns(time) |