Class: Protocol::HTTP1::Body::Chunked

Inherits:
HTTP::Body::Readable
  • Object
show all
Defined in:
lib/protocol/http1/body/chunked.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Chunked

TODO maybe this should take a stream rather than a connection?



28
29
30
31
32
33
34
# File 'lib/protocol/http1/body/chunked.rb', line 28

def initialize(connection)
  @connection = connection
  @finished = false
  
  @length = 0
  @count = 0
end

Instance Method Details

#close(error = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/protocol/http1/body/chunked.rb', line 40

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

#empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/protocol/http1/body/chunked.rb', line 36

def empty?
  @finished
end

#inspectObject



71
72
73
# File 'lib/protocol/http1/body/chunked.rb', line 71

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

#readObject



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

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