Class: FaradayMiddleware::Gzip

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday_middleware/gzip.rb

Overview

Middleware to automatically decompress response bodies. If the “Accept-Encoding” header wasn’t set in the request, this sets it to “gzip,deflate” and appropriately handles the compressed response from the server. This resembles what Ruby 1.9+ does internally in Net::HTTP#get.

This middleware is NOT necessary when these adapters are used:

  • net_http on Ruby 1.9+

  • net_http_persistent on Ruby 2.0+

  • em_http

Constant Summary collapse

BROTLI_SUPPORTED =
optional_dependency 'brotli'
ACCEPT_ENCODING =
'Accept-Encoding'
CONTENT_ENCODING =
'Content-Encoding'
CONTENT_LENGTH =
'Content-Length'
SUPPORTED_ENCODINGS =
supported_encodings.join(',').freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.optional_dependency(lib = nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/faraday_middleware/gzip.rb', line 18

def self.optional_dependency(lib = nil)
  lib ? require(lib) : yield
  true
rescue LoadError, NameError
  false
end

.supported_encodingsObject



27
28
29
30
31
# File 'lib/faraday_middleware/gzip.rb', line 27

def self.supported_encodings
  encodings = %w[gzip deflate]
  encodings << 'br' if BROTLI_SUPPORTED
  encodings
end

Instance Method Details

#brotli_inflate(body) ⇒ Object



82
83
84
# File 'lib/faraday_middleware/gzip.rb', line 82

def brotli_inflate(body)
  Brotli.inflate(body)
end

#call(env) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/faraday_middleware/gzip.rb', line 38

def call(env)
  env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS
  @app.call(env).on_complete do |response_env|
    if response_env[:body].empty?
      reset_body(response_env, &method(:raw_body))
    else
      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
end

#inflate(body) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/faraday_middleware/gzip.rb', line 68

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

#raw_body(body) ⇒ Object



86
87
88
# File 'lib/faraday_middleware/gzip.rb', line 86

def raw_body(body)
  body
end

#reset_body(env) ⇒ Object



56
57
58
59
60
# File 'lib/faraday_middleware/gzip.rb', line 56

def reset_body(env)
  env[:body] = yield(env[:body])
  env[:response_headers].delete(CONTENT_ENCODING)
  env[:response_headers][CONTENT_LENGTH] = env[:body].length
end

#uncompress_gzip(body) ⇒ Object



62
63
64
65
66
# File 'lib/faraday_middleware/gzip.rb', line 62

def uncompress_gzip(body)
  io = StringIO.new(body)
  gzip_reader = Zlib::GzipReader.new(io, encoding: 'ASCII-8BIT')
  gzip_reader.read
end