lib/protocol/http1/body/fixed.rb in protocol-http1-0.23.0 vs lib/protocol/http1/body/fixed.rb in protocol-http1-0.24.0

- old
+ new

@@ -7,58 +7,63 @@ module Protocol module HTTP1 module Body class Fixed < HTTP::Body::Readable - def initialize(stream, length) - @stream = stream + def initialize(connection, length) + @connection = connection @length = length @remaining = length end attr :length attr :remaining def empty? - @stream.nil? or @remaining == 0 + @connection.nil? or @remaining == 0 end def discard - if stream = @stream - @stream = nil + if connection = @connection + @connection = nil if @remaining != 0 - stream.close_read + connection.close_read end end end def close(error = nil) self.discard super end - # @raises EOFError if the stream is closed before the expected length is read. + # @raises EOFError if the connection is closed before the expected length is read. def read if @remaining > 0 - if @stream - # `readpartial` will raise `EOFError` if the stream is finished, or `IOError` if the stream is closed. - chunk = @stream.readpartial(@remaining) + if @connection + # `readpartial` will raise `EOFError` if the connection is finished, or `IOError` if the connection is closed. + chunk = @connection.readpartial(@remaining) @remaining -= chunk.bytesize + if @remaining == 0 + @connection.receive_end_stream! + @connection = nil + end + return chunk end - # If the stream has been closed before we have read the expected length, raise an error: - raise EOFError, "Stream closed before expected length was read!" + # If the connection has been closed before we have read the expected length, raise an error: + raise EOFError, "connection closed before expected length was read!" end end def inspect - "\#<#{self.class} length=#{@length} remaining=#{@remaining} state=#{@stream ? 'open' : 'closed'}>" + "\#<#{self.class} length=#{@length} remaining=#{@remaining} state=#{@connection ? 'open' : 'closed'}>" end end end end end