Skip to content

Commit

Permalink
Fix streaming by having it create a File object, which can be handled…
Browse files Browse the repository at this point in the history
… by Rack servers as appropriate
  • Loading branch information
Carlhuda committed Feb 23, 2010
1 parent ee54104 commit 3345af6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
20 changes: 6 additions & 14 deletions actionpack/lib/action_controller/metal/streaming.rb
Expand Up @@ -79,6 +79,8 @@ module Streaming
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
# for the Cache-Control header spec.
def send_file(path, options = {}) #:doc:
# self.response_body = File.open(path)

raise MissingFile, "Cannot read file #{path}" unless File.file?(path) and File.readable?(path)

options[:length] ||= File.size(path)
Expand All @@ -90,19 +92,9 @@ def send_file(path, options = {}) #:doc:
if options[:x_sendfile]
head options[:status], X_SENDFILE_HEADER => path
else
if options[:stream]
# TODO : Make render :text => proc {} work with the new base
render :status => options[:status], :text => Proc.new { |response, output|
len = options[:buffer_size] || 4096
File.open(path, 'rb') do |file|
while buf = file.read(len)
output.write(buf)
end
end
}
else
File.open(path, 'rb') { |file| render :status => options[:status], :text => file.read }
end
self.status = options[:status] || 200
self.content_type = options[:content_type] if options.key?(:content_type)
self.response_body = File.open(path, "rb")
end
end

Expand Down Expand Up @@ -139,7 +131,7 @@ def send_file(path, options = {}) #:doc:
# instead. See ActionController::Base#render for more information.
def send_data(data, options = {}) #:doc:
send_file_headers! options.merge(:length => data.bytesize)
render :status => options[:status], :text => data
render options.slice(:status, :content_type).merge(:text => data)
end

private
Expand Down
1 change: 0 additions & 1 deletion actionpack/lib/action_controller/metal/testing.rb
Expand Up @@ -13,7 +13,6 @@ def process_with_new_base_test(request, response)
if cookies = @_request.env['action_dispatch.cookies']
cookies.write(@_response)
end
@_response.body ||= self.response_body
@_response.prepare!
set_test_assigns
ret
Expand Down
14 changes: 11 additions & 3 deletions actionpack/test/controller/send_file_test.rb
Expand Up @@ -46,15 +46,17 @@ def test_file_nostream
response = nil
assert_nothing_raised { response = process('file') }
assert_not_nil response
assert_kind_of String, response.body
assert_equal file_data, response.body
body = response.body
assert_kind_of String, body
assert_equal file_data, body
end

def test_file_stream
response = nil
assert_nothing_raised { response = process('file') }
assert_not_nil response
assert_kind_of Array, response.body_parts
assert response.body_parts.respond_to?(:each)
assert response.body_parts.respond_to?(:to_path)

require 'stringio'
output = StringIO.new
Expand Down Expand Up @@ -160,6 +162,12 @@ def test_send_file_headers_with_bad_symbol
assert_equal 500, @response.status
end

define_method "test_send_#{method}_content_type" do
@controller.options = { :stream => false, :content_type => "application/x-ruby" }
assert_nothing_raised { assert_not_nil process(method) }
assert_equal "application/x-ruby", @response.content_type
end

define_method "test_default_send_#{method}_status" do
@controller.options = { :stream => false }
assert_nothing_raised { assert_not_nil process(method) }
Expand Down
1 change: 1 addition & 0 deletions activesupport/lib/active_support/ruby/shim.rb
Expand Up @@ -17,3 +17,4 @@
require 'active_support/core_ext/string/interpolation'
require 'active_support/core_ext/rexml'
require 'active_support/core_ext/time/conversions'
require 'active_support/core_ext/file/path'
4 changes: 4 additions & 0 deletions activesupport/test/core_ext/file_test.rb
Expand Up @@ -57,6 +57,10 @@ def test_atomic_write_preserves_default_file_permissions
File.unlink(file_name) rescue nil
end

def test_responds_to_to_path
assert_equal __FILE__, File.open(__FILE__, "r").to_path
end

private
def file_name
"atomic.file"
Expand Down

0 comments on commit 3345af6

Please sign in to comment.