lib/protocol/http1/body/remainder.rb in protocol-http1-0.21.0 vs lib/protocol/http1/body/remainder.rb in protocol-http1-0.22.0
- old
+ new
@@ -6,56 +6,42 @@
require 'protocol/http/body/readable'
module Protocol
module HTTP1
module Body
+ # A body that reads all remaining data from the stream.
class Remainder < HTTP::Body::Readable
BLOCK_SIZE = 1024 * 64
# block_size may be removed in the future. It is better managed by stream.
def initialize(stream)
@stream = stream
- @empty = false
end
def empty?
- @empty or @stream.closed?
+ @stream.nil?
end
def close(error = nil)
- # We can't really do anything in this case except close the connection.
- @stream.close
- @empty = true
+ if @stream
+ # We can't really do anything in this case except close the connection.
+ @stream.close_read
+ @stream = nil
+ end
super
end
- # TODO this is a bit less efficient in order to maintain compatibility with `IO`.
def read
- @stream.readpartial(BLOCK_SIZE)
+ @stream&.readpartial(BLOCK_SIZE)
rescue EOFError, IOError
- @empty = true
-
+ @stream = nil
# I noticed that in some cases you will get EOFError, and in other cases IOError!?
return nil
end
- def call(stream)
- self.each do |chunk|
- stream.write(chunk)
- end
-
- stream.flush
- end
-
- def join
- @stream.read
- ensure
- @empty = true
- end
-
def inspect
- "\#<#{self.class} #{@stream.closed? ? 'closed' : 'open'}>"
+ "\#<#{self.class} state=#{@stream ? 'open' : 'closed'}>"
end
end
end
end
end