Skip to content

Commit

Permalink
Mon Nov 5 09:55:05 2012 Takeyuki FUJIOKA <xibbar@ruby-lang.org>
Browse files Browse the repository at this point in the history
* lib/cgi/core.rb: remove tempfile more early.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37471 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
xibbar committed Nov 5, 2012
1 parent d5528ec commit 1362d81
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/cgi/core.rb
Expand Up @@ -479,10 +479,12 @@ def read_multipart(boundary, content_length)
bufsize = 10 * 1024
max_count = MAX_MULTIPART_COUNT
n = 0
tempfiles = []
while true
(n += 1) < max_count or raise StandardError.new("too many parameters.")
## create body (StringIO or Tempfile)
body = create_body(bufsize < content_length)
tempfiles << body if body.kind_of? Tempfile
class << body
if method_defined?(:path)
alias local_path path
Expand Down Expand Up @@ -540,6 +542,7 @@ def local_path
name = $1 || $2 || ''
if body.original_filename.empty?
value=body.read.dup.force_encoding(@accept_charset)
body.unlink if body.kind_of? Tempfile
(params[name] ||= []) << value
unless value.valid_encoding?
if @accept_charset_error_block
Expand All @@ -563,6 +566,14 @@ class << params[name].last;self;end.class_eval do
raise EOFError, "bad boundary end of body part" unless boundary_end =~ /--/
params.default = []
params
ensure
if $!
tempfiles.each {|t|
if t.path
t.unlink
end
}
end
end # read_multipart
private :read_multipart
def create_body(is_large) #:nodoc:
Expand Down
16 changes: 16 additions & 0 deletions test/cgi/test_cgi_multipart.rb
Expand Up @@ -107,6 +107,7 @@ class CGIMultipartTest < Test::Unit::TestCase

def setup
ENV['REQUEST_METHOD'] = 'POST'
@tempfiles = []
end

def teardown
Expand All @@ -115,6 +116,9 @@ def teardown
end
$stdin.close() if $stdin.is_a?(Tempfile)
$stdin = STDIN
@tempfiles.each {|t|
t.unlink
}
end

def _prepare(data)
Expand All @@ -133,6 +137,7 @@ def _prepare(data)
ENV['REQUEST_METHOD'] = 'POST'
## set $stdin
tmpfile = Tempfile.new('test_cgi_multipart')
@tempfiles << tmpfile
tmpfile.binmode
tmpfile << input
tmpfile.rewind()
Expand Down Expand Up @@ -168,6 +173,16 @@ def _test_multipart
assert_equal(hash[:filename] || '', cgi[name].original_filename) #if hash[:filename]
assert_equal(hash[:content_type] || '', cgi[name].content_type) #if hash[:content_type]
end
ensure
if cgi
cgi.params.each {|name, vals|
vals.each {|val|
if val.kind_of?(Tempfile) && val.path
val.unlink
end
}
}
end
end


Expand Down Expand Up @@ -314,6 +329,7 @@ def test_cgi_multipart_boundary_10240 # [Bug #3866]
cgi = RUBY_VERSION>="1.9" ? CGI.new(:accept_charset=>"UTF-8") : CGI.new
assert_equal(cgi['foo'], 'bar')
assert_equal(cgi['file'].read, 'b'*10134)
cgi['file'].unlink if cgi['file'].kind_of? Tempfile
end

###
Expand Down

0 comments on commit 1362d81

Please sign in to comment.