Skip to content

Instantly share code, notes, and snippets.

@robotfelix
Forked from gregclermont/README.md
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotfelix/46758dc1c615e1908a1d to your computer and use it in GitHub Desktop.
Save robotfelix/46758dc1c615e1908a1d to your computer and use it in GitHub Desktop.

Forked from a gist by gregclermont

Original README

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

Added by me

edit: added 0f8cd05

module Rack
class Deflater
def call(env)
status, headers, body = @app.call(env)
headers = Utils::HeaderHash.new(headers)
# Skip compressing empty entity body responses and responses with
# no-transform set.
if Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
headers['Cache-Control'].to_s =~ /\bno-transform\b/
return [status, headers, body]
end
request = Request.new(env)
encoding = Utils.select_best_encoding(%w(gzip deflate identity),
request.accept_encoding)
# Set the Vary HTTP header.
vary = headers["Vary"].to_s.split(",").map { |v| v.strip }
unless vary.include?("*") || vary.include?("Accept-Encoding")
headers["Vary"] = vary.push("Accept-Encoding").join(",")
end
case encoding
when "gzip"
headers['Content-Encoding'] = "gzip"
headers.delete('Content-Length')
mtime = headers.key?("Last-Modified") ?
Time.httpdate(headers["Last-Modified"]) : Time.now
[status, headers, GzipStream.new(body, mtime)]
when "deflate"
headers['Content-Encoding'] = "deflate"
headers.delete('Content-Length')
[status, headers, DeflateStream.new(body)]
when "identity"
[status, headers, body]
when nil
message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found."
bp = Rack::BodyProxy.new([message]) { body.close if body.respond_to?(:close) }
[406, {"Content-Type" => "text/plain", "Content-Length" => message.length.to_s}, bp]
end
end
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
@nunosilva800
Copy link

@robotfelix I'm getting these errors after applying the patch:

/app/config/initializers/rack_patch.rb in call at line 4
NoMethodError: undefined method `id' for nil:NilClass
/app/config/initializers/rack_patch.rb in call at line 4
RuntimeError: invalid state

Any idead on waht might be?
The 1st one happens ina GET request, the 2nd on a POST

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment