Skip to content

Instantly share code, notes, and snippets.

@alexisbernard
Created March 9, 2026 08:41
Show Gist options
  • Select an option

  • Save alexisbernard/746ea5e390420345e54ee4d5b930f9dc to your computer and use it in GitHub Desktop.

Select an option

Save alexisbernard/746ea5e390420345e54ee4d5b930f9dc to your computer and use it in GitHub Desktop.
Rack::Inflater to decompress request bodies according to Content-Encoding header.
# frozen_string_literal: true
# Decrompress request's body when HTTP header Content-Encoding is present.
# Only deflate and gzip encodings are supported.
class Rack::Inflater
def initialize(app)
@app = app
end
def call(env)
if env["rack.input"] && (encoding = env["HTTP_CONTENT_ENCODING"])
if (body = decompress(env["rack.input"].read, encoding))
env["rack.input"] = StringIO.new(body)
env["CONTENT_LENGTH"] = body.bytesize
env.delete("HTTP_CONTENT_ENCODING")
end
end
@app.call(env)
end
def decompress(string, encoding)
case encoding
when "gzip" then Zlib.gunzip(string)
when "deflate" then Zlib.inflate(string)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment