diff --git a/httpclient/src/main/java/org/apache/http/conn/util/PublicSuffixMatcher.java b/httpclient/src/main/java/org/apache/http/conn/util/PublicSuffixMatcher.java index e06937054..1793377fe 100644 --- a/httpclient/src/main/java/org/apache/http/conn/util/PublicSuffixMatcher.java +++ b/httpclient/src/main/java/org/apache/http/conn/util/PublicSuffixMatcher.java @@ -142,17 +142,17 @@ public String getDomainRoot(final String domain, final DomainType expectedType) if (domain.startsWith(".")) { return null; } - String domainName = null; - String segment = domain.toLowerCase(Locale.ROOT); + final String normalized = domain.toLowerCase(Locale.ROOT); + String segment = normalized; + String result = null; while (segment != null) { - // An exception rule takes priority over any other matching rule. - if (hasException(IDN.toUnicode(segment), expectedType)) { + final String key = IDN.toUnicode(segment); + if (hasException(key, expectedType)) { return segment; } - - if (hasRule(IDN.toUnicode(segment), expectedType)) { - break; + if (hasRule(key, expectedType)) { + return result; } final int nextdot = segment.indexOf('.'); @@ -160,15 +160,13 @@ public String getDomainRoot(final String domain, final DomainType expectedType) if (nextSegment != null) { if (hasRule("*." + IDN.toUnicode(nextSegment), expectedType)) { - break; + return result; } } - if (nextdot != -1) { - domainName = segment; - } + result = segment; segment = nextSegment; } - return domainName; + return normalized; } /** diff --git a/httpclient/src/test/java/org/apache/http/conn/util/TestPublicSuffixListParser.java b/httpclient/src/test/java/org/apache/http/conn/util/TestPublicSuffixListParser.java index 35ac9d57b..a1cb415d3 100644 --- a/httpclient/src/test/java/org/apache/http/conn/util/TestPublicSuffixListParser.java +++ b/httpclient/src/test/java/org/apache/http/conn/util/TestPublicSuffixListParser.java @@ -52,7 +52,7 @@ public void testParse() throws Exception { in.close(); } Assert.assertNotNull(suffixList); - Assert.assertEquals(Arrays.asList("jp", "ac.jp", "*.tokyo.jp", "no", "h\u00E5.no"), suffixList.getRules()); + Assert.assertEquals(Arrays.asList("xx", "jp", "ac.jp", "*.tokyo.jp", "no", "h\u00E5.no"), suffixList.getRules()); Assert.assertEquals(Arrays.asList("metro.tokyo.jp"), suffixList.getExceptions()); } diff --git a/httpclient/src/test/java/org/apache/http/conn/util/TestPublicSuffixMatcher.java b/httpclient/src/test/java/org/apache/http/conn/util/TestPublicSuffixMatcher.java index 8c124fff8..cb986c0b8 100644 --- a/httpclient/src/test/java/org/apache/http/conn/util/TestPublicSuffixMatcher.java +++ b/httpclient/src/test/java/org/apache/http/conn/util/TestPublicSuffixMatcher.java @@ -63,13 +63,13 @@ public void testGetDomainRoot() throws Exception { Assert.assertEquals("example.xx", matcher.getDomainRoot("www.blah.blah.example.XX")); Assert.assertEquals(null, matcher.getDomainRoot("xx")); Assert.assertEquals(null, matcher.getDomainRoot("jp")); - Assert.assertEquals(null, matcher.getDomainRoot("example")); - Assert.assertEquals("example.example", matcher.getDomainRoot("example.example")); Assert.assertEquals(null, matcher.getDomainRoot("ac.jp")); Assert.assertEquals(null, matcher.getDomainRoot("any.tokyo.jp")); Assert.assertEquals("metro.tokyo.jp", matcher.getDomainRoot("metro.tokyo.jp")); Assert.assertEquals("blah.blah.tokyo.jp", matcher.getDomainRoot("blah.blah.tokyo.jp")); Assert.assertEquals("blah.ac.jp", matcher.getDomainRoot("blah.blah.ac.jp")); + Assert.assertEquals("garbage", matcher.getDomainRoot("garbage")); + Assert.assertEquals("garbage.garbage", matcher.getDomainRoot("garbage.garbage")); } @Test diff --git a/httpclient/src/test/java/org/apache/http/impl/cookie/TestPublicSuffixListParser.java b/httpclient/src/test/java/org/apache/http/impl/cookie/TestPublicSuffixListParser.java index 50816ba2e..49e35161f 100644 --- a/httpclient/src/test/java/org/apache/http/impl/cookie/TestPublicSuffixListParser.java +++ b/httpclient/src/test/java/org/apache/http/impl/cookie/TestPublicSuffixListParser.java @@ -101,7 +101,7 @@ public void testParseLocal() throws Exception { Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.local", 80, "/stuff", false))); cookie.setDomain(".blah"); - Assert.assertFalse(filter.match(cookie, new CookieOrigin("somehost.blah", 80, "/stuff", false))); + Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.blah", 80, "/stuff", false))); } @Test diff --git a/httpclient/src/test/resources/suffixlist.txt b/httpclient/src/test/resources/suffixlist.txt index f5ff28378..6aa880c90 100644 --- a/httpclient/src/test/resources/suffixlist.txt +++ b/httpclient/src/test/resources/suffixlist.txt @@ -23,6 +23,7 @@ // . // +xx jp ac.jp *.tokyo.jp