Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class CompressedRequests
def initialize(app)
@app = app
end
def method_handled?(env)
!!(env['REQUEST_METHOD'] =~ /(POST|PUT)/)
end
def encoding_handled?(env)
['gzip', 'deflate'].include? env['HTTP_CONTENT_ENCODING']
end
def call(env)
if method_handled?(env) && encoding_handled?(env)
extracted = decode(env['rack.input'], env['HTTP_CONTENT_ENCODING'])
env.delete('HTTP_CONTENT_ENCODING')
env['CONTENT_LENGTH'] = extracted.bytesize
env['rack.input'] = StringIO.new(extracted)
end
status, headers, response = @app.call(env)
return [status, headers, response]
end
def decode(input, content_encoding)
case content_encoding
when 'gzip' then Zlib::GzipReader.new(input).read
when 'deflate' then Zlib::Inflate.inflate(input.read)
end
end
end
@plentz

This comment has been minimized.

Copy link

@plentz plentz commented Oct 25, 2016

@mattbrictson how do you use it with rails 5?

@sshock

This comment has been minimized.

Copy link

@sshock sshock commented Apr 28, 2020

It's working for me in rails 6 with config.middleware.insert_after ::Rack::Sendfile, ::CompressedRequests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.