Created
March 9, 2026 08:41
-
-
Save alexisbernard/746ea5e390420345e54ee4d5b930f9dc to your computer and use it in GitHub Desktop.
Rack::Inflater to decompress request bodies according to Content-Encoding header.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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