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, #length

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
	
	@length = 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



65
66
67
# File 'lib/async/http/body/chunked.rb', line 65

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

#readObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/async/http/body/chunked.rb', line 44

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

#stop(error) ⇒ Object



39
40
41
42
# File 'lib/async/http/body/chunked.rb', line 39

def stop(error)
	@protocol.close
	@finished = true
end