Sha256: 698af91565a68c98b78911047752dc4a4df6ee4c817b1bdeacb15663f977d82b

Contents?: true

Size: 1.55 KB

Versions: 20

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require "forwardable"
require "uri"
require "stringio"
require "zlib"

module HTTPX
  module Transcoder
    module GZIP
      class Deflater < Transcoder::Deflater
        def initialize(body)
          @compressed_chunk = "".b
          super
        end

        def deflate(chunk)
          @deflater ||= Zlib::GzipWriter.new(self)

          if chunk.nil?
            unless @deflater.closed?
              @deflater.flush
              @deflater.close
              compressed_chunk
            end
          else
            @deflater.write(chunk)
            compressed_chunk
          end
        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(Zlib::MAX_WBITS + 32)
          @bytesize = bytesize
        end

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

      module_function

      def encode(body)
        Deflater.new(body)
      end

      def decode(response, bytesize: nil)
        bytesize ||= response.headers.key?("content-length") ? response.headers["content-length"].to_i : Float::INFINITY
        Inflater.new(bytesize)
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
httpx-1.3.4 lib/httpx/transcoder/gzip.rb
httpx-1.3.3 lib/httpx/transcoder/gzip.rb
httpx-1.3.2 lib/httpx/transcoder/gzip.rb
httpx-1.3.1 lib/httpx/transcoder/gzip.rb
httpx-1.3.0 lib/httpx/transcoder/gzip.rb
httpx-1.2.6 lib/httpx/transcoder/gzip.rb
httpx-1.2.4 lib/httpx/transcoder/gzip.rb
httpx-1.2.3 lib/httpx/transcoder/gzip.rb
httpx-1.2.2 lib/httpx/transcoder/gzip.rb
httpx-1.2.1 lib/httpx/transcoder/gzip.rb
httpx-1.2.0 lib/httpx/transcoder/gzip.rb
httpx-1.1.5 lib/httpx/transcoder/gzip.rb
httpx-1.1.4 lib/httpx/transcoder/gzip.rb
httpx-1.1.3 lib/httpx/transcoder/gzip.rb
httpx-1.1.2 lib/httpx/transcoder/gzip.rb
httpx-1.1.1 lib/httpx/transcoder/gzip.rb
httpx-1.1.0 lib/httpx/transcoder/gzip.rb
httpx-1.0.2 lib/httpx/transcoder/gzip.rb
httpx-1.0.1 lib/httpx/transcoder/gzip.rb
httpx-1.0.0 lib/httpx/transcoder/gzip.rb