Skip to content

Commit

Permalink
flushing output should write to the stream rather than mutating the r…
Browse files Browse the repository at this point in the history
…esponse object
  • Loading branch information
tenderlove committed Jul 30, 2012
1 parent 19e68e9 commit a6bdae1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
20 changes: 12 additions & 8 deletions actionpack/lib/action_dispatch/http/response.rb
Expand Up @@ -42,7 +42,7 @@ class Response
alias_method :headers, :header alias_method :headers, :header


delegate :[], :[]=, :to => :@header delegate :[], :[]=, :to => :@header
delegate :each, :to => :@body delegate :each, :to => :@stream


# Sets the HTTP response's content MIME type. For example, in the controller # Sets the HTTP response's content MIME type. For example, in the controller
# you could write this: # you could write this:
Expand Down Expand Up @@ -106,8 +106,6 @@ def initialize(status = 200, header = {}, body = [])
@committed = false @committed = false
@content_type = nil @content_type = nil
@charset = nil @charset = nil
@stream = build_buffer self, @body



if content_type = self[CONTENT_TYPE] if content_type = self[CONTENT_TYPE]
type, charset = content_type.split(/;\s*charset=/) type, charset = content_type.split(/;\s*charset=/)
Expand Down Expand Up @@ -162,14 +160,14 @@ def message


def respond_to?(method) def respond_to?(method)
if method.to_sym == :to_path if method.to_sym == :to_path
@body.respond_to?(:to_path) stream.respond_to?(:to_path)
else else
super super
end end
end end


def to_path def to_path
@body.to_path stream.to_path
end end


def body def body
Expand All @@ -183,11 +181,17 @@ def body
def body=(body) def body=(body)
@blank = true if body == EMPTY @blank = true if body == EMPTY


@body = munge_body_object(body) if body.respond_to?(:to_path)
@stream = body
else
@stream = build_buffer self, munge_body_object(body)
end
end end


def body_parts def body_parts
@body parts = []
@stream.each { |x| parts << x }
parts
end end


def set_cookie(key, value) def set_cookie(key, value)
Expand All @@ -208,7 +212,7 @@ def location=(url)
end end


def close def close
@body.close if @body.respond_to?(:close) stream.close
end end


def to_a def to_a
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/capture_helper.rb
Expand Up @@ -213,7 +213,7 @@ def with_output_buffer(buf = nil) #:nodoc:
# Add the output buffer to the response body and start a new one. # Add the output buffer to the response body and start a new one.
def flush_output_buffer #:nodoc: def flush_output_buffer #:nodoc:
if output_buffer && !output_buffer.empty? if output_buffer && !output_buffer.empty?
response.body_parts << output_buffer response.stream.write output_buffer
self.output_buffer = output_buffer.respond_to?(:clone_empty) ? output_buffer.clone_empty : output_buffer[0, 0] self.output_buffer = output_buffer.respond_to?(:clone_empty) ? output_buffer.clone_empty : output_buffer[0, 0]
nil nil
end end
Expand Down
6 changes: 3 additions & 3 deletions actionpack/test/controller/send_file_test.rb
Expand Up @@ -51,14 +51,14 @@ def test_file_stream
response = nil response = nil
assert_nothing_raised { response = process('file') } assert_nothing_raised { response = process('file') }
assert_not_nil response assert_not_nil response
assert_respond_to response.body_parts, :each assert_respond_to response.stream, :each
assert_respond_to response.body_parts, :to_path assert_respond_to response.stream, :to_path


require 'stringio' require 'stringio'
output = StringIO.new output = StringIO.new
output.binmode output.binmode
output.string.force_encoding(file_data.encoding) output.string.force_encoding(file_data.encoding)
assert_nothing_raised { response.body_parts.each { |part| output << part.to_s } } response.body_parts.each { |part| output << part.to_s }
assert_equal file_data, output.string assert_equal file_data, output.string
end end


Expand Down

0 comments on commit a6bdae1

Please sign in to comment.