Sha256: c9958bf4c308a88f07360dc8dff7343dba6e62938946c3dc3f20ba296a313178

Contents?: true

Size: 1.42 KB

Versions: 14

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true
module Excon
  module Middleware
    class Decompress < Excon::Middleware::Base
      def request_call(datum)
        unless datum.has_key?(:response_block)
          key = datum[:headers].keys.detect {|k| k.to_s.casecmp('Accept-Encoding') == 0 } || 'Accept-Encoding'
          if datum[:headers][key].to_s.empty?
            datum[:headers][key] = 'deflate, gzip'
          end
        end
        @stack.request_call(datum)
      end

      def response_call(datum)
        body = datum[:response][:body]
        unless datum.has_key?(:response_block) || body.nil? || body.empty?
          if (key = datum[:response][:headers].keys.detect {|k| k.casecmp('Content-Encoding') == 0 })
            encodings = Utils.split_header_value(datum[:response][:headers][key])
            if (encoding = encodings.last)
              if encoding.casecmp('deflate') == 0
                # assume inflate omits header
                datum[:response][:body] = Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(body)
                encodings.pop
              elsif encoding.casecmp('gzip') == 0 || encoding.casecmp('x-gzip') == 0
                datum[:response][:body] = Zlib::GzipReader.new(StringIO.new(body)).read
                encodings.pop
              end
              datum[:response][:headers][key] = encodings.join(', ')
            end
          end
        end
        @stack.response_call(datum)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/excon-0.88.0/lib/excon/middlewares/decompress.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/excon-0.88.0/lib/excon/middlewares/decompress.rb
excon-0.88.0 lib/excon/middlewares/decompress.rb
excon-0.87.0 lib/excon/middlewares/decompress.rb
excon-0.86.0 lib/excon/middlewares/decompress.rb
excon-0.85.0 lib/excon/middlewares/decompress.rb
excon-0.84.0 lib/excon/middlewares/decompress.rb
excon-0.83.0 lib/excon/middlewares/decompress.rb
excon-0.82.0 lib/excon/middlewares/decompress.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/excon-0.81.0/lib/excon/middlewares/decompress.rb
excon-0.81.0 lib/excon/middlewares/decompress.rb
excon-0.80.1 lib/excon/middlewares/decompress.rb
excon-0.80.0 lib/excon/middlewares/decompress.rb
excon-0.79.0 lib/excon/middlewares/decompress.rb