Module: HTTPX::Plugins::Compression::ResponseBodyMethods

Defined in:
lib/httpx/plugins/compression.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#encodingsObject (readonly)

Returns the value of attribute encodings


47
48
49
# File 'lib/httpx/plugins/compression.rb', line 47

def encodings
  @encodings
end

Instance Method Details

#closeObject


84
85
86
87
88
89
90
# File 'lib/httpx/plugins/compression.rb', line 84

def close
  super

  return unless defined?(@_decoders)

  @_decoders.each(&:close)
end

#initializeObject


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/httpx/plugins/compression.rb', line 49

def initialize(*)
  @encodings = []

  super

  return unless @headers.key?("content-encoding")

  @_decoders = @headers.get("content-encoding").map do |encoding|
    decoder = Compression.registry(encoding).decoder    # do not uncompress if there is no decoder available. In fact, we can't reliably
    # continue decompressing beyond that, so ignore.

    break unless decoder

    @encodings << encoding
    decoder
  end

  # remove encodings that we are able to decode
  @headers["content-encoding"] = @headers.get("content-encoding") - @encodings

  @_compressed_length = if @headers.key?("content-length")
    @headers["content-length"].to_i
  else
    Float::INFINITY
  end
end

#write(chunk) ⇒ Object


76
77
78
79
80
81
82
# File 'lib/httpx/plugins/compression.rb', line 76

def write(chunk)
  return super unless defined?(@_compressed_length)

  @_compressed_length -= chunk.bytesize
  chunk = decompress(chunk)
  super(chunk)
end