2016-08-06 12:51:43 -04:00
|
|
|
require "active_support/core_ext/module/attribute_accessors"
|
|
|
|
require "action_dispatch/http/filter_redirect"
|
|
|
|
require "action_dispatch/http/cache"
|
|
|
|
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
|
2015-09-24 13:33:25 -04:00
|
|
|
class Header < DelegateClass(Hash) # :nodoc:
|
|
|
|
def initialize(response, header)
|
|
|
|
@response = response
|
|
|
|
super(header)
|
|
|
|
end
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
def []=(k, v)
|
2015-10-05 19:50:37 -04:00
|
|
|
if @response.sending? || @response.sent?
|
2016-08-06 12:51:43 -04:00
|
|
|
raise ActionDispatch::IllegalStateError, "header already sent"
|
2015-09-24 13:33:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge(other)
|
|
|
|
self.class.new @response, __getobj__.merge(other)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_hash
|
|
|
|
__getobj__.dup
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-30 23:34:53 -04:00
|
|
|
# The request that the response is responding to.
|
|
|
|
attr_accessor :request
|
|
|
|
|
|
|
|
# The HTTP status code.
|
2011-05-23 01:34:06 -04:00
|
|
|
attr_reader :status
|
2013-05-30 23:34:53 -04:00
|
|
|
|
2015-06-16 16:53:57 -04:00
|
|
|
# Get headers for this response.
|
2015-06-15 20:54:08 -04:00
|
|
|
attr_reader :header
|
2013-08-31 18:01:39 -04:00
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
alias_method :headers, :header
|
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
delegate :[], :[]=, to: :@header
|
2016-03-03 01:43:18 -05:00
|
|
|
|
|
|
|
def each(&block)
|
|
|
|
sending!
|
|
|
|
x = @stream.each(&block)
|
|
|
|
sent!
|
|
|
|
x
|
|
|
|
end
|
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
|
2015-08-25 21:35:44 -04:00
|
|
|
NO_CONTENT_CODES = [100, 101, 102, 204, 205, 304]
|
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
|
2015-10-06 16:38:50 -04:00
|
|
|
# Aliasing these off because AD::Http::Cache::Response defines them
|
|
|
|
alias :_cache_control :cache_control
|
|
|
|
alias :_cache_control= :cache_control=
|
|
|
|
|
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
|
2015-07-13 20:48:56 -04:00
|
|
|
@str_body = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
@str_body ||= begin
|
2016-08-06 12:51:43 -04:00
|
|
|
buf = ""
|
2016-08-06 13:55:02 -04:00
|
|
|
each { |chunk| buf << chunk }
|
2016-03-03 01:43:18 -05:00
|
|
|
buf
|
|
|
|
end
|
2012-07-29 18:51:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def write(string)
|
|
|
|
raise IOError, "closed stream" if closed?
|
|
|
|
|
2015-07-13 20:48:56 -04:00
|
|
|
@str_body = nil
|
2012-07-29 18:51:21 -04:00
|
|
|
@response.commit!
|
|
|
|
@buf.push string
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
2016-03-03 01:43:18 -05:00
|
|
|
if @str_body
|
|
|
|
return enum_for(:each) unless block_given?
|
|
|
|
|
|
|
|
yield @str_body
|
|
|
|
else
|
|
|
|
each_chunk(&block)
|
|
|
|
end
|
2012-07-29 18:51:21 -04:00
|
|
|
end
|
|
|
|
|
2014-05-31 21:05:00 -04:00
|
|
|
def abort
|
|
|
|
end
|
|
|
|
|
2012-07-29 18:51:21 -04:00
|
|
|
def close
|
|
|
|
@response.commit!
|
|
|
|
@closed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def closed?
|
|
|
|
@closed
|
|
|
|
end
|
2016-03-03 01:43:18 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def each_chunk(&block)
|
|
|
|
@buf.each(&block) # extract into own method
|
|
|
|
end
|
2012-07-29 18:51:21 -04:00
|
|
|
end
|
|
|
|
|
2015-09-23 17:39:45 -04:00
|
|
|
def self.create(status = 200, header = {}, body = [], default_headers: self.default_headers)
|
|
|
|
header = merge_default_headers(header, default_headers)
|
|
|
|
new status, header, body
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.merge_default_headers(original, default)
|
|
|
|
default.respond_to?(:merge) ? default.merge(original) : original
|
|
|
|
end
|
|
|
|
|
2013-05-30 23:34:53 -04:00
|
|
|
# The underlying body, as a streamable object.
|
2012-07-29 18:51:21 -04:00
|
|
|
attr_reader :stream
|
|
|
|
|
2015-09-23 17:39:45 -04:00
|
|
|
def initialize(status = 200, header = {}, body = [])
|
2012-07-27 18:54:11 -04:00
|
|
|
super()
|
|
|
|
|
2015-09-24 13:33:25 -04:00
|
|
|
@header = Header.new(self, header)
|
2012-08-09 09:45:30 -04:00
|
|
|
|
2015-06-15 20:54:08 -04:00
|
|
|
self.body, self.status = body, 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
|
|
|
|
2012-07-27 18:54:11 -04:00
|
|
|
@cv = new_cond
|
|
|
|
@committed = false
|
2014-03-12 20:40:08 -04:00
|
|
|
@sending = false
|
|
|
|
@sent = false
|
2009-12-14 16:47:52 -05: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
|
|
|
|
2015-10-03 22:14:37 -04:00
|
|
|
def has_header?(key); headers.key? key; end
|
2015-08-27 14:12:03 -04:00
|
|
|
def get_header(key); headers[key]; end
|
|
|
|
def set_header(key, v); headers[key] = v; end
|
|
|
|
def delete_header(key); headers.delete key; end
|
|
|
|
|
2012-07-27 18:54:11 -04:00
|
|
|
def await_commit
|
|
|
|
synchronize do
|
|
|
|
@cv.wait_until { @committed }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-12 20:40:08 -04:00
|
|
|
def await_sent
|
|
|
|
synchronize { @cv.wait_until { @sent } }
|
|
|
|
end
|
|
|
|
|
2012-07-27 18:54:11 -04:00
|
|
|
def commit!
|
|
|
|
synchronize do
|
2014-03-12 20:40:08 -04:00
|
|
|
before_committed
|
2012-07-27 18:54:11 -04:00
|
|
|
@committed = true
|
|
|
|
@cv.broadcast
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-12 20:40:08 -04:00
|
|
|
def sending!
|
|
|
|
synchronize do
|
|
|
|
before_sending
|
|
|
|
@sending = true
|
|
|
|
@cv.broadcast
|
|
|
|
end
|
2012-07-27 18:54:11 -04:00
|
|
|
end
|
|
|
|
|
2014-03-12 20:40:08 -04:00
|
|
|
def sent!
|
|
|
|
synchronize do
|
|
|
|
@sent = true
|
|
|
|
@cv.broadcast
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sending?; synchronize { @sending }; end
|
|
|
|
def committed?; synchronize { @committed }; end
|
|
|
|
def sent?; synchronize { @sent }; end
|
|
|
|
|
2013-01-26 11:36:38 -05:00
|
|
|
# Sets the HTTP status code.
|
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
|
|
|
|
|
2013-05-30 23:34:53 -04:00
|
|
|
# Sets the HTTP content type.
|
2012-03-13 15:21:41 -04:00
|
|
|
def content_type=(content_type)
|
2016-08-29 17:34:39 -04:00
|
|
|
return unless content_type
|
|
|
|
new_header_info = parse_content_type(content_type.to_s)
|
2016-08-29 17:57:40 -04:00
|
|
|
prev_header_info = parsed_content_type_header
|
2016-12-19 13:31:42 -05:00
|
|
|
charset = new_header_info.charset || prev_header_info.charset
|
|
|
|
charset ||= self.class.default_charset unless prev_header_info.mime_type
|
|
|
|
set_content_type new_header_info.mime_type, charset
|
2015-09-08 18:05:19 -04:00
|
|
|
end
|
|
|
|
|
2015-09-08 18:25:40 -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.
|
2015-09-08 18:05:19 -04:00
|
|
|
|
2015-09-08 18:25:40 -04:00
|
|
|
def content_type
|
2016-08-29 17:57:40 -04:00
|
|
|
parsed_content_type_header.mime_type
|
2015-09-08 18:05:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def sending_file=(v)
|
|
|
|
if true == v
|
|
|
|
self.charset = false
|
|
|
|
end
|
2012-03-13 15:21:41 -04:00
|
|
|
end
|
|
|
|
|
2016-10-26 17:13:15 -04:00
|
|
|
# Sets the HTTP character set. In case of +nil+ parameter
|
2016-01-13 20:30:54 -05:00
|
|
|
# it sets the charset to utf-8.
|
2015-06-16 17:58:50 -04:00
|
|
|
#
|
|
|
|
# response.charset = 'utf-16' # => 'utf-16'
|
2015-07-28 06:25:39 -04:00
|
|
|
# response.charset = nil # => 'utf-8'
|
2015-06-15 19:09:44 -04:00
|
|
|
def charset=(charset)
|
2016-08-29 17:57:40 -04:00
|
|
|
header_info = parsed_content_type_header
|
2015-09-08 18:05:19 -04:00
|
|
|
if false == charset
|
2015-09-08 18:29:18 -04:00
|
|
|
set_header CONTENT_TYPE, header_info.mime_type
|
2015-09-08 18:05:19 -04:00
|
|
|
else
|
2015-09-08 18:55:10 -04:00
|
|
|
content_type = header_info.mime_type
|
2015-09-08 18:29:18 -04:00
|
|
|
set_content_type content_type, charset || self.class.default_charset
|
2015-09-08 18:05:19 -04:00
|
|
|
end
|
2015-06-15 19:09:44 -04:00
|
|
|
end
|
|
|
|
|
2015-09-08 18:25:40 -04:00
|
|
|
# The charset of the response. HTML wants to know the encoding of the
|
|
|
|
# content you're giving them, so we need to send that along.
|
|
|
|
def charset
|
2016-08-29 17:57:40 -04:00
|
|
|
header_info = parsed_content_type_header
|
2015-09-08 18:25:40 -04:00
|
|
|
header_info.charset || self.class.default_charset
|
|
|
|
end
|
|
|
|
|
2013-01-26 11:36:38 -05:00
|
|
|
# The response code of the request.
|
2009-04-26 12:12:33 -04:00
|
|
|
def response_code
|
2009-07-31 00:32:24 -04:00
|
|
|
@status
|
2009-04-26 12:12:33 -04:00
|
|
|
end
|
|
|
|
|
2013-01-26 11:36:38 -05:00
|
|
|
# Returns a string to ensure compatibility with <tt>Net::HTTPResponse</tt>.
|
2009-04-26 12:12:33 -04:00
|
|
|
def code
|
2009-07-31 00:32:24 -04:00
|
|
|
@status.to_s
|
2009-04-26 12:12:33 -04:00
|
|
|
end
|
|
|
|
|
2013-01-26 11:36:38 -05:00
|
|
|
# Returns the corresponding message for the current HTTP status code:
|
|
|
|
#
|
2013-01-20 12:41:02 -05:00
|
|
|
# response.status = 200
|
|
|
|
# response.message # => "OK"
|
|
|
|
#
|
|
|
|
# response.status = 404
|
|
|
|
# response.message # => "Not Found"
|
2013-01-26 11:36:38 -05:00
|
|
|
#
|
2009-04-26 12:12:33 -04:00
|
|
|
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
|
|
|
|
2013-01-26 11:36:38 -05:00
|
|
|
# Returns the content of the response as a string. This contains the contents
|
2013-01-16 02:48:13 -05:00
|
|
|
# of any calls to <tt>render</tt>.
|
2009-03-13 00:47:34 -04:00
|
|
|
def body
|
2015-07-13 20:48:56 -04:00
|
|
|
@stream.body
|
2009-03-13 00:47:34 -04:00
|
|
|
end
|
2008-12-19 18:15:22 -05:00
|
|
|
|
2015-10-05 19:50:37 -04:00
|
|
|
def write(string)
|
|
|
|
@stream.write string
|
|
|
|
end
|
|
|
|
|
2013-01-16 06:26:17 -05:00
|
|
|
# Allows you to manually set or override the response body.
|
2009-03-13 00:47:34 -04:00
|
|
|
def body=(body)
|
2012-07-29 20:02:00 -04:00
|
|
|
if body.respond_to?(:to_path)
|
|
|
|
@stream = body
|
|
|
|
else
|
2013-06-28 19:51:32 -04:00
|
|
|
synchronize do
|
|
|
|
@stream = build_buffer self, munge_body_object(body)
|
|
|
|
end
|
2012-07-29 20:02:00 -04:00
|
|
|
end
|
2009-03-13 00:47:34 -04:00
|
|
|
end
|
|
|
|
|
2015-10-05 19:50:37 -04:00
|
|
|
# Avoid having to pass an open file handle as the response body.
|
|
|
|
# Rack::Sendfile will usually intercept the response and uses
|
|
|
|
# the path directly, so there is no reason to open the file.
|
|
|
|
class FileBody #:nodoc:
|
|
|
|
attr_reader :to_path
|
|
|
|
|
|
|
|
def initialize(path)
|
|
|
|
@to_path = path
|
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
File.binread(to_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Stream the file's contents if Rack::Sendfile isn't present.
|
|
|
|
def each
|
2016-08-06 12:51:43 -04:00
|
|
|
File.open(to_path, "rb") do |file|
|
2015-10-05 19:50:37 -04:00
|
|
|
while chunk = file.read(16384)
|
|
|
|
yield chunk
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Send the file stored at +path+ as the response body.
|
|
|
|
def send_file(path)
|
|
|
|
commit!
|
|
|
|
@stream = FileBody.new(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_body!
|
|
|
|
@stream = build_buffer(self, [])
|
|
|
|
end
|
|
|
|
|
2009-03-13 00:47:34 -04:00
|
|
|
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
|
|
|
|
|
2013-05-30 23:34:53 -04:00
|
|
|
# The location header we'll be responding with.
|
2009-04-26 12:12:33 -04:00
|
|
|
alias_method :redirect_url, :location
|
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
|
|
|
|
|
2014-05-31 21:05:00 -04:00
|
|
|
def abort
|
|
|
|
if stream.respond_to?(:abort)
|
|
|
|
stream.abort
|
|
|
|
elsif stream.respond_to?(:close)
|
|
|
|
# `stream.close` should really be reserved for a close from the
|
|
|
|
# other direction, but we must fall back to it for
|
|
|
|
# compatibility.
|
|
|
|
stream.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-30 23:34:53 -04:00
|
|
|
# Turns the Response into a Rack-compatible array of the status, headers,
|
2015-05-20 13:22:18 -04:00
|
|
|
# and body. Allows explicit splatting:
|
2014-09-05 16:33:20 -04:00
|
|
|
#
|
|
|
|
# status, headers, body = *response
|
2009-08-25 15:14:31 -04:00
|
|
|
def to_a
|
2015-06-15 20:54:08 -04:00
|
|
|
commit!
|
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
|
2014-09-05 16:33:20 -04: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 = {}
|
2015-08-27 14:12:03 -04:00
|
|
|
if header = get_header(SET_COOKIE)
|
2009-04-26 21:27:41 -04:00
|
|
|
header = header.split("\n") if header.respond_to?(:to_str)
|
|
|
|
header.each do |cookie|
|
2016-08-06 12:51:43 -04:00
|
|
|
if pair = cookie.split(";").first
|
2009-04-26 21:27:41 -04:00
|
|
|
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
|
|
|
|
2015-09-08 18:25:40 -04:00
|
|
|
ContentTypeHeader = Struct.new :mime_type, :charset
|
2015-09-08 18:57:07 -04:00
|
|
|
NullContentTypeHeader = ContentTypeHeader.new nil, nil
|
2015-09-08 18:25:40 -04:00
|
|
|
|
2016-08-29 17:57:40 -04:00
|
|
|
def parse_content_type(content_type)
|
2015-09-08 18:25:40 -04:00
|
|
|
if content_type
|
|
|
|
type, charset = content_type.split(/;\s*charset=/)
|
2016-12-09 14:29:06 -05:00
|
|
|
type = nil if type && type.empty?
|
2015-09-08 18:29:18 -04:00
|
|
|
ContentTypeHeader.new(type, charset)
|
2015-09-08 18:25:40 -04:00
|
|
|
else
|
2015-09-08 18:57:07 -04:00
|
|
|
NullContentTypeHeader
|
2015-09-08 18:25:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-29 17:57:40 -04:00
|
|
|
# Small internal convenience method to get the parsed version of the current
|
|
|
|
# content type header.
|
|
|
|
def parsed_content_type_header
|
|
|
|
parse_content_type(get_header(CONTENT_TYPE))
|
|
|
|
end
|
|
|
|
|
2015-09-08 18:25:40 -04:00
|
|
|
def set_content_type(content_type, charset)
|
2016-08-06 12:51:43 -04:00
|
|
|
type = (content_type || "").dup
|
2015-09-08 18:25:40 -04:00
|
|
|
type << "; charset=#{charset}" if charset
|
|
|
|
set_header CONTENT_TYPE, type
|
|
|
|
end
|
|
|
|
|
2014-03-12 20:40:08 -04:00
|
|
|
def before_committed
|
2015-06-15 20:54:08 -04:00
|
|
|
return if committed?
|
|
|
|
assign_default_content_type_and_charset!
|
|
|
|
handle_conditional_get!
|
2015-09-24 13:27:21 -04:00
|
|
|
handle_no_content!
|
2014-03-12 20:40:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def before_sending
|
2016-01-05 21:13:40 -05:00
|
|
|
# Normally we've already committed by now, but it's possible
|
|
|
|
# (e.g., if the controller action tries to read back its own
|
|
|
|
# response) to get here before that. In that case, we must force
|
|
|
|
# an "early" commit: we're about to freeze the headers, so this is
|
|
|
|
# our last chance.
|
|
|
|
commit! unless committed?
|
|
|
|
|
2015-12-01 14:28:01 -05:00
|
|
|
headers.freeze
|
2015-12-06 16:28:45 -05:00
|
|
|
request.commit_cookie_jar! unless committed?
|
2014-03-12 19:07:04 -04:00
|
|
|
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
|
|
|
|
|
2015-06-15 20:54:08 -04:00
|
|
|
def assign_default_content_type_and_charset!
|
2015-09-08 18:55:10 -04:00
|
|
|
return if content_type
|
2009-12-17 23:10:06 -05:00
|
|
|
|
2016-08-29 17:57:40 -04:00
|
|
|
ct = parsed_content_type_header
|
2015-10-05 01:14:04 -04:00
|
|
|
set_content_type(ct.mime_type || Mime[:html].to_s,
|
2015-09-08 18:25:40 -04:00
|
|
|
ct.charset || self.class.default_charset)
|
2011-04-19 05:54:12 -04:00
|
|
|
end
|
2012-07-29 23:26:40 -04:00
|
|
|
|
2014-05-31 21:05:00 -04:00
|
|
|
class RackBody
|
|
|
|
def initialize(response)
|
|
|
|
@response = response
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(*args, &block)
|
|
|
|
@response.each(*args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
# Rack "close" maps to Response#abort, and *not* Response#close
|
|
|
|
# (which is used when the controller's finished writing)
|
|
|
|
@response.abort
|
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
@response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to?(method, include_private = false)
|
2016-08-06 12:51:43 -04:00
|
|
|
if method.to_s == "to_path"
|
2014-05-31 21:05:00 -04:00
|
|
|
@response.stream.respond_to?(method)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_path
|
|
|
|
@response.stream.to_path
|
|
|
|
end
|
2014-09-03 11:16:37 -04:00
|
|
|
|
|
|
|
def to_ary
|
|
|
|
nil
|
|
|
|
end
|
2014-05-31 21:05:00 -04:00
|
|
|
end
|
|
|
|
|
2015-09-24 13:27:21 -04:00
|
|
|
def handle_no_content!
|
|
|
|
if NO_CONTENT_CODES.include?(@status)
|
|
|
|
@header.delete CONTENT_TYPE
|
2016-08-06 12:51:43 -04:00
|
|
|
@header.delete "Content-Length"
|
2015-09-24 13:27:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-29 23:26:40 -04:00
|
|
|
def rack_response(status, header)
|
2015-09-08 19:25:44 -04:00
|
|
|
if NO_CONTENT_CODES.include?(status)
|
2012-07-29 23:26:40 -04:00
|
|
|
[status, header, []]
|
|
|
|
else
|
2014-05-31 21:05:00 -04:00
|
|
|
[status, header, RackBody.new(self)]
|
2012-07-29 23:26:40 -04:00
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-07-16 07:32:15 -04:00
|
|
|
end
|