internal ParamsCollectionEnumerator(ParamsCollection coll)
 {
     this._Coll = coll;
     this._Pos  = -1;
 }
Exemple #2
0
        internal bool HandleRoute(HttpContext context, string httpMethod, string path, ParamsCollection pathParams)
        {
            if (!this.Routes.TryGetValue(httpMethod, out var routes))
            {
                return(false);
            }

            foreach (var route in routes)
            {
                var match = route.Pattern.Match(path);
                if (!match.Success)
                {
                    continue;
                }

                if (pathParams == null)
                {
                    pathParams = new ParamsCollection();
                }

                for (int i = 1; i < match.Groups.Count; i++)
                {
                    var g = match.Groups[i];
                    if (!g.Success)
                    {
                        continue;
                    }

                    var key = route.PatternKeys[i - 1];
                    var val = HttpUtility.UrlDecode(g.Value);

                    if (key.Name != null)
                    {
                        pathParams.Add(key.Name, val);
                    }

                    if (key.Index != null)
                    {
                        pathParams.Set(key.Index.Value, val);
                    }
                }

                try
                {
                    if (route.TargetAction != null)
                    {
                        route.TargetAction(context, path, pathParams);
                        return(true);
                    }
                    else if (route.ITarget != null)
                    {
                        route.ITarget.ProcessRequest(context, path, pathParams);
                        return(true);
                    }
                    else if (route.Handler != null)
                    {
                        var subPath = path.Remove(0, match.Value.Length);
                        if (!subPath.StartsWith("/"))
                        {
                            subPath = "/" + subPath;
                        }

                        if (route.Handler.HandleRoute(context, httpMethod, subPath, pathParams))
                        {
                            return(true);
                        }
                    }
                }
                catch (ThreadAbortException)
                {
                    // Ignore
                    return(true);
                }
                catch (System.Exception ex)
                {
                    if (OnExceptionAction != null)
                    {
                        OnExceptionAction(context, ex);
                        return(true);
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }

            return(false);
        }