in src/msha/routes-engine/rules/routes.ts [112:166]
export function tryGetMatchingRoute(req: http.IncomingMessage, userConfig: SWAConfigFile | undefined) {
const host = `${SWA_CLI_APP_PROTOCOL}://${req?.headers?.host}`;
const sanitizedUrl = new URL(req.url!, host);
const requestPathFileExtension = path.extname(sanitizedUrl.toString());
const isFileRequest = !!requestPathFileExtension;
const requestMethod = req.method;
const isLegacyConfigFile = userConfig?.isLegacyConfigFile;
if (userConfig?.routes?.length === 0) {
return;
}
let routeDef: SWAConfigFileRoute | undefined = undefined;
for (let i = 0; i < userConfig?.routes?.length!; i++) {
routeDef = userConfig?.routes[i];
let route = routeDef?.route;
if (!route) {
// this is an invalid route, ignore it
continue;
}
const isMatchingRoute = isLegacyConfigFile
? doesRequestPathMatchLegacyRoute(sanitizedUrl.pathname, routeDef, isAuthRequest(req), isFileRequest)
: doesRequestPathMatchRoute(
sanitizedUrl.pathname,
routeDef,
requestMethod,
routeDef?.methods,
AUTH_STATUS.NoAuth /* TODO get the right auth status */
);
if (isMatchingRoute) {
// if the rule isn't a redirect rule, no need to check for circular redirect
if (!routeDef?.redirect) {
return routeDef;
}
// this rule will result in an infinite redirect loop so keep searching for another rule
const redirectUrl = new URL(routeDef.redirect || "/", host);
if (sanitizedUrl.toString() === redirectUrl.pathname || sanitizedUrl.toString() === redirectUrl.toString()) {
continue;
}
routeDef = {
...routeDef,
redirect: redirectUrl.toString(),
};
return routeDef;
}
}
return;
}