dvHostedRequest function

DVHostedRequest dvHostedRequest(
  1. DVHosts hosts, {
  2. required String? host,
  3. required String rawPath,
  4. String? query,
  5. required bool isRoute(
    1. String internalPath
    ),
})

rawPath, asked for on host, against hosts.

The host is matched (an unknown one is the primary), the path mapped through its mount and trimmed of trailing slashes as an unhosted request is. When isRoute says that path is a route and another host owns it, DVHostedRequest.redirect is that route's public URL, query kept. A path no route serves is never redirected: it is not found where it was asked.

Implementation

DVHostedRequest dvHostedRequest(
  DVHosts hosts, {
  required String? host,
  required String rawPath,
  String? query,
  required bool Function(String internalPath) isRoute,
}) {
  final DVHostMatch requested = hosts.matchOrPrimary(host);
  final String mapped = requested.internalPath(rawPath);
  final String path = mapped == '/'
      ? '/'
      : mapped.replaceAll(RegExp(r'/+$'), '');
  final DVHostMatch owner = hosts.owner(path);
  final bool elsewhere = owner.origin != requested.origin && isRoute(path);
  return DVHostedRequest(
    hosts: hosts,
    requested: requested,
    owner: owner,
    path: path,
    redirect: elsewhere ? hosts.publicUrl(path, query: query) : null,
  );
}