1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add :status option to send_data and send_file. Defaults to '200 OK'. Closes #5243.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4400 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2006-06-02 00:51:56 +00:00
parent 9e7e89a561
commit ac66cf1289
3 changed files with 27 additions and 6 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Add :status option to send_data and send_file. Defaults to '200 OK'. #5243 [Manfred Stienstra <m.stienstra@fngtps.com>]
* Routing rewrite. Simpler, faster, easier to understand. The published API for config/routes.rb is unchanged, but nearly everything else is different, so expect breakage in plugins and libs that try to fiddle with routes. [Nicholas Seckar, Jamis Buck]
map.connect '/foo/:id', :controller => '...', :action => '...'

View file

@ -28,6 +28,7 @@ module ActionController #:nodoc:
# or to read the entire file before sending (false). Defaults to true.
# * <tt>:buffer_size</tt> - specifies size (in bytes) of the buffer used to stream the file.
# Defaults to 4096.
# * <tt>:status</tt> - specifies the status code to send with the response. Defaults to '200 OK'.
#
# The default Content-Type and Content-Disposition headers are
# set to download arbitrary binary files in as many browsers as
@ -37,9 +38,12 @@ module ActionController #:nodoc:
# Simple download:
# send_file '/path/to.zip'
#
# Show a JPEG in browser:
# Show a JPEG in the browser:
# send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'
#
# Show a 404 page in the browser:
# send_file '/path/to/404.html, :type => 'text/html; charset=utf-8', :status => 404
#
# Read about the other Content-* HTTP headers if you'd like to
# provide the user with more information (such as Content-Description).
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11
@ -61,7 +65,7 @@ module ActionController #:nodoc:
@performed_render = false
if options[:stream]
render :text => Proc.new { |response, output|
render :status => options[:status], :text => Proc.new { |response, output|
logger.info "Streaming file #{path}" unless logger.nil?
len = options[:buffer_size] || 4096
File.open(path, 'rb') do |file|
@ -81,7 +85,7 @@ module ActionController #:nodoc:
}
else
logger.info "Sending file #{path}" unless logger.nil?
File.open(path, 'rb') { |file| render :text => file.read }
File.open(path, 'rb') { |file| render :status => options[:status], :text => file.read }
end
end
@ -93,6 +97,7 @@ module ActionController #:nodoc:
# * <tt>:type</tt> - specifies an HTTP content type.
# Defaults to 'application/octet-stream'.
# * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.
# * <tt>:status</tt> - specifies the status code to send with the response. Defaults to '200 OK'.
# Valid values are 'inline' and 'attachment' (default).
#
# Generic data download:
@ -109,7 +114,7 @@ module ActionController #:nodoc:
logger.info "Sending data #{options[:filename]}" unless logger.nil?
send_file_headers! options.merge(:length => data.size)
@performed_render = false
render :text => data
render :status => options[:status], :text => data
end
private
@ -139,4 +144,4 @@ module ActionController #:nodoc:
@headers['Cache-Control'] = 'private' if @headers['Cache-Control'] == 'no-cache'
end
end
end
end

View file

@ -85,11 +85,25 @@ class SendFileTest < Test::Unit::TestCase
assert_equal 'type', h['Content-Type']
assert_equal 'disposition; filename="filename"', h['Content-Disposition']
assert_equal 'binary', h['Content-Transfer-Encoding']
# test overriding Cache-Control: no-cache header to fix IE open/save dialog
@controller.headers = { 'Cache-Control' => 'no-cache' }
@controller.send(:send_file_headers!, options)
h = @controller.headers
assert_equal 'private', h['Cache-Control']
end
%w(file data).each do |method|
define_method "test_send_#{method}_status" do
@controller.options = { :stream => false, :status => 500 }
assert_nothing_raised { assert_not_nil process(method) }
assert_equal '500', @controller.headers['Status']
end
define_method "test_default_send_#{method}_status" do
@controller.options = { :stream => false }
assert_nothing_raised { assert_not_nil process(method) }
assert_equal ActionController::Base::DEFAULT_RENDER_STATUS_CODE, @controller.headers['Status']
end
end
end