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

- old
+ new

@@ -1,11 +1,11 @@ # frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2024, by Samuel Williams. -require 'protocol/http/body/readable' +require "protocol/http/body/readable" module Protocol module HTTP1 module Body class Fixed < HTTP::Body::Readable @@ -21,32 +21,35 @@ def empty? @stream.nil? or @remaining == 0 end - def close(error = nil) - if @stream - # If we are closing the body without fully reading it, the underlying connection is now in an undefined state. + def discard + if stream = @stream + @stream = nil + if @remaining != 0 - @stream.close_read + stream.close_read end - - @stream = nil end + end + + def close(error = nil) + self.discard super end # @raises EOFError if the stream 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. - if chunk = @stream.readpartial(@remaining) - @remaining -= chunk.bytesize - - return chunk - end + chunk = @stream.readpartial(@remaining) + + @remaining -= chunk.bytesize + + 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!" end