aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/response.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-08-07 23:43:12 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2008-08-07 23:43:12 -0700
commitb7529ed1cc7cfd8df5fd1b069e2881d39d3d984c (patch)
treeef69ba48ba47e42981101aab20aebae1d111c684 /actionpack/lib/action_controller/response.rb
parente43d1c226d09afe49b25f5e3a351c4c10371933a (diff)
downloadrails-b7529ed1cc7cfd8df5fd1b069e2881d39d3d984c.tar.gz
rails-b7529ed1cc7cfd8df5fd1b069e2881d39d3d984c.tar.bz2
rails-b7529ed1cc7cfd8df5fd1b069e2881d39d3d984c.zip
Simplifying usage of ETags and Last-Modified and conditional GET requests
Diffstat (limited to 'actionpack/lib/action_controller/response.rb')
-rw-r--r--actionpack/lib/action_controller/response.rb54
1 files changed, 28 insertions, 26 deletions
diff --git a/actionpack/lib/action_controller/response.rb b/actionpack/lib/action_controller/response.rb
index da352b6993..a85fad0d39 100644
--- a/actionpack/lib/action_controller/response.rb
+++ b/actionpack/lib/action_controller/response.rb
@@ -37,12 +37,20 @@ module ActionController # :nodoc:
attr_accessor :body
# The headers of the response, as a Hash. It maps header names to header values.
attr_accessor :headers
- attr_accessor :session, :cookies, :assigns, :template, :redirected_to, :redirected_to_method_params, :layout
+ attr_accessor :session, :cookies, :assigns, :template, :layout
+ attr_accessor :redirected_to, :redirected_to_method_params
def initialize
@body, @headers, @session, @assigns = "", DEFAULT_HEADERS.merge("cookie" => []), [], []
end
+ def status; headers['Status'] end
+ def status=(status) headers['Status'] = status end
+
+ def location; headers['Location'] end
+ def location=(url) headers['Location'] = url end
+
+
# Sets the HTTP response's content MIME type. For example, in the controller
# you could write this:
#
@@ -70,35 +78,29 @@ module ActionController # :nodoc:
charset.blank? ? nil : charset.strip.split("=")[1]
end
- def redirect(to_url, response_status)
- self.headers["Status"] = response_status
- self.headers["Location"] = to_url
+ def last_modified
+ Time.rfc2822(headers['Last-Modified'])
+ end
- self.body = "<html><body>You are being <a href=\"#{to_url}\">redirected</a>.</body></html>"
+ def last_modified=(utc_time)
+ headers['Last-Modified'] = utc_time.httpdate
end
- def prepare!
- handle_conditional_get!
- convert_content_type!
- set_content_length!
+ def etag; headers['ETag'] end
+ def etag=(etag)
+ headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}")
end
- # Sets the Last-Modified response header. Returns whether it's older than
- # the If-Modified-Since request header.
- def last_modified!(utc_time)
- headers['Last-Modified'] ||= utc_time.httpdate
- if request && since = request.headers['HTTP_IF_MODIFIED_SINCE']
- utc_time <= Time.rfc2822(since)
- end
+ def redirect(url, status)
+ self.status = status
+ self.location = url
+ self.body = "<html><body>You are being <a href=\"#{url}\">redirected</a>.</body></html>"
end
- # Sets the ETag response header. Returns whether it matches the
- # If-None-Match request header.
- def etag!(tag)
- headers['ETag'] ||= %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(tag))}")
- if request && request.headers['HTTP_IF_NONE_MATCH'] == headers['ETag']
- true
- end
+ def prepare!
+ handle_conditional_get!
+ convert_content_type!
+ set_content_length!
end
private
@@ -106,15 +108,15 @@ module ActionController # :nodoc:
if nonempty_ok_response?
set_conditional_cache_control!
- if etag!(body)
- headers['Status'] = '304 Not Modified'
+ self.etag ||= body
+ if request && request.etag_matches?(etag)
+ self.status = '304 Not Modified'
self.body = ''
end
end
end
def nonempty_ok_response?
- status = headers['Status']
ok = !status || status[0..2] == '200'
ok && body.is_a?(String) && !body.empty?
end