Refactor internal request handling into a functional core (#17636)