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.



49
50
51
# File 'lib/httpx/plugins/compression.rb', line 49

def encodings
  @encodings
end

Instance Method Details

#closeObject



88
89
90
91
92
93
94
# File 'lib/httpx/plugins/compression.rb', line 88

def close
  super

  return unless defined?(@_decoders)

  @_decoders.each(&:close)
end

#initializeObject



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

def initialize(*, **)
  @encodings = []

  super

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

  @_decoders = @headers.get("content-encoding").map do |encoding|
    next if encoding == "identity"

    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.compact

  # 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



80
81
82
83
84
85
86
# File 'lib/httpx/plugins/compression.rb', line 80

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

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