UnescapeDataString() public static method

public static UnescapeDataString ( string stringToUnescape ) : string
stringToUnescape string
return string
Example #1
0
        private string GetUnescapedAbsolutePath(Uri uri)
        {
            string absolutePath = Uri.UnescapeDataString(uri.AbsolutePath);

            return(absolutePath.EndsWith("/", StringComparison.Ordinal) ? absolutePath.Substring(0, absolutePath.Length - 1) : absolutePath);
        }
Example #2
0
        public UriTemplateMatch Match(Uri baseAddress, Uri candidate)
        {
            CheckBaseAddress(baseAddress);
            if (candidate == null)
            {
                throw new ArgumentNullException("candidate");
            }

            var us = baseAddress.LocalPath;

            if (us [us.Length - 1] != '/')
            {
                baseAddress = new Uri(baseAddress.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.Unescaped) + '/' + baseAddress.Query, baseAddress.IsAbsoluteUri ? UriKind.Absolute : UriKind.RelativeOrAbsolute);
            }
            if (IgnoreTrailingSlash)
            {
                us = candidate.LocalPath;
                if (us.Length > 0 && us [us.Length - 1] != '/')
                {
                    candidate = new Uri(candidate.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.Unescaped) + '/' + candidate.Query, candidate.IsAbsoluteUri ? UriKind.Absolute : UriKind.RelativeOrAbsolute);
                }
            }

            int i = 0, c = 0;
            UriTemplateMatch m = new UriTemplateMatch();

            m.BaseUri    = baseAddress;
            m.Template   = this;
            m.RequestUri = candidate;
            var vc             = m.BoundVariables;

            string cp = Uri.UnescapeDataString(baseAddress.MakeRelativeUri(new Uri(baseAddress, candidate.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped))).ToString());

            if (IgnoreTrailingSlash && cp [cp.Length - 1] == '/')
            {
                cp = cp.Substring(0, cp.Length - 1);
            }

            int tEndCp = cp.IndexOf('?');

            if (tEndCp >= 0)
            {
                cp = cp.Substring(0, tEndCp);
            }

            if (template.Length > 0 && template [0] == '/')
            {
                i++;
            }
            if (cp.Length > 0 && cp [0] == '/')
            {
                c++;
            }

            foreach (string name in path)
            {
                if (name == wild_path_name)
                {
                    vc [name] = cp.Substring(c);                      // all remaining paths.
                    continue;
                }
                int n = StringIndexOf(template, '{' + name + '}', i);
                if (String.CompareOrdinal(cp, c, template, i, n - i) != 0)
                {
                    return(null);                    // doesn't match before current template part.
                }
                c += n - i;
                i  = n + 2 + name.Length;
                int ce = cp.IndexOf('/', c);
                if (ce < 0)
                {
                    ce = cp.Length;
                }
                string value = cp.Substring(c, ce - c);
                if (value.Length == 0)
                {
                    return(null);                    // empty => mismatch
                }
                vc [name] = value;
                m.RelativePathSegments.Add(value);
                c += value.Length;
            }
            int  tEnd    = template.IndexOf('?');
            int  wildIdx = template.IndexOf('*');
            bool wild    = wildIdx >= 0;

            if (tEnd < 0)
            {
                tEnd = template.Length;
            }
            if (wild)
            {
                tEnd = wildIdx - 1;
            }
            if (!wild && (cp.Length - c) != (tEnd - i) ||
                String.CompareOrdinal(cp, c, template, i, tEnd - i) != 0)
            {
                return(null);                // suffix doesn't match
            }
            if (wild)
            {
                c += tEnd - i;
                foreach (var pe in cp.Substring(c).Split(slashSep, StringSplitOptions.RemoveEmptyEntries))
                {
                    m.WildcardPathSegments.Add(pe);
                }
            }
            if (candidate.Query.Length == 0)
            {
                return(m);
            }


            string [] parameters = Uri.UnescapeDataString(candidate.Query.Substring(1)).Split('&');                // chop first '?'
            foreach (string parameter in parameters)
            {
                string [] pair = parameter.Split('=');
                m.QueryParameters.Add(pair [0], pair [1]);
                if (!query_params.ContainsKey(pair [0]))
                {
                    continue;
                }
                string templateName = query_params [pair [0]];
                vc.Add(templateName, pair [1]);
            }

            return(m);
        }
 public void Open(Uri uri)
 {
     this.Uri = uri;
     var decodedPath = Uri.UnescapeDataString(uri.AbsolutePath);
     this.path = Path.GetFullPath(decodedPath);
     fileName = Path.GetFileName(path);
     DisplayName = fileName;
 }
Example #4
0
        public static string GetBaseDirectory()
        {
            var uri = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase ?? string.Empty);

            return(Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path)));
        }