Skip to content

Commit

Permalink
Trim IE's full file path in multipart uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jan 17, 2009
1 parent 2d383fa commit 5368e30
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/rack/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ def self.parse_multipart(env)

if filename
body.rewind

# Take the basename of the upload's original filename.
# This handles the full Windows paths given by Internet Explorer
# (and perhaps other broken user agents) without affecting
# those which give the lone filename.
filename =~ /^(?:.*[:\\\/])?(.*)/m
filename = $1

data = {:filename => filename, :type => content_type,
:name => name, :tempfile => body, :head => head}
else
Expand Down
6 changes: 6 additions & 0 deletions test/multipart/ie
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--AaB03x
Content-Disposition: form-data; name="files"; filename="C:\Documents and Settings\Administrator\Desktop\file1.txt"
Content-Type: text/plain

contents
--AaB03x--
15 changes: 14 additions & 1 deletion test/spec_rack_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Rack::Utils.parse_query("my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F").
should.equal "my weird field" => "q1!2\"'w$5&7/z8)?"
end

specify "should build query strings correctly" do
Rack::Utils.build_query("foo" => "bar").should.equal "foo=bar"
Rack::Utils.build_query("foo" => ["bar", "quux"]).
Expand Down Expand Up @@ -184,6 +184,19 @@ def context env, app=@app; app.call(env); end
params["files"][:tempfile].read.length.should.equal 26473
end

specify "should parse IE multipart upload and clean up filename" do
env = Rack::MockRequest.env_for("/", multipart_fixture(:ie))
params = Rack::Utils::Multipart.parse_multipart(env)
params["files"][:type].should.equal "text/plain"
params["files"][:filename].should.equal "file1.txt"
params["files"][:head].should.equal "Content-Disposition: form-data; " +
"name=\"files\"; " +
'filename="C:\Documents and Settings\Administrator\Desktop\file1.txt"' +
"\r\nContent-Type: text/plain\r\n"
params["files"][:name].should.equal "files"
params["files"][:tempfile].read.should.equal "contents"
end

specify "rewinds input after parsing upload" do
options = multipart_fixture(:text)
input = options[:input]
Expand Down

0 comments on commit 5368e30

Please sign in to comment.