4
I was trying to do a reverse proxy using Webrick when I discovered that the version I had installed from Webrick did not contain the methods necessary to make a "chunked" worthy reading. So I applied the following Monkey patch:
class WEBrick::HTTPRequest
##
# Prepares the HTTPRequest object for use as the
# source for IO.copy_stream
def body_reader
@body_tmp = []
@body_rd = Fiber.new do
body do |buf|
@body_tmp << buf
Fiber.yield
end
end
@body_rd.resume # grab the first chunk and yield
self
end
# for IO.copy_stream. Note: we may return a larger string than +size+
# here; but IO.copy_stream does not care.
def readpartial(size, buf = ''.b) # :nodoc
res = @body_tmp.shift or raise EOFError, 'end of file reached'
buf.replace(res)
res.clear
@body_rd.resume # get more chunks
buf
end
end
I saw that he uses Fiber
to do the reading on Hunks, but did not understand what class Fiber
is neither what the Fiber.yield
different from a simple yield
.
That one Fiber
has some purpose with threads
? Your big gain is in using it with yield
s, or has any other use case other than this?