Exemple #1
0
        public RouteEntry Find(HTTPRequest.Method method, string url, Dictionary <string, string> query)
        {
            if (string.IsNullOrEmpty(url))
            {
                return(null);
            }

            var table = _routes[method];

            if (!table.ContainsKey(url))
            {
                url = FindRouteWithArgs(method, url, query);

                if (url == null)
                {
                    return(null);
                }
            }

            if (table.ContainsKey(url))
            {
                return(table[url]);
            }

            return(null);
        }
Exemple #2
0
        public void Register(HTTPRequest.Method method, string path, int priority, Func <HTTPRequest, object> handler)
        {
            path = StringUtils.FixUrl(path);

            Dictionary <int, string> names;

            // TODO this probably only is necessary when creating a new RouteEntry
            if (path.Contains("{"))
            {
                string[] s     = path.Split('/');
                var      regex = new Regex(@"{([A-z]\w+)}");

                var sb = new StringBuilder();
                names = new Dictionary <int, string>();

                for (int i = 0; i < s.Length; i++)
                {
                    if (i > 0)
                    {
                        sb.Append('/');
                    }

                    var match = regex.Match(s[i]);
                    if (match.Success)
                    {
                        sb.Append("*");
                        names[i] = match.Groups[1].Value;
                    }
                    else
                    {
                        sb.Append(s[i]);
                    }
                }

                path = sb.ToString();
            }
            else
            {
                names = null;
            }

            var dic = _routes[method];

            RouteEntry entry;

            if (dic.ContainsKey(path))
            {
                entry = dic[path];
            }
            else
            {
                entry     = new RouteEntry(names);
                dic[path] = entry;
            }

            entry.Handlers.Add(new RouteEndPoint(handler, priority));
            entry.Handlers.Sort((x, y) => y.Priority.CompareTo(x.Priority));
        }
Exemple #3
0
        public void RegisterHandler(HTTPRequest.Method method, string path, int priority, Func <HTTPRequest, object> handler)
        {
            if (Running)
            {
                throw new Exception("Can't register new handlers when running");
            }

            _router.Register(method, path, priority, handler);
        }
Exemple #4
0
        private string FindRouteWithArgs(HTTPRequest.Method method, string url, Dictionary <string, string> query)
        {
            //bool hasSlash = urlPath.Contains("/");
            var splitter = new char[] { '/' };

            string[] s  = url.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
            var      sb = new StringBuilder();

            var table = _routes[method];

            var bitLen = 1 << s.Length;

            for (int i = 0; i < bitLen; i++)
            {
                sb.Length = 0;

                for (int j = 0; j < s.Length; j++)
                {
                    if (j > 0)
                    {
                        sb.Append('/');
                    }

                    bool isSet = (i & (1 << j)) != 0;
                    sb.Append(isSet ? s[j] : "*");
                }

                var path = sb.ToString();

                if (table.ContainsKey(path))
                {
                    var route = table[path];

                    if (route.Names != null)
                    {
                        foreach (var entry in route.Names)
                        {
                            query[entry.Value] = s[entry.Key];
                        }
                    }

                    return(path);
                }
            }

            return(null);
        }
Exemple #5
0
 public EndPointAttribute(HTTPRequest.Method method, string path, int priority = 0)
 {
     this.Method   = method;
     this.Path     = path;
     this.Priority = priority;
 }