2007-02-18 19:27:19 -05:00
|
|
|
require 'digest/md5'
|
2010-04-16 21:56:35 -04:00
|
|
|
require 'active_support/core_ext/class/attribute_accessors'
|
2012-07-27 18:54:11 -04:00
|
|
|
require 'monitor'
|
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('/')
|
2012-04-13 01:20:20 -04:00
|
|
|
# puts response.body
|
2008-07-28 07:26:59 -04:00
|
|
|
# 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
|
2012-07-29 20:02:00 -04:00
|
|
|
delegate :each, :to => :@stream
|
2011-04-19 05:54:12 -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.
|
2012-03-13 15:21:41 -04:00
|
|
|
attr_accessor :charset
|
|
|
|
attr_reader :content_type
|
2011-04-19 05:54:12 -04:00
|
|
|
|
2011-12-07 11:51:23 -05:00
|
|
|
CONTENT_TYPE = "Content-Type".freeze
|
|
|
|
SET_COOKIE = "Set-Cookie".freeze
|
|
|
|
LOCATION = "Location".freeze
|
2012-03-13 15:21:41 -04:00
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
cattr_accessor(:default_charset) { "utf-8" }
|
2012-08-09 09:45:30 -04:00
|
|
|
cattr_accessor(:default_headers)
|
2009-04-26 12:12:33 -04:00
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
include Rack::Response::Helpers
|
2012-09-30 18:00:44 -04:00
|
|
|
include ActionDispatch::Http::FilterRedirect
|
2011-05-10 10:53:57 -04:00
|
|
|
include ActionDispatch::Http::Cache::Response
|
2012-07-27 18:54:11 -04:00
|
|
|
include MonitorMixin
|
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
|
|
|
|
2012-07-29 18:51:21 -04:00
|
|
|
class Buffer # :nodoc:
|
|
|
|
def initialize(response, buf)
|
|
|
|
@response = response
|
|
|
|
@buf = buf
|
|
|
|
@closed = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(string)
|
|
|
|
raise IOError, "closed stream" if closed?
|
|
|
|
|
|
|
|
@response.commit!
|
|
|
|
@buf.push string
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
|
|
|
@buf.each(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@response.commit!
|
|
|
|
@closed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def closed?
|
|
|
|
@closed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :stream
|
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
def initialize(status = 200, header = {}, body = [])
|
2012-07-27 18:54:11 -04:00
|
|
|
super()
|
|
|
|
|
2012-08-13 14:20:04 -04:00
|
|
|
header = merge_default_headers(header, self.class.default_headers)
|
2012-08-09 09:45:30 -04:00
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
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
|
2012-07-27 18:54:11 -04:00
|
|
|
@blank = false
|
|
|
|
@cv = new_cond
|
|
|
|
@committed = false
|
2012-07-29 17:39:03 -04:00
|
|
|
@content_type = nil
|
|
|
|
@charset = nil
|
2009-12-14 16:47:52 -05:00
|
|
|
|
2011-12-07 11:51:23 -05:00
|
|
|
if content_type = self[CONTENT_TYPE]
|
2011-05-10 10:53:57 -04:00
|
|
|
type, charset = content_type.split(/;\s*charset=/)
|
|
|
|
@content_type = Mime::Type.lookup(type)
|
2011-12-06 11:46:41 -05:00
|
|
|
@charset = charset || self.class.default_charset
|
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
|
|
|
|
2012-07-27 18:54:11 -04:00
|
|
|
def await_commit
|
|
|
|
synchronize do
|
|
|
|
@cv.wait_until { @committed }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit!
|
|
|
|
synchronize do
|
|
|
|
@committed = true
|
|
|
|
@cv.broadcast
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def committed?
|
2012-07-29 18:51:21 -04:00
|
|
|
@committed
|
2012-07-27 18:54:11 -04:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2012-03-13 15:21:41 -04:00
|
|
|
def content_type=(content_type)
|
|
|
|
@content_type = content_type.to_s
|
|
|
|
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
|
2012-07-29 20:02:00 -04:00
|
|
|
stream.respond_to?(:to_path)
|
2010-02-23 19:29:29 -05:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_path
|
2012-07-29 20:02:00 -04:00
|
|
|
stream.to_path
|
2010-02-23 19:29:29 -05:00
|
|
|
end
|
|
|
|
|
2009-03-13 00:47:34 -04:00
|
|
|
def body
|
2011-12-12 22:45:16 -05:00
|
|
|
strings = []
|
|
|
|
each { |part| strings << part.to_s }
|
|
|
|
strings.join
|
2009-03-13 00:47:34 -04:00
|
|
|
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
|
|
|
|
2012-07-29 20:02:00 -04:00
|
|
|
if body.respond_to?(:to_path)
|
|
|
|
@stream = body
|
|
|
|
else
|
|
|
|
@stream = build_buffer self, munge_body_object(body)
|
|
|
|
end
|
2009-03-13 00:47:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def body_parts
|
2012-07-29 20:02:00 -04:00
|
|
|
parts = []
|
|
|
|
@stream.each { |x| parts << x }
|
|
|
|
parts
|
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
|
2011-12-07 11:51:23 -05:00
|
|
|
headers[LOCATION]
|
2009-04-26 12:12:33 -04:00
|
|
|
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)
|
2011-12-07 11:51:23 -05:00
|
|
|
headers[LOCATION] = url
|
2009-04-26 12:12:33 -04:00
|
|
|
end
|
2008-08-08 02:43:12 -04:00
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
def close
|
2012-07-29 22:38:33 -04:00
|
|
|
stream.close if stream.respond_to?(:close)
|
2011-05-10 10:53:57 -04:00
|
|
|
end
|
|
|
|
|
2009-08-25 15:14:31 -04:00
|
|
|
def to_a
|
2012-07-29 23:26:40 -04:00
|
|
|
rack_response @status, @header.to_hash
|
2008-12-19 18:15:22 -05:00
|
|
|
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 = {}
|
2011-12-07 11:51:23 -05: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
|
|
|
|
2012-08-13 14:20:04 -04:00
|
|
|
def merge_default_headers(original, default)
|
|
|
|
return original unless default.respond_to?(:merge)
|
|
|
|
|
|
|
|
default.merge(original)
|
|
|
|
end
|
|
|
|
|
2012-07-29 18:51:21 -04:00
|
|
|
def build_buffer(response, body)
|
|
|
|
Buffer.new response, body
|
|
|
|
end
|
|
|
|
|
|
|
|
def munge_body_object(body)
|
|
|
|
body.respond_to?(:each) ? body : [body]
|
|
|
|
end
|
|
|
|
|
2012-07-29 23:26:40 -04:00
|
|
|
def assign_default_content_type_and_charset!(headers)
|
2011-04-19 05:54:12 -04:00
|
|
|
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
|
2012-12-30 16:49:46 -05:00
|
|
|
@charset ||= self.class.default_charset unless @charset == false
|
2009-12-17 23:10:06 -05:00
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
type = @content_type.to_s.dup
|
2012-12-30 16:49:46 -05:00
|
|
|
type << "; charset=#{@charset}" if append_charset?
|
2009-12-17 23:10:06 -05:00
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
headers[CONTENT_TYPE] = type
|
|
|
|
end
|
2012-07-29 23:26:40 -04:00
|
|
|
|
2012-12-30 16:49:46 -05:00
|
|
|
def append_charset?
|
|
|
|
!@sending_file && @charset != false
|
|
|
|
end
|
|
|
|
|
2012-07-29 23:26:40 -04:00
|
|
|
def rack_response(status, header)
|
|
|
|
assign_default_content_type_and_charset!(header)
|
|
|
|
handle_conditional_get!
|
|
|
|
|
|
|
|
header[SET_COOKIE] = header[SET_COOKIE].join("\n") if header[SET_COOKIE].respond_to?(:join)
|
|
|
|
|
|
|
|
if [204, 304].include?(@status)
|
|
|
|
header.delete CONTENT_TYPE
|
|
|
|
[status, header, []]
|
|
|
|
else
|
|
|
|
[status, header, self]
|
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-07-16 07:32:15 -04:00
|
|
|
end
|