Public Class methods
new(buffer, trailers = false)
[show source]
# File lib/httpx/transcoder/chunker.rb 41 def initialize(buffer, trailers = false) 42 @buffer = buffer 43 @chunk_buffer = "".b 44 @finished = false 45 @state = :length 46 @trailers = trailers 47 end
Public Instance methods
each()
[show source]
# File lib/httpx/transcoder/chunker.rb 53 def each 54 loop do 55 case @state 56 when :length 57 index = @buffer.index(CRLF) 58 return unless index && index.positive? 59 60 # Read hex-length 61 hexlen = @buffer.byteslice(0, index) 62 @buffer = @buffer.byteslice(index..-1) || "".b 63 hexlen[/\h/] || raise(Error, "wrong chunk size line: #{hexlen}") 64 @chunk_length = hexlen.hex 65 # check if is last chunk 66 @finished = @chunk_length.zero? 67 nextstate(:crlf) 68 when :crlf 69 crlf_size = @finished && !@trailers ? 4 : 2 70 # consume CRLF 71 return if @buffer.bytesize < crlf_size 72 raise Error, "wrong chunked encoding format" unless @buffer.start_with?(CRLF * (crlf_size / 2)) 73 74 @buffer = @buffer.byteslice(crlf_size..-1) 75 if @chunk_length.nil? 76 nextstate(:length) 77 else 78 return if @finished 79 80 nextstate(:data) 81 end 82 when :data 83 chunk = @buffer.byteslice(0, @chunk_length) 84 @buffer = @buffer.byteslice(@chunk_length..-1) || "".b 85 @chunk_buffer << chunk 86 @chunk_length -= chunk.bytesize 87 if @chunk_length.zero? 88 yield @chunk_buffer unless @chunk_buffer.empty? 89 @chunk_buffer.clear 90 @chunk_length = nil 91 nextstate(:crlf) 92 end 93 end 94 break if @buffer.empty? 95 end 96 end
finished?()
[show source]
# File lib/httpx/transcoder/chunker.rb 98 def finished? 99 @finished 100 end