aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/lib/action_dispatch/http/upload.rb13
-rw-r--r--actionpack/test/dispatch/uploaded_file_test.rb12
2 files changed, 24 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/http/upload.rb b/actionpack/lib/action_dispatch/http/upload.rb
index 37effade4f..ed6140dac5 100644
--- a/actionpack/lib/action_dispatch/http/upload.rb
+++ b/actionpack/lib/action_dispatch/http/upload.rb
@@ -4,7 +4,7 @@ module ActionDispatch
attr_accessor :original_filename, :content_type, :tempfile, :headers
def initialize(hash)
- @original_filename = hash[:filename]
+ @original_filename = encode_filename(hash[:filename])
@content_type = hash[:type]
@headers = hash[:head]
@tempfile = hash[:tempfile]
@@ -30,6 +30,17 @@ module ActionDispatch
def size
@tempfile.size
end
+
+ private
+ def encode_filename(filename)
+ # Encode the filename in the default_external encoding, unless it is nil or we're in 1.8
+ if "ruby".encoding_aware? && filename
+ encoding = Encoding.default_external
+ filename.force_encoding(encoding)
+ else
+ filename
+ end
+ end
end
module Upload
diff --git a/actionpack/test/dispatch/uploaded_file_test.rb b/actionpack/test/dispatch/uploaded_file_test.rb
index e2a7f1bad7..5e0c5fb21e 100644
--- a/actionpack/test/dispatch/uploaded_file_test.rb
+++ b/actionpack/test/dispatch/uploaded_file_test.rb
@@ -12,6 +12,18 @@ module ActionDispatch
uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
assert_equal 'foo', uf.original_filename
end
+
+ if "ruby".encoding_aware?
+ def test_filename_should_be_in_default_encoding
+ Encoding.default_external = "UTF-16LE"
+ uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
+ assert "UTF-16LE", uf.original_filename.encoding.to_s
+
+ Encoding.default_external = "UTF-8"
+ uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
+ assert "UTF-8", uf.original_filename.encoding.to_s
+ end
+ end
def test_content_type
uf = Http::UploadedFile.new(:type => 'foo', :tempfile => Object.new)