mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-04 00:00:37 -04:00
simplified the compression API, sharing more across compressors
This commit is contained in:
parent
8b45c626ce
commit
6319d83b81
@ -44,10 +44,11 @@ module HTTPX
|
||||
class CompressEncoder
|
||||
attr_reader :content_type
|
||||
|
||||
def initialize(raw)
|
||||
def initialize(raw, encoder)
|
||||
@content_type = raw.content_type
|
||||
@raw = raw.respond_to?(:read) ? raw : StringIO.new(raw.to_s)
|
||||
@buffer = StringIO.new("".b, File::RDWR)
|
||||
@encoder = encoder
|
||||
end
|
||||
|
||||
def each(&blk)
|
||||
@ -56,7 +57,7 @@ module HTTPX
|
||||
@buffer.rewind
|
||||
return @buffer.each(&blk)
|
||||
end
|
||||
compress(&blk)
|
||||
@encoder.compress(@raw, @buffer, &blk)
|
||||
end
|
||||
|
||||
def to_s
|
||||
@ -66,7 +67,7 @@ module HTTPX
|
||||
end
|
||||
|
||||
def bytesize
|
||||
compress
|
||||
@encoder.compress(@raw, @buffer)
|
||||
@buffer.size
|
||||
end
|
||||
|
||||
|
@ -22,18 +22,33 @@ module HTTPX
|
||||
end
|
||||
end
|
||||
|
||||
module Encoder
|
||||
module_function
|
||||
|
||||
def compress(raw, buffer, chunk_size: 16_384)
|
||||
return unless buffer.size.zero?
|
||||
raw.rewind
|
||||
begin
|
||||
while chunk = raw.read(chunk_size)
|
||||
compressed = ::Brotli.deflate(chunk)
|
||||
buffer << compressed
|
||||
yield compressed if block_given?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module BrotliTranscoder
|
||||
module_function
|
||||
|
||||
def encode(payload)
|
||||
::Brotli.deflate(payload)
|
||||
CompressEncoder.new(payload, Encoder)
|
||||
end
|
||||
|
||||
def decode(io)
|
||||
::Brotli.inflate(io)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
register_plugin :"compression/brotli", Compression::Brotli
|
||||
|
@ -15,24 +15,24 @@ module HTTPX
|
||||
end
|
||||
|
||||
module DeflateTranscoder
|
||||
class Encoder < CompressEncoder
|
||||
private
|
||||
module Encoder
|
||||
module_function
|
||||
|
||||
def compress
|
||||
return unless @buffer.size.zero?
|
||||
@raw.rewind
|
||||
def compress(raw, buffer, chunk_size: 16_384)
|
||||
return unless buffer.size.zero?
|
||||
raw.rewind
|
||||
begin
|
||||
deflater = Zlib::Deflate.new(Zlib::BEST_COMPRESSION,
|
||||
Zlib::MAX_WBITS,
|
||||
Zlib::MAX_MEM_LEVEL,
|
||||
Zlib::HUFFMAN_ONLY)
|
||||
while chunk = @raw.read(16_384)
|
||||
while chunk = raw.read(chunk_size)
|
||||
compressed = deflater.deflate(chunk)
|
||||
@buffer << compressed
|
||||
buffer << compressed
|
||||
yield compressed if block_given?
|
||||
end
|
||||
last = deflater.finish
|
||||
@buffer << last
|
||||
buffer << last
|
||||
yield last if block_given?
|
||||
ensure
|
||||
deflater.close
|
||||
@ -70,7 +70,7 @@ module HTTPX
|
||||
end
|
||||
|
||||
def encode(payload)
|
||||
Encoder.new(payload)
|
||||
CompressEncoder.new(payload, Encoder)
|
||||
end
|
||||
|
||||
def decode(io)
|
||||
|
@ -14,43 +14,43 @@ module HTTPX
|
||||
end
|
||||
|
||||
module GZIPTranscoder
|
||||
class Encoder < CompressEncoder
|
||||
def write(chunk)
|
||||
@compressed_chunk = chunk
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def compressed_chunk
|
||||
compressed = @compressed_chunk
|
||||
compressed
|
||||
ensure
|
||||
@compressed_chunk = nil
|
||||
end
|
||||
|
||||
def compress
|
||||
return unless @buffer.size.zero?
|
||||
@raw.rewind
|
||||
class Encoder
|
||||
def compress(raw, buffer, chunk_size: 16_384)
|
||||
return unless buffer.size.zero?
|
||||
raw.rewind
|
||||
begin
|
||||
gzip = Zlib::GzipWriter.new(self)
|
||||
|
||||
while chunk = @raw.read(16_384)
|
||||
while chunk = raw.read(chunk_size)
|
||||
gzip.write(chunk)
|
||||
gzip.flush
|
||||
compressed = compressed_chunk
|
||||
@buffer << compressed
|
||||
buffer << compressed
|
||||
yield compressed if block_given?
|
||||
end
|
||||
ensure
|
||||
gzip.close
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def write(chunk)
|
||||
@compressed_chunk = chunk
|
||||
end
|
||||
|
||||
def compressed_chunk
|
||||
compressed = @compressed_chunk
|
||||
compressed
|
||||
ensure
|
||||
@compressed_chunk = nil
|
||||
end
|
||||
end
|
||||
|
||||
module_function
|
||||
|
||||
def encode(payload)
|
||||
Encoder.new(payload)
|
||||
CompressEncoder.new(payload, Encoder.new)
|
||||
end
|
||||
|
||||
def decode(io)
|
||||
|
Loading…
x
Reference in New Issue
Block a user