diff --git a/CHANGES b/CHANGES index aefd0b61..b26530f3 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,8 @@ * No longer include Sinatra::Delegator in Object, instead extend the main object only. (Konstantin Haase) + * Add :status option support to send_file. (Konstantin Haase) + * Exception#code is only used when :use_code is enabled and displays a warning. Moreover, it will be ignored if the value is not between 400 and 599. You should use Exception#http_status instead. (Konstantin Haase) diff --git a/README.rdoc b/README.rdoc index 90ba76f8..0a8e8a9e 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1070,6 +1070,9 @@ The options are: [length] Content-Length header, defaults to file size. +[status] + Status code to be send. Useful when sending a static file as an error page. + If supported by the Rack handler, other means than streaming from the Ruby process will be used. If you use this helper method, Sinatra will automatically handle range requests. diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 9731ef3c..11fd038e 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -229,7 +229,7 @@ module Sinatra file.path = path result = file.serving env result[1].each { |k,v| headers[k] ||= v } - halt result[0], result[2] + halt opts[:status] || result[0], result[2] rescue Errno::ENOENT not_found end diff --git a/test/helpers_test.rb b/test/helpers_test.rb index 42d1f9a1..f0bd7de1 100644 --- a/test/helpers_test.rb +++ b/test/helpers_test.rb @@ -729,6 +729,12 @@ class HelpersTest < Test::Unit::TestCase assert_equal 'attachment; filename="foo.txt"', response['Content-Disposition'] end + it 'allows setting a custom status code' do + send_file_app :status => 201 + get '/file.txt' + assert_status 201 + end + it "is able to send files with unkown mime type" do @file = File.dirname(__FILE__) + '/file.foobar' File.open(@file, 'wb') { |io| io.write('Hello World') }