Apache Nutch is an extensible and scalable web crawler
NUTCH-3176 URLUtil and urlnormalizer-basic: add support for IDNA2008
Sebastian Nagel84699a62c21a
diff --git a/src/plugin/urlnormalizer-basic/src/test/org/apache/nutch/net/urlnormalizer/basic/TestBasicURLNormalizer.java b/src/plugin/urlnormalizer-basic/src/test/org/apache/nutch/net/urlnormalizer/basic/TestBasicURLNormalizer.javaindex 1d3813ac8..090c25f2d 100644--- a/src/plugin/urlnormalizer-basic/src/test/org/apache/nutch/net/urlnormalizer/basic/TestBasicURLNormalizer.java+++ b/src/plugin/urlnormalizer-basic/src/test/org/apache/nutch/net/urlnormalizer/basic/TestBasicURLNormalizer.java@@ -258,15 +258,24 @@ public class TestBasicURLNormalizer {// test Internationalized Domain NamesBasicURLNormalizer norm = new BasicURLNormalizer();conf = NutchConfiguration.create();++ // to ASCII normalizationconf.set(BasicURLNormalizer.NORM_HOST_IDN, "toAscii");norm.setConf(conf);normalizeTest(norm, "https://нэб.рф/", "https://xn--90ax2c.xn--p1ai/");// verify escaping of percent-encoded characters in IDNs (NUTCH-2824)normalizeTest(norm, "https://www.0251-sachverst%c3%a4ndiger.de/","https://www.xn--0251-sachverstndiger-ozb.de/");+ // verify that host names with uppercase characters are normalized+ normalizeTest(norm, "https://нЭб.РФ/", "https://xn--90ax2c.xn--p1ai/");++ // to Unicode normalizationconf.set(BasicURLNormalizer.NORM_HOST_IDN, "toUnicode");norm.setConf(conf);normalizeTest(norm, "https://xn--90ax2c.xn--p1ai/", "https://нэб.рф/");+ // verify that host names with uppercase characters are normalized+ normalizeTest(norm, "https://Xn--90Ax2c.xN--P1ai/", "https://нэб.рф/");+// test removal of trailing dotconf.setBoolean(BasicURLNormalizer.NORM_HOST_TRIM_TRAILING_DOT, true);norm.setConf(conf);@@ -301,17 +310,34 @@ public class TestBasicURLNormalizer {// Verify that characters not in Unicode 3.2 do not fail the normalizationnormalizeTest(norm, "https://example.ᬩᬮᬶ.id/", "https://example.xn--9tfky.id/");+ // mixed encodings (Unicode, Punycode, percent encoding)+ normalizeTest(norm, "https://xn--p1ai.%D1%80%D1%84/",+ "https://xn--p1ai.xn--p1ai/");+ normalizeTest(norm, "https://xn--p1ai.рф/", "https://xn--p1ai.xn--p1ai/");+// test conversion to Unicode (IDNA2008)conf.set(BasicURLNormalizer.NORM_HOST_IDN, "toUnicode");norm.setConf(conf);normalizeTest(norm, "https://xn--strae-oqa.de/", "https://straße.de/");normalizeTest(norm, "https://example.xn--9tfky.id/", "https://example.ᬩᬮᬶ.id/");+ // mixed encodings (Unicode, Punycode, percent encoding), mixed case+ normalizeTest(norm, "https://xN--p1aI.Xn--P1ai/", "https://рф.рф/");+ normalizeTest(norm, "https://xN--p1Ai.%D1%80%d1%84/", "https://рф.рф/");+ normalizeTest(norm, "https://булГаков.xN--p1Ai.%D1%80%d1%84/", "https://булгаков.рф.рф/");+ normalizeTest(norm, "https://гоГоль.%d1%80%D1%84.Рф/", "https://гоголь.рф.рф/");+// test conversion to Unicode (IDNA2003)conf.setBoolean(BasicURLNormalizer.NORM_HOST_IDNA_2008, false);norm.setConf(conf);normalizeTest(norm, "https://xn--strae-oqa.de/", "https://xn--strae-oqa.de/");normalizeTest(norm, "https://example.xn--9tfky.id/", "https://example.ᬩᬮᬶ.id/");++ // mixed encodings (Unicode, Punycode, percent encoding), mixed case+ normalizeTest(norm, "https://xN--p1aI.Xn--P1ai/", "https://рф.рф/");+ normalizeTest(norm, "https://xN--p1Ai.%D1%80%d1%84/", "https://рф.рф/");+ normalizeTest(norm, "https://булГаков.xN--p1Ai.%D1%80%d1%84/", "https://булгаков.рф.рф/");+ normalizeTest(norm, "https://гоГоль.%d1%80%D1%84.Рф/", "https://гоголь.рф.рф/");}/**diff --git a/src/test/org/apache/nutch/util/TestURLUtil.java b/src/test/org/apache/nutch/util/TestURLUtil.javaindex b0f036a1c..200ea59a0 100644--- a/src/test/org/apache/nutch/util/TestURLUtil.java+++ b/src/test/org/apache/nutch/util/TestURLUtil.java@@ -16,6 +16,12 @@*/package org.apache.nutch.util;+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNotNull;+import static org.junit.jupiter.api.Assertions.assertNull;+import static org.junit.jupiter.api.Assertions.assertThrows;+import java.net.MalformedURLException;import java.net.URL;@@ -23,10 +29,6 @@ import org.junit.jupiter.api.Test;import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.CsvSource;-import static org.junit.jupiter.api.Assertions.assertEquals;-import static org.junit.jupiter.api.Assertions.assertNotNull;-import static org.junit.jupiter.api.Assertions.assertNull;-/** Test class for URLUtil */public class TestURLUtil {@@ -323,6 +325,14 @@ public class TestURLUtil {// IDNA2008assertEquals("http://straße.de/",URLUtil.toUNICODE("http://xn--strae-oqa.de/"));+ // host names with uppercase characters+ assertEquals("https://googie.com/",+ URLUtil.toUNICODE("https://googIe.com/"));+ assertEquals("https://googie.com/", URLUtil.toASCII("https://googIe.com/"));+ assertEquals("https://xn--90ax2c.xn--p1ai/",+ URLUtil.toASCII("https://нЭб.РФ/"));+ assertEquals("https://нэб.рф/",+ URLUtil.toUNICODE("https://Xn--90Ax2c.xN--P1ai/"));}@Test@@ -350,15 +360,39 @@ public class TestURLUtil {"www.xn--evir-zoa.com,www.çevir.com,IDNA2003,true", //"xn--uni-tbingen-xhb.de,uni-tübingen.de,IDNA2003,true", //"example.xn--9tfky.id,example.ᬩᬮᬶ.id,IDNA2008,true", //+ // Test examples from whatwg-url"xn--53h.example,☕.example,IDNA2008,true", //"xn--0ca.xn--ssa73l,à.א̈,IDNA2008,true", //- // Note: IDNA2008 and IDNA2003 deviate for the following example+ "xn--mgba3gch31f060k.com,\u0646\u0627\u0645\u0647\u200c\u0627\u06cc.com,IDNA2008,true", //+ /* Note: IDNA2008 and IDNA2003 deviate for the following examples,+ * cf. https://www.unicode.org/reports/tr46/#IDNA2003-Section */"xn--strae-oqa.de,straße.de,IDNA2008,true", //"strasse.de,straße.de,IDNA2003,false", //"strasse.de,strasse.de,IDNA2003,true", //+ "xn--fa-hia.de,faß.de,IDNA2008,true", //+ "fass.de,faß.de,IDNA2003,false", //+ "fass.de,fass.de,IDNA2003,true", //+ "xn--nxasmm1c.com,βόλος.com,IDNA2008,true", //+ "xn--nxasmq6b.com,βόλος.com,IDNA2003,false", //+ "xn--nxasmq6b.com,βόλοσ.com,IDNA2003,true", //+ "xn--10cl1a0b660p.com,ශ්රී.com,IDNA2008,true", //+ "xn--10cl1a0b.com,ශ්රී.com,IDNA2003,false", //+ "xn--10cl1a0b.com,ශ්රී.com,IDNA2003,true", //+ "xn--mgba3gch31f060k.com,نامهای.com,IDNA2008,true", //+ "xn--mgba3gch31f.com,نامهای.com,IDNA2003,false", //+ "xn--mgba3gch31f.com,نامهای.com,IDNA2003,true", //+ // mixed lowercase/uppercase: no round trip conversion+ "xn--bb-eka.at,ÖBB.at,IDNA2003,false", //+ "xn--bb-eka.at,öbb.at,IDNA2003,true", //+ // mixed encoding (Punycode and Unicode)+ "xn--p1ai.xn--p1ai,рф.xn--p1ai,IDNA2003,false", //+ "xn--p1ai.xn--p1ai,xn--p1ai.рф,IDNA2003,false", //+ // percent-encoding is not supported+ // "xn--p1ai.xn--p1ai,xn--p1ai.%D1%80%D1%84,IDNA2003,false", //})public final void testConvertHost(String ascii, String unicode, String type,boolean roundTrip) throws Exception {+ System.out.println(ascii + " <> " + unicode);if ("IDNA2008".equals(type)) {assertEquals(ascii, URLUtil.convertIDNA2008(unicode, true));assertEquals(unicode, URLUtil.convertIDNA2008(ascii, false));@@ -380,6 +414,38 @@ public class TestURLUtil {}}+ @Test+ public final void testConvertHostInvalid() {+ // broken Punycode+ assertDoesNotThrow(() -> assertEquals("xn--xn--bss-7z6ccid.com",+ URLUtil.convertIDNA2003("xn--xn--bss-7z6ccid.com", false, true)));++ // invalid Punycode+ assertThrows(MalformedURLException.class,+ () -> URLUtil.convertIDNA2008("xn--0.pt", false));++ // IDNA2003 not allowing characters not in Unicode 3.2+ assertThrows(MalformedURLException.class,+ () -> URLUtil.convertIDNA2003("☕.example", true, true));+ assertDoesNotThrow(() -> assertEquals("xn--53h.example",+ URLUtil.convertIDNA2003("xn--53h.example", false, true)));++ // IDNA2008 invalid,+ // cf. https://www.unicode.org/reports/tr46/#Implementation_Notes+ // cf. https://www.unicode.org/Public/17.0.0/idna/IdnaTestV2.txt+ // disallowed character: ⒈ (U+2488 - DIGIT ONE FULL STOP)+ assertThrows(MalformedURLException.class,+ () -> URLUtil.convertIDNA2008("\u2488com", true));+ assertThrows(MalformedURLException.class,+ () -> URLUtil.convertIDNA2008("xn--acom-0w1b", false));+ assertThrows(MalformedURLException.class,+ () -> URLUtil.convertIDNA2008("xn--xn--a--gua.pt", false));+ assertThrows(MalformedURLException.class,+ () -> URLUtil.convertIDNA2008("xn--a-ä.pt", false));+ assertThrows(MalformedURLException.class,+ () -> URLUtil.convertIDNA2008("xn--a-ä.pt", true));+ }+@Testpublic void testFileProtocol() throws Exception {// keep one single slash NUTCH-1483