diff options
author | Yehuda Katz <wycats@gmail.com> | 2009-07-30 21:00:39 -0700 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-08-02 19:39:33 -0400 |
commit | 503ce1d01ce6c8eee9818f4e76a9f880bb1a291d (patch) | |
tree | cd7e9febdd4afab332bc677c76986c3bf1481930 /actionpack/lib/action_dispatch | |
parent | f2a35723c8876697d5a7ebfdf329cee54d8a39ac (diff) | |
download | rails-503ce1d01ce6c8eee9818f4e76a9f880bb1a291d.tar.gz rails-503ce1d01ce6c8eee9818f4e76a9f880bb1a291d.tar.bz2 rails-503ce1d01ce6c8eee9818f4e76a9f880bb1a291d.zip |
Update cache_control to be a Hash of options that is used to build the header.
* Significantly simplifies setting and modifying cache control in other areas
Diffstat (limited to 'actionpack/lib/action_dispatch')
-rw-r--r-- | actionpack/lib/action_dispatch/http/response.rb | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb index e58b4b5e19..32cfb5ae44 100644 --- a/actionpack/lib/action_dispatch/http/response.rb +++ b/actionpack/lib/action_dispatch/http/response.rb @@ -32,8 +32,8 @@ module ActionDispatch # :nodoc: # end # end class Response < Rack::Response - DEFAULT_HEADERS = { "Cache-Control" => "no-cache" } attr_accessor :request + attr_reader :cache_control attr_writer :header alias_method :headers=, :header= @@ -42,7 +42,8 @@ module ActionDispatch # :nodoc: def initialize super - @header = Rack::Utils::HeaderHash.new(DEFAULT_HEADERS) + @cache_control = {} + @header = Rack::Utils::HeaderHash.new end # The response code of the request @@ -196,7 +197,7 @@ module ActionDispatch # :nodoc: private def handle_conditional_get! - if etag? || last_modified? + if etag? || last_modified? || !cache_control.empty? set_conditional_cache_control! elsif nonempty_ok_response? self.etag = body @@ -207,6 +208,8 @@ module ActionDispatch # :nodoc: end set_conditional_cache_control! + else + headers["Cache-Control"] = "no-cache" end end @@ -220,9 +223,20 @@ module ActionDispatch # :nodoc: end def set_conditional_cache_control! - if headers['Cache-Control'] == DEFAULT_HEADERS['Cache-Control'] - headers['Cache-Control'] = 'private, max-age=0, must-revalidate' + if cache_control.empty? + cache_control.merge!(:public => false, :max_age => 0, :must_revalidate => true) end + + public_cache, max_age, must_revalidate, extras = + cache_control.values_at(:public, :max_age, :must_revalidate, :extras) + + options = [] + options << "max-age=#{max_age}" if max_age + options << (public_cache ? "public" : "private") + options << "must-revalidate" if must_revalidate + options.concat(extras) if extras + + headers["Cache-Control"] = options.join(", ") end def convert_content_type! |