lib/httpx/plugins/compression.rb in httpx-0.5.0 vs lib/httpx/plugins/compression.rb in httpx-0.5.1
- old
+ new
@@ -21,10 +21,18 @@
def self.extra_options(options)
options.merge(headers: { "accept-encoding" => Compression.registry.keys })
end
+ module RequestMethods
+ def initialize(*)
+ super
+ # forego compression in the Range cases
+ @headers.delete("accept-encoding") if @headers.key?("range")
+ end
+ end
+
module RequestBodyMethods
def initialize(*)
super
return if @body.nil?
@@ -34,17 +42,31 @@
@headers["content-length"] = @body.bytesize unless chunked?
end
end
module ResponseBodyMethods
+ attr_reader :encodings
+
def initialize(*)
+ @encodings = []
+
super
return unless @headers.key?("content-encoding")
@_decoders = @headers.get("content-encoding").map do |encoding|
- Compression.registry(encoding).decoder
+ 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