Add support for IPv6 in System.Uri.DnsSafeHost
authorSebastien Pouliot <sebastien@ximian.com>
Fri, 17 Sep 2010 18:10:25 +0000 (14:10 -0400)
committerSebastien Pouliot <sebastien@ximian.com>
Fri, 17 Sep 2010 21:25:37 +0000 (17:25 -0400)
* 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

mcs/class/System/System/Uri.cs
mcs/class/System/Test/System/UriTest2.cs

index 8cc2b8c500b5cd71a9eac72496ccf63ab3c337a6..21389deb5f4f8790a81a015c3673b65ab73aafce 100644 (file)
@@ -81,6 +81,7 @@ namespace System {
                private bool isUnc;
                private bool isOpaquePart;
                private bool isAbsoluteUri = true;
+               private long scope_id;
 
                private List<string> segments;
                
@@ -676,11 +677,16 @@ namespace System {
                        }
                }
                
-               [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);
                        }
                }
 
@@ -1568,9 +1574,10 @@ namespace System {
                        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))
index 43039a8ff89d948e205974ebd840f47a1acbe43e..30114a5b9f51a86c61ca4633b8fba4cd43225864 100644 (file)
@@ -895,5 +895,21 @@ TextWriter sw = Console.Out;
                        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");
+               }
        }\r
 }\r