Sha256: d816db6cd9782842ecdc2860bbef6ba37050a614bf51060c26521773e13108e2
Contents?: true
Size: 1.87 KB
Versions: 7
Compression:
Stored size: 1.87 KB
Contents
# frozen_string_literal: true module HTTPX module Transcoder module GRPCEncoding class Deflater extend Forwardable attr_reader :content_type def initialize(body, compressed:) @content_type = body.content_type @body = BodyReader.new(body) @compressed = compressed end def bytesize return @body.bytesize if @body.respond_to?(:bytesize) Float::INFINITY end def read(length = nil, outbuf = nil) buf = @body.read(length, outbuf) return unless buf compressed_flag = @compressed ? 1 : 0 buf = outbuf if outbuf buf.prepend([compressed_flag, buf.bytesize].pack("CL>")) buf end end class Inflater def initialize(response) @encodings = response.headers.get("grpc-encoding") @response = response end def call(message, &blk) data = "".b until message.empty? compressed, size = message.unpack("CL>") encoded_data = message.byteslice(5..size + 5 - 1) if compressed == 1 @encodings.reverse_each do |encoding| decoder = @response.body.class.initialize_inflater_by_encoding(encoding, @response, bytesize: encoded_data.bytesize) encoded_data = decoder.call(encoded_data) blk.call(encoded_data) if blk data << encoded_data end else blk.call(encoded_data) if blk data << encoded_data end message = message.byteslice((size + 5)..-1) end data end end def self.encode(*args, **kwargs) Deflater.new(*args, **kwargs) end def self.decode(response) Inflater.new(response) end end end end
Version data entries
7 entries across 7 versions & 1 rubygems