Sha256: d669e7abe1aec9628bfd74e222f22abea5627c02920d8107cd2b08b7ac27fbf2

Contents?: true

Size: 1.93 KB

Versions: 26

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2023, by Samuel Williams.

require_relative 'wrapper'

require 'zlib'

module Protocol
	module HTTP
		module Body
			class ZStream < Wrapper
				DEFAULT_LEVEL = 7
				
				DEFLATE = -Zlib::MAX_WBITS
				GZIP =  Zlib::MAX_WBITS | 16
				
				ENCODINGS = {
					'deflate' => DEFLATE,
					'gzip' => GZIP,
				}
				
				def initialize(body, stream)
					super(body)
					
					@stream = stream
					
					@input_length = 0
					@output_length = 0
				end
				
				def close(error = nil)
					@stream.close unless @stream.closed?
					
					super
				end
				
				def length
					# We don't know the length of the output until after it's been compressed.
					nil
				end
				
				attr :input_length
				attr :output_length
				
				def ratio
					if @input_length != 0
						@output_length.to_f / @input_length.to_f
					else
						1.0
					end
				end
				
				def inspect
					"#{super} | \#<#{self.class} #{(ratio*100).round(2)}%>"
				end
			end
			
			class Deflate < ZStream
				def self.for(body, window_size = GZIP, level = DEFAULT_LEVEL)
					self.new(body, Zlib::Deflate.new(level, window_size))
				end
				
				def stream?
					# We might want to revisit this design choice.
					# We could wrap the streaming body in a Deflate stream, but that would require an extra stream wrapper which we don't have right now. See also `Digestable#stream?`.
					false
				end
				
				def read
					return if @stream.finished?
					
					# The stream might have been closed while waiting for the chunk to come in.
					if chunk = super
						@input_length += chunk.bytesize
						
						chunk = @stream.deflate(chunk, Zlib::SYNC_FLUSH)
						
						@output_length += chunk.bytesize
						
						return chunk
					elsif !@stream.closed?
						chunk = @stream.finish
						
						@output_length += chunk.bytesize
						
						return chunk.empty? ? nil : chunk
					end
				end
			end
		end
	end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
protocol-http-0.32.0 lib/protocol/http/body/deflate.rb
protocol-http-0.31.0 lib/protocol/http/body/deflate.rb
protocol-http-0.30.0 lib/protocol/http/body/deflate.rb
protocol-http-0.29.0 lib/protocol/http/body/deflate.rb
protocol-http-0.28.2 lib/protocol/http/body/deflate.rb
protocol-http-0.28.1 lib/protocol/http/body/deflate.rb
protocol-http-0.28.0 lib/protocol/http/body/deflate.rb
protocol-http-0.27.0 lib/protocol/http/body/deflate.rb
protocol-http-0.26.8 lib/protocol/http/body/deflate.rb
protocol-http-0.26.7 lib/protocol/http/body/deflate.rb
protocol-http-0.26.6 lib/protocol/http/body/deflate.rb
protocol-http-0.26.5 lib/protocol/http/body/deflate.rb
protocol-http-0.26.4 lib/protocol/http/body/deflate.rb
protocol-http-0.26.3 lib/protocol/http/body/deflate.rb
protocol-http-0.26.2 lib/protocol/http/body/deflate.rb
protocol-http-0.26.1 lib/protocol/http/body/deflate.rb
protocol-http-0.26.0 lib/protocol/http/body/deflate.rb
protocol-http-0.25.0 lib/protocol/http/body/deflate.rb
protocol-http-0.24.7 lib/protocol/http/body/deflate.rb
protocol-http-0.24.6 lib/protocol/http/body/deflate.rb