Class: Async::HTTP::Body::Chunked

Inherits:
Readable
  • Object
show all
Defined in:
lib/async/http/body/chunked.rb

Instance Method Summary collapse

Methods inherited from Readable

#close, #each, #join, #stop

Constructor Details

#initialize(protocol) ⇒ Chunked

Returns a new instance of Chunked.



27
28
29
30
31
32
33
# File 'lib/async/http/body/chunked.rb', line 27

def initialize(protocol)
	@protocol = protocol
	@finished = false
	
	@bytesize = 0
	@count = 0
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/async/http/body/chunked.rb', line 35

def empty?
	@finished
end

#inspectObject



61
62
63
# File 'lib/async/http/body/chunked.rb', line 61

def inspect
	"\#<#{self.class} #{@bytesize} bytes read in #{@count} chunks>"
end

#readObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/async/http/body/chunked.rb', line 39

def read
	return nil if @finished
	
	size = @protocol.read_line.to_i(16)
	
	if size == 0
		@protocol.read_line
		
		@finished = true
		
		return nil
	end
	
	chunk = @protocol.stream.read(size)
	@protocol.read_line # Consume the trailing CRLF
	
	@bytesize += size
	@count += 1
	
	return chunk
end