resolve method
Resolves any redirects for path.
Returns null if no redirect exists for path.
Collapses redirect chains (e.g. A -> B -> C becomes A -> C) and throws
StateError if a redirect cycle is detected or maxHops is exceeded.
Implementation
RedirectResolution? resolve(String path) {
var current = _normalize(path);
final visited = <String>{current};
var hops = 0;
int? finalStatus;
while (_rules.containsKey(current)) {
hops++;
if (hops > maxHops) {
throw StateError(
'Redirect chain exceeded maximum hops ($maxHops): $path',
);
}
final rule = _rules[current]!;
finalStatus ??= rule.statusCode;
final next = _normalize(rule.toPath);
_validateDestination(rule.toPath);
if (!visited.add(next)) {
throw StateError(
'Redirect loop detected for path: $path (cycle at $next)',
);
}
current = next;
}
if (hops == 0) return null;
return RedirectResolution(
targetPath: current,
statusCode: finalStatus ?? 301,
hopCount: hops,
);
}