Class: Faraday::Gzip::Middleware
- Inherits:
-
Middleware
- Object
- Middleware
- Faraday::Gzip::Middleware
- Defined in:
- lib/faraday/gzip/middleware.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. Based on github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/gzip.rb
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
- #brotli_inflate(body) ⇒ Object
- #call(env) ⇒ Object
- #inflate(body) ⇒ Object
- #raw_body(body) ⇒ Object
- #reset_body(env) ⇒ Object
- #uncompress_gzip(body) ⇒ Object
Class Method Details
.optional_dependency(lib = nil) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/faraday/gzip/middleware.rb', line 14 def self.optional_dependency(lib = nil) lib ? require(lib) : yield true rescue LoadError, NameError false end |
.supported_encodings ⇒ Object
23 24 25 26 27 |
# File 'lib/faraday/gzip/middleware.rb', line 23 def self.supported_encodings encodings = %w[gzip deflate] encodings << 'br' if BROTLI_SUPPORTED encodings end |
Instance Method Details
#brotli_inflate(body) ⇒ Object
79 80 81 |
# File 'lib/faraday/gzip/middleware.rb', line 79 def brotli_inflate(body) Brotli.inflate(body) end |
#call(env) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/faraday/gzip/middleware.rb', line 34 def call(env) env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS @app.call(env).on_complete do |response_env| if empty_body?(response_env) reset_body(response_env) { |body| raw_body(body) } else case response_env[:response_headers][CONTENT_ENCODING] when 'gzip' reset_body(response_env) { |body| uncompress_gzip(body) } when 'deflate' reset_body(response_env) { |body| inflate(body) } when 'br' reset_body(response_env) { |body| brotli_inflate(body) } end end end end |
#inflate(body) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/faraday/gzip/middleware.rb', line 65 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
83 84 85 |
# File 'lib/faraday/gzip/middleware.rb', line 83 def raw_body(body) body end |
#reset_body(env) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/faraday/gzip/middleware.rb', line 52 def reset_body(env) env[:body] = yield(env[:body]) env[:response_headers].delete(CONTENT_ENCODING) env[:response_headers][CONTENT_LENGTH] = env[:body].nil? ? 0 : env[:body].length end |
#uncompress_gzip(body) ⇒ Object
59 60 61 62 63 |
# File 'lib/faraday/gzip/middleware.rb', line 59 def uncompress_gzip(body) io = StringIO.new(body) gzip_reader = Zlib::GzipReader.new(io, encoding: 'ASCII-8BIT') gzip_reader.read end |