From ac66cf1289a18504668baffeb1808540103ddc7a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 2 Jun 2006 00:51:56 +0000 Subject: 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 --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/streaming.rb | 15 ++++++++++----- actionpack/test/controller/send_file_test.rb | 16 +++++++++++++++- 3 files changed, 27 insertions(+), 6 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 2af08a5ae2..9d6dcfc80d 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add :status option to send_data and send_file. Defaults to '200 OK'. #5243 [Manfred Stienstra ] + * 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 => '...' diff --git a/actionpack/lib/action_controller/streaming.rb b/actionpack/lib/action_controller/streaming.rb index c265c86045..618888d096 100644 --- a/actionpack/lib/action_controller/streaming.rb +++ b/actionpack/lib/action_controller/streaming.rb @@ -28,6 +28,7 @@ module ActionController #:nodoc: # or to read the entire file before sending (false). Defaults to true. # * :buffer_size - specifies size (in bytes) of the buffer used to stream the file. # Defaults to 4096. + # * :status - 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: # * :type - specifies an HTTP content type. # Defaults to 'application/octet-stream'. # * :disposition - specifies whether the file will be shown inline or downloaded. + # * :status - 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 \ No newline at end of file +end diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb index 683d6eba60..4c97e2d5d2 100644 --- a/actionpack/test/controller/send_file_test.rb +++ b/actionpack/test/controller/send_file_test.rb @@ -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 -- cgit v1.2.3