Sha256: 1e3c51e724a9efeda9c9e824dbcb0e0dd5e03bae88769e3feda27f0af62c4a4b

Contents?: true

Size: 1.95 KB

Versions: 42

Compression:

Stored size: 1.95 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(klass)
          klass.default_options.encodings.register "gzip", self
        end

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

          def deflate(raw, buffer = "".b, chunk_size: 16_384)
            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?
            buffer
          end

          private

          def write(chunk)
            @compressed_chunk << chunk
          end

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

        class Inflater
          def initialize(bytesize)
            @inflater = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
            @bytesize = bytesize
            @buffer = nil
          end

          def inflate(chunk)
            buffer = @inflater.inflate(chunk)
            @bytesize -= chunk.bytesize
            if @bytesize <= 0
              buffer << @inflater.finish
              @inflater.close
            end
            buffer
          end
        end

        module_function

        def deflater
          Deflater.new
        end

        def inflater(bytesize)
          Inflater.new(bytesize)
        end
      end
    end
    register_plugin :"compression/gzip", Compression::GZIP
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
httpx-0.22.2 lib/httpx/plugins/compression/gzip.rb
httpx-0.22.1 lib/httpx/plugins/compression/gzip.rb
httpx-0.22.0 lib/httpx/plugins/compression/gzip.rb
httpx-0.21.1 lib/httpx/plugins/compression/gzip.rb
httpx-0.21.0 lib/httpx/plugins/compression/gzip.rb
httpx-0.20.5 lib/httpx/plugins/compression/gzip.rb
httpx-0.20.4 lib/httpx/plugins/compression/gzip.rb
httpx-0.20.3 lib/httpx/plugins/compression/gzip.rb
httpx-0.20.2 lib/httpx/plugins/compression/gzip.rb
httpx-0.20.1 lib/httpx/plugins/compression/gzip.rb
httpx-0.20.0 lib/httpx/plugins/compression/gzip.rb
httpx-0.19.8 lib/httpx/plugins/compression/gzip.rb
httpx-0.19.7 lib/httpx/plugins/compression/gzip.rb
httpx-0.19.6 lib/httpx/plugins/compression/gzip.rb
httpx-0.19.5 lib/httpx/plugins/compression/gzip.rb
httpx-0.19.4 lib/httpx/plugins/compression/gzip.rb
httpx-0.19.3 lib/httpx/plugins/compression/gzip.rb
httpx-0.19.2 lib/httpx/plugins/compression/gzip.rb
httpx-0.19.1 lib/httpx/plugins/compression/gzip.rb
httpx-0.19.0 lib/httpx/plugins/compression/gzip.rb