Sha256: 69a7f50852234e0ebba24be761bcaef9fa87155f80ab04a9614fdb0b2af9e0ba

Contents?: true

Size: 1.62 KB

Versions: 67

Compression:

Stored size: 1.62 KB

Contents

require 'faraday'

module SplitIoClient
  module FaradayMiddleware
    class Gzip < Faraday::Middleware
      ACCEPT_ENCODING = 'Accept-Encoding'.freeze
      CONTENT_ENCODING = 'Content-Encoding'.freeze
      CONTENT_LENGTH = 'Content-Length'.freeze
      SUPPORTED_ENCODINGS = 'gzip,deflate'.freeze
      RUBY_ENCODING = '1.9'.respond_to?(:force_encoding)

      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))
          end
        end
      end

      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)
        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

      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
    end
  end
end

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
splitclient-rb-5.1.2.pre.rc1-java lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.2.pre.rc1 lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.2.pre.rc21 lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.2.pre.rc21-java lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.1-java lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.1 lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.1.pre.rc2-java lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.1.pre.rc2 lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.1.pre.rc1-java lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.1.pre.rc1 lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.0-java lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.0 lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.0.pre.rc1-java lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.1.0.pre.rc1 lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.0.3-java lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.0.3 lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.0.3.pre.rc1-java lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.0.3.pre.rc1 lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.0.2-java lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb
splitclient-rb-5.0.2 lib/splitclient-rb/engine/api/faraday_middleware/gzip.rb