예제 #1
0
        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);
        }
예제 #2
0
        public Router()
        {
            _parameterMapping = new Dictionary <string, Regex>
            {
                [IntegerUrlParameter] = new Regex(IntegerUrlParameter)
            };

            _pathTree = new PathTreeNode();
        }
예제 #3
0
        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);
        }
예제 #4
0
 public bool TryGetSubNode(string subDomain, out PathTreeNode treeNode)
 {
     return(_subDomains.TryGetValue(subDomain, out treeNode));
 }