Sha256: cc6007ccc17c8cf85f88b526533a0fe20cdfec7fed55423d6205dc96efe38785

Contents?: true

Size: 1.36 KB

Versions: 20

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require "forwardable"
require_relative "body_reader"

module HTTPX
  module Transcoder
    class Deflater
      extend Forwardable

      attr_reader :content_type

      def initialize(body)
        @content_type = body.content_type
        @body = BodyReader.new(body)
        @closed = false
      end

      def bytesize
        buffer_deflate!

        @buffer.size
      end

      def read(length = nil, outbuf = nil)
        return @buffer.read(length, outbuf) if @buffer

        return if @closed

        chunk = @body.read(length)

        compressed_chunk = deflate(chunk)

        return unless compressed_chunk

        if outbuf
          outbuf.clear.force_encoding(Encoding::BINARY)
          outbuf << compressed_chunk
        else
          compressed_chunk
        end
      end

      def close
        return if @closed

        @buffer.close if @buffer

        @body.close

        @closed = true
      end

      private

      # rubocop:disable Naming/MemoizedInstanceVariableName
      def buffer_deflate!
        return @buffer if defined?(@buffer)

        buffer = Response::Buffer.new(
          threshold_size: Options::MAX_BODY_THRESHOLD_SIZE
        )
        ::IO.copy_stream(self, buffer)

        buffer.rewind

        @buffer = buffer
      end
      # rubocop:enable Naming/MemoizedInstanceVariableName
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

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