public void AddHandler(string url, HttpMethod method, HandlerFunc handler) { if (string.IsNullOrEmpty(url) || (url.Length == 1 && url[0] == '/')) { _pathTree.Add(method, handler); return; } if (url[0] == '/') { url = url.Substring(1); } string[] path = url.Split('/'); PathTreeNode finiteNode = _pathTree; foreach (var segment in path) { if (finiteNode.TryGetSubNode(segment, out PathTreeNode sub)) { if (sub != null) { finiteNode = sub; } } else { finiteNode = finiteNode.Add(segment); } } finiteNode.Add(method, handler); }
public Router() { _parameterMapping = new Dictionary <string, Regex> { [IntegerUrlParameter] = new Regex(IntegerUrlParameter) }; _pathTree = new PathTreeNode(); }
public PathTreeNode Add(string subDomain) { if (string.IsNullOrEmpty(subDomain)) { return(null); } var sub = new PathTreeNode(); int pos = subDomain.IndexOf('/'); if (pos > 0) { subDomain = subDomain.Substring(0, pos); } _subDomains.Add(subDomain, sub); return(sub); }
public bool TryGetSubNode(string subDomain, out PathTreeNode treeNode) { return(_subDomains.TryGetValue(subDomain, out treeNode)); }