Sha256: 06e44886195371de1e1cac48f5d3606062729b6d6d6f97f9772488f9e07a0cf0

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require "forwardable"

module HTTPX
  module Plugins
    module Compression
      module GZIP
        def self.load_dependencies(*)
          require "zlib"
        end

        def self.configure(*)
          Compression.register "gzip", self
        end

        class Encoder
          def initialize
            @compressed_chunk = "".b
          end

          def deflate(raw, buffer, chunk_size:)
            gzip = Zlib::GzipWriter.new(self)

            begin
              while (chunk = raw.read(chunk_size))
                gzip.write(chunk)
                gzip.flush
                compressed = compressed_chunk
                buffer << compressed
                yield compressed if block_given?
              end
            ensure
              gzip.close
            end

            return unless (compressed = compressed_chunk)

            buffer << compressed
            yield compressed if block_given?
          end

          private

          def write(chunk)
            @compressed_chunk << chunk
          end

          def compressed_chunk
            @compressed_chunk.dup
          ensure
            @compressed_chunk.clear
          end
        end

        module_function

        def encoder
          Encoder.new
        end

        def decoder
          Decoder.new(Zlib::Inflate.new(32 + Zlib::MAX_WBITS))
        end
      end
    end
    register_plugin :"compression/gzip", Compression::GZIP
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
httpx-0.9.0 lib/httpx/plugins/compression/gzip.rb
httpx-0.8.2 lib/httpx/plugins/compression/gzip.rb
httpx-0.8.1 lib/httpx/plugins/compression/gzip.rb
httpx-0.8.0 lib/httpx/plugins/compression/gzip.rb