-
-
Save relistan/2109707 to your computer and use it in GitHub Desktop.
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 |
Sorry to miss this question. It should be usable on anything that supports standard Rack middleware, including Rails 2.
Thanks for this gist! It’s working great.
I found that for a payload that uses multibyte strings, the content length was not getting set correctly. The solution is to use .bytesize
instead of .length
.
env['CONTENT_LENGTH'] = extracted.bytesize
Here's my fork with the fix: https://gist.github.com/mbrictson/9106778
It looks like the account name above moved, the updated location is:
config.middleware.insert_before ActionDispatch::ParamsParser, "CompressedRequests"
this does not work with rails 5.
This is fixed for the CONTENT_LENGTH
issue now. I don't know Rails 5 @plentz, so if you knew what needs to go there instead, please post that here.
It's working for me in rails 6 with config.middleware.insert_after ::Rack::Sendfile, ::CompressedRequests
- status, headers, response = @app.call(env)
- return [status, headers, response]
+ @app.call(env)
had a similar effect 💚
Used an index in insert_before
which worked in rails 5 in all envs.
config.middleware.insert_before 0, CompressedRequests
Can this be used in Rails 2?