Fix case where 'incomplete' IPv6 address are used in UriBuilder
authorSebastien Pouliot <sebastien@ximian.com>
Fri, 17 Sep 2010 12:43:39 +0000 (08:43 -0400)
committerSebastien Pouliot <sebastien@ximian.com>
Fri, 17 Sep 2010 21:25:35 +0000 (17:25 -0400)
* System/UriBuilder.cs: Fix Host property to add the missing brackets
on IPv6 address (if missing from input)

* Test/System/UriBuilderTest.cs: Add test cases with incomplete IPv6
addresses. Add test case with a relative Uri used in UriBuilder ctor

mcs/class/System/System/UriBuilder.cs
mcs/class/System/Test/System/UriBuilderTest.cs

index b43c2db5c25d182d636b77b2d7bd55df9a2d6379..2c317a4bae9f00c8be3e325c1dccbf460480777d 100644 (file)
@@ -141,7 +141,13 @@ namespace System
                public string Host {
                        get { return host; }
                        set {
-                               host = (value == null) ? String.Empty : value;
+                               if (String.IsNullOrEmpty (value))
+                                       host = String.Empty;
+                               else if ((value.IndexOf (':') != -1) && (value [0] != '[')) {
+                                       host = "[" + value + "]";
+                               } else {
+                                       host = value;
+                               }
                                modified = true;
                        }
                }
index 86f22e41574d576398a4591711977895807004ba..d7360378c2b238dea5049301c29e3101b629e2fd 100644 (file)
@@ -102,6 +102,14 @@ namespace MonoTests.System
                        // should have thrown an ArgumentException because extraValue must start with '?' or '#' character.
                }
                
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void Constructor_RelativeUri ()
+               {
+                       Uri relative = new Uri ("../dir/subdir/file", UriKind.RelativeOrAbsolute);
+                       UriBuilder ub = new UriBuilder (relative);
+               }
+
                [Test]
                public void UserInfo ()
                {
@@ -276,6 +284,26 @@ namespace MonoTests.System
                        Assert.AreEqual ("[0001:0002:0003:0004:0005:0006:0007:0008]", ub.Host, "Host.2");
                }
 
+               [Test]
+               public void IPv6_Host_IncompleteAddress ()
+               {
+                       UriBuilder ub = new UriBuilder ("http", "1:2:3:4:5:6:7:8", 8080, "/dir/subdir/file");
+                       Assert.AreEqual ("[1:2:3:4:5:6:7:8]", ub.Host, "1.Host");
+                       Assert.AreEqual ("http://[1:2:3:4:5:6:7:8]:8080/dir/subdir/file", ub.ToString (), "1.ToString ()");
+
+                       ub = new UriBuilder ("http", "1:", 8080, "/dir/subdir/file");
+                       Assert.AreEqual ("[1:]", ub.Host, "2.Host");
+                       Assert.AreEqual ("http://[1:]:8080/dir/subdir/file", ub.ToString (), "2.ToString ()");
+
+                       ub = new UriBuilder ("http", "[1:", 8080, "/dir/subdir/file");
+                       Assert.AreEqual ("[1:", ub.Host, "3.Host");
+                       Assert.AreEqual ("http://[1::8080/dir/subdir/file", ub.ToString (), "3.ToString ()");
+
+                       ub = new UriBuilder ("http", "1:2]", 8080, "/dir/subdir/file");
+                       Assert.AreEqual ("[1:2]]", ub.Host, "4.Host");
+                       Assert.AreEqual ("http://[1:2]]:8080/dir/subdir/file", ub.ToString (), "4.ToString ()");
+               }
+
                [Test]
                public void Path_UriAbsolutePath_Path ()
                {