public static bool TryParseComponents (string uri, UriKind kind, out UriElements elements, out string error)
		{
			uri = uri.Trim ();

			ParserState state = new ParserState (uri, kind);
			elements = state.elements;
			error = null;

			if (uri.Length == 0 && (kind == UriKind.Relative || kind == UriKind.RelativeOrAbsolute)){
				state.elements.isAbsoluteUri = false;
				return true;
			}
			
			if (uri.Length <= 1 && kind == UriKind.Absolute) {
				error = "Absolute URI is too short";
				return false;
			}

			bool ok = ParseFilePath (state) &&
				ParseScheme (state);

			var scheme = state.elements.scheme;
			UriParser parser = null;
			if (!string.IsNullOrEmpty (scheme)) {
				parser = UriParser.GetParser (scheme);
				if (parser != null && !(parser is DefaultUriParser))
					return true;
			}

			ok = ok &&
				ParseAuthority (state) &&
				ParsePath (state) &&
				ParseQuery (state) &&
				ParseFragment (state);

			if (string.IsNullOrEmpty (state.elements.host) &&
				(scheme == Uri.UriSchemeHttp || scheme == Uri.UriSchemeGopher || scheme == Uri.UriSchemeNntp ||
				scheme == Uri.UriSchemeHttps || scheme == Uri.UriSchemeFtp))
				state.error = "Invalid URI: The Authority/Host could not be parsed.";

			if (!string.IsNullOrEmpty (state.elements.host) &&
				Uri.CheckHostName (state.elements.host) == UriHostNameType.Unknown)
				state.error = "Invalid URI: The hostname could not be parsed.";

			if (!string.IsNullOrEmpty (state.error)) {
				elements = null;
				error = state.error;
				return false;
			}
			
			return true;
		}
Beispiel #2
0
        public static bool TryParseComponents(string uri, UriKind kind, out UriElements elements, out string error)
        {
            uri = uri.Trim();

            ParserState state = new ParserState(uri, kind);

            elements = state.elements;
            error    = null;

            if (uri.Length == 0 && (kind == UriKind.Relative || kind == UriKind.RelativeOrAbsolute))
            {
                state.elements.isAbsoluteUri = false;
                return(true);
            }

            if (uri.Length <= 1 && kind == UriKind.Absolute)
            {
                error = "Absolute URI is too short";
                return(false);
            }

            bool ok = ParseFilePath(state) &&
                      ParseScheme(state);

            var       scheme = state.elements.scheme;
            UriParser parser = null;

            if (!string.IsNullOrEmpty(scheme))
            {
                parser = UriParser.GetParser(scheme);
                if (parser != null && !(parser is DefaultUriParser))
                {
                    return(true);
                }
            }

            ok = ok &&
                 ParseAuthority(state) &&
                 ParsePath(state) &&
                 ParseQuery(state) &&
                 ParseFragment(state);

            if (string.IsNullOrEmpty(state.elements.host) &&
                (scheme == Uri.UriSchemeHttp || scheme == Uri.UriSchemeGopher || scheme == Uri.UriSchemeNntp ||
                 scheme == Uri.UriSchemeHttps || scheme == Uri.UriSchemeFtp))
            {
                state.error = "Invalid URI: The Authority/Host could not be parsed.";
            }

            if (!string.IsNullOrEmpty(state.elements.host) &&
                Uri.CheckHostName(state.elements.host) == UriHostNameType.Unknown)
            {
                state.error = "Invalid URI: The hostname could not be parsed.";
            }

            if (!string.IsNullOrEmpty(state.error))
            {
                elements = null;
                error    = state.error;
                return(false);
            }

            return(true);
        }
Beispiel #3
0
 public ParserState(string uri, UriKind kind)
 {
     remaining = uri;
     this.kind = kind;
     elements  = new UriElements();
 }
Beispiel #4
0
        internal string GetComponentsHelper(Uri uri, UriComponents components, UriFormat format)
        {
            UriElements elements = UriParseComponents.ParseComponents(uri.OriginalString.Trim(), UriKind.Absolute);

            string scheme = scheme_name;
            int    dp     = default_port;

            if ((scheme == null) || (scheme == "*"))
            {
                scheme = elements.scheme;
                dp     = Uri.GetDefaultPort(scheme);
            }
            else if (String.Compare(scheme, elements.scheme, true) != 0)
            {
                throw new SystemException("URI Parser: scheme mismatch: " + scheme + " vs. " + elements.scheme);
            }

            var formatFlags = UriHelper.FormatFlags.None;

            if (UriHelper.HasCharactersToNormalize(uri.OriginalString))
            {
                formatFlags |= UriHelper.FormatFlags.HasUriCharactersToNormalize;
            }

            if (uri.UserEscaped)
            {
                formatFlags |= UriHelper.FormatFlags.UserEscaped;
            }

            if (!string.IsNullOrEmpty(elements.host))
            {
                formatFlags |= UriHelper.FormatFlags.HasHost;
            }

            // it's easier to answer some case directly (as the output isn't identical
            // when mixed with others components, e.g. leading slash, # ...)
            switch (components)
            {
            case UriComponents.Scheme:
                return(scheme);

            case UriComponents.UserInfo:
                return(elements.user ?? "");

            case UriComponents.Host:
                return(elements.host);

            case UriComponents.Port: {
                int p = elements.port;
                if (p != null && p >= 0 && p != dp)
                {
                    return(p.ToString(CultureInfo.InvariantCulture));
                }
                return(String.Empty);
            }

            case UriComponents.Path:
                var path = elements.path;
                if (scheme != Uri.UriSchemeMailto && scheme != Uri.UriSchemeNews)
                {
                    path = IgnoreFirstCharIf(elements.path, '/');
                }
                return(UriHelper.FormatAbsolute(path, scheme, UriComponents.Path, format, formatFlags));

            case UriComponents.Query:
                return(UriHelper.FormatAbsolute(elements.query, scheme, UriComponents.Query, format, formatFlags));

            case UriComponents.Fragment:
                return(UriHelper.FormatAbsolute(elements.fragment, scheme, UriComponents.Fragment, format, formatFlags));

            case UriComponents.StrongPort: {
                return(elements.port >= 0
                                ? elements.port.ToString(CultureInfo.InvariantCulture)
                                : dp.ToString(CultureInfo.InvariantCulture));
            }

            case UriComponents.SerializationInfoString:
                components = UriComponents.AbsoluteUri;
                break;
            }

            // now we deal with multiple flags...

            StringBuilder sb = new StringBuilder();

            if ((components & UriComponents.Scheme) != 0)
            {
                sb.Append(scheme);
                sb.Append(elements.delimiter);
            }

            if ((components & UriComponents.UserInfo) != 0)
            {
                string userinfo = elements.user;
                if (userinfo != null)
                {
                    sb.Append(elements.user);
                    sb.Append('@');
                }
            }

            if ((components & UriComponents.Host) != 0)
            {
                sb.Append(elements.host);
            }

            // for StrongPort always show port - even if -1
            // otherwise only display if ut's not the default port
            if ((components & UriComponents.StrongPort) != 0)
            {
                sb.Append(":");
                if (elements.port >= 0)
                {
                    sb.Append(elements.port);
                }
                else
                {
                    sb.Append(dp);
                }
            }

            if ((components & UriComponents.Port) != 0)
            {
                int p = elements.port;
                if (p != null && p >= 0 && p != dp)
                {
                    sb.Append(":");
                    sb.Append(elements.port);
                }
            }

            if ((components & UriComponents.Path) != 0)
            {
                string path = elements.path;
                if ((components & UriComponents.PathAndQuery) != 0 &&
                    (path.Length == 0 || !path.StartsWith("/", StringComparison.Ordinal)) &&
                    elements.delimiter == Uri.SchemeDelimiter)
                {
                    sb.Append("/");
                }
                sb.Append(UriHelper.FormatAbsolute(path, scheme, UriComponents.Path, format, formatFlags));
            }

            if ((components & UriComponents.Query) != 0)
            {
                string query = elements.query;
                if (query != null)
                {
                    sb.Append("?");
                    sb.Append(UriHelper.FormatAbsolute(query, scheme, UriComponents.Query, format, formatFlags));
                }
            }

            if ((components & UriComponents.Fragment) != 0)
            {
                string f = elements.fragment;
                if (f != null)
                {
                    sb.Append("#");
                    sb.Append(UriHelper.FormatAbsolute(f, scheme, UriComponents.Fragment, format, formatFlags));
                }
            }
            return(sb.ToString());
        }
		public ParserState (string uri, UriKind kind)
		{
			remaining = uri;
			this.kind = kind;
			elements  = new UriElements ();
		}
 public ParserState(string uri)
 {
     remaining = uri;
     elements  = new UriElements();
 }
Beispiel #7
0
		public ParserState (string uri)
		{
			remaining = uri;
			elements  = new UriElements ();
		}
Beispiel #8
0
        // protected methods
        protected internal virtual string GetComponents(Uri uri, UriComponents components, UriFormat format)
        {
            if ((format < UriFormat.UriEscaped) || (format > UriFormat.SafeUnescaped))
            {
                throw new ArgumentOutOfRangeException("format");
            }

            UriElements elements = UriParseComponents.ParseComponents(uri.OriginalString.Trim());

            string scheme = scheme_name;
            int    dp     = default_port;

            if ((scheme == null) || (scheme == "*"))
            {
                scheme = elements.scheme;
                dp     = Uri.GetDefaultPort(scheme);
            }
            else if (String.Compare(scheme, elements.scheme, true) != 0)
            {
                throw new SystemException("URI Parser: scheme mismatch: " + scheme + " vs. " + elements.scheme);
            }

            // it's easier to answer some case directly (as the output isn't identical
            // when mixed with others components, e.g. leading slash, # ...)
            switch (components)
            {
            case UriComponents.Scheme:
                return(scheme);

            case UriComponents.UserInfo:
                return(elements.user);

            case UriComponents.Host:
                return(elements.host);

            case UriComponents.Port: {
                string p = elements.port;
                if (p != null && p.Length != 0 && p != dp.ToString())
                {
                    return(p);
                }
                return(String.Empty);
            }

            case UriComponents.Path:
                return(Format(IgnoreFirstCharIf(elements.path, '/'), format));

            case UriComponents.Query:
                return(Format(elements.query, format));

            case UriComponents.Fragment:
                return(Format(elements.fragment, format));

            case UriComponents.StrongPort: {
                return(elements.port.Length != 0 ? elements.port : dp.ToString());
            }

            case UriComponents.SerializationInfoString:
                components = UriComponents.AbsoluteUri;
                break;
            }

            // now we deal with multiple flags...

            StringBuilder sb = new StringBuilder();

            if ((components & UriComponents.Scheme) != 0)
            {
                sb.Append(scheme);
                sb.Append(Uri.GetSchemeDelimiter(scheme));
            }

            if ((components & UriComponents.UserInfo) != 0)
            {
                string userinfo = elements.user;
                if (!String.IsNullOrEmpty(userinfo))
                {
                    sb.Append(elements.user);
                    sb.Append('@');
                }
            }

            if ((components & UriComponents.Host) != 0)
            {
                sb.Append(elements.host);
            }

            // for StrongPort always show port - even if -1
            // otherwise only display if ut's not the default port
            if ((components & UriComponents.StrongPort) != 0)
            {
                sb.Append(":");
                if (elements.port.Length != 0)
                {
                    sb.Append(elements.port);
                }
                else
                {
                    sb.Append(dp);
                }
            }

            if ((components & UriComponents.Port) != 0)
            {
                string p = elements.port;
                if (p != null && p.Length != 0 && p != dp.ToString())
                {
                    sb.Append(":");
                    sb.Append(elements.port);
                }
            }

            if ((components & UriComponents.Path) != 0)
            {
                if ((components & UriComponents.PathAndQuery) != 0 &&
                    (elements.path.Length == 0 || !elements.path.StartsWith("/")))
                {
                    sb.Append("/");
                }
                sb.Append(elements.path);
            }

            if ((components & UriComponents.Query) != 0)
            {
                string query = elements.query;
                if (!String.IsNullOrEmpty(query))
                {
                    sb.Append("?");
                    sb.Append(elements.query);
                }
            }

            string result = Format(sb.ToString(), format);

            if ((components & UriComponents.Fragment) != 0)
            {
                string f = elements.fragment;
                if (!String.IsNullOrEmpty(f))
                {
                    result += "#" + Format(f, format);
                }
            }
            return(result);
        }