class Async::HTTP::Body::Hijack

A body which is designed for hijacked server responses - a response which uses a block to read and write the request and response bodies respectively.

def self.response(request, status, headers, &block)

def self.response(request, status, headers, &block)
	::Protocol::HTTP::Response[status, headers, self.wrap(request, &block)]
end

def self.wrap(request = nil, &block)

def self.wrap(request = nil, &block)
	self.new(block, request&.body)
end

def call(stream)

def call(stream)
	return @block.call(stream)
end

def empty?

Has the producer called #finish and has the reader consumed the nil token?
def empty?
	if @stream
		@stream.empty?
	else
		false
	end
end

def initialize(block, input = nil)

def initialize(block, input = nil)
	@block = block
	@input = input
	
	@task = nil
	@stream = nil
end

def inspect

def inspect
	"\#<#{self.class} #{@block.inspect}>"
end

def read

Read the next available chunk.
def read
	unless @task
		@stream = Stream.new(@input)
		
		@task = Task.current.async do |task|
			task.annotate "Streaming hijacked body."
			
			@block.call(@stream)
		end
	end
	
	return @stream.output.read
end

def ready?

def ready?
	if @stream
		@stream.output.ready?
	end
end

def stream?

We prefer streaming directly as it's the lowest overhead.
def stream?
	true
end

def to_s

def to_s
	"<Hijack #{@block.class}>"
end