Sha256: 4e56f244945471a4f5caa2e1df1aff7eabfa38bcc06cffb3343634c873e95f9c

Contents?: true

Size: 1.89 KB

Versions: 8

Compression:

Stored size: 1.89 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 Deflater
          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

        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

8 entries across 8 versions & 1 rubygems

Version Path
httpx-0.12.0 lib/httpx/plugins/compression/gzip.rb
httpx-0.11.3 lib/httpx/plugins/compression/gzip.rb
httpx-0.11.2 lib/httpx/plugins/compression/gzip.rb
httpx-0.11.1 lib/httpx/plugins/compression/gzip.rb
httpx-0.11.0 lib/httpx/plugins/compression/gzip.rb
httpx-0.10.2 lib/httpx/plugins/compression/gzip.rb
httpx-0.10.1 lib/httpx/plugins/compression/gzip.rb
httpx-0.10.0 lib/httpx/plugins/compression/gzip.rb