class HTTPX::Transcoder::Chunker::Decoder

  1. lib/httpx/transcoder/chunker.rb
Superclass: Object

Methods

Public Class

  1. new

Public Instance

  1. each
  2. finished?
  3. to_s

Public Class methods

new (buffer, trailers = false)
[show source]
   # File lib/httpx/transcoder/chunker.rb
42 def initialize(buffer, trailers = false)
43   @buffer = buffer
44   @chunk_length = nil
45   @chunk_buffer = "".b
46   @finished = false
47   @state = :length
48   @trailers = trailers
49 end

Public Instance methods

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