lib/protocol/http1/body/chunked.rb in protocol-http1-0.8.2 vs lib/protocol/http1/body/chunked.rb in protocol-http1-0.8.3

- old
+ new

@@ -23,12 +23,12 @@ module Protocol module HTTP1 module Body class Chunked < HTTP::Body::Readable # TODO maybe this should take a stream rather than a connection? - def initialize(connection) - @connection = connection + def initialize(stream) + @stream = stream @finished = false @length = 0 @count = 0 end @@ -38,39 +38,45 @@ end def close(error = nil) # We only close the connection if we haven't completed reading the entire body: unless @finished - @connection.close + @stream.close @finished = true end super end def read return nil if @finished - length = @connection.read_line.to_i(16) + length = read_line.to_i(16) if length == 0 @finished = true - @connection.read_line + read_line return nil end - chunk = @connection.stream.read(length) - @connection.read_line # Consume the trailing CRLF + chunk = @stream.read(length) + read_line # Consume the trailing CRLF @length += length @count += 1 return chunk end def inspect "\#<#{self.class} #{@length} bytes read in #{@count} chunks>" + end + + private + + def read_line + @stream.gets(chomp: true) end end end end end