Exemplo n.º 1
0
 private static void AddUndeclaredRouteParameters(HttpParsedRoute parsedRoute, IDictionary <string, object> routeDefaults, IList <ApiParameterDescription> parameterDescriptions)
 {
     foreach (PathSegment path in parsedRoute.PathSegments)
     {
         PathContentSegment content = path as PathContentSegment;
         if (content != null)
         {
             foreach (PathSubsegment subSegment in content.Subsegments)
             {
                 PathParameterSubsegment parameter = subSegment as PathParameterSubsegment;
                 if (parameter != null)
                 {
                     object parameterValue;
                     string parameterName = parameter.ParameterName;
                     if (!parameterDescriptions.Any(p => String.Equals(p.Name, parameterName, StringComparison.OrdinalIgnoreCase)) &&
                         (!routeDefaults.TryGetValue(parameterName, out parameterValue) ||
                          parameterValue != RouteParameter.Optional))
                     {
                         parameterDescriptions.Add(
                             new ApiParameterDescription
                         {
                             Name   = parameterName,
                             Source = ApiParameterSource.FromUri
                         });
                     }
                 }
             }
         }
     }
 }
        // Segments have the following order:
        // 1 - Literal segments
        // 2 - Constrained parameter segments / Multi-part segments
        // 3 - Unconstrained parameter segments
        // 4 - Constrained wildcard parameter segments
        // 5 - Unconstrained wildcard parameter segments
        internal static int ComputeDigit(PathContentSegment segment, IDictionary <string, object> constraints)
        {
            if (segment.Subsegments.Count > 1)
            {
                // Multi-part segments should appear after literal segments but before parameter segments
                return(2);
            }

            PathSubsegment subsegment = segment.Subsegments[0];

            // Literal segments always go first
            if (subsegment is PathLiteralSubsegment)
            {
                return(1);
            }
            else
            {
                PathParameterSubsegment parameterSegment = subsegment as PathParameterSubsegment;
                Contract.Assert(parameterSegment != null);
                int order = parameterSegment.IsCatchAll ? 5 : 3;

                // If there is a route constraint for the parameter, reduce order by 1
                // Constrained parameters end up with order 2, Constrained catch alls end up with order 4
                if (constraints != null && constraints.ContainsKey(parameterSegment.ParameterName))
                {
                    order--;
                }

                return(order);
            }
        }
Exemplo n.º 3
0
        private static void MatchCatchAll(PathContentSegment contentPathSegment, IEnumerable <string> remainingRequestSegments, HttpRouteValueDictionary defaultValues, HttpRouteValueDictionary matchedValues)
        {
            object obj2;
            string str = string.Join(string.Empty, remainingRequestSegments.ToArray <string>());
            PathParameterSubsegment subsegment = contentPathSegment.Subsegments[0] as PathParameterSubsegment;

            if (str.Length > 0)
            {
                obj2 = str;
            }
            else
            {
                defaultValues.TryGetValue(subsegment.ParameterName, out obj2);
            }
            matchedValues.Add(subsegment.ParameterName, obj2);
        }
Exemplo n.º 4
0
        private static Exception ValidateUriSegment(List <PathSubsegment> pathSubsegments, HashSet <string> usedParameterNames)
        {
            bool segmentContainsCatchAll = false;

            Type previousSegmentType = null;

            foreach (PathSubsegment subsegment in pathSubsegments)
            {
                if (previousSegmentType != null)
                {
                    if (previousSegmentType == subsegment.GetType())
                    {
                        return(Error.Argument("routeTemplate", ErrorResources.Route_CannotHaveConsecutiveParameters));
                    }
                }
                previousSegmentType = subsegment.GetType();

                PathLiteralSubsegment literalSubsegment = subsegment as PathLiteralSubsegment;
                if (literalSubsegment != null)
                {
                    // Nothing to validate for literals - everything is valid
                }
                else
                {
                    PathParameterSubsegment parameterSubsegment = subsegment as PathParameterSubsegment;
                    if (parameterSubsegment != null)
                    {
                        string parameterName = parameterSubsegment.ParameterName;

                        if (parameterSubsegment.IsCatchAll)
                        {
                            segmentContainsCatchAll = true;
                        }

                        // Check for valid characters in the parameter name
                        if (!IsValidParameterName(parameterName))
                        {
                            return(Error.Argument("routeTemplate", ErrorResources.Route_InvalidParameterName, parameterName));
                        }

                        if (usedParameterNames.Contains(parameterName))
                        {
                            return(Error.Argument("routeTemplate", ErrorResources.Route_RepeatedParameter, parameterName));
                        }
                        else
                        {
                            usedParameterNames.Add(parameterName);
                        }
                    }
                    else
                    {
                        Contract.Assert(false, "Invalid path subsegment type");
                    }
                }
            }

            if (segmentContainsCatchAll && (pathSubsegments.Count != 1))
            {
                return(Error.Argument("routeTemplate", ErrorResources.Route_CannotHaveCatchAllInMultiSegment));
            }

            return(null);
        }
Exemplo n.º 5
0
        private static bool MatchContentPathSegment(PathContentSegment routeSegment, string requestPathSegment, HttpRouteValueDictionary defaultValues, HttpRouteValueDictionary matchedValues)
        {
            if (string.IsNullOrEmpty(requestPathSegment))
            {
                if (routeSegment.Subsegments.Count <= 1)
                {
                    object obj2;
                    PathParameterSubsegment subsegment3 = routeSegment.Subsegments[0] as PathParameterSubsegment;
                    if (subsegment3 == null)
                    {
                        return(false);
                    }
                    if (defaultValues.TryGetValue(subsegment3.ParameterName, out obj2))
                    {
                        matchedValues.Add(subsegment3.ParameterName, obj2);
                        return(true);
                    }
                }
                return(false);
            }
            if (routeSegment.Subsegments.Count == 1)
            {
                return(MatchSingleContentPathSegment(routeSegment.Subsegments[0], requestPathSegment, matchedValues));
            }
            int length = requestPathSegment.Length;
            int num2   = routeSegment.Subsegments.Count - 1;
            PathParameterSubsegment subsegment  = null;
            PathLiteralSubsegment   subsegment2 = null;

            while (num2 >= 0)
            {
                int num3 = length;
                PathParameterSubsegment subsegment4 = routeSegment.Subsegments[num2] as PathParameterSubsegment;
                if (subsegment4 != null)
                {
                    subsegment = subsegment4;
                }
                else
                {
                    PathLiteralSubsegment subsegment5 = routeSegment.Subsegments[num2] as PathLiteralSubsegment;
                    if (subsegment5 != null)
                    {
                        subsegment2 = subsegment5;
                        int startIndex = length - 1;
                        if (subsegment != null)
                        {
                            startIndex--;
                        }
                        if (startIndex < 0)
                        {
                            return(false);
                        }
                        int num5 = requestPathSegment.LastIndexOf(subsegment5.Literal, startIndex, StringComparison.OrdinalIgnoreCase);
                        if (num5 == -1)
                        {
                            return(false);
                        }
                        if ((num2 == (routeSegment.Subsegments.Count - 1)) && ((num5 + subsegment5.Literal.Length) != requestPathSegment.Length))
                        {
                            return(false);
                        }
                        num3 = num5;
                    }
                }
                if ((subsegment != null) && (((subsegment2 != null) && (subsegment4 == null)) || (num2 == 0)))
                {
                    int num6;
                    int num7;
                    if (subsegment2 == null)
                    {
                        if (num2 == 0)
                        {
                            num6 = 0;
                        }
                        else
                        {
                            num6 = num3;
                        }
                        num7 = length;
                    }
                    else if ((num2 == 0) && (subsegment4 != null))
                    {
                        num6 = 0;
                        num7 = length;
                    }
                    else
                    {
                        num6 = num3 + subsegment2.Literal.Length;
                        num7 = length - num6;
                    }
                    string str = requestPathSegment.Substring(num6, num7);
                    if (string.IsNullOrEmpty(str))
                    {
                        return(false);
                    }
                    matchedValues.Add(subsegment.ParameterName, str);
                    subsegment  = null;
                    subsegment2 = null;
                }
                length = num3;
                num2--;
            }
            if (length != 0)
            {
                return(routeSegment.Subsegments[0] is PathParameterSubsegment);
            }
            return(true);
        }