diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-08-14 21:45:14 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-08-14 21:45:14 -0700 |
commit | 8aad8cb3906fce40fa583839fec7f8591fab8ec7 (patch) | |
tree | e6b903348c95ed060ddac963209065304fabd336 /actionpack/lib/action_controller/response.rb | |
parent | f1f4e84a7ef88d941f6508673bb448de640d6f77 (diff) | |
download | rails-8aad8cb3906fce40fa583839fec7f8591fab8ec7.tar.gz rails-8aad8cb3906fce40fa583839fec7f8591fab8ec7.tar.bz2 rails-8aad8cb3906fce40fa583839fec7f8591fab8ec7.zip |
Set cache control to require revalidation if cache freshness response headers are set. Don't set Content-Length header if 304 status.
Diffstat (limited to 'actionpack/lib/action_controller/response.rb')
-rw-r--r-- | actionpack/lib/action_controller/response.rb | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/actionpack/lib/action_controller/response.rb b/actionpack/lib/action_controller/response.rb index a85fad0d39..7b57f499bb 100644 --- a/actionpack/lib/action_controller/response.rb +++ b/actionpack/lib/action_controller/response.rb @@ -79,7 +79,13 @@ module ActionController # :nodoc: end def last_modified - Time.rfc2822(headers['Last-Modified']) + if last = headers['Last-Modified'] + Time.httpdate(last) + end + end + + def last_modified? + headers.include?('Last-Modified') end def last_modified=(utc_time) @@ -87,6 +93,7 @@ module ActionController # :nodoc: end def etag; headers['ETag'] end + def etag?; headers.include?('ETag') end def etag=(etag) headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}") end @@ -98,22 +105,22 @@ module ActionController # :nodoc: end def prepare! + set_content_length! handle_conditional_get! convert_content_type! - set_content_length! end private def handle_conditional_get! if nonempty_ok_response? - set_conditional_cache_control! - self.etag ||= body if request && request.etag_matches?(etag) self.status = '304 Not Modified' self.body = '' end end + + set_conditional_cache_control! if etag? || last_modified? end def nonempty_ok_response? @@ -142,7 +149,9 @@ module ActionController # :nodoc: # Don't set the Content-Length for block-based bodies as that would mean reading it all into memory. Not nice # for, say, a 2GB streaming file. def set_content_length! - self.headers["Content-Length"] = body.size unless body.respond_to?(:call) + unless body.respond_to?(:call) || (status && status[0..2] == '304') + self.headers["Content-Length"] ||= body.size + end end end end |