diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-08-15 08:39:37 -0700 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-08-15 08:39:37 -0700 |
commit | b4dce47694ec7e3d3940d15141eec7f2c4e7a486 (patch) | |
tree | b903d9d05169e430f8b150dcf12698f894bcd8e8 /actionpack | |
parent | 8055cd65688d5aee9bf756c8483f2c30f75d06cc (diff) | |
parent | bccc35b13e4782bde4a8def9b52c21f1eda0b4b1 (diff) | |
download | rails-b4dce47694ec7e3d3940d15141eec7f2c4e7a486.tar.gz rails-b4dce47694ec7e3d3940d15141eec7f2c4e7a486.tar.bz2 rails-b4dce47694ec7e3d3940d15141eec7f2c4e7a486.zip |
Merge pull request #7027 from erichmenge/patch/jruby_send_file
Backport 5c51cd0 to fix an issue with jRuby encodings. Fixes #6844
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG.md | 6 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/data_streaming.rb | 24 |
2 files changed, 28 insertions, 2 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 6a7087a4bd..9172c78eba 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -7,6 +7,12 @@ *Ravil Bayramgalin* +* Performance Improvement to send_file: Avoid having to pass an open file handle as the response body. Rack::Sendfile + will usually intercept the response and just uses the path directly, so no reason to open the file. This performance + improvement also resolves an issue with jRuby encodings, and is the reason for the backport, see issue #6844. + + *Jeremy Kemper & Erich Menge* + ## Rails 3.2.8 (Aug 9, 2012) ## diff --git a/actionpack/lib/action_controller/metal/data_streaming.rb b/actionpack/lib/action_controller/metal/data_streaming.rb index 0670a58d97..62e16ea047 100644 --- a/actionpack/lib/action_controller/metal/data_streaming.rb +++ b/actionpack/lib/action_controller/metal/data_streaming.rb @@ -75,7 +75,27 @@ module ActionController #:nodoc: self.status = options[:status] || 200 self.content_type = options[:content_type] if options.key?(:content_type) - self.response_body = File.open(path, "rb") + self.response_body = FileBody.new(path) + end + + # 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 + + # Stream the file's contents if Rack::Sendfile isn't present. + def each + File.open(to_path, 'rb') do |file| + while chunk = file.read(16384) + yield chunk + end + end + end end # Sends the given binary data to the browser. This method is similar to @@ -115,7 +135,7 @@ module ActionController #:nodoc: private def send_file_headers!(options) type_provided = options.has_key?(:type) - + options.update(DEFAULT_SEND_FILE_OPTIONS.merge(options)) [:type, :disposition].each do |arg| raise ArgumentError, ":#{arg} option required" if options[arg].nil? |