2007-02-18 19:27:19 -05:00
|
|
|
require 'digest/md5'
|
2009-05-13 04:10:37 -04:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
2007-02-18 19:27:19 -05:00
|
|
|
|
2009-01-27 19:54:01 -05:00
|
|
|
module ActionDispatch # :nodoc:
|
2008-12-19 18:15:22 -05:00
|
|
|
# Represents an HTTP response generated by a controller action. One can use
|
|
|
|
# an ActionController::Response object to retrieve the current state
|
|
|
|
# of the response, or customize the response. An Response object can
|
|
|
|
# either represent a "real" HTTP response (i.e. one that is meant to be sent
|
|
|
|
# back to the web browser) or a test response (i.e. one that is generated
|
|
|
|
# from integration tests). See CgiResponse and TestResponse, respectively.
|
2008-07-28 07:26:59 -04:00
|
|
|
#
|
2008-12-19 18:15:22 -05:00
|
|
|
# Response is mostly a Ruby on Rails framework implement detail, and
|
|
|
|
# should never be used directly in controllers. Controllers should use the
|
|
|
|
# methods defined in ActionController::Base instead. For example, if you want
|
|
|
|
# to set the HTTP response's content MIME type, then use
|
|
|
|
# ActionControllerBase#headers instead of Response#headers.
|
2008-07-28 07:26:59 -04:00
|
|
|
#
|
2008-12-19 18:15:22 -05:00
|
|
|
# Nevertheless, integration tests may want to inspect controller responses in
|
|
|
|
# more detail, and that's when Response can be useful for application
|
|
|
|
# developers. Integration test methods such as
|
|
|
|
# ActionController::Integration::Session#get and
|
|
|
|
# ActionController::Integration::Session#post return objects of type
|
|
|
|
# TestResponse (which are of course also of type Response).
|
2008-07-28 07:26:59 -04:00
|
|
|
#
|
|
|
|
# For example, the following demo integration "test" prints the body of the
|
|
|
|
# controller response to the console:
|
|
|
|
#
|
|
|
|
# class DemoControllerTest < ActionController::IntegrationTest
|
|
|
|
# def test_print_root_path_to_console
|
|
|
|
# get('/')
|
|
|
|
# puts @response.body
|
|
|
|
# end
|
|
|
|
# end
|
2008-12-19 18:15:22 -05:00
|
|
|
class Response < Rack::Response
|
2007-02-18 19:27:19 -05:00
|
|
|
attr_accessor :request
|
2009-07-31 00:00:39 -04:00
|
|
|
attr_reader :cache_control
|
2008-07-28 07:26:59 -04:00
|
|
|
|
2009-04-26 12:12:33 -04:00
|
|
|
attr_writer :header
|
|
|
|
alias_method :headers=, :header=
|
|
|
|
|
2008-08-21 21:11:09 -04:00
|
|
|
delegate :default_charset, :to => 'ActionController::Base'
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def initialize
|
2009-05-24 22:25:53 -04:00
|
|
|
super
|
2009-07-31 00:00:39 -04:00
|
|
|
@cache_control = {}
|
|
|
|
@header = Rack::Utils::HeaderHash.new
|
2009-03-13 00:47:34 -04:00
|
|
|
end
|
2008-12-19 18:15:22 -05:00
|
|
|
|
2009-07-31 00:32:24 -04:00
|
|
|
def status=(status)
|
|
|
|
@status = status.to_i
|
|
|
|
end
|
|
|
|
|
2009-04-26 12:12:33 -04:00
|
|
|
# The response code of the request
|
|
|
|
def response_code
|
2009-07-31 00:32:24 -04:00
|
|
|
@status
|
2009-04-26 12:12:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a String to ensure compatibility with Net::HTTPResponse
|
|
|
|
def code
|
2009-07-31 00:32:24 -04:00
|
|
|
@status.to_s
|
2009-04-26 12:12:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
2009-07-31 00:32:24 -04:00
|
|
|
StatusCodes::STATUS_CODES[@status]
|
2009-04-26 12:12:33 -04:00
|
|
|
end
|
2009-04-30 15:28:42 -04:00
|
|
|
alias_method :status_message, :message
|
2009-04-26 12:12:33 -04:00
|
|
|
|
2009-03-13 00:47:34 -04:00
|
|
|
def body
|
|
|
|
str = ''
|
|
|
|
each { |part| str << part.to_s }
|
|
|
|
str
|
|
|
|
end
|
2008-12-19 18:15:22 -05:00
|
|
|
|
2009-03-13 00:47:34 -04:00
|
|
|
def body=(body)
|
2009-06-15 14:21:08 -04:00
|
|
|
@body = body.respond_to?(:to_str) ? [body] : body
|
2009-03-13 00:47:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def body_parts
|
|
|
|
@body
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2009-04-26 12:12:33 -04:00
|
|
|
def location
|
|
|
|
headers['Location']
|
|
|
|
end
|
|
|
|
alias_method :redirect_url, :location
|
2008-08-08 02:43:12 -04:00
|
|
|
|
2009-04-26 12:12:33 -04:00
|
|
|
def location=(url)
|
|
|
|
headers['Location'] = url
|
|
|
|
end
|
2008-08-08 02:43:12 -04:00
|
|
|
|
2008-07-28 07:26:59 -04:00
|
|
|
# Sets the HTTP response's content MIME type. For example, in the controller
|
|
|
|
# you could write this:
|
|
|
|
#
|
|
|
|
# response.content_type = "text/plain"
|
|
|
|
#
|
|
|
|
# If a character set has been defined for this response (see charset=) then
|
|
|
|
# the character set information will also be included in the content type
|
|
|
|
# information.
|
2009-06-15 14:21:08 -04:00
|
|
|
attr_accessor :charset, :content_type
|
2006-09-17 12:20:32 -04:00
|
|
|
|
2008-08-08 02:43:12 -04:00
|
|
|
def last_modified
|
2008-08-15 00:45:14 -04:00
|
|
|
if last = headers['Last-Modified']
|
|
|
|
Time.httpdate(last)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_modified?
|
|
|
|
headers.include?('Last-Modified')
|
2008-08-08 02:43:12 -04:00
|
|
|
end
|
2007-02-18 19:27:19 -05:00
|
|
|
|
2008-08-08 02:43:12 -04:00
|
|
|
def last_modified=(utc_time)
|
|
|
|
headers['Last-Modified'] = utc_time.httpdate
|
2007-02-18 19:27:19 -05:00
|
|
|
end
|
2004-12-31 12:07:20 -05:00
|
|
|
|
2008-10-21 10:58:12 -04:00
|
|
|
def etag
|
|
|
|
headers['ETag']
|
|
|
|
end
|
2008-12-19 17:49:06 -05:00
|
|
|
|
2008-10-21 10:58:12 -04:00
|
|
|
def etag?
|
|
|
|
headers.include?('ETag')
|
|
|
|
end
|
2008-12-19 17:49:06 -05:00
|
|
|
|
2008-08-08 02:43:12 -04:00
|
|
|
def etag=(etag)
|
2008-12-15 15:36:33 -05:00
|
|
|
if etag.blank?
|
|
|
|
headers.delete('ETag')
|
|
|
|
else
|
|
|
|
headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}")
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-02-18 19:27:19 -05:00
|
|
|
|
2008-08-21 21:11:09 -04:00
|
|
|
def sending_file?
|
|
|
|
headers["Content-Transfer-Encoding"] == "binary"
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_default_content_type_and_charset!
|
2009-06-15 14:21:08 -04:00
|
|
|
return if !headers["Content-Type"].blank?
|
|
|
|
|
|
|
|
@content_type ||= Mime::HTML
|
|
|
|
@charset ||= default_charset
|
|
|
|
|
|
|
|
type = @content_type.to_s.dup
|
|
|
|
type << "; charset=#{@charset}" unless sending_file?
|
|
|
|
|
|
|
|
headers["Content-Type"] = type
|
2008-08-21 21:11:09 -04:00
|
|
|
end
|
|
|
|
|
2008-08-08 02:43:12 -04:00
|
|
|
def prepare!
|
2008-08-21 21:11:09 -04:00
|
|
|
assign_default_content_type_and_charset!
|
2008-08-08 02:43:12 -04:00
|
|
|
handle_conditional_get!
|
2009-07-31 00:32:24 -04:00
|
|
|
self["Set-Cookie"] ||= ""
|
2008-07-16 07:32:15 -04:00
|
|
|
end
|
2007-02-18 21:25:01 -05:00
|
|
|
|
2008-12-19 18:15:22 -05:00
|
|
|
def each(&callback)
|
|
|
|
if @body.respond_to?(:call)
|
|
|
|
@writer = lambda { |x| callback.call(x) }
|
|
|
|
@body.call(self, self)
|
|
|
|
else
|
2009-05-02 15:57:40 -04:00
|
|
|
@body.each { |part| callback.call(part.to_s) }
|
2008-12-19 18:15:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@writer = callback
|
|
|
|
@block.call(self) if @block
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(str)
|
2009-03-13 00:47:34 -04:00
|
|
|
str = str.to_s
|
|
|
|
@writer.call str
|
2008-12-19 18:15:22 -05:00
|
|
|
str
|
|
|
|
end
|
|
|
|
|
2008-12-20 22:25:09 -05:00
|
|
|
def set_cookie(key, value)
|
2009-02-07 01:08:28 -05:00
|
|
|
if value.has_key?(:http_only)
|
2009-02-07 16:37:54 -05:00
|
|
|
ActiveSupport::Deprecation.warn(
|
|
|
|
"The :http_only option in ActionController::Response#set_cookie " +
|
|
|
|
"has been renamed. Please use :httponly instead.", caller)
|
2009-02-07 01:08:28 -05:00
|
|
|
value[:httponly] ||= value.delete(:http_only)
|
2008-12-20 22:25:09 -05:00
|
|
|
end
|
2009-02-07 01:08:28 -05:00
|
|
|
|
|
|
|
super(key, value)
|
2008-12-20 22:25:09 -05:00
|
|
|
end
|
|
|
|
|
2009-04-26 12:12:33 -04:00
|
|
|
# Returns the response cookies, converted to a Hash of (name => value) pairs
|
|
|
|
#
|
|
|
|
# assert_equal 'AuthorOfNewPage', r.cookies['author']
|
|
|
|
def cookies
|
|
|
|
cookies = {}
|
2009-04-26 21:27:41 -04:00
|
|
|
if header = headers['Set-Cookie']
|
|
|
|
header = header.split("\n") if header.respond_to?(:to_str)
|
|
|
|
header.each do |cookie|
|
|
|
|
if pair = cookie.split(';').first
|
|
|
|
key, value = pair.split("=").map { |v| Rack::Utils.unescape(v) }
|
|
|
|
cookies[key] = value
|
|
|
|
end
|
|
|
|
end
|
2009-04-26 12:12:33 -04:00
|
|
|
end
|
|
|
|
cookies
|
|
|
|
end
|
|
|
|
|
2007-02-18 19:27:19 -05:00
|
|
|
private
|
2008-12-19 17:49:06 -05:00
|
|
|
def handle_conditional_get!
|
2009-07-31 00:00:39 -04:00
|
|
|
if etag? || last_modified? || !cache_control.empty?
|
2008-12-19 17:49:06 -05:00
|
|
|
set_conditional_cache_control!
|
|
|
|
elsif nonempty_ok_response?
|
|
|
|
self.etag = body
|
|
|
|
|
|
|
|
if request && request.etag_matches?(etag)
|
2009-07-31 00:32:24 -04:00
|
|
|
self.status = 304
|
2009-03-13 00:47:34 -04:00
|
|
|
self.body = []
|
2008-12-19 17:49:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
set_conditional_cache_control!
|
2009-07-31 00:00:39 -04:00
|
|
|
else
|
|
|
|
headers["Cache-Control"] = "no-cache"
|
2008-12-19 17:49:06 -05:00
|
|
|
end
|
2007-02-18 19:27:19 -05:00
|
|
|
end
|
|
|
|
|
2008-07-16 07:32:15 -04:00
|
|
|
def nonempty_ok_response?
|
2009-07-31 00:32:24 -04:00
|
|
|
ok = !@status || @status == 200
|
2009-03-13 05:15:51 -04:00
|
|
|
ok && string_body?
|
|
|
|
end
|
|
|
|
|
|
|
|
def string_body?
|
|
|
|
!body_parts.respond_to?(:call) && body_parts.any? && body_parts.all? { |part| part.is_a?(String) }
|
2008-07-16 07:32:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_conditional_cache_control!
|
2009-07-31 00:00:39 -04:00
|
|
|
if cache_control.empty?
|
|
|
|
cache_control.merge!(:public => false, :max_age => 0, :must_revalidate => true)
|
2008-07-16 07:32:15 -04:00
|
|
|
end
|
2009-07-31 00:00:39 -04:00
|
|
|
|
|
|
|
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(", ")
|
2008-07-16 07:32:15 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-07-16 07:32:15 -04:00
|
|
|
end
|