Skip to content

Commit

Permalink
If file extension is unknown, send_file will always fall back to appl…
Browse files Browse the repository at this point in the history
…ication/octet-stream rather than complaining. Fixes #127.
  • Loading branch information
rkh committed Nov 26, 2010
1 parent 45f954f commit 6d3d45d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def mime_type(type)
# 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 }
Expand All @@ -165,10 +166,8 @@ def send_file(path, opts={})
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
Expand Down
8 changes: 8 additions & 0 deletions test/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ def send_file_app(opts={})
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
Expand Down

0 comments on commit 6d3d45d

Please sign in to comment.