Exemplo n.º 1
0
		internal static bool SchemeContains (UriSchemes keys, UriSchemes flag)
		{
			return (keys & flag) != 0;
		}
Exemplo n.º 2
0
		private static bool NeedToUnescape (char c, UriSchemes scheme, UriComponents component, UriKind uriKind,
			UriFormat uriFormat, FormatFlags formatFlags)
		{
			if ((formatFlags & FormatFlags.IPv6Host) != 0)
				return false;

			if (uriFormat == UriFormat.Unescaped)
				return true;

			UriSchemes sDecoders = UriSchemes.NetPipe | UriSchemes.NetTcp;

			if (!IriParsing)
				sDecoders |= UriSchemes.Http | UriSchemes.Https;

			if (c == '/' || c == '\\') {
				if (!IriParsing && uriKind == UriKind.Absolute && uriFormat != UriFormat.UriEscaped &&
					uriFormat != UriFormat.SafeUnescaped)
					return true;

				if (SchemeContains (scheme, UriSchemes.File)) {
					return component != UriComponents.Fragment &&
						   (component != UriComponents.Query || !IriParsing);
				}

				return component != UriComponents.Query && component != UriComponents.Fragment &&
					   SchemeContains (scheme, sDecoders);
			}

			if (c == '?') {
				//Avoid creating new query
				if (SupportsQuery (scheme) && component == UriComponents.Path)
					return false;

				if (!IriParsing && uriFormat == ToStringUnescape) {
					if (SupportsQuery (scheme))
						return component == UriComponents.Query || component == UriComponents.Fragment;

					return component == UriComponents.Fragment;
				}

				return false;
			}

			if (c == '#')
				return false;

			if (uriFormat == ToStringUnescape && !IriParsing) {
				if (uriKind == UriKind.Relative)
					return false;

				switch (c) {
				case '$':
				case '&':
				case '+':
				case ',':
				case ';':
				case '=':
				case '@':
					return true;
				}

				if (c < 0x20 || c == 0x7f)
					return true;
			}

			if (uriFormat == UriFormat.SafeUnescaped || uriFormat == ToStringUnescape) {
				switch (c) {
				case '-':
				case '.':
				case '_':
				case '~':
					return true;
				case ' ':
				case '!':
				case '"':
				case '\'':
				case '(':
				case ')':
				case '*':
				case '<':
				case '>':
				case '^':
				case '`':
				case '{':
				case '}':
				case '|':
					return uriKind != UriKind.Relative ||
						(IriParsing && (formatFlags & FormatFlags.HasUriCharactersToNormalize) != 0);
				case ':':
				case '[':
				case ']':
					return uriKind != UriKind.Relative;
				}

				if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
					return true;

				if (c > 0x7f)
					return true;

				return false;
			}

			if (uriFormat == UriFormat.UriEscaped) {
				if (!IriParsing) {
					if (c == '.') {
						if (SchemeContains (scheme, UriSchemes.File))
							return component != UriComponents.Fragment;

						return component != UriComponents.Query && component != UriComponents.Fragment &&
							   SchemeContains (scheme, sDecoders);
					}

					return false;
				}
				
				switch (c) {
				case '-':
				case '.':
				case '_':
				case '~':
					return true;
				}

				if ((formatFlags & FormatFlags.HasUriCharactersToNormalize) != 0) {
					switch (c) {
					case '!':
					case '\'':
					case '(':
					case ')':
					case '*':
					case ':':
					case '[':
					case ']':
						return true;
					}
				}

				if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
					return true;

				return false;
			}

			return false;
		}
Exemplo n.º 3
0
		private static bool NeedToEscape (char c, UriSchemes scheme, UriComponents component, UriKind uriKind,
			UriFormat uriFormat, FormatFlags formatFlags)
		{
			if ((formatFlags & FormatFlags.IPv6Host) != 0)
				return false;

			if (c == '?') {
				if (uriFormat == UriFormat.Unescaped)
					return false;

				if (!SupportsQuery (scheme))
					return component != UriComponents.Fragment;

				return false;
			}

			if (c == '#') {
				//Avoid removing fragment
				if (component == UriComponents.Path || component == UriComponents.Query)
					return false;

				if (component == UriComponents.Fragment &&
					(uriFormat == ToStringUnescape || uriFormat == UriFormat.SafeUnescaped) &&
					(formatFlags & FormatFlags.HasFragmentPercentage) != 0)
					return true;

				return false;
			}

			if (uriFormat == UriFormat.SafeUnescaped || uriFormat == ToStringUnescape) {
				if (c == '%')
					return uriKind != UriKind.Relative;
			}

			if (uriFormat == UriFormat.SafeUnescaped) {
				if (c < 0x20 || c == 0x7F)
					return true;
			}

			if (uriFormat == UriFormat.UriEscaped) {
				if (c < 0x20 || c >= 0x7F)
					return component != UriComponents.Host;

				switch (c) {
				case ' ':
				case '"':
				case '%':
				case '<':
				case '>':
				case '^':
				case '`':
				case '{':
				case '}':
				case '|':
					return true;
				case '[':
				case ']':
					return !IriParsing;
				case '\\':
					return component != UriComponents.Path ||
						   SchemeContains (scheme,
							   UriSchemes.Gopher | UriSchemes.Ldap | UriSchemes.Mailto | UriSchemes.Nntp |
							   UriSchemes.Telnet | UriSchemes.News | UriSchemes.Custom);
				}
			}

			return false;
		}
Exemplo n.º 4
0
		private static string FormatString (string str, UriSchemes scheme, UriKind uriKind,
			UriComponents component, UriFormat uriFormat, FormatFlags formatFlags)
		{
			var s = new StringBuilder ();
			int len = str.Length;
			for (int i = 0; i < len; i++) {
				char c = str [i];
				if (c == '%') {
					int iStart = i;
					char surrogate;
					bool invalidUnescape;
					char x = Uri.HexUnescapeMultiByte (str, ref i, out surrogate, out invalidUnescape);


					if (invalidUnescape
					) {
						s.Append (c);
						i = iStart;
						continue;
					}

					string cStr = str.Substring (iStart, i-iStart);
					s.Append (FormatChar (x, surrogate, cStr, scheme, uriKind, component, uriFormat, formatFlags));

					i--;
				} else
					s.Append (FormatChar (c, char.MinValue, "" + c, scheme, uriKind, component, uriFormat, formatFlags));
			}
			
			return s.ToString ();
		}
Exemplo n.º 5
0
		private static string FormatChar (char c, char surrogate, string cStr, UriSchemes scheme, UriKind uriKind,
			UriComponents component, UriFormat uriFormat, FormatFlags formatFlags)
		{
			var isEscaped = cStr.Length != 1;

			var userEscaped = (formatFlags & FormatFlags.UserEscaped) != 0;
			if (!isEscaped && !userEscaped && NeedToEscape (c, scheme, component, uriKind, uriFormat, formatFlags))
				return HexEscapeMultiByte (c);

			if (isEscaped && (
				(userEscaped && c < 0xFF) ||
				!NeedToUnescape (c, scheme, component, uriKind, uriFormat, formatFlags))) {
				if (IriParsing &&
					(c == '<' || c == '>' || c == '^' || c == '{' || c == '|' || c ==  '}' || c > 0x7F) &&
					(formatFlags & FormatFlags.HasUriCharactersToNormalize) != 0)
					return cStr.ToUpperInvariant (); //Upper case escape

				return cStr; //Keep original case
			}

			if ((formatFlags & FormatFlags.NoSlashReplace) == 0 &&
				c == '\\' && component == UriComponents.Path) {
				if (!IriParsing && uriFormat != UriFormat.UriEscaped &&
					SchemeContains (scheme, UriSchemes.Http | UriSchemes.Https))
					return "/";

				if (SchemeContains (scheme, UriSchemes.Http | UriSchemes.Https | UriSchemes.Ftp | UriSchemes.CustomWithHost))
					return (isEscaped && uriFormat != UriFormat.UriEscaped) ? "\\" : "/";

				if (SchemeContains (scheme, UriSchemes.NetPipe | UriSchemes.NetTcp | UriSchemes.File))
					return "/";

				if (SchemeContains (scheme, UriSchemes.Custom) &&
					(formatFlags & FormatFlags.HasWindowsPath) == 0)
					return "/";
			}

			var ret = c.ToString (CultureInfo.InvariantCulture);
			if (surrogate != char.MinValue)
				ret += surrogate.ToString (CultureInfo.InvariantCulture);

			return ret;
		}
Exemplo n.º 6
0
        private static bool NeedToUnescape(char c, UriSchemes scheme, UriComponents component, UriKind uriKind,
                                           UriFormat uriFormat, FormatFlags formatFlags)
        {
            if ((formatFlags & FormatFlags.IPv6Host) != 0)
            {
                return(false);
            }

            if (uriFormat == UriFormat.Unescaped)
            {
                return(true);
            }

            UriSchemes sDecoders = UriSchemes.NetPipe | UriSchemes.NetTcp;

            if (!IriParsing)
            {
                sDecoders |= UriSchemes.Http | UriSchemes.Https;
            }

            if (c == '/' || c == '\\')
            {
                if (!IriParsing && uriKind == UriKind.Absolute && uriFormat != UriFormat.UriEscaped &&
                    uriFormat != UriFormat.SafeUnescaped)
                {
                    return(true);
                }

                if (SchemeContains(scheme, UriSchemes.File))
                {
                    return(component != UriComponents.Fragment &&
                           (component != UriComponents.Query || !IriParsing));
                }

                return(component != UriComponents.Query && component != UriComponents.Fragment &&
                       SchemeContains(scheme, sDecoders));
            }

            if (c == '?')
            {
                //Avoid creating new query
                if (SupportsQuery(scheme) && component == UriComponents.Path)
                {
                    return(false);
                }

                if (!IriParsing && uriFormat == ToStringUnescape)
                {
                    if (SupportsQuery(scheme))
                    {
                        return(component == UriComponents.Query || component == UriComponents.Fragment);
                    }

                    return(component == UriComponents.Fragment);
                }

                return(false);
            }

            if (c == '#')
            {
                return(false);
            }

            if (uriFormat == ToStringUnescape && !IriParsing)
            {
                if (uriKind == UriKind.Relative)
                {
                    return(false);
                }

                switch (c)
                {
                case '$':
                case '&':
                case '+':
                case ',':
                case ';':
                case '=':
                case '@':
                    return(true);
                }

                if (c < 0x20 || c == 0x7f)
                {
                    return(true);
                }
            }

            if (uriFormat == UriFormat.SafeUnescaped || uriFormat == ToStringUnescape)
            {
                switch (c)
                {
                case '-':
                case '.':
                case '_':
                case '~':
                    return(true);

                case ' ':
                case '!':
                case '"':
                case '\'':
                case '(':
                case ')':
                case '*':
                case '<':
                case '>':
                case '^':
                case '`':
                case '{':
                case '}':
                case '|':
                    return(uriKind != UriKind.Relative ||
                           (IriParsing && (formatFlags & FormatFlags.HasUriCharactersToNormalize) != 0));

                case ':':
                case '[':
                case ']':
                    return(uriKind != UriKind.Relative);
                }

                if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
                {
                    return(true);
                }

                if (c > 0x7f)
                {
                    return(true);
                }

                return(false);
            }

            if (uriFormat == UriFormat.UriEscaped)
            {
                if (!IriParsing)
                {
                    if (c == '.')
                    {
                        if (SchemeContains(scheme, UriSchemes.File))
                        {
                            return(component != UriComponents.Fragment);
                        }

                        return(component != UriComponents.Query && component != UriComponents.Fragment &&
                               SchemeContains(scheme, sDecoders));
                    }

                    return(false);
                }

                switch (c)
                {
                case '-':
                case '.':
                case '_':
                case '~':
                    return(true);
                }

                if ((formatFlags & FormatFlags.HasUriCharactersToNormalize) != 0)
                {
                    switch (c)
                    {
                    case '!':
                    case '\'':
                    case '(':
                    case ')':
                    case '*':
                    case ':':
                    case '[':
                    case ']':
                        return(true);
                    }
                }

                if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
                {
                    return(true);
                }

                return(false);
            }

            return(false);
        }
Exemplo n.º 7
0
		internal static bool SupportsQuery (UriSchemes scheme)
		{
			if (SchemeContains (scheme, UriSchemes.File))
				return IriParsing;

			return !SchemeContains (scheme, UriSchemes.Ftp | UriSchemes.Gopher | UriSchemes.Nntp | UriSchemes.Telnet | UriSchemes.News);
		}
Exemplo n.º 8
0
 internal static bool SchemeContains(UriSchemes keys, UriSchemes flag)
 {
     return((keys & flag) != 0);
 }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
 public void WriteRecordRoute(UriSchemes scheme, ByteArrayPart host, int port)
 {
     Write(C.RecordRoute, C.HCOLON, C.SP, C.LAQUOT, scheme.ToByteArrayPart(), C.HCOLON, host, C.HCOLON);
     Write(port);
     Write(C.SEMI, C.lr, C.RAQUOT, C.CRLF);
 }
Exemplo n.º 11
0
        private static bool NeedToEscape(char c, UriSchemes scheme, UriComponents component, UriKind uriKind,
                                         UriFormat uriFormat, FormatFlags formatFlags)
        {
            if ((formatFlags & FormatFlags.IPv6Host) != 0)
            {
                return(false);
            }

            if (c == '?')
            {
                if (uriFormat == UriFormat.Unescaped)
                {
                    return(false);
                }

                if (!SupportsQuery(scheme))
                {
                    return(component != UriComponents.Fragment);
                }

                return(false);
            }

            if (c == '#')
            {
                //Avoid removing fragment
                if (component == UriComponents.Path || component == UriComponents.Query)
                {
                    return(false);
                }

                if (component == UriComponents.Fragment &&
                    (uriFormat == ToStringUnescape || uriFormat == UriFormat.SafeUnescaped) &&
                    (formatFlags & FormatFlags.HasFragmentPercentage) != 0)
                {
                    return(true);
                }

                return(false);
            }

            if (uriFormat == UriFormat.SafeUnescaped || uriFormat == ToStringUnescape)
            {
                if (c == '%')
                {
                    return(uriKind != UriKind.Relative);
                }
            }

            if (uriFormat == UriFormat.SafeUnescaped)
            {
                if (c < 0x20 || c == 0x7F)
                {
                    return(true);
                }
            }

            if (uriFormat == UriFormat.UriEscaped)
            {
                if (c < 0x20 || c >= 0x7F)
                {
                    return(component != UriComponents.Host);
                }

                switch (c)
                {
                case ' ':
                case '"':
                case '%':
                case '<':
                case '>':
                case '^':
                case '`':
                case '{':
                case '}':
                case '|':
                    return(true);

                case '[':
                case ']':
                    return(true);

                case '\\':
                    return(component != UriComponents.Path ||
                           SchemeContains(scheme,
                                          UriSchemes.Gopher | UriSchemes.Ldap | UriSchemes.Mailto | UriSchemes.Nntp |
                                          UriSchemes.Telnet | UriSchemes.News | UriSchemes.Custom));
                }
            }

            return(false);
        }
Exemplo n.º 12
0
		public void WriteRecordRoute(UriSchemes scheme, ByteArrayPart host, int port)
		{
			Write(C.RecordRoute, C.HCOLON, C.SP, C.LAQUOT, scheme.ToByteArrayPart(), C.HCOLON, host, C.HCOLON);
			Write(port);
			Write(C.SEMI, C.lr, C.RAQUOT, C.CRLF);
		}
Exemplo n.º 13
0
		public void WriteRequestLine(Methods method, UriSchemes scheme, ByteArrayPart user, ByteArrayPart domain)
		{
			Method = method;

			Write(method.ToByteArrayPart(), C.SP);
			Write(scheme.ToByteArrayPart(), C.HCOLON, user, C.At, domain);
			Write(C.SP, C.SIP_2_0, C.CRLF);
		}
Exemplo n.º 14
0
        private static string FormatChar(char c, char surrogate, string cStr, UriSchemes scheme, UriKind uriKind,
                                         UriComponents component, UriFormat uriFormat, FormatFlags formatFlags)
        {
            var isEscaped = cStr.Length != 1;

            var userEscaped = (formatFlags & FormatFlags.UserEscaped) != 0;

            if (!isEscaped && !userEscaped && NeedToEscape(c, scheme, component, uriKind, uriFormat, formatFlags))
            {
                return(HexEscapeMultiByte(c));
            }

            if (isEscaped && (
#if NET_4_0
                    (userEscaped && c < 0xFF) ||
#endif
                    !NeedToUnescape(c, scheme, component, uriKind, uriFormat, formatFlags)))
            {
                if (IriParsing &&
                    (c == '<' || c == '>' || c == '^' || c == '{' || c == '|' || c == '}' || c > 0x7F) &&
                    (formatFlags & FormatFlags.HasUriCharactersToNormalize) != 0)
                {
                    return(cStr.ToUpperInvariant()); //Upper case escape
                }
                return(cStr);                        //Keep original case
            }

            if ((formatFlags & FormatFlags.NoSlashReplace) == 0 &&
                c == '\\' && component == UriComponents.Path)
            {
                if (!IriParsing && uriFormat != UriFormat.UriEscaped &&
                    SchemeContains(scheme, UriSchemes.Http | UriSchemes.Https))
                {
                    return("/");
                }

                if (SchemeContains(scheme, UriSchemes.Http | UriSchemes.Https | UriSchemes.Ftp | UriSchemes.CustomWithHost))
                {
                    return((isEscaped && uriFormat != UriFormat.UriEscaped) ? "\\" : "/");
                }

                if (SchemeContains(scheme, UriSchemes.NetPipe | UriSchemes.NetTcp | UriSchemes.File))
                {
                    return("/");
                }

                if (SchemeContains(scheme, UriSchemes.Custom) &&
                    (formatFlags & FormatFlags.HasWindowsPath) == 0)
                {
                    return("/");
                }
            }

            var ret = c.ToString(CultureInfo.InvariantCulture);
            if (surrogate != char.MinValue)
            {
                ret += surrogate.ToString(CultureInfo.InvariantCulture);
            }

            return(ret);
        }