Permalink
Browse files

Add :status option to send_data and send_file. Defaults to '200 OK'. C…

…loses #5243.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4400 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9e7e89a commit ac66cf1289a18504668baffeb1808540103ddc7a @jeremy jeremy committed Jun 2, 2006
View
@@ -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 => '...'
@@ -28,6 +28,7 @@ module Streaming
# 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 Streaming
# 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 @@ def send_file(path, options = {}) #:doc:
@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 @@ def send_file(path, options = {}) #:doc:
}
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 @@ def send_file(path, options = {}) #:doc:
# * <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 @@ def send_data(data, options = {}) #:doc:
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 @@ def send_file_headers!(options)
@headers['Cache-Control'] = 'private' if @headers['Cache-Control'] == 'no-cache'
end
end
-end
+end
@@ -85,11 +85,25 @@ def test_send_file_headers!
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

0 comments on commit ac66cf1

Please sign in to comment.