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'
|
2010-03-28 08:15:02 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2010-04-16 21:56:35 -04:00
|
|
|
require 'active_support/core_ext/class/attribute_accessors'
|
2007-02-18 19:27:19 -05:00
|
|
|
|
2009-01-27 19:54:01 -05:00
|
|
|
module ActionDispatch # :nodoc:
|
2010-08-28 18:04:14 -04:00
|
|
|
# Represents an HTTP response generated by a controller action. Use it to
|
|
|
|
# retrieve the current state of the response, or customize the response. It can
|
|
|
|
# either represent a real HTTP response (i.e. one that is meant to be sent
|
|
|
|
# back to the web browser) or a TestResponse (i.e. one that is generated
|
|
|
|
# from integration tests).
|
2008-07-28 07:26:59 -04:00
|
|
|
#
|
2010-08-28 18:04:14 -04:00
|
|
|
# \Response is mostly a Ruby on \Rails framework implementation detail, and
|
2008-12-19 18:15:22 -05:00
|
|
|
# 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
|
2010-08-28 18:04:14 -04:00
|
|
|
# more detail, and that's when \Response can be useful for application
|
2008-12-19 18:15:22 -05:00
|
|
|
# developers. Integration test methods such as
|
2009-09-24 00:37:31 -04:00
|
|
|
# ActionDispatch::Integration::Session#get and
|
|
|
|
# ActionDispatch::Integration::Session#post return objects of type
|
2010-08-28 18:04:14 -04:00
|
|
|
# TestResponse (which are of course also of type \Response).
|
2008-07-28 07:26:59 -04:00
|
|
|
#
|
2010-08-28 18:04:14 -04:00
|
|
|
# For example, the following demo integration test prints the body of the
|
2008-07-28 07:26:59 -04:00
|
|
|
# controller response to the console:
|
|
|
|
#
|
2009-09-24 00:37:31 -04:00
|
|
|
# class DemoControllerTest < ActionDispatch::IntegrationTest
|
2008-07-28 07:26:59 -04:00
|
|
|
# def test_print_root_path_to_console
|
|
|
|
# get('/')
|
|
|
|
# puts @response.body
|
|
|
|
# end
|
|
|
|
# end
|
2011-04-19 05:54:12 -04:00
|
|
|
class Response
|
2011-05-23 01:34:06 -04:00
|
|
|
attr_accessor :request, :header
|
|
|
|
attr_reader :status
|
2011-04-19 05:54:12 -04:00
|
|
|
attr_writer :sending_file
|
2008-07-28 07:26:59 -04:00
|
|
|
|
2009-04-26 12:12:33 -04:00
|
|
|
alias_method :headers=, :header=
|
2011-04-19 05:54:12 -04:00
|
|
|
alias_method :headers, :header
|
|
|
|
|
|
|
|
delegate :[], :[]=, :to => :@header
|
|
|
|
delegate :each, :to => :@body
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
attr_accessor :charset, :content_type
|
|
|
|
|
|
|
|
CONTENT_TYPE = "Content-Type"
|
|
|
|
|
|
|
|
cattr_accessor(:default_charset) { "utf-8" }
|
2009-04-26 12:12:33 -04:00
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
include Rack::Response::Helpers
|
|
|
|
include ActionDispatch::Http::Cache::Response
|
Got overhead down from 127 to 85. All tests pass:
* Tentatively replaced HeaderHash with SimpleHeaderHash, which does not preserve
case but does handle converting Arrays to Strings in to_hash. This requires
further discussion.
* Moved default_charset to ActionDispatch::Response to avoid having to hop over
to ActionController. Ideally, this would be a constant on AD::Response, but
some tests expect to be able to change it dynamically and I didn't want to change
them yet.
* Completely override #initialize from Rack::Response. Previously, it was creating
a HeaderHash, and then we were creating an entirely new one. There is no way to
call super without incurring the overhead of creating a HeaderHash.
* Override #write from Rack::Response. Its implementation tracks Content-Length,
and doing so adds additional overhead that could be mooted if other middleware
changes the body. It is more efficiently done at the top-level server.
* Change sending_file to an instance_variable instead of header inspection. In
general, if a state is important, it should be set as a property of the response
not reconstructed later.
* Set the Etag to @body instead of .body. AS::Cache.expand_cache_key handles
Arrays fine, and it's more efficient to let it handle the body parts, since
it is not forced to create a joined String.
* If we detect the default cache control case, just set it, rather than setting
the constituent parts and then running the normal (expensive) code to generate
the string.
2009-08-10 11:40:41 -04:00
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
def initialize(status = 200, header = {}, body = [])
|
|
|
|
self.body, self.header, self.status = body, header, status
|
Got overhead down from 127 to 85. All tests pass:
* Tentatively replaced HeaderHash with SimpleHeaderHash, which does not preserve
case but does handle converting Arrays to Strings in to_hash. This requires
further discussion.
* Moved default_charset to ActionDispatch::Response to avoid having to hop over
to ActionController. Ideally, this would be a constant on AD::Response, but
some tests expect to be able to change it dynamically and I didn't want to change
them yet.
* Completely override #initialize from Rack::Response. Previously, it was creating
a HeaderHash, and then we were creating an entirely new one. There is no way to
call super without incurring the overhead of creating a HeaderHash.
* Override #write from Rack::Response. Its implementation tracks Content-Length,
and doing so adds additional overhead that could be mooted if other middleware
changes the body. It is more efficiently done at the top-level server.
* Change sending_file to an instance_variable instead of header inspection. In
general, if a state is important, it should be set as a property of the response
not reconstructed later.
* Set the Etag to @body instead of .body. AS::Cache.expand_cache_key handles
Arrays fine, and it's more efficient to let it handle the body parts, since
it is not forced to create a joined String.
* If we detect the default cache control case, just set it, rather than setting
the constituent parts and then running the normal (expensive) code to generate
the string.
2009-08-10 11:40:41 -04:00
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
@sending_file = false
|
|
|
|
@blank = false
|
2009-12-14 16:47:52 -05:00
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
if content_type = self["Content-Type"]
|
|
|
|
type, charset = content_type.split(/;\s*charset=/)
|
|
|
|
@content_type = Mime::Type.lookup(type)
|
|
|
|
@charset = charset || "UTF-8"
|
2010-02-19 22:19:20 -05:00
|
|
|
end
|
Got overhead down from 127 to 85. All tests pass:
* Tentatively replaced HeaderHash with SimpleHeaderHash, which does not preserve
case but does handle converting Arrays to Strings in to_hash. This requires
further discussion.
* Moved default_charset to ActionDispatch::Response to avoid having to hop over
to ActionController. Ideally, this would be a constant on AD::Response, but
some tests expect to be able to change it dynamically and I didn't want to change
them yet.
* Completely override #initialize from Rack::Response. Previously, it was creating
a HeaderHash, and then we were creating an entirely new one. There is no way to
call super without incurring the overhead of creating a HeaderHash.
* Override #write from Rack::Response. Its implementation tracks Content-Length,
and doing so adds additional overhead that could be mooted if other middleware
changes the body. It is more efficiently done at the top-level server.
* Change sending_file to an instance_variable instead of header inspection. In
general, if a state is important, it should be set as a property of the response
not reconstructed later.
* Set the Etag to @body instead of .body. AS::Cache.expand_cache_key handles
Arrays fine, and it's more efficient to let it handle the body parts, since
it is not forced to create a joined String.
* If we detect the default cache control case, just set it, rather than setting
the constituent parts and then running the normal (expensive) code to generate
the string.
2009-08-10 11:40:41 -04:00
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
prepare_cache_control!
|
|
|
|
|
|
|
|
yield self if block_given?
|
|
|
|
end
|
2010-02-19 22:19:20 -05:00
|
|
|
|
2009-07-31 00:32:24 -04:00
|
|
|
def status=(status)
|
2009-12-22 17:08:03 -05:00
|
|
|
@status = Rack::Utils.status_code(status)
|
2009-07-31 00:32:24 -04:00
|
|
|
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-12-22 17:08:03 -05:00
|
|
|
Rack::Utils::HTTP_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
|
|
|
|
2010-02-23 19:29:29 -05:00
|
|
|
def respond_to?(method)
|
|
|
|
if method.to_sym == :to_path
|
|
|
|
@body.respond_to?(:to_path)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_path
|
|
|
|
@body.to_path
|
|
|
|
end
|
|
|
|
|
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-08-10 18:49:33 -04:00
|
|
|
EMPTY = " "
|
|
|
|
|
2009-03-13 00:47:34 -04:00
|
|
|
def body=(body)
|
2009-08-10 18:49:33 -04:00
|
|
|
@blank = true if body == EMPTY
|
2011-04-20 15:54:19 -04:00
|
|
|
|
|
|
|
# Explicitly check for strings. This is *wrong* theoretically
|
|
|
|
# but if we don't check this, the performance on string bodies
|
|
|
|
# is bad on Ruby 1.8 (because strings responds to each then).
|
|
|
|
@body = if body.respond_to?(:to_str) || !body.respond_to?(:each)
|
|
|
|
[body]
|
|
|
|
else
|
|
|
|
body
|
|
|
|
end
|
2009-03-13 00:47:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def body_parts
|
|
|
|
@body
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
def set_cookie(key, value)
|
|
|
|
::Rack::Utils.set_cookie_header!(header, key, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_cookie(key, value={})
|
|
|
|
::Rack::Utils.delete_cookie_header!(header, key, value)
|
|
|
|
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
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
def close
|
|
|
|
@body.close if @body.respond_to?(:close)
|
|
|
|
end
|
|
|
|
|
2009-08-25 15:14:31 -04:00
|
|
|
def to_a
|
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!
|
2007-02-18 21:25:01 -05:00
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
@header["Set-Cookie"] = @header["Set-Cookie"].join("\n") if @header["Set-Cookie"].respond_to?(:join)
|
2009-08-25 15:14:31 -04:00
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
if [204, 304].include?(@status)
|
|
|
|
@header.delete "Content-Type"
|
|
|
|
[@status, @header, []]
|
2008-12-19 18:15:22 -05:00
|
|
|
else
|
2011-04-19 05:54:12 -04:00
|
|
|
[@status, @header, self]
|
2008-12-19 18:15:22 -05:00
|
|
|
end
|
|
|
|
end
|
2011-04-19 05:54:12 -04:00
|
|
|
alias prepare! to_a
|
|
|
|
alias to_ary to_a # For implicit splat on 1.9.2
|
2008-12-19 18:15:22 -05:00
|
|
|
|
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 = {}
|
2010-05-17 19:42:35 -04:00
|
|
|
if header = self["Set-Cookie"]
|
2009-04-26 21:27:41 -04:00
|
|
|
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
|
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
private
|
2009-12-17 23:10:06 -05:00
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
def assign_default_content_type_and_charset!
|
|
|
|
return if headers[CONTENT_TYPE].present?
|
2009-12-17 23:10:06 -05:00
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
@content_type ||= Mime::HTML
|
|
|
|
@charset ||= self.class.default_charset
|
2009-12-17 23:10:06 -05:00
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
type = @content_type.to_s.dup
|
|
|
|
type << "; charset=#{@charset}" unless @sending_file
|
2009-12-17 23:10:06 -05:00
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
headers[CONTENT_TYPE] = type
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-07-16 07:32:15 -04:00
|
|
|
end
|