This file combines 7bda8d4
& d860397
from rack 1.5.2 to fix a race condition in Rack::Deflater
.
It can be used to patch rack 1.4.5 for apps that can't upgrade to rack 1.5.2 (e.g. as an initializer in a rails 3.2 app)
edit: added 6c186f2
, too
module Rack | |
class Deflater | |
class GzipStream | |
def initialize(body, mtime) | |
@body = body | |
@mtime = mtime | |
@closed = false | |
end | |
def each(&block) | |
@writer = block | |
gzip =::Zlib::GzipWriter.new(self) | |
gzip.mtime = @mtime | |
@body.each { |part| | |
gzip.write(part) | |
gzip.flush | |
} | |
ensure | |
gzip.close | |
@writer = nil | |
end | |
def close | |
return if @closed | |
@closed = true | |
@body.close if @body.respond_to?(:close) | |
end | |
end | |
class DeflateStream | |
def initialize(body) | |
@body = body | |
@closed = false | |
end | |
def each | |
deflator = ::Zlib::Deflate.new(*DEFLATE_ARGS) | |
@body.each { |part| yield deflator.deflate(part, Zlib::SYNC_FLUSH) } | |
yield deflator.finish | |
nil | |
ensure | |
deflator.close | |
end | |
def close | |
return if @closed | |
@closed = true | |
@body.close if @body.respond_to?(:close) | |
end | |
end | |
end | |
end |