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 |
This comment has been minimized.
This comment has been minimized.
Awesome! To get this to work with Rails 3, I had to insert it before ActionDispatch::ParamsParser: config.middleware.insert_before ActionDispatch::ParamsParser, "CompressedRequests" |
This comment has been minimized.
This comment has been minimized.
Thanks for adding that! |
This comment has been minimized.
This comment has been minimized.
Can this be used in Rails 2? |
This comment has been minimized.
This comment has been minimized.
Sorry to miss this question. It should be usable on anything that supports standard Rack middleware, including Rails 2. |
This comment has been minimized.
This comment has been minimized.
Thanks for this gist! It’s working great. |
This comment has been minimized.
This comment has been minimized.
I found that for a payload that uses multibyte strings, the content length was not getting set correctly. The solution is to use env['CONTENT_LENGTH'] = extracted.bytesize Here's my fork with the fix: https://gist.github.com/mbrictson/9106778 |
This comment has been minimized.
This comment has been minimized.
It looks like the account name above moved, the updated location is: |
This comment has been minimized.
This comment has been minimized.
config.middleware.insert_before ActionDispatch::ParamsParser, "CompressedRequests" this does not work with rails 5. |
This comment has been minimized.
This comment has been minimized.
This is fixed for the |
This comment has been minimized.
This comment has been minimized.
It's working for me in rails 6 with |
This comment has been minimized.
This comment has been minimized.
- status, headers, response = @app.call(env)
- return [status, headers, response]
+ @app.call(env) had a similar effect Used an index in config.middleware.insert_before 0, CompressedRequests |
This comment has been minimized.
Rack Middleware to Handle gzip and deflate Content Encodings
This fork has been simplified, clarified and for the common case (gzip) will reduce by one the number of times a StringIO is created and populated with the request body. Also fixes a bug where it would try to handle any content-encoding even though it can only handle two of them. Thanks to subdigital for the original.