public ParametersBase(string source)
 {
   if (String.IsNullOrEmpty(source)) return;
   _Source = source;
   int typeTail = source.IndexOf(";");
   if (typeTail == -1)
   {
     _Type = source;
     return;
   }
   _Type = source.Substring(0, typeTail);
   string p = source.Substring(typeTail + 1, source.Length - typeTail - 1);
   _Parameters = new ParametersCollection();
   Regex myReg = new Regex(@"(?<key>.+?)=((""(?<value>.+?)"")|((?<value>[^\;]+)))[\;]{0,1}", RegexOptions.Singleline);
   MatchCollection mc = myReg.Matches(p);
   foreach (Match m in mc)
   {
     if (!_Parameters.ContainsKey(m.Groups["key"].Value))
     {
       _Parameters.Add(m.Groups["key"].Value.Trim(), m.Groups["value"].Value);
     }
   }
 }
        public ParametersCollection Match(string virtualPath, ParametersCollection defaultValues)
        {
            IList <string> source = RouteParser.SplitUrlToPathSegmentStrings(virtualPath);

            if (defaultValues == null)
            {
                defaultValues = new ParametersCollection();
            }
            ParametersCollection matchedValues = new ParametersCollection();
            bool flag  = false;
            bool flag2 = false;

            for (int i = 0; i < this.PathSegments.Count; i++)
            {
                PathSegment segment = this.PathSegments[i];
                if (source.Count <= i)
                {
                    flag = true;
                }
                string a = flag ? null : source[i];
                if (segment is SeparatorPathSegment)
                {
                    if (!flag && !string.Equals(a, "/", StringComparison.Ordinal))
                    {
                        return(null);
                    }
                }
                else
                {
                    ContentPathSegment contentPathSegment = segment as ContentPathSegment;
                    if (contentPathSegment != null)
                    {
                        if (contentPathSegment.IsCatchAll)
                        {
                            this.MatchCatchAll(contentPathSegment, source.Skip <string>(i), defaultValues, matchedValues);
                            flag2 = true;
                        }
                        else if (!this.MatchContentPathSegment(contentPathSegment, a, defaultValues, matchedValues))
                        {
                            return(null);
                        }
                    }
                }
            }
            if (!flag2 && (this.PathSegments.Count < source.Count))
            {
                for (int j = this.PathSegments.Count; j < source.Count; j++)
                {
                    if (!RouteParser.IsSeparator(source[j]))
                    {
                        return(null);
                    }
                }
            }
            if (defaultValues != null)
            {
                foreach (var pair in defaultValues)
                {
                    if (!matchedValues.ContainsKey(pair.Key))
                    {
                        matchedValues.Add(pair.Key, pair.Value);
                    }
                }
            }
            return(matchedValues);
        }
Exemple #3
0
        private static ResponseStatus OnResolveRoute(IUrlRequest request, bool throwException, out IRoutingContext context)
        {
            Guard.ArgumentNotNull(request, "request");
            Guard.ArgumentValue((!ValidateUrl(request.RequestUrl, false)), "request", INVALID_REQUEST_URL);

            // default value
            context = null;

            // we check the url
            if (!ValidateUrl(request.RequestUrl, throwException))
            {
                return(ResponseStatus.UrlInvalid);
            }

            // we get the route data first
            RouteData routeData = RouteTable.Routes.GetRouteData(request);

            if (routeData == null)
            {
                if (throwException)
                {
                    throw new InvalidOperationException(NO_ROUTE_MATCHES);
                }
                return(ResponseStatus.UrlNotFound);
            }

            // we get the route handler
            IRouteHandler routeHandler = routeData.RouteHandler;

            if (routeHandler == null)
            {
                if (throwException)
                {
                    throw new InvalidOperationException(NO_ROUTE_HANDLER);
                }
                return(ResponseStatus.HandlerNotFound);
            }

            // we merge the dictionaries (into the request parameters)
            var _requestParameters = new ParametersCollection();

            // we add the data-tokens specified with route, n_ote these can be overriden
            if (routeData.DataTokens != null && routeData.DataTokens.Count > 0)
            {
                // we add the values, from the routes data, if it exists then we update the value
                foreach (var _parameter in routeData.DataTokens)
                {
                    _requestParameters.Add(_parameter.Key, _parameter.Value);
                }
            }

            // we add the values found in the url merged with the default values, n_ote these can be overriden
            if (routeData.Values != null && routeData.Values.Count > 0)
            {
                // we add the values, from the routes data, if it exists then we update the value
                foreach (var _parameter in routeData.Values)
                {
                    if (_requestParameters.ContainsKey(_parameter.Key))
                    {
                        _requestParameters[_parameter.Key] = _parameter.Value;
                    }
                    else
                    {
                        _requestParameters.Add(_parameter.Key, _parameter.Value);
                    }
                }
            }

            // and our passed in parameters will override any existing entry
            if (request.RequestParameters != null && request.RequestParameters.Count > 0)
            {
                foreach (var _parameter in request.RequestParameters)
                {
                    if (_requestParameters.ContainsKey(_parameter.Key))
                    {
                        _requestParameters[_parameter.Key] = _parameter.Value;
                    }
                    else
                    {
                        _requestParameters.Add(_parameter.Key, _parameter.Value);
                    }
                }
            }

            // we setup the request cotext and get the response
            context = new RoutingContext(request, routeData, _requestParameters);
            return(ResponseStatus.Success);
        }
        public BoundUrl Bind(ParametersCollection currentValues, ParametersCollection values,
                             ParametersCollection defaultValues, ParametersCollection constraints)
        {
            if (currentValues == null)
            {
                currentValues = new ParametersCollection();
            }
            if (values == null)
            {
                values = new ParametersCollection();
            }
            if (defaultValues == null)
            {
                defaultValues = new ParametersCollection();
            }
            ParametersCollection acceptedValues  = new ParametersCollection();
            List <string>        unusedNewValues = new List <string>(values.Keys);

            ForEachParameter(this.PathSegments, delegate(ParameterSubsegment parameterSubsegment)
            {
                object obj2;
                object obj3;
                string parameterName = parameterSubsegment.ParameterName;
                bool flag            = values.TryGetValue(parameterName, out obj2);
                if (flag)
                {
                    unusedNewValues.Remove(parameterName);
                }
                bool flag22 = currentValues.TryGetValue(parameterName, out obj3);
                if ((flag && flag22) && !RoutePartsEqual(obj3, obj2))
                {
                    return(false);
                }
                if (flag)
                {
                    if (IsRoutePartNonEmpty(obj2))
                    {
                        acceptedValues.Add(parameterName, obj2);
                    }
                }
                else if (flag22)
                {
                    acceptedValues.Add(parameterName, obj3);
                }
                return(true);
            });

            foreach (var pair in values)
            {
                if (IsRoutePartNonEmpty(pair.Value) && !acceptedValues.ContainsKey(pair.Key))
                {
                    acceptedValues.Add(pair.Key, pair.Value);
                }
            }

            foreach (var pair2 in currentValues)
            {
                string str = pair2.Key;
                if (!acceptedValues.ContainsKey(str) && (GetParameterSubsegment(this.PathSegments, str) == null))
                {
                    acceptedValues.Add(str, pair2.Value);
                }
            }

            ForEachParameter(this.PathSegments, delegate(ParameterSubsegment parameterSubsegment)
            {
                object obj2;
                if (!acceptedValues.ContainsKey(parameterSubsegment.ParameterName) &&
                    !IsParameterRequired(parameterSubsegment, defaultValues, out obj2))
                {
                    acceptedValues.Add(parameterSubsegment.ParameterName, obj2);
                }
                return(true);
            });

            if (!ForEachParameter(this.PathSegments, delegate(ParameterSubsegment parameterSubsegment)
            {
                object obj2;
                if (IsParameterRequired(parameterSubsegment, defaultValues, out obj2) &&
                    !acceptedValues.ContainsKey(parameterSubsegment.ParameterName))
                {
                    return(false);
                }
                return(true);
            }))
            {
                return(null);
            }

            ParametersCollection otherDefaultValues = new ParametersCollection(defaultValues);

            ForEachParameter(this.PathSegments, delegate(ParameterSubsegment parameterSubsegment)
            {
                otherDefaultValues.Remove(parameterSubsegment.ParameterName);
                return(true);
            });

            foreach (var pair3 in otherDefaultValues)
            {
                object obj2;
                if (values.TryGetValue(pair3.Key, out obj2))
                {
                    unusedNewValues.Remove(pair3.Key);
                    if (!RoutePartsEqual(obj2, pair3.Value))
                    {
                        return(null);
                    }
                }
            }

            StringBuilder builder  = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            bool          flag2    = false;

            for (int i = 0; i < this.PathSegments.Count; i++)
            {
                PathSegment segment = this.PathSegments[i];
                if (segment is SeparatorPathSegment)
                {
                    if (flag2 && (builder2.Length > 0))
                    {
                        builder.Append(builder2.ToString());
                        builder2.Length = 0;
                    }
                    flag2 = false;
                    builder2.Append("/");
                }
                else
                {
                    ContentPathSegment segment2 = segment as ContentPathSegment;
                    if (segment2 != null)
                    {
                        bool flag3 = false;
                        foreach (PathSubsegment subsegment2 in segment2.Subsegments)
                        {
                            LiteralSubsegment subsegment3 = subsegment2 as LiteralSubsegment;
                            if (subsegment3 != null)
                            {
                                flag2 = true;
                                builder2.Append(Uri.EscapeUriString(subsegment3.Literal));
                            }
                            else
                            {
                                ParameterSubsegment subsegment4 = subsegment2 as ParameterSubsegment;
                                if (subsegment4 != null)
                                {
                                    object obj3;
                                    object obj4;
                                    if (flag2 && (builder2.Length > 0))
                                    {
                                        builder.Append(builder2.ToString());
                                        builder2.Length = 0;
                                        flag3           = true;
                                    }
                                    flag2 = false;
                                    if (acceptedValues.TryGetValue(subsegment4.ParameterName, out obj3))
                                    {
                                        unusedNewValues.Remove(subsegment4.ParameterName);
                                    }
                                    defaultValues.TryGetValue(subsegment4.ParameterName, out obj4);
                                    if (RoutePartsEqual(obj3, obj4))
                                    {
                                        builder2.Append(Uri.EscapeUriString(Convert.ToString(obj3, CultureInfo.InvariantCulture)));
                                        continue;
                                    }
                                    if (builder2.Length > 0)
                                    {
                                        builder.Append(builder2.ToString());
                                        builder2.Length = 0;
                                    }
                                    builder.Append(Uri.EscapeUriString(Convert.ToString(obj3, CultureInfo.InvariantCulture)));
                                    flag3 = true;
                                }
                            }
                        }
                        if (flag3 && (builder2.Length > 0))
                        {
                            builder.Append(builder2.ToString());
                            builder2.Length = 0;
                        }
                    }
                }
            }

            if (flag2 && (builder2.Length > 0))
            {
                builder.Append(builder2.ToString());
            }
            if (constraints != null)
            {
                foreach (var pair4 in constraints)
                {
                    unusedNewValues.Remove(pair4.Key);
                }
            }

            if (unusedNewValues.Count > 0)
            {
                bool flag5 = true;
                foreach (string str2 in unusedNewValues)
                {
                    object obj5;
                    if (acceptedValues.TryGetValue(str2, out obj5))
                    {
                        builder.Append(flag5 ? '?'
         : '&');
                        flag5 = false;
                        builder.Append(Uri.EscapeDataString(str2));
                        builder.Append('=');
                        builder.Append(Uri.EscapeDataString(Convert.ToString(obj5, CultureInfo.InvariantCulture)));
                    }
                }
            }

            return(new BoundUrl {
                Url = builder.ToString(), Values = acceptedValues
            });
        }