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:
|
2008-12-19 18:15:22 -05:00
|
|
|
# Represents an HTTP response generated by a controller action. One can use
|
2009-09-24 00:37:31 -04:00
|
|
|
# an ActionDispatch::Response object to retrieve the current state
|
2008-12-19 18:15:22 -05:00
|
|
|
# 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
|
2009-09-24 00:37:31 -04:00
|
|
|
# ActionDispatch::Integration::Session#get and
|
|
|
|
# ActionDispatch::Integration::Session#post return objects of type
|
2008-12-19 18:15:22 -05:00
|
|
|
# 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:
|
|
|
|
#
|
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
|
2008-12-19 18:15:22 -05:00
|
|
|
class Response < Rack::Response
|
2009-08-10 18:49:33 -04:00
|
|
|
attr_accessor :request, :blank
|
2008-07-28 07:26:59 -04:00
|
|
|
|
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
|
|
|
attr_writer :header, :sending_file
|
2009-04-26 12:12:33 -04:00
|
|
|
alias_method :headers=, :header=
|
|
|
|
|
2010-02-19 22:19:20 -05:00
|
|
|
module Setup
|
|
|
|
def initialize(status = 200, header = {}, body = [])
|
|
|
|
@writer = lambda { |x| @body << x }
|
|
|
|
@block = nil
|
|
|
|
@length = 0
|
|
|
|
|
2010-02-26 21:24:55 -05:00
|
|
|
@status, @header = status, header
|
|
|
|
self.body = body
|
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
|
|
|
|
2010-02-19 22:19:20 -05:00
|
|
|
@cookie = []
|
|
|
|
@sending_file = false
|
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
|
|
|
|
2010-02-19 22:19:20 -05:00
|
|
|
@blank = false
|
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
|
|
|
|
2010-02-19 22:19:20 -05:00
|
|
|
if content_type = self["Content-Type"]
|
|
|
|
type, charset = content_type.split(/;\s*charset=/)
|
|
|
|
@content_type = Mime::Type.lookup(type)
|
|
|
|
@charset = charset || "UTF-8"
|
|
|
|
end
|
2009-12-14 16:47:52 -05:00
|
|
|
|
2010-02-19 22:19:20 -05:00
|
|
|
yield self if block_given?
|
|
|
|
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
|
|
|
end
|
|
|
|
|
2010-02-19 22:19:20 -05:00
|
|
|
include Setup
|
|
|
|
include ActionDispatch::Http::Cache::Response
|
|
|
|
|
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
|
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
|
|
|
|
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
|
|
|
CONTENT_TYPE = "Content-Type"
|
|
|
|
|
|
|
|
cattr_accessor(:default_charset) { "utf-8" }
|
2008-08-21 21:11:09 -04:00
|
|
|
|
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!
|
2010-01-16 18:21:46 -05:00
|
|
|
self["Set-Cookie"] = @cookie.join("\n") unless @cookie.blank?
|
2010-02-19 22:19:20 -05:00
|
|
|
self["ETag"] = @_etag if @_etag
|
2009-08-25 15:14:31 -04:00
|
|
|
super
|
2008-07-16 07:32:15 -04:00
|
|
|
end
|
2007-02-18 21:25:01 -05:00
|
|
|
|
2009-08-25 15:14:31 -04:00
|
|
|
alias prepare! to_a
|
|
|
|
|
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
|
|
|
|
|
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-08-10 18:49:33 -04:00
|
|
|
if header = @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
|
|
|
|
|
2009-08-10 18:49:33 -04:00
|
|
|
def set_cookie(key, value)
|
|
|
|
case value
|
|
|
|
when Hash
|
|
|
|
domain = "; domain=" + value[:domain] if value[:domain]
|
|
|
|
path = "; path=" + value[:path] if value[:path]
|
|
|
|
# According to RFC 2109, we need dashes here.
|
|
|
|
# N.B.: cgi.rb uses spaces...
|
|
|
|
expires = "; expires=" + value[:expires].clone.gmtime.
|
|
|
|
strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires]
|
|
|
|
secure = "; secure" if value[:secure]
|
|
|
|
httponly = "; HttpOnly" if value[:httponly]
|
|
|
|
value = value[:value]
|
|
|
|
end
|
|
|
|
value = [value] unless Array === value
|
|
|
|
cookie = Rack::Utils.escape(key) + "=" +
|
|
|
|
value.map { |v| Rack::Utils.escape v }.join("&") +
|
|
|
|
"#{domain}#{path}#{expires}#{secure}#{httponly}"
|
|
|
|
|
|
|
|
@cookie << cookie
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_cookie(key, value={})
|
|
|
|
@cookie.reject! { |cookie|
|
|
|
|
cookie =~ /\A#{Rack::Utils.escape(key)}=/
|
|
|
|
}
|
|
|
|
|
|
|
|
set_cookie(key,
|
|
|
|
{:value => '', :path => nil, :domain => nil,
|
|
|
|
:expires => Time.at(0) }.merge(value))
|
|
|
|
end
|
|
|
|
|
2007-02-18 19:27:19 -05:00
|
|
|
private
|
2009-12-17 23:10:06 -05:00
|
|
|
def assign_default_content_type_and_charset!
|
|
|
|
return if headers[CONTENT_TYPE].present?
|
|
|
|
|
|
|
|
@content_type ||= Mime::HTML
|
|
|
|
@charset ||= self.class.default_charset
|
|
|
|
|
|
|
|
type = @content_type.to_s.dup
|
|
|
|
type << "; charset=#{@charset}" unless @sending_file
|
|
|
|
|
|
|
|
headers[CONTENT_TYPE] = type
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-07-16 07:32:15 -04:00
|
|
|
end
|