1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2019-2022, by Samuel Williams.
require 'protocol/http/body/readable'
module Protocol
module HTTP1
module Body
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
end
def empty?
@stream.eof? or @stream.closed?
end
def close(error = nil)
# We can't really do anything in this case except close the connection.
@stream.close
super
end
# TODO this is a bit less efficient in order to maintain compatibility with `IO`.
def read
@stream.readpartial(BLOCK_SIZE)
rescue EOFError, IOError
# 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
end
def inspect
"\#<#{self.class} #{@stream.closed? ? 'closed' : 'open'}>"
end
end
end
end
end
|