Sha256: dc49f91d7c671e1effe85a7589dca9e282a49fb5b02c55a0913559a07e9d59ae

Contents?: true

Size: 1.09 KB

Versions: 10

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require "set"

module HTTP
  module Features
    class AutoInflate < Feature
      SUPPORTED_ENCODING = Set.new(%w[deflate gzip x-gzip]).freeze
      private_constant :SUPPORTED_ENCODING

      def wrap_response(response)
        return response unless supported_encoding?(response)

        options = {
          :status => response.status,
          :version => response.version,
          :headers => response.headers,
          :proxy_headers => response.proxy_headers,
          :connection => response.connection,
          :body => stream_for(response.connection)
        }

        options[:uri] = response.uri if response.uri

        Response.new(options)
      end

      def stream_for(connection)
        Response::Body.new(Response::Inflater.new(connection))
      end

      private

      def supported_encoding?(response)
        content_encoding = response.headers.get(Headers::CONTENT_ENCODING).first
        content_encoding && SUPPORTED_ENCODING.include?(content_encoding)
      end

      HTTP::Options.register_feature(:auto_inflate, self)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
http-4.4.1 lib/http/features/auto_inflate.rb
http-4.4.0 lib/http/features/auto_inflate.rb
http-4.3.0 lib/http/features/auto_inflate.rb
http-4.2.0 lib/http/features/auto_inflate.rb
http-4.1.1 lib/http/features/auto_inflate.rb
http-4.1.0 lib/http/features/auto_inflate.rb
http-4.0.5 lib/http/features/auto_inflate.rb
http-4.0.4 lib/http/features/auto_inflate.rb
http-5.0.0.pre lib/http/features/auto_inflate.rb
http-4.0.3 lib/http/features/auto_inflate.rb