Ejemplo n.º 1
0
        bool PathMatches(HttpHandlerAction handler, string uri)
        {
            bool result = false;
            string[] handlerPaths = handler.Path.Split (',');
            int slash = uri.LastIndexOf ('/');
            string origUri = uri;
            if (slash != -1)
                uri = uri.Substring (slash);

            SearchPattern sp = null;
            foreach (string handlerPath in handlerPaths) {
                if (handlerPath == "*")
                    continue; // ignore

                string matchExact = null;
                string endsWith = null;

                if (handlerPath.Length > 0) {
                    if (handlerPath [0] == '*' && (handlerPath.IndexOf ('*', 1) == -1))
                        endsWith = handlerPath.Substring (1);

                    if (handlerPath.IndexOf ('*') == -1)
                        if (handlerPath [0] != '/') {
                            string vpath = HttpRuntime.AppDomainAppVirtualPath;

                            if (vpath == "/")
                                vpath = String.Empty;

                            matchExact = String.Concat (vpath, "/", handlerPath);
                        }
                }

                if (matchExact != null) {
                    result = matchExact.Length == origUri.Length && origUri.EndsWith (matchExact, StringComparison.OrdinalIgnoreCase);
                    if (result)
                        break;
                    continue;
                }
                if (endsWith != null) {
                    result = uri.EndsWith (endsWith, StringComparison.OrdinalIgnoreCase);
                    if (result)
                        break;
                    continue;
                }

                string pattern;
                if (handlerPath.Length > 0 && handlerPath [0] == '/')
                    pattern = handlerPath.Substring (1);
                else
                    pattern = handlerPath;

                if (sp == null)
                    sp = new SearchPattern (pattern, true);
                else
                    sp.SetPattern (pattern, true);

                if (sp.IsMatch (origUri)) {
                    result = true;
                    break;
                }
            }

            return result;
        }
Ejemplo n.º 2
0
		/// <summary>
		/// Returns true if the string matches the pattern which may contain * and ? wildcards.
		/// </summary>
		/// <param name="pattern">Pattern to match.</param>
		/// <param name="fileName">Filename to match.</param>
		/// <param name="caseSensitive">If the match is case sensitive or not.</param>
		/// <returns>True if the patterna and the fileName match, false if not.</returns>
		/// <remarks>
		/// Based on robagar C# port of Jack Handy Codeproject article:
		/// http://www.codeproject.com/string/wildcmp.asp#xx1000279xx
		/// </remarks>
		private bool Match(string pattern, string fileName, bool caseSensitive)
		{
			// if not concerned about case, convert both string and pattern
			// to lower case for comparison
			if (!caseSensitive)
			{
				pattern = pattern.ToLower();
				fileName = fileName.ToLower();
			}            		
            if(string.IsNullOrEmpty(pattern))return false;//如果空字符 返回没有
            if (pattern.CompareTo("*")==0 || pattern.CompareTo("*.*")==0) {
                return true;//快速匹配
            }           
            // if pattern doesn't actually contain any wildcards, use simple equality
            //if (pattern.IndexOfAny(Wildcards) == -1)//不存在匹配符号
            //	return (fileName == pattern);
            bool findP = false;
            foreach (var v1 in Wildcards) {
                foreach (var v2 in pattern) {
                    if (v1.CompareTo(v2) == 0) {
                        findP = true; break;
                    }
                }
            }
            if (!findP) return (fileName == pattern);
			// otherwise do pattern matching
            SearchPattern mSearchPattern =new SearchPattern(pattern);
            return mSearchPattern.IsMatch(fileName);
            //下面逻辑可能有点问题
			int i = 0;
			int j = 0;
			while (i < fileName.Length && j < pattern.Length && pattern[j] != '*')
			{
				if ((pattern[j] != fileName[i]) && (pattern[j] != '?'))
				{
					return false;
				}
				i++;
				j++;
			}

			// if we have reached the end of the pattern without finding a * wildcard,
			// the match must fail if the string is longer or shorter than the pattern
			if (j == pattern.Length)
				return fileName.Length == pattern.Length;

			int cp = 0;
			int mp = 0;
			while (i < fileName.Length)
			{
				if (j < pattern.Length && pattern[j] == '*')
				{
					if ((j++) >= pattern.Length)
					{
						return true;
					}
					mp = j;
					cp = i + 1;
				}
				else if (j < pattern.Length && (pattern[j] == fileName[i] || pattern[j] == '?'))
				{
					j++;
					i++;
				}
				else
				{
					j = mp;
					i = cp++;
				}
			}

			while (j < pattern.Length && pattern[j] == '*')
			{
				j++;
			}

			return j >= pattern.Length;
		}