class Gzip
-
em_http
- net_http_persistent on Ruby 2.0+
- net_http on Ruby 1.9+
This middleware is NOT necessary when these adapters are used:
server. This resembles what Ruby 1.9+ does internally in Net::HTTP#get.
“gzip,deflate” and appropriately handles the compressed response from the
“Accept-Encoding” header wasn’t set in the request, this sets it to
Middleware to automatically decompress response bodies. If the
def brotli_inflate(body)
def brotli_inflate(body) self.class.dependency 'brotli' Brotli.inflate(body) end
def call(env)
def call(env) env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS @app.call(env).on_complete do |response_env| case response_env[:response_headers][CONTENT_ENCODING] when 'gzip' reset_body(response_env, &method(:uncompress_gzip)) when 'deflate' reset_body(response_env, &method(:inflate)) when 'br' reset_body(response_env, &method(:brotli_inflate)) end end end
def inflate(body)
def inflate(body) # Inflate as a DEFLATE (RFC 1950+RFC 1951) stream Zlib::Inflate.inflate(body) rescue Zlib::DataError # Fall back to inflating as a "raw" deflate stream which # Microsoft servers return inflate = Zlib::Inflate.new(-Zlib::MAX_WBITS) begin inflate.inflate(body) ensure inflate.close end end
def reset_body(env)
def reset_body(env) env[:body] = yield(env[:body]) env[:response_headers].delete(CONTENT_ENCODING) env[:response_headers][CONTENT_LENGTH] = env[:body].length end
def uncompress_gzip(body)
def uncompress_gzip(body) io = StringIO.new(body) gzip_reader = if RUBY_ENCODING Zlib::GzipReader.new(io, :encoding => 'ASCII-8BIT') else Zlib::GzipReader.new(io) end gzip_reader.read end