lib/protocol/http1/body/fixed.rb in protocol-http1-0.21.0 vs lib/protocol/http1/body/fixed.rb in protocol-http1-0.22.0
- old
+ new
@@ -9,51 +9,53 @@
module HTTP1
module Body
class Fixed < HTTP::Body::Readable
def initialize(stream, length)
@stream = stream
+
@length = length
@remaining = length
end
attr :length
attr :remaining
def empty?
- @remaining == 0
+ @stream.nil? or @remaining == 0
end
def close(error = nil)
- # If we are closing the body without fully reading it, the underlying connection is now in an undefined state.
- if @remaining != 0
- @stream.close
+ if @stream
+ # If we are closing the body without fully reading it, the underlying connection is now in an undefined state.
+ if @remaining != 0
+ @stream.close_read
+ end
+
+ @stream = nil
end
super
end
# @raises EOFError if the stream is closed before the expected length is read.
def read
if @remaining > 0
- # `readpartial` will raise `EOFError` if the stream is closed/finished:
- if chunk = @stream.readpartial(@remaining)
- @remaining -= chunk.bytesize
-
- return chunk
+ if @stream
+ # `readpartial` will raise `EOFError` if the stream is finished, or `IOError` if the stream is closed.
+ if chunk = @stream.readpartial(@remaining)
+ @remaining -= chunk.bytesize
+
+ return chunk
+ end
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!"
end
end
- def join
- buffer = @stream.read(@remaining)
-
- @remaining = 0
-
- return buffer
- end
-
def inspect
- "\#<#{self.class} length=#{@length} remaining=#{@remaining}>"
+ "\#<#{self.class} length=#{@length} remaining=#{@remaining} state=#{@stream ? 'open' : 'closed'}>"
end
end
end
end
end