lib/httpx/plugins/compression/gzip.rb in httpx-0.9.0 vs lib/httpx/plugins/compression/gzip.rb in httpx-0.10.0

- old
+ new

@@ -12,11 +12,11 @@ def self.configure(*) Compression.register "gzip", self end - class Encoder + class Deflater def initialize @compressed_chunk = "".b end def deflate(raw, buffer, chunk_size:) @@ -51,17 +51,35 @@ ensure @compressed_chunk.clear end end + class Inflater + def initialize(bytesize) + @inflater = Zlib::Inflate.new(32 + Zlib::MAX_WBITS) + @bytesize = bytesize + @buffer = nil + end + + def inflate(chunk) + buffer = @inflater.inflate(chunk) + @bytesize -= chunk.bytesize + if @bytesize <= 0 + buffer << @inflater.finish + @inflater.close + end + buffer + end + end + module_function - def encoder - Encoder.new + def deflater + Deflater.new end - def decoder - Decoder.new(Zlib::Inflate.new(32 + Zlib::MAX_WBITS)) + def inflater(bytesize) + Inflater.new(bytesize) end end end register_plugin :"compression/gzip", Compression::GZIP end