lib/rack/multipart/parser.rb in rack-2.0.9.2 vs lib/rack/multipart/parser.rb in rack-2.0.9.3

- old
+ new

@@ -1,10 +1,11 @@ require 'rack/utils' module Rack module Multipart class MultipartPartLimitError < Errno::EMFILE; end + class MultipartTotalPartLimitError < StandardError; end class Parser BUFSIZE = 16384 TEXT_PLAIN = "text/plain" TEMPFILE_FACTORY = lambda { |filename, content_type| @@ -136,11 +137,12 @@ body = String.new klass = BufferPart end @mime_parts[mime_index] = klass.new(body, head, filename, content_type, name) - check_open_files + + check_part_limits end def on_mime_body mime_index, content @mime_parts[mime_index].body << content end @@ -148,14 +150,24 @@ def on_mime_finish mime_index end private - def check_open_files - if Utils.multipart_part_limit > 0 - if @open_files >= Utils.multipart_part_limit + def check_part_limits + file_limit = Utils.multipart_file_limit + part_limit = Utils.multipart_total_part_limit + + if file_limit && file_limit > 0 + if @open_files >= file_limit @mime_parts.each(&:close) raise MultipartPartLimitError, 'Maximum file multiparts in content reached' + end + end + + if part_limit && part_limit > 0 + if @mime_parts.size >= part_limit + @mime_parts.each(&:close) + raise MultipartTotalPartLimitError, 'Maximum total multiparts in content reached' end end end end