Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does Uri.CheckHostName() return UriHostNameType.Basic instead of UriHostNameType.Dns or UriHostNameType.Unknown?

Tags:

c#

mono

The documentation at http://msdn.microsoft.com/en-us/library/system.urihostnametype.aspx and http://msdn.microsoft.com/en-us/library/system.uri.checkhostname.aspx is a bit unclear.

Uri.CheckHostName(string) returns UriHostNameType.Dns for a valid DNS hostname, and it returns UriHostNameType.Unknown when the string contains invalid characters and so on. Under what conditions does this method ever return UriHostNameType.Basic?

like image 530
Ash Avatar asked Aug 20 '13 17:08

Ash


3 Answers

Now the .NET Core is open source we can know for sure.

Just like Mono, it never returns UriHostNameType.Basic.

Link to source code

like image 146
Adam Avatar answered Oct 22 '22 05:10

Adam


It occurred to me that I could just check the Mono source code to answer my question. Here is the CheckHostName method from https://github.com/mono/mono/blob/master/mcs/class/System/System/Uri.cs:

    public static UriHostNameType CheckHostName (string name) 
    {
        if (name == null || name.Length == 0)
            return UriHostNameType.Unknown;

        if (IsIPv4Address (name)) 
            return UriHostNameType.IPv4;

        if (IsDomainAddress (name))
            return UriHostNameType.Dns;             

        IPv6Address addr;
        if (IPv6Address.TryParse (name, out addr))
            return UriHostNameType.IPv6;

        return UriHostNameType.Unknown;
    }

It appears that UriHostNameType.Basic isn't used at all. Maybe the Microsoft implementation can return this value?

like image 25
Ash Avatar answered Oct 22 '22 04:10

Ash


UriHostNameType.Basic is used by the UriBuilder and Uri, when there is a DNS host name where at least 1 of the domain labels begins with a "-" (dash) or an "_" (underscore). For example, "_caldavs._tcp.google.com" will return UriHostNameType.Basic.

However, Uri.CheckHostName(string) appears to never return this value. For the same DNS host name above it returns UriHostNameType.Unknown.

like image 30
William Bosacker Avatar answered Oct 22 '22 03:10

William Bosacker