예제 #1
0
        private static string Format(string str, string schemeName, UriKind uriKind,
                                     UriComponents component, UriFormat uriFormat, FormatFlags formatFlags)
        {
            if (string.IsNullOrEmpty(str))
            {
                return("");
            }

            if (UriHelper.HasCharactersToNormalize(str))
            {
                formatFlags |= UriHelper.FormatFlags.HasComponentCharactersToNormalize | FormatFlags.HasUriCharactersToNormalize;
            }

            if (component == UriComponents.Fragment && UriHelper.HasPercentage(str))
            {
                formatFlags |= UriHelper.FormatFlags.HasFragmentPercentage;
            }

            if (component == UriComponents.Host &&
                str.Length > 1 && str [0] == '[' && str [str.Length - 1] == ']')
            {
                formatFlags |= UriHelper.FormatFlags.IPv6Host;
            }

            if (component == UriComponents.Path &&
                str.Length >= 2 && str [1] != ':' &&
                ('a' <= str [0] && str [0] <= 'z') || ('A' <= str [0] && str [0] <= 'Z'))
            {
                formatFlags |= UriHelper.FormatFlags.HasWindowsPath;
            }

            UriSchemes scheme = GetScheme(schemeName);

            if (scheme == UriSchemes.Custom && (formatFlags & FormatFlags.HasHost) != 0)
            {
                scheme = UriSchemes.CustomWithHost;
            }

            var reduceAfter = UriSchemes.Http | UriSchemes.Https | UriSchemes.File | UriSchemes.NetPipe | UriSchemes.NetTcp;

            if (IriParsing)
            {
                reduceAfter |= UriSchemes.Ftp;
            }
            else if (component == UriComponents.Path &&
                     (formatFlags & FormatFlags.NoSlashReplace) == 0)
            {
                if (scheme == UriSchemes.Ftp)
                {
                    str = Reduce(str.Replace('\\', '/'), !IriParsing);
                }
                if (scheme == UriSchemes.CustomWithHost)
                {
                    str = Reduce(str.Replace('\\', '/'), false);
                }
            }

            str = FormatString(str, scheme, uriKind, component, uriFormat, formatFlags);

            if (component == UriComponents.Path &&
                (formatFlags & FormatFlags.NoReduce) == 0)
            {
                if (SchemeContains(scheme, reduceAfter))
                {
                    str = Reduce(str, !IriParsing);
                }
                if (IriParsing && scheme == UriSchemes.CustomWithHost)
                {
                    str = Reduce(str, false);
                }
            }

            return(str);
        }