System.Uri.CheckHostName C# (CSharp) Method

CheckHostName() public static method

public static CheckHostName ( string name ) : UriHostNameType
name string
return UriHostNameType
        public static UriHostNameType CheckHostName(string name)
        {
            if ((object)name == null || name.Length == 0 || name.Length > short.MaxValue)
            {
                return UriHostNameType.Unknown;
            }
            int end = name.Length;
            unsafe
            {
                fixed (char* fixedName = name)
                {
                    if (name[0] == '[' && name[name.Length - 1] == ']')
                    {
                        // we require that _entire_ name is recognized as ipv6 address
                        if (IPv6AddressHelper.IsValid(fixedName, 1, ref end) && end == name.Length)
                        {
                            return UriHostNameType.IPv6;
                        }
                    }
                    end = name.Length;
                    if (IPv4AddressHelper.IsValid(fixedName, 0, ref end, false, false, false) && end == name.Length)
                    {
                        return UriHostNameType.IPv4;
                    }
                    end = name.Length;
                    bool dummyBool = false;
                    if (DomainNameHelper.IsValid(fixedName, 0, ref end, ref dummyBool, false) && end == name.Length)
                    {
                        return UriHostNameType.Dns;
                    }

                    end = name.Length;
                    dummyBool = false;
                    if (DomainNameHelper.IsValidByIri(fixedName, 0, ref end, ref dummyBool, false)
                        && end == name.Length)
                    {
                        return UriHostNameType.Dns;
                    }
                }

                //This checks the form without []
                end = name.Length + 2;
                // we require that _entire_ name is recognized as ipv6 address
                name = "[" + name + "]";
                fixed (char* newFixedName = name)
                {
                    if (IPv6AddressHelper.IsValid(newFixedName, 1, ref end) && end == name.Length)
                    {
                        return UriHostNameType.IPv6;
                    }
                }
            }
            return UriHostNameType.Unknown;
        }

Usage Example

Example #1
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);
        }