If file extension is unknown, send_file will always fall back to application/octet-stream rather than complaining. Fixes #127.

This commit is contained in:
Konstantin Haase 2010-11-26 09:12:31 +01:00
parent 45f954fa32
commit 6d3d45df10
2 changed files with 12 additions and 5 deletions

View File

@ -140,7 +140,8 @@ module Sinatra
# Set the Content-Type of the response body given a media type or file
# extension.
def content_type(type, params={})
mime_type = mime_type(type)
default = params.delete :default
mime_type = mime_type(type) || default
fail "Unknown media type: %p" % type if mime_type.nil?
mime_type = mime_type.dup
unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
@ -165,10 +166,8 @@ module Sinatra
stat = File.stat(path)
last_modified stat.mtime
content_type opts[:type] ||
File.extname(path) ||
response['Content-Type'] ||
'application/octet-stream'
content_type opts[:type] || File.extname(path),
:default => response['Content-Type'] || 'application/octet-stream'
if opts[:disposition] == 'attachment' || opts[:filename]
attachment opts[:filename] || path

View File

@ -446,6 +446,14 @@ class HelpersTest < Test::Unit::TestCase
get '/file.txt'
assert_equal 'attachment; filename="foo.txt"', response['Content-Disposition']
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') }
send_file_app
get '/file.txt'
assert_equal 'application/octet-stream', response['Content-Type']
end
end
describe 'cache_control' do