Class: HTTPX::Transcoder::Chunker::Decoder
- Inherits:
-
Object
- Object
- HTTPX::Transcoder::Chunker::Decoder
- Extended by:
- Forwardable
- Defined in:
- lib/httpx/transcoder/chunker.rb
Instance Method Summary collapse
- #each ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(buffer, trailers = false) ⇒ Decoder
constructor
A new instance of Decoder.
- #to_s ⇒ Object
Constructor Details
#initialize(buffer, trailers = false) ⇒ Decoder
Returns a new instance of Decoder.
42 43 44 45 46 47 48 49 |
# File 'lib/httpx/transcoder/chunker.rb', line 42 def initialize(buffer, trailers = false) @buffer = buffer @chunk_length = nil @chunk_buffer = "".b @finished = false @state = :length @trailers = trailers end |
Instance Method Details
#each ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/httpx/transcoder/chunker.rb', line 55 def each loop do case @state when :length index = @buffer.index(CRLF) return unless index && index.positive? # Read hex-length hexlen = @buffer.byteslice(0, index) @buffer = @buffer.byteslice(index..-1) || "".b hexlen[/\h/] || raise(Error, "wrong chunk size line: #{hexlen}") @chunk_length = hexlen.hex # check if is last chunk @finished = @chunk_length.zero? nextstate(:crlf) when :crlf crlf_size = @finished && !@trailers ? 4 : 2 # consume CRLF return if @buffer.bytesize < crlf_size raise Error, "wrong chunked encoding format" unless @buffer.start_with?(CRLF * (crlf_size / 2)) @buffer = @buffer.byteslice(crlf_size..-1) if @chunk_length.nil? nextstate(:length) else return if @finished nextstate(:data) end when :data chunk = @buffer.byteslice(0, @chunk_length) @buffer = @buffer.byteslice(@chunk_length..-1) || "".b @chunk_buffer << chunk @chunk_length -= chunk.bytesize if @chunk_length.zero? yield @chunk_buffer unless @chunk_buffer.empty? @chunk_buffer.clear @chunk_length = nil nextstate(:crlf) end end break if @buffer.empty? end end |
#finished? ⇒ Boolean
100 101 102 |
# File 'lib/httpx/transcoder/chunker.rb', line 100 def finished? @finished end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/httpx/transcoder/chunker.rb', line 51 def to_s @buffer end |