Do not inherit from Rack::Response, remove a shit-ton of unused code.

This commit is contained in:
José Valim 2011-04-19 11:54:12 +02:00
parent 6380f1a9f4
commit a66c917235
5 changed files with 61 additions and 76 deletions

View File

@ -114,13 +114,7 @@ module AbstractController
# :api: plugin
def render_to_string(*args, &block)
options = _normalize_render(*args, &block)
if self.response_body = render_to_body(options)
string = ""
response_body.each { |r| string << r }
string
end
ensure
self.response_body = nil
render_to_body(options)
end
# Raw rendering of a template to a Rack-compatible body.

View File

@ -18,6 +18,17 @@ module ActionController
response_body
end
# Overwrite render_to_string because body can now be set to a rack body.
def render_to_string(*)
if self.response_body = super
string = ""
response_body.each { |r| string << r }
string
end
ensure
self.response_body = nil
end
private
# Normalize arguments by catching blocks and setting them on :update.

View File

@ -32,24 +32,35 @@ module ActionDispatch # :nodoc:
# puts @response.body
# end
# end
class Response < Rack::Response
attr_accessor :request, :blank
class Response
attr_accessor :request, :header, :status
attr_writer :sending_file
attr_writer :header, :sending_file
alias_method :headers=, :header=
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" }
module Setup
def initialize(status = 200, header = {}, body = [])
@writer = lambda { |x| @body << x }
@block = nil
@length = 0
self.body, self.header, self.status = body, header, status
@header = header
self.body, self.status = body, status
@cookie = []
@sending_file = false
@blank = false
if content_type = self["Content-Type"]
@ -62,6 +73,7 @@ module ActionDispatch # :nodoc:
end
end
include Rack::Response::Helpers
include Setup
include ActionDispatch::Http::Cache::Response
@ -106,13 +118,21 @@ module ActionDispatch # :nodoc:
def body=(body)
@blank = true if body == EMPTY
@body = body.respond_to?(:to_str) ? [body] : body
@body = body.respond_to?(:each) ? body : [body]
end
def body_parts
@body
end
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
def location
headers['Location']
end
@ -122,46 +142,21 @@ module ActionDispatch # :nodoc:
headers['Location'] = url
end
# 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" }
def to_a
assign_default_content_type_and_charset!
handle_conditional_get!
self["Set-Cookie"] = self["Set-Cookie"].join("\n") if self["Set-Cookie"].respond_to?(:join)
super
end
alias prepare! to_a
@header["Set-Cookie"] = @header["Set-Cookie"].join("\n") if @header["Set-Cookie"].respond_to?(:join)
def each(&callback)
if @body.respond_to?(:call)
@writer = lambda { |x| callback.call(x) }
@body.call(self, self)
if [204, 304].include?(@status)
@header.delete "Content-Type"
[@status, @header, []]
else
@body.each { |part| callback.call(part.to_s) }
[@status, @header, self]
end
@writer = callback
@block.call(self) if @block
end
def write(str)
str = str.to_s
@writer.call str
str
end
alias prepare! to_a
alias to_ary to_a # For implicit splat on 1.9.2
# Returns the response cookies, converted to a Hash of (name => value) pairs
#
@ -180,18 +175,18 @@ module ActionDispatch # :nodoc:
cookies
end
private
def assign_default_content_type_and_charset!
return if headers[CONTENT_TYPE].present?
private
@content_type ||= Mime::HTML
@charset ||= self.class.default_charset
def assign_default_content_type_and_charset!
return if headers[CONTENT_TYPE].present?
type = @content_type.to_s.dup
type << "; charset=#{@charset}" unless @sending_file
@content_type ||= Mime::HTML
@charset ||= self.class.default_charset
headers[CONTENT_TYPE] = type
end
type = @content_type.to_s.dup
type << "; charset=#{@charset}" unless @sending_file
headers[CONTENT_TYPE] = type
end
end
end

View File

@ -79,6 +79,7 @@ module ActionDispatch
@used = Set.new
@closed = false
@flashes = {}
@now = nil
end
def []=(k, v) #:nodoc:

View File

@ -33,22 +33,6 @@ class ResponseTest < ActiveSupport::TestCase
}, headers)
end
test "streaming block" do
@response.body = Proc.new do |response, output|
5.times { |n| output.write(n) }
end
status, headers, body = @response.to_a
assert_equal 200, status
assert_equal({
"Content-Type" => "text/html; charset=utf-8"
}, headers)
parts = []
body.each { |part| parts << part.to_s }
assert_equal ["0", "1", "2", "3", "4"], parts
end
test "content type" do
[204, 304].each do |c|
@response.status = c.to_s