class Falcon::Adapters::Output

def self.wrap(status, headers, body, request = nil)

@parameter body [Object] The `rack` response body.
@parameter headers [Protocol::HTTP::Headers] The response headers.
@parameter status [Integer] The response status.
Wraps an array into a buffered body.
def self.wrap(status, headers, body, request = nil)
	# In no circumstance do we want this header propagating out:
	if length = headers.delete(CONTENT_LENGTH)
		# We don't really trust the user to provide the right length to the transport.
		length = Integer(length)
	end
	
	# If we have an Async::HTTP body, we return it directly:
	if body.is_a?(::Protocol::HTTP::Body::Readable)
		# warn "Returning #{body.class} as body is falcon-specific and may be removed in the future!"
		return body
	end
	
	# Otherwise, we have a more typical response body:
	if status == 200 and body.respond_to?(:to_path)
		begin
			# Don't mangle partial responses (206)
			return ::Protocol::HTTP::Body::File.open(body.to_path).tap do
				body.close if body.respond_to?(:close) # Close the original body.
			end
		rescue Errno::ENOENT
			# If the file is not available, ignore.
		end
	end
	
	# If we have a streaming body, we hijack the connection:
	unless body.respond_to?(:each)
		return Async::HTTP::Body::Hijack.new(body, request&.body)
	end
	
	if body.is_a?(Array)
		length ||= body.sum(&:bytesize)
		return self.new(body, length)
	else
		return self.new(body, length)
	end
end