/**
* 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));
}
/**
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));
}
/**