private static void BuildBranches(PathBranch branch) { branch.Build(); foreach (var child in branch.Children) { BuildBranches(child); } }
private static List <RouteResult> BranchSearch(PathBranch branch, string method, IReadOnlyList <string> pathTokens) { var list = new List <RouteResult>(); var matchedTokens = new RequestDictionary(); return(BranchSearch(branch, method, pathTokens, 0, matchedTokens, list)); }
public void AddChild(PathBranch pathBranch) { var key = GetKey(pathBranch.Token); if (ChildrenDict.ContainsKey(key)) { return; } ChildrenDict[key] = pathBranch; }
public PathBranch FindOrAddChildByToken(RouteToken token) { var key = GetKey(token); var pathBranch = FindChild(key); if (pathBranch != null) { return(pathBranch); } pathBranch = new PathBranch { Token = token }; ChildrenDict[key] = pathBranch; return(pathBranch); }
private static void AllRoutes(PathBranch branch, List <Route> routes) { if (branch.Routes != null) { foreach (var route in branch.Routes) { if (route.ClassHandler != null || route.ActionHandler != null) { routes.Add(route); } } } foreach (var branchChild in branch.Children) { AllRoutes(branchChild, routes); } }
public void Compile() { var root = new PathBranch(); foreach (var rb in RouteBuilder.AllChildren) { var options = rb.RouteOptions; options.Defaults.TryGetValue("action", out var action); if (options.ClassHandler == null && options.ActionHandler == null || options.Methods == null) { continue; } BranchSorter(Route.FromOptions(options), root); } BuildBranches(root); Root = root; }
public void BranchSorter(Route route, PathBranch root) { var i = 0; var branch = root; while (true) { branch = branch.FindOrAddChildByToken(route.Tokens[i]); if (i == route.Tokens.Count - 1) { branch.Routes.Add(route); return; } var next = i + 1; if (next < route.Tokens.Count && (route.Tokens[next].Optional || route.Tokens[next].Greedy)) { branch.Routes.Add(route); } i = i + 1; } }
private static List <RouteResult> BranchSearch(PathBranch branch, string method, IReadOnlyList <string> pathTokens, int tokenIndex, RequestDictionary matchedTokens, List <RouteResult> matchedResults) { if (tokenIndex > pathTokens.Count - 1) { return(matchedResults); } var token = pathTokens[tokenIndex]; foreach (var child in branch.Children) { if (child.Token.Text != null) { if (child.Token.Text != token || child.Routes == null) { continue; } foreach (var route in child.Routes) { if (route.Methods.Contains(method)) { matchedResults.Add(new RouteResult { Route = route, Vars = matchedTokens }); } } BranchSearch(child, method, pathTokens, tokenIndex + 1, matchedTokens, matchedResults); } else if (child.Token.MatchAny || child.Token.Matcher != null) { if (child.Token.Matcher != null) { var match = child.Token.Matcher.Match(token); if (!match.Success) { continue; } } var name = child.Token.Name; if (child.Token.Greedy) { matchedTokens[name] = string.Join("/", pathTokens.Skip(tokenIndex)); } else { matchedTokens[name] = token; } if (child.Routes != null) { foreach (var route in child.Routes) { if (route.Methods.Contains(method)) { matchedResults.Add(new RouteResult { Route = route, Vars = matchedTokens }); } } } if (!child.Token.Greedy) { BranchSearch(child, method, pathTokens, tokenIndex + 1, new RequestDictionary(matchedTokens), matchedResults); } } } return(matchedResults); }