Skip to content

Commit

Permalink
Add support for IPv6 in System.Uri.DnsSafeHost
Browse files Browse the repository at this point in the history
* System/Uri.cs: Fix DnsSafeHost for IPv6 addresses, removing the []
brackets and adding, when available, the scope id

* Test/System/UriTest2.cs: Add DnsSafeHost test cases for IPv6 address,
one without scope id, another with a scope id
  • Loading branch information
Sebastien Pouliot committed Sep 17, 2010
1 parent b3619d3 commit 6ad868a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
15 changes: 11 additions & 4 deletions mcs/class/System/System/Uri.cs
Expand Up @@ -81,6 +81,7 @@ public class Uri : ISerializable {
private bool isUnc;
private bool isOpaquePart;
private bool isAbsoluteUri = true;
private long scope_id;

private List<string> segments;

Expand Down Expand Up @@ -676,11 +677,16 @@ private bool IsLocalIdenticalToAbsolutePath ()
}
}

[MonoTODO ("add support for IPv6 address")]
public string DnsSafeHost {
get {
EnsureAbsoluteUri ();
return Unescape (Host);
string host = Host;
if (HostNameType == UriHostNameType.IPv6) {
host = Host.Substring (1, Host.Length - 2);
if (scope_id != 0)
host += "%" + scope_id.ToString ();
}
return Unescape (host);
}
}

Expand Down Expand Up @@ -1568,9 +1574,10 @@ private string ParseNoExceptions (UriKind kind, string uriString)
if (!badhost && (host.Length > 1) && (host[0] == '[') && (host[host.Length - 1] == ']')) {
IPv6Address ipv6addr;

if (IPv6Address.TryParse (host, out ipv6addr))
if (IPv6Address.TryParse (host, out ipv6addr)) {
host = "[" + ipv6addr.ToString (true) + "]";
else
scope_id = ipv6addr.ScopeId;
} else
badhost = true;
}
if (badhost && (Parser is DefaultUriParser || Parser == null))
Expand Down
16 changes: 16 additions & 0 deletions mcs/class/System/Test/System/UriTest2.cs
Expand Up @@ -895,5 +895,21 @@ public void ColonButNoPort ()
Assert.AreEqual (21, uri.Port, "2.Port");
Assert.IsTrue (uri.IsDefaultPort, "2.IsDefaultPort");
}

[Test]
public void IPv6SafeDnsName ()
{
Uri uri = new Uri ("http://[1:2:3:4:5:6:7:8]");
Assert.AreEqual (UriHostNameType.IPv6, uri.HostNameType, "1.HostNameType");
Assert.AreEqual ("[0001:0002:0003:0004:0005:0006:0007:0008]", uri.Authority, "1.Authority");
Assert.AreEqual ("0001:0002:0003:0004:0005:0006:0007:0008", uri.DnsSafeHost, "1.DnsSafeHost");
Assert.AreEqual ("[0001:0002:0003:0004:0005:0006:0007:0008]", uri.Host, "1.Host");

uri = new Uri ("http://[fe80::200:39ff:fe36:1a2d%4]/temp/example.htm");
Assert.AreEqual (UriHostNameType.IPv6, uri.HostNameType, "1.HostNameType");
Assert.AreEqual ("[FE80:0000:0000:0000:0200:39FF:FE36:1A2D]", uri.Authority, "2.Authority");
Assert.AreEqual ("FE80:0000:0000:0000:0200:39FF:FE36:1A2D%4", uri.DnsSafeHost, "2.DnsSafeHost");
Assert.AreEqual ("[FE80:0000:0000:0000:0200:39FF:FE36:1A2D]", uri.Host, "2.Host");
}
}
}

0 comments on commit 6ad868a

Please sign in to comment.