Sha256: 73f92b994d40615b78389340aadbaa3f4c430b4cf965b562be0f94d913ae54f1

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require "zlib"

module HTTP
  module Features
    class AutoDeflate < Feature
      attr_reader :method

      def initialize(*)
        super

        @method = @opts.key?(:method) ? @opts[:method].to_s : "gzip"

        raise Error, "Only gzip and deflate methods are supported" unless %w(gzip deflate).include?(@method)
      end

      def deflate(headers, body)
        return body unless body
        return body unless body.is_a?(String)

        # We need to delete Content-Length header. It will be set automatically
        # by HTTP::Request::Writer
        headers.delete(Headers::CONTENT_LENGTH)

        headers[Headers::CONTENT_ENCODING] = method

        case method
        when "gzip" then
          StringIO.open do |out|
            Zlib::GzipWriter.wrap(out) do |gz|
              gz.write body
              gz.finish
              out.tap(&:rewind).read
            end
          end
        when "deflate" then
          Zlib::Deflate.deflate(body)
        else
          raise ArgumentError, "Unsupported deflate method: #{method}"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
http-2.2.2 lib/http/features/auto_deflate.rb
http-2.2.1 lib/http/features/auto_deflate.rb
http-2.2.0 lib/http/features/auto_deflate.rb