mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-05 00:02:38 -04:00
moved deflate and gzip to separate file
This commit is contained in:
parent
61c82a0e1d
commit
518ed5280e
@ -5,8 +5,9 @@ module HTTPX
|
|||||||
module Compression
|
module Compression
|
||||||
ACCEPT_ENCODING = %w[gzip deflate].freeze
|
ACCEPT_ENCODING = %w[gzip deflate].freeze
|
||||||
|
|
||||||
def self.load_dependencies(*)
|
def self.configure(klass, *)
|
||||||
require "zlib"
|
klass.plugin(:"compression/gzip")
|
||||||
|
klass.plugin(:"compression/deflate")
|
||||||
end
|
end
|
||||||
|
|
||||||
module RequestMethods
|
module RequestMethods
|
||||||
@ -78,116 +79,6 @@ module HTTPX
|
|||||||
end
|
end
|
||||||
end
|
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
|
|
||||||
begin
|
|
||||||
gzip = Zlib::GzipWriter.new(self)
|
|
||||||
|
|
||||||
while chunk = @raw.read(16_384)
|
|
||||||
gzip.write(chunk)
|
|
||||||
gzip.flush
|
|
||||||
compressed = compressed_chunk
|
|
||||||
@buffer << compressed
|
|
||||||
yield compressed if block_given?
|
|
||||||
end
|
|
||||||
ensure
|
|
||||||
gzip.close
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module_function
|
|
||||||
|
|
||||||
def encode(payload)
|
|
||||||
Encoder.new(payload)
|
|
||||||
end
|
|
||||||
|
|
||||||
def decode(io)
|
|
||||||
Zlib::GzipReader.new(io, window_size: 32 + Zlib::MAX_WBITS)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module DeflateTranscoder
|
|
||||||
class Encoder < CompressEncoder
|
|
||||||
private
|
|
||||||
|
|
||||||
def compress
|
|
||||||
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)
|
|
||||||
compressed = deflater.deflate(chunk)
|
|
||||||
@buffer << compressed
|
|
||||||
yield compressed if block_given?
|
|
||||||
end
|
|
||||||
last = deflater.finish
|
|
||||||
@buffer << last
|
|
||||||
yield last if block_given?
|
|
||||||
ensure
|
|
||||||
deflater.close
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module_function
|
|
||||||
|
|
||||||
class Decoder
|
|
||||||
def initialize(io)
|
|
||||||
@io = io
|
|
||||||
@inflater = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
|
|
||||||
@buffer = StringIO.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def rewind
|
|
||||||
@buffer.rewind
|
|
||||||
end
|
|
||||||
|
|
||||||
def read(*args)
|
|
||||||
return @buffer.read(*args) if @io.eof?
|
|
||||||
chunk = @io.read(*args)
|
|
||||||
inflated_chunk = @inflater.inflate(chunk)
|
|
||||||
inflated_chunk << @inflater.finish if @io.eof?
|
|
||||||
@buffer << chunk
|
|
||||||
inflated_chunk
|
|
||||||
end
|
|
||||||
|
|
||||||
def close
|
|
||||||
@io.close
|
|
||||||
@io.unlink if @io.respond_to?(:unlink)
|
|
||||||
@inflater.close
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def encode(payload)
|
|
||||||
Encoder.new(payload)
|
|
||||||
end
|
|
||||||
|
|
||||||
def decode(io)
|
|
||||||
Decoder.new(io)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Transcoder.register "gzip", GZIPTranscoder
|
|
||||||
Transcoder.register "deflate", DeflateTranscoder
|
|
||||||
end
|
end
|
||||||
register_plugin :compression, Compression
|
register_plugin :compression, Compression
|
||||||
end
|
end
|
||||||
|
85
lib/httpx/plugins/compression/deflate.rb
Normal file
85
lib/httpx/plugins/compression/deflate.rb
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module HTTPX
|
||||||
|
module Plugins
|
||||||
|
module Compression
|
||||||
|
module Deflate
|
||||||
|
|
||||||
|
def self.load_dependencies(*)
|
||||||
|
require "stringio"
|
||||||
|
require "zlib"
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.configure(*)
|
||||||
|
Transcoder.register "deflate", DeflateTranscoder
|
||||||
|
end
|
||||||
|
|
||||||
|
module DeflateTranscoder
|
||||||
|
class Encoder < CompressEncoder
|
||||||
|
private
|
||||||
|
|
||||||
|
def compress
|
||||||
|
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)
|
||||||
|
compressed = deflater.deflate(chunk)
|
||||||
|
@buffer << compressed
|
||||||
|
yield compressed if block_given?
|
||||||
|
end
|
||||||
|
last = deflater.finish
|
||||||
|
@buffer << last
|
||||||
|
yield last if block_given?
|
||||||
|
ensure
|
||||||
|
deflater.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module_function
|
||||||
|
|
||||||
|
class Decoder
|
||||||
|
def initialize(io)
|
||||||
|
@io = io
|
||||||
|
@inflater = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
|
||||||
|
@buffer = StringIO.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def rewind
|
||||||
|
@buffer.rewind
|
||||||
|
end
|
||||||
|
|
||||||
|
def read(*args)
|
||||||
|
return @buffer.read(*args) if @io.eof?
|
||||||
|
chunk = @io.read(*args)
|
||||||
|
inflated_chunk = @inflater.inflate(chunk)
|
||||||
|
inflated_chunk << @inflater.finish if @io.eof?
|
||||||
|
@buffer << chunk
|
||||||
|
inflated_chunk
|
||||||
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
@io.close
|
||||||
|
@io.unlink if @io.respond_to?(:unlink)
|
||||||
|
@inflater.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def encode(payload)
|
||||||
|
Encoder.new(payload)
|
||||||
|
end
|
||||||
|
|
||||||
|
def decode(io)
|
||||||
|
Decoder.new(io)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
register_plugin :"compression/deflate", Compression::Deflate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
65
lib/httpx/plugins/compression/gzip.rb
Normal file
65
lib/httpx/plugins/compression/gzip.rb
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module HTTPX
|
||||||
|
module Plugins
|
||||||
|
module Compression
|
||||||
|
module GZIP
|
||||||
|
|
||||||
|
def self_load_dependencies(*)
|
||||||
|
require "zlib"
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.configure(*)
|
||||||
|
Transcoder.register "gzip", GZIPTranscoder
|
||||||
|
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
|
||||||
|
begin
|
||||||
|
gzip = Zlib::GzipWriter.new(self)
|
||||||
|
|
||||||
|
while chunk = @raw.read(16_384)
|
||||||
|
gzip.write(chunk)
|
||||||
|
gzip.flush
|
||||||
|
compressed = compressed_chunk
|
||||||
|
@buffer << compressed
|
||||||
|
yield compressed if block_given?
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
gzip.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module_function
|
||||||
|
|
||||||
|
def encode(payload)
|
||||||
|
Encoder.new(payload)
|
||||||
|
end
|
||||||
|
|
||||||
|
def decode(io)
|
||||||
|
Zlib::GzipReader.new(io, window_size: 32 + Zlib::MAX_WBITS)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
register_plugin :"compression/gzip", Compression::GZIP
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user