2007-12-21 08:48:59 +01:00
|
|
|
require 'digest/md5'
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
module ActionController
|
|
|
|
class AbstractResponse #:nodoc:
|
|
|
|
DEFAULT_HEADERS = { "Cache-Control" => "no-cache" }
|
2007-12-21 08:48:59 +01:00
|
|
|
attr_accessor :request
|
2007-02-09 09:04:31 +01:00
|
|
|
attr_accessor :body, :headers, :session, :cookies, :assigns, :template, :redirected_to, :redirected_to_method_params, :layout
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
@body, @headers, @session, @assigns = "", DEFAULT_HEADERS.merge("cookie" => []), [], []
|
|
|
|
end
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def content_type=(mime_type)
|
2007-12-21 08:48:59 +01:00
|
|
|
self.headers["Content-Type"] = charset ? "#{mime_type}; charset=#{charset}" : mime_type
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def content_type
|
2007-12-21 08:48:59 +01:00
|
|
|
content_type = String(headers["Content-Type"] || headers["type"]).split(";")[0]
|
2007-02-09 09:04:31 +01:00
|
|
|
content_type.blank? ? nil : content_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def charset=(encoding)
|
2007-12-21 08:48:59 +01:00
|
|
|
self.headers["Content-Type"] = "#{content_type || Mime::HTML}; charset=#{encoding}"
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def charset
|
2007-12-21 08:48:59 +01:00
|
|
|
charset = String(headers["Content-Type"] || headers["type"]).split(";")[1]
|
2007-02-09 09:04:31 +01:00
|
|
|
charset.blank? ? nil : charset.strip.split("=")[1]
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def redirect(to_url, response_status)
|
|
|
|
self.headers["Status"] = response_status
|
|
|
|
self.headers["Location"] = to_url
|
|
|
|
|
|
|
|
self.body = "<html><body>You are being <a href=\"#{to_url}\">redirected</a>.</body></html>"
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def prepare!
|
|
|
|
handle_conditional_get!
|
|
|
|
convert_content_type!
|
|
|
|
set_content_length!
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
def handle_conditional_get!
|
|
|
|
if body.is_a?(String) && (headers['Status'] ? headers['Status'][0..2] == '200' : true) && !body.empty?
|
|
|
|
self.headers['ETag'] ||= %("#{Digest::MD5.hexdigest(body)}")
|
|
|
|
self.headers['Cache-Control'] = 'private, max-age=0, must-revalidate' if headers['Cache-Control'] == DEFAULT_HEADERS['Cache-Control']
|
|
|
|
|
|
|
|
if request.headers['HTTP_IF_NONE_MATCH'] == headers['ETag']
|
|
|
|
self.headers['Status'] = '304 Not Modified'
|
|
|
|
self.body = ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_content_type!
|
|
|
|
if content_type = headers.delete("Content-Type")
|
|
|
|
self.headers["type"] = content_type
|
|
|
|
end
|
|
|
|
if content_type = headers.delete("Content-type")
|
|
|
|
self.headers["type"] = content_type
|
|
|
|
end
|
|
|
|
if content_type = headers.delete("content-type")
|
|
|
|
self.headers["type"] = content_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|