예제 #1
0
        public bool Matches(HttpRequestMessage request, string paramTemplate)
        {
            var result = MatchPattern.Match(paramTemplate);

            if (!result.Success)
            {
                return(false);
            }


            var parameterValues = new Dictionary <string, object>();

            for (int i = 1; i < result.Groups.Count; i++)
            {
                var name  = MatchPattern.GroupNameFromNumber(i);
                var value = result.Groups[i].Value;
                parameterValues.Add(name, value);
            }

            foreach (var constraint in _Constraints)
            {
                var match = constraint.Value.Match(request, null, constraint.Key, parameterValues, HttpRouteDirection.UriResolution);
                if (match == false)
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #2
0
        protected override System.Threading.Tasks.Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            PathRouteData pathRouteData;

            // Message handlers cause this method to be re-entrant.  Determine if this is the first time in or not.
            var beforeMessageHandler = IsBeforeMessageHandler(request, _MessageHandler);

            if (beforeMessageHandler || _MessageHandler == null)
            {
                pathRouteData = RetrieveRouteData(request);

                // Parse Parameters from URL and add them to the PathRouteData
                var result = MatchPattern.Match(pathRouteData.CurrentSegment);

                for (int i = 1; i < result.Groups.Count; i++)
                {
                    var name  = MatchPattern.GroupNameFromNumber(i);
                    var value = result.Groups[i].Value;
                    pathRouteData.SetParameter(name, value);
                }
            }
            else
            {
                pathRouteData = (PathRouteData)GetRouteData(request);
            }



            if (beforeMessageHandler)
            {
                return(base.SendAsync(request, cancellationToken));  // Call message handler, which will call back into here.
            }

            pathRouteData.AddRouter(this); // Track ApiRouters used to resolve path

            if (pathRouteData.EndOfPath())
            {
                if (HasController)
                {
                    return(Dispatch(request, cancellationToken));
                }
                else
                {
                    var tcs = new TaskCompletionSource <HttpResponseMessage>();
                    tcs.SetResult(new HttpResponseMessage(HttpStatusCode.NotFound));
                    return(tcs.Task);
                }
            }
            else
            {
                pathRouteData.MoveToNext();

                var nextRouter = GetMatchingRouter(request, pathRouteData.CurrentSegment);

                if (nextRouter != null)
                {
                    return(nextRouter.SendAsync(request, cancellationToken));
                }

                var tcs = new TaskCompletionSource <HttpResponseMessage>();
                tcs.SetResult(new HttpResponseMessage(HttpStatusCode.NotFound));
                return(tcs.Task);
            }
        }