From 9269d22c56762076e3b775ea65ec1d40494d2402 Mon Sep 17 00:00:00 2001 From: James Tucker Date: Fri, 18 Jul 2014 16:07:27 -0700 Subject: support empty string multipart filename Closes #702. Enables support for IE11. --- lib/rack/multipart/parser.rb | 4 ++++ test/multipart/ie11 | 6 ++++++ test/spec_multipart.rb | 22 +++++++++++++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/multipart/ie11 diff --git a/lib/rack/multipart/parser.rb b/lib/rack/multipart/parser.rb index 22f9734b..324c7d8c 100644 --- a/lib/rack/multipart/parser.rb +++ b/lib/rack/multipart/parser.rb @@ -116,6 +116,10 @@ module Rack name = filename end + if filename && filename.empty? + filename = name + end + if filename (@env['rack.tempfiles'] ||= []) << body = Tempfile.new("RackMultipart") body.binmode if body.respond_to?(:binmode) diff --git a/test/multipart/ie11 b/test/multipart/ie11 new file mode 100644 index 00000000..6c3da47f --- /dev/null +++ b/test/multipart/ie11 @@ -0,0 +1,6 @@ +--AaB03x +Content-Disposition: form-data; name="files"; filename="" +Content-Type: text/plain + +contents +--AaB03x-- diff --git a/test/spec_multipart.rb b/test/spec_multipart.rb index 2acb6e0d..959a5cff 100644 --- a/test/spec_multipart.rb +++ b/test/spec_multipart.rb @@ -230,12 +230,15 @@ describe Rack::Multipart do params["files"][:tempfile].read.should.equal "contents" end - should "not include file params if no file was selected" do + # n.b. this case used to be "do not include", but because ie11 now does this + # for selected files, that can no longer be done. It was decided that not + # losing data is better, and no browser is documented with this behavior. + should "include file params if no file was selected" do env = Rack::MockRequest.env_for("/", multipart_fixture(:none)) params = Rack::Multipart.parse_multipart(env) params["submit-name"].should.equal "Larry" - params["files"].should.equal nil - params.keys.should.not.include "files" + params["files"].should.not.equal nil + params.keys.should.include "files" end should "parse multipart/mixed" do @@ -267,6 +270,19 @@ describe Rack::Multipart do params["files"][:tempfile].read.should.equal "contents" end + should "parse IE11 multipart upload" do + env = Rack::MockRequest.env_for("/", multipart_fixture(:ie11)) + params = Rack::Multipart.parse_multipart(env) + params["files"][:type].should.equal "text/plain" + params["files"][:filename].should.equal "files" + params["files"][:head].should.equal "Content-Disposition: form-data; " + + "name=\"files\"; " + + 'filename=""' + + "\r\nContent-Type: text/plain\r\n" + params["files"][:name].should.equal "files" + params["files"][:tempfile].read.should.equal "contents" + end + should "parse filename and modification param" do env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_and_modification_param)) params = Rack::Multipart.parse_multipart(env) -- cgit v1.2.3-24-ge0c7