aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller/base.rb12
-rw-r--r--actionpack/lib/action_controller/integration.rb10
-rw-r--r--actionpack/lib/action_controller/response.rb41
-rw-r--r--actionpack/lib/action_controller/test_process.rb4
-rw-r--r--actionpack/lib/action_view/base.rb6
-rw-r--r--actionpack/lib/action_view/helpers/capture_helper.rb8
-rw-r--r--actionpack/test/controller/rack_test.rb2
-rw-r--r--actionpack/test/controller/send_file_test.rb4
-rw-r--r--actionpack/test/template/body_parts_test.rb22
-rw-r--r--actionpack/test/template/output_buffer_test.rb35
10 files changed, 118 insertions, 26 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index 0facf7066d..c6dd99e959 100644
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -984,6 +984,7 @@ module ActionController #:nodoc:
# of sending it as the response body to the browser.
def render_to_string(options = nil, &block) #:doc:
render(options, &block)
+ response.body
ensure
response.content_type = nil
erase_render_results
@@ -1020,7 +1021,7 @@ module ActionController #:nodoc:
# Clears the rendered results, allowing for another render to be performed.
def erase_render_results #:nodoc:
- response.body = nil
+ response.body = []
@performed_render = false
end
@@ -1247,13 +1248,12 @@ module ActionController #:nodoc:
response.status = interpret_status(status || DEFAULT_RENDER_STATUS_CODE)
if append_response
- response.body ||= ''
- response.body << text.to_s
+ response.body_parts << text.to_s
else
response.body = case text
- when Proc then text
- when nil then " " # Safari doesn't pass the headers of the return if the response is zero length
- else text.to_s
+ when Proc then text
+ when nil then [" "] # Safari doesn't pass the headers of the return if the response is zero length
+ else [text.to_s]
end
end
end
diff --git a/actionpack/lib/action_controller/integration.rb b/actionpack/lib/action_controller/integration.rb
index 26b695570b..fda6b639d1 100644
--- a/actionpack/lib/action_controller/integration.rb
+++ b/actionpack/lib/action_controller/integration.rb
@@ -332,11 +332,13 @@ module ActionController
@cookies[name] = value
end
- @body = ""
if body.is_a?(String)
- @body << body
+ @body_parts = [body]
+ @body = body
else
- body.each { |part| @body << part }
+ @body_parts = []
+ body.each { |part| @body_parts << part.to_s }
+ @body = @body_parts.join
end
if @controller = ActionController::Base.last_instantiation
@@ -349,7 +351,7 @@ module ActionController
@response = Response.new
@response.status = status.to_s
@response.headers.replace(@headers)
- @response.body = @body
+ @response.body = @body_parts
end
# Decorate the response with the standard behavior of the
diff --git a/actionpack/lib/action_controller/response.rb b/actionpack/lib/action_controller/response.rb
index ccff473df0..febe4ccf29 100644
--- a/actionpack/lib/action_controller/response.rb
+++ b/actionpack/lib/action_controller/response.rb
@@ -40,14 +40,28 @@ module ActionController # :nodoc:
delegate :default_charset, :to => 'ActionController::Base'
def initialize
- @status = 200
+ super
@header = Rack::Utils::HeaderHash.new(DEFAULT_HEADERS)
+ @session, @assigns = [], []
+ end
- @writer = lambda { |x| @body << x }
- @block = nil
+ def body
+ str = ''
+ each { |part| str << part.to_s }
+ str
+ end
- @body = "",
- @session, @assigns = [], []
+ def body=(body)
+ @body =
+ if body.is_a?(String)
+ [body]
+ else
+ body
+ end
+ end
+
+ def body_parts
+ @body
end
def location; headers['Location'] end
@@ -152,7 +166,7 @@ module ActionController # :nodoc:
@writer = lambda { |x| callback.call(x) }
@body.call(self, self)
elsif @body.is_a?(String)
- @body.each_line(&callback)
+ callback.call(@body)
else
@body.each(&callback)
end
@@ -162,7 +176,8 @@ module ActionController # :nodoc:
end
def write(str)
- @writer.call str.to_s
+ str = str.to_s
+ @writer.call str
str
end
@@ -186,7 +201,7 @@ module ActionController # :nodoc:
if request && request.etag_matches?(etag)
self.status = '304 Not Modified'
- self.body = ''
+ self.body = []
end
set_conditional_cache_control!
@@ -195,7 +210,11 @@ module ActionController # :nodoc:
def nonempty_ok_response?
ok = !status || status.to_s[0..2] == '200'
- ok && body.is_a?(String) && !body.empty?
+ ok && string_body?
+ end
+
+ def string_body?
+ !body_parts.respond_to?(:call) && body_parts.any? && body_parts.all? { |part| part.is_a?(String) }
end
def set_conditional_cache_control!
@@ -216,8 +235,8 @@ module ActionController # :nodoc:
headers.delete('Content-Length')
elsif length = headers['Content-Length']
headers['Content-Length'] = length.to_s
- elsif !body.respond_to?(:call) && (!status || status.to_s[0..2] != '304')
- headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
+ elsif string_body? && (!status || status.to_s[0..2] != '304')
+ headers["Content-Length"] = Rack::Utils.bytesize(body).to_s
end
end
diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb
index dbaec00bee..9dd09c30b4 100644
--- a/actionpack/lib/action_controller/test_process.rb
+++ b/actionpack/lib/action_controller/test_process.rb
@@ -258,11 +258,11 @@ module ActionController #:nodoc:
# Returns binary content (downloadable file), converted to a String
def binary_content
- raise "Response body is not a Proc: #{body.inspect}" unless body.kind_of?(Proc)
+ raise "Response body is not a Proc: #{body_parts.inspect}" unless body_parts.kind_of?(Proc)
require 'stringio'
sio = StringIO.new
- body.call(self, sio)
+ body_parts.call(self, sio)
sio.rewind
sio.read
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index e19acc5c29..9c0134e7f7 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -303,6 +303,12 @@ module ActionView #:nodoc:
self.template = last_template
end
+ def punctuate_body!(part)
+ flush_output_buffer
+ response.body_parts << part
+ nil
+ end
+
private
# Evaluates the local assigns and controller ivars, pushes them to the view.
def _evaluate_assigns_and_ivars #:nodoc:
diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb
index e86ca27f31..9e39536653 100644
--- a/actionpack/lib/action_view/helpers/capture_helper.rb
+++ b/actionpack/lib/action_view/helpers/capture_helper.rb
@@ -131,6 +131,14 @@ module ActionView
ensure
self.output_buffer = old_buffer
end
+
+ # Add the output buffer to the response body and start a new one.
+ def flush_output_buffer #:nodoc:
+ if output_buffer && output_buffer != ''
+ response.body_parts << output_buffer
+ self.output_buffer = ''
+ end
+ end
end
end
end
diff --git a/actionpack/test/controller/rack_test.rb b/actionpack/test/controller/rack_test.rb
index b550d3db78..89bf4fdacc 100644
--- a/actionpack/test/controller/rack_test.rb
+++ b/actionpack/test/controller/rack_test.rb
@@ -258,7 +258,7 @@ class RackResponseTest < BaseRackTest
}, headers)
parts = []
- body.each { |part| parts << part }
+ body.each { |part| parts << part.to_s }
assert_equal ["0", "1", "2", "3", "4"], parts
end
end
diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb
index a27e951929..3d1904fee9 100644
--- a/actionpack/test/controller/send_file_test.rb
+++ b/actionpack/test/controller/send_file_test.rb
@@ -44,12 +44,12 @@ class SendFileTest < ActionController::TestCase
response = nil
assert_nothing_raised { response = process('file') }
assert_not_nil response
- assert_kind_of Proc, response.body
+ assert_kind_of Proc, response.body_parts
require 'stringio'
output = StringIO.new
output.binmode
- assert_nothing_raised { response.body.call(response, output) }
+ assert_nothing_raised { response.body_parts.call(response, output) }
assert_equal file_data, output.string
end
diff --git a/actionpack/test/template/body_parts_test.rb b/actionpack/test/template/body_parts_test.rb
new file mode 100644
index 0000000000..4c82b75cdc
--- /dev/null
+++ b/actionpack/test/template/body_parts_test.rb
@@ -0,0 +1,22 @@
+require 'abstract_unit'
+
+class BodyPartsTest < ActionController::TestCase
+ RENDERINGS = [Object.new, Object.new, Object.new]
+
+ class TestController < ActionController::Base
+ def index
+ RENDERINGS.each do |rendering|
+ response.template.punctuate_body! rendering
+ end
+ @performed_render = true
+ end
+ end
+
+ tests TestController
+
+ def test_body_parts
+ get :index
+ assert_equal RENDERINGS, @response.body_parts
+ assert_equal RENDERINGS.join, @response.body
+ end
+end
diff --git a/actionpack/test/template/output_buffer_test.rb b/actionpack/test/template/output_buffer_test.rb
new file mode 100644
index 0000000000..6d8eab63dc
--- /dev/null
+++ b/actionpack/test/template/output_buffer_test.rb
@@ -0,0 +1,35 @@
+require 'abstract_unit'
+
+class OutputBufferTest < ActionController::TestCase
+ class TestController < ActionController::Base
+ def index
+ render :text => 'foo'
+ end
+ end
+
+ tests TestController
+
+ def test_flush_output_buffer
+ # Start with the default body parts
+ get :index
+ assert_equal ['foo'], @response.body_parts
+ assert_nil @response.template.output_buffer
+
+ # Nil output buffer is skipped
+ @response.template.flush_output_buffer
+ assert_nil @response.template.output_buffer
+ assert_equal ['foo'], @response.body_parts
+
+ # Empty output buffer is skipped
+ @response.template.output_buffer = ''
+ @response.template.flush_output_buffer
+ assert_equal '', @response.template.output_buffer
+ assert_equal ['foo'], @response.body_parts
+
+ # Flushing appends the output buffer to the body parts
+ @response.template.output_buffer = 'bar'
+ @response.template.flush_output_buffer
+ assert_equal '', @response.template.output_buffer
+ assert_equal ['foo', 'bar'], @response.body_parts
+ end
+end