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

#each, #finish, #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

#close(error = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/async/http/body/chunked.rb', line 39

def close(error = nil)
  # We only close the connection if we haven't completed reading the entire body:
  unless @finished
    @protocol.close
    @finished = true
  end
  
  super
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @finished
end

#inspectObject



70
71
72
# File 'lib/async/http/body/chunked.rb', line 70

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

#readObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/async/http/body/chunked.rb', line 49

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