Skip to content
Permalink
Browse files Browse the repository at this point in the history
Fix parsing performance for unquoted filenames
Special thanks to Paul Rogers & Eric Wong

Conflicts:
	test/spec_multipart.rb
  • Loading branch information
raggi authored and James Tucker committed Jan 4, 2013
1 parent e8d1bec commit c9f65df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rack/multipart.rb
Expand Up @@ -12,7 +12,7 @@ module Multipart
MULTIPART = %r|\Amultipart/.*boundary=\"?([^\";,]+)\"?|n
TOKEN = /[^\s()<>,;:\\"\/\[\]?=]+/
CONDISP = /Content-Disposition:\s*#{TOKEN}\s*/i
DISPPARM = /;\s*(#{TOKEN})=("(?:\\"|[^"])*"|#{TOKEN})*/
DISPPARM = /;\s*(#{TOKEN})=("(?:\\"|[^"])*"|#{TOKEN})/
RFC2183 = /^#{CONDISP}(#{DISPPARM})+$/i
BROKEN_QUOTED = /^#{CONDISP}.*;\sfilename="(.*?)"(?:\s*$|\s*;\s*#{TOKEN}=)/i
BROKEN_UNQUOTED = /^#{CONDISP}.*;\sfilename=(#{TOKEN})/i
Expand All @@ -31,4 +31,4 @@ def build_multipart(params, first = true)
end

end
end
end
20 changes: 20 additions & 0 deletions test/spec_multipart.rb
Expand Up @@ -295,4 +295,24 @@ def multipart_file(name)
message.should.equal "value must be a Hash"
end

should "parse very long unquoted multipart file names" do
data = <<-EOF
--AaB03x\r
Content-Type: text/plain\r
Content-Disposition: attachment; name=file; filename=#{'long' * 100}\r
\r
contents\r
--AaB03x--\r
EOF

options = {
"CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
"CONTENT_LENGTH" => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for("/", options)
params = Rack::Utils::Multipart.parse_multipart(env)

params["file"][:filename].should.equal('long' * 100)
end
end

0 comments on commit c9f65df

Please sign in to comment.