diff --git a/lib/rack/deflater.rb b/lib/rack/deflater.rb index e7da3a3ae..a5711333c 100644 --- a/lib/rack/deflater.rb +++ b/lib/rack/deflater.rb @@ -45,6 +45,7 @@ def call(env) when "identity" [status, headers, body] when nil + body.close if body.respond_to?(:close) message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found." [406, {"Content-Type" => "text/plain", "Content-Length" => message.length.to_s}, [message]] end @@ -64,6 +65,7 @@ def each(&block) gzip.write(part) gzip.flush } + ensure @body.close if @body.respond_to?(:close) gzip.close @writer = nil @@ -90,9 +92,11 @@ def initialize(body) def each deflater = ::Zlib::Deflate.new(*DEFLATE_ARGS) @body.each { |part| yield deflater.deflate(part, Zlib::SYNC_FLUSH) } - @body.close if @body.respond_to?(:close) yield deflater.finish nil + ensure + @body.close if @body.respond_to?(:close) + deflater.close end end end