fix: malformed charset param (#2263)
[rack.git] / lib / rack / tempfile_reaper.rb
blob0b94cc73a157b52de72fb9706079c3c2947d972b
1 # frozen_string_literal: true
3 require_relative 'constants'
4 require_relative 'body_proxy'
6 module Rack
8   # Middleware tracks and cleans Tempfiles created throughout a request (i.e. Rack::Multipart)
9   # Ideas/strategy based on posts by Eric Wong and Charles Oliver Nutter
10   # https://groups.google.com/forum/#!searchin/rack-devel/temp/rack-devel/brK8eh-MByw/sw61oJJCGRMJ
11   class TempfileReaper
12     def initialize(app)
13       @app = app
14     end
16     def call(env)
17       env[RACK_TEMPFILES] ||= []
19       begin
20         _, _, body = response = @app.call(env)
21       rescue Exception
22         env[RACK_TEMPFILES]&.each(&:close!)
23         raise
24       end
26       response[2] = BodyProxy.new(body) do
27         env[RACK_TEMPFILES]&.each(&:close!)
28       end
30       response
31     end
32   end
33 end