Class: Faraday::Gzip::Middleware
- Inherits:
-
Middleware
- Object
- Middleware
- Faraday::Gzip::Middleware
- Defined in:
- lib/faraday/gzip/middleware.rb
Overview
Faraday middleware for decompression
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
-
.optional_dependency(lib = nil) ⇒ Object
System method required by Faraday.
-
.supported_encodings ⇒ Object
Returns supported encodings, adds brotli if the corresponding dependency is present.
Instance Method Summary collapse
-
#brotli_inflate(body) ⇒ Object
Process brotli.
-
#call(env) ⇒ Object
Main method to process the response.
-
#find_processor(response_env) ⇒ Object
Finds a proper processor.
-
#inflate(body) ⇒ Object
Process deflate.
-
#raw_body(body) ⇒ Object
Do not process anything, leave body as is.
-
#reset_body(env, processor) ⇒ Object
Calls the proper processor to decompress body.
-
#uncompress_gzip(body) ⇒ Object
Process gzip.
Class Method Details
.optional_dependency(lib = nil) ⇒ Object
System method required by Faraday
16 17 18 19 20 21 |
# File 'lib/faraday/gzip/middleware.rb', line 16 def self.optional_dependency(lib = nil) lib ? require(lib) : yield true rescue LoadError, NameError false end |
.supported_encodings ⇒ Object
Returns supported encodings, adds brotli if the corresponding dependency is present
27 28 29 30 31 |
# File 'lib/faraday/gzip/middleware.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
Process brotli
89 90 91 |
# File 'lib/faraday/gzip/middleware.rb', line 89 def brotli_inflate(body) Brotli.inflate(body) end |
#call(env) ⇒ Object
Main method to process the response
39 40 41 42 43 44 45 |
# File 'lib/faraday/gzip/middleware.rb', line 39 def call(env) env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS @app.call(env).on_complete do |response_env| reset_body(response_env, find_processor(response_env)) end end |
#find_processor(response_env) ⇒ Object
Finds a proper processor
48 49 50 51 52 53 54 |
# File 'lib/faraday/gzip/middleware.rb', line 48 def find_processor(response_env) if empty_body?(response_env) ->(body) { raw_body(body) } else processors[response_env[:response_headers][CONTENT_ENCODING]] end end |
#inflate(body) ⇒ Object
Process deflate
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/faraday/gzip/middleware.rb', line 74 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
Do not process anything, leave body as is
94 95 96 |
# File 'lib/faraday/gzip/middleware.rb', line 94 def raw_body(body) body end |
#reset_body(env, processor) ⇒ Object
Calls the proper processor to decompress body
57 58 59 60 61 62 63 64 |
# File 'lib/faraday/gzip/middleware.rb', line 57 def reset_body(env, processor) return if processor.nil? env[:body] = processor.call(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
Process gzip
67 68 69 70 71 |
# File 'lib/faraday/gzip/middleware.rb', line 67 def uncompress_gzip(body) io = StringIO.new(body) gzip_reader = Zlib::GzipReader.new(io, encoding: 'ASCII-8BIT') gzip_reader.read end |