OSDN Git Service

Fix a problem where URL.equals() was returning true when both hosts couldn't resolve.
authorJesse Wilson <jessewilson@google.com>
Wed, 29 Sep 2010 00:31:05 +0000 (17:31 -0700)
committerJesse Wilson <jessewilson@google.com>
Wed, 29 Sep 2010 00:31:05 +0000 (17:31 -0700)
See http://b/3045007

Change-Id: I83f2a0d8888dd30aaf6099049ce8008487d4337d

luni/src/main/java/java/net/URLStreamHandler.java
luni/src/test/java/libcore/java/net/OldURLTest.java

index d95b2a5..dae193b 100644 (file)
@@ -387,17 +387,33 @@ public abstract class URLStreamHandler {
     /**
      * Compares two URL objects whether they refer to the same host.
      *
-     * @param url1
-     *            the first URL to be compared.
-     * @param url2
-     *            the second URL to be compared.
+     * @param a the first URL to be compared.
+     * @param b the second URL to be compared.
      * @return {@code true} if both URLs refer to the same host, {@code false}
      *         otherwise.
      */
-    protected boolean hostsEqual(URL url1, URL url2) {
-        String host1 = getHost(url1), host2 = getHost(url2);
-        return (host1 != null && host1.equalsIgnoreCase(host2))
-                || Objects.equal(getHostAddress(url1), getHostAddress(url2));
+    protected boolean hostsEqual(URL a, URL b) {
+        /*
+         * URLs with the same case-insensitive host name have equal hosts
+         */
+        String aHost = getHost(a);
+        String bHost = getHost(b);
+        if (aHost != null && aHost.equalsIgnoreCase(bHost)) {
+            return true;
+        }
+
+        /*
+         * Call out to DNS to resolve the host addresses. If this succeeds for
+         * both addresses and both addresses yield the same InetAddress, report
+         * equality.
+         *
+         * Although it's consistent with historical behavior of the RI, this
+         * approach is fundamentally broken. In particular, acting upon this
+         * result is bogus because a single server may serve content for many
+         * unrelated host names.
+         */
+        InetAddress aResolved = getHostAddress(a);
+        return aResolved != null && aResolved.equals(getHostAddress(b));
     }
 
     /**
index 6b3dfa0..9953451 100644 (file)
@@ -292,31 +292,22 @@ public class OldURLTest extends TestCase {
         args = {java.lang.Object.class}
     )
     public void testEqualsObject() throws MalformedURLException {
-        URL testURL1 = new URL("http", "www.apache.org:8080", "test.html");
-        URL wrongProto = new URL("ftp", "www.apache.org:8080", "test.html");
-        URL wrongPort = new URL("http", "www.apache.org:8082", "test.html");
-        URL wrongHost = new URL("http", "www.apache2.org:8080", "test.html");
-        URL wrongRef = new URL("http", "www.apache.org:8080",
-                "test2.html#BOTTOM");
+        URL testURL1 = new URL("http", "www.apache.org", 8080, "test.html");
+        URL wrongProto = new URL("ftp", "www.apache.org", 8080, "test.html");
+        URL wrongPort = new URL("http", "www.apache.org", 8082, "test.html");
+        URL wrongHost = new URL("http", "www.apache2.org", 8080, "test.html");
+        URL wrongRef = new URL("http", "www.apache.org", 8080, "test2.html#BOTTOM");
         URL testURL2 = new URL("http://www.apache.org:8080/test.html");
 
-
-        assertFalse("Assert 0: error in equals: not same", testURL1
-                .equals(wrongProto));
-        assertFalse("Assert 1: error in equals: not same", testURL1
-                .equals(wrongPort));
-        assertFalse("Assert 2: error in equals: not same", testURL1
-                .equals(wrongHost));
-        assertFalse("Assert 3: error in equals: not same", testURL1
-                .equals(wrongRef));
-
-        assertFalse("Assert 4: error in equals: not same", testURL1
-                .equals(testURL2));
+        assertFalse("Assert 0: error in equals: not same", testURL1.equals(wrongProto));
+        assertFalse("Assert 1: error in equals: not same", testURL1.equals(wrongPort));
+        assertFalse("Assert 2: error in equals: not same", testURL1.equals(wrongHost));
+        assertFalse("Assert 3: error in equals: not same", testURL1.equals(wrongRef));
+        assertFalse("Assert 4: error in equals: not same", testURL1.equals(testURL2));
 
         URL testURL3 = new URL("http", "www.apache.org", "/test.html");
         URL testURL4 = new URL("http://www.apache.org/test.html");
-        assertTrue("Assert 4: error in equals: same", testURL3
-                .equals(testURL4));
+        assertTrue("Assert 4: error in equals: same", testURL3.equals(testURL4));
     }
 
     /**