cgi-tempfile.patch

Akira Tanaka, 07/18/2012 08:37 PM

Download (2.95 KB)

View differences:

lib/cgi/core.rb (working copy)
479 479
      bufsize = 10 * 1024
480 480
      max_count = MAX_MULTIPART_COUNT
481 481
      n = 0
482
      tempfiles = []
482 483
      while true
483 484
        (n += 1) < max_count or raise StandardError.new("too many parameters.")
484 485
        ## create body (StringIO or Tempfile)
485 486
        body = create_body(bufsize < content_length)
487
        tempfiles << body if body.kind_of? Tempfile
486 488
        class << body
487 489
          if method_defined?(:path)
488 490
            alias local_path path
......
540 542
        name = $1 || $2 || ''
541 543
        if body.original_filename.empty?
542 544
          value=body.read.dup.force_encoding(@accept_charset)
545
          body.unlink if body.kind_of? Tempfile
543 546
          (params[name] ||= []) << value
544 547
          unless value.valid_encoding?
545 548
            if @accept_charset_error_block
......
563 566
      raise EOFError, "bad boundary end of body part" unless boundary_end =~ /--/
564 567
      params.default = []
565 568
      params
569
    ensure
570
      if $!
571
        tempfiles.each {|t|
572
          if t.path
573
            t.unlink
574
          end
575
        }
576
      end
566 577
    end # read_multipart
567 578
    private :read_multipart
568 579
    def create_body(is_large)  #:nodoc:
test/cgi/test_cgi_multipart.rb (working copy)
107 107

  
108 108
  def setup
109 109
    ENV['REQUEST_METHOD'] = 'POST'
110
    @tempfiles = []
110 111
  end
111 112

  
112 113
  def teardown
......
115 116
    end
116 117
    $stdin.close() if $stdin.is_a?(Tempfile)
117 118
    $stdin = STDIN
119
    @tempfiles.each {|t|
120
      t.unlink
121
    }
118 122
  end
119 123

  
120 124
  def _prepare(data)
......
133 137
    ENV['REQUEST_METHOD'] = 'POST'
134 138
    ## set $stdin
135 139
    tmpfile = Tempfile.new('test_cgi_multipart')
140
    @tempfiles << tmpfile
136 141
    tmpfile.binmode
137 142
    tmpfile << input
138 143
    tmpfile.rewind()
......
168 173
      assert_equal(hash[:filename] || '', cgi[name].original_filename)  #if hash[:filename]
169 174
      assert_equal(hash[:content_type] || '', cgi[name].content_type)  #if hash[:content_type]
170 175
    end
176
  ensure
177
    if cgi
178
      cgi.params.each {|name, vals|
179
        vals.each {|val|
180
          if val.kind_of?(Tempfile) && val.path
181
            val.unlink
182
          end
183
        }
184
      }
185
    end
171 186
  end
172 187

  
173 188

  
......
314 329
    cgi = RUBY_VERSION>="1.9" ? CGI.new(:accept_charset=>"UTF-8") : CGI.new
315 330
    assert_equal(cgi['foo'], 'bar')
316 331
    assert_equal(cgi['file'].read, 'b'*10134)
332
    cgi['file'].unlink if cgi['file'].kind_of? Tempfile
317 333
  end
318 334

  
319 335
  ###