class Falcon::Adapters::Rack

def call(request)

def call(request)
	request_path, query_string = request.path.split('?', 2)
	server_name, server_port = (request.authority || '').split(':', 2)
	
	env = {
		RACK_VERSION => [2, 0, 0],
		
		ASYNC_HTTP_REQUEST => request,
		
		RACK_INPUT => Input.new(request.body),
		RACK_ERRORS => $stderr,
		RACK_LOGGER => Async.logger,
		
		RACK_MULTITHREAD => true,
		RACK_MULTIPROCESS => true,
		RACK_RUNONCE => false,
		
		# The HTTP request method, such as “GET” or “POST”. This cannot ever be an empty string, and so is always required.
		REQUEST_METHOD => request.method,
		
		# The initial portion of the request URL's “path” that corresponds to the application object, so that the application knows its virtual “location”. This may be an empty string, if the application corresponds to the “root” of the server.
		SCRIPT_NAME => '',
		
		# The remainder of the request URL's “path”, designating the virtual “location” of the request's target within the application. This may be an empty string, if the request URL targets the application root and does not have a trailing slash. This value may be percent-encoded when originating from a URL.
		PATH_INFO => request_path,
		REQUEST_PATH => request_path,
		REQUEST_URI => request.path,
		# The portion of the request URL that follows the ?, if any. May be empty, but is always required!
		QUERY_STRING => query_string || '',
		
		# The server protocol (e.g. HTTP/1.1):
		SERVER_PROTOCOL => request.version,
		
		# The request scheme:
		RACK_URL_SCHEME => request.scheme,
		
		# I'm not sure what sane defaults should be here:
		SERVER_NAME => server_name || '',
		SERVER_PORT => server_port || '',
	}
	
	self.unwrap_request(request, env)
	
	if request.push?
		env[RACK_EARLY_HINTS] = EarlyHints.new(request)
	end
	
	if request.hijack?
		env[RACK_IS_HIJACK] = true
		
		env[RACK_HIJACK] = lambda do
			wrapper = request.hijack
			
			# We dup this as it might be taken out of the normal control flow, and the io will be closed shortly after returning from this method.
			io = wrapper.io.dup
			wrapper.close
			
			# This is implicitly returned:
			env[RACK_HIJACK_IO] = io
		end
	else
		env[RACK_IS_HIJACK] = false
	end
	
	status, headers, body = @app.call(env)
	
	# Partial hijack is not supported/tested.
	# if hijack = headers.delete('rack.hijack')
	# 	body = Async::HTTP::Body::Writable.new
	# 
	# 	Task.current.async do
	# 		hijack.call(body)
	# 	end
	# 	return nil
	# end
	
	# if env['rack.hijack_io']
	# 	return nil
	# end
	
	return make_response(request, status, headers, body)
rescue => exception
	@logger.error(self) {exception}
	
	return failure_response(exception)
end