Apache Nutch is an extensible and scalable web crawler
NUTCH-797 Fix parse-tika and parse-html to use relative URL resolution per RFC-3986.
Andrzej Bialeckie990031699b7
diff --git a/CHANGES.txt b/CHANGES.txtindex 7d9b8e62f..98188c4bc 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,9 @@ Nutch Change LogRelease 1.4 - Current development+* NUTCH-797 Fix parse-tika and parse-html to use relative URL resolution per RFC-3986+ (Robert Hohman, ab)+* NUTCH-1154 Upgrade to Tika 0.10. NOTE: Tika's new RTF parser may ignore moretext in malformed documents than previously - see TIKA-748 for details. (ab)diff --git a/src/java/org/apache/nutch/util/URLUtil.java b/src/java/org/apache/nutch/util/URLUtil.javaindex 18b594908..e2f31180e 100644--- a/src/java/org/apache/nutch/util/URLUtil.java+++ b/src/java/org/apache/nutch/util/URLUtil.java@@ -26,6 +26,108 @@ import org.apache.nutch.util.domain.DomainSuffixes;/** Utility class for URL analysis */public class URLUtil {++ /**+ * Resolve relative URL-s and fix a few java.net.URL errors+ * in handling of URLs with embedded params and pure query+ * targets.+ * @param base base url+ * @param target target url (may be relative)+ * @return resolved absolute url.+ * @throws MalformedURLException+ */+ public static URL resolveURL(URL base, String target)+ throws MalformedURLException {+ target = target.trim();++ /* this is probably not needed anymore - see NUTCH-797.+ // handle params that are embedded into the base url - move them to target+ // so URL class constructs the new url class properly+ if (base.toString().indexOf(';') > 0)+ return fixEmbeddedParams(base, target);+ */++ // handle the case that there is a target that is a pure query,+ // for example+ // http://careers3.accenture.com/Careers/ASPX/Search.aspx?co=0&sk=0+ // It has urls in the page of the form href="?co=0&sk=0&pg=1", and by+ // default+ // URL constructs the base+target combo as+ // http://careers3.accenture.com/Careers/ASPX/?co=0&sk=0&pg=1, incorrectly+ // dropping the Search.aspx target+ //+ // Browsers handle these just fine, they must have an exception similar to+ // this+ if (target.startsWith("?")) {+ return fixPureQueryTargets(base, target);+ }++ return new URL(base, target);+ }++ /** Handle the case in RFC3986 section 5.4.1 example 7, and similar. */+ static URL fixPureQueryTargets(URL base, String target)+ throws MalformedURLException {+ if (!target.startsWith("?")) return new URL(base, target);++ String basePath = base.getPath();+ String baseRightMost = "";+ int baseRightMostIdx = basePath.lastIndexOf("/");+ if (baseRightMostIdx != -1) {+ baseRightMost = basePath.substring(baseRightMostIdx + 1);+ }++ if (target.startsWith("?")) target = baseRightMost + target;++ return new URL(base, target);+ }++ /**+ * Handles cases where the url param information is encoded into the base url+ * as opposed to the target.+ * <p>+ * If the taget contains params (i.e. ';xxxx') information then the target+ * params information is assumed to be correct and any base params information+ * is ignored. If the base contains params information but the tareget does+ * not, then the params information is moved to the target allowing it to be+ * correctly determined by the java.net.URL class.+ *+ * @param base+ * The base URL.+ * @param target+ * The target path from the base URL.+ *+ * @return URL A URL with the params information correctly encoded.+ *+ * @throws MalformedURLException+ * If the url is not a well formed URL.+ */+ private static URL fixEmbeddedParams(URL base, String target)+ throws MalformedURLException {++ // the target contains params information or the base doesn't then no+ // conversion necessary, return regular URL+ if (target.indexOf(';') >= 0 || base.toString().indexOf(';') == -1) {+ return new URL(base, target);+ }++ // get the base url and it params information+ String baseURL = base.toString();+ int startParams = baseURL.indexOf(';');+ String params = baseURL.substring(startParams);++ // if the target has a query string then put the params information after+ // any path but before the query string, otherwise just append to the path+ int startQS = target.indexOf('?');+ if (startQS >= 0) {+ target = target.substring(0, startQS) + params+ + target.substring(startQS);+ } else {+ target += params;+ }++ return new URL(base, target);+ }private static Pattern IP_PATTERN = Pattern.compile("(\\d{1,3}\\.){3}(\\d{1,3})");diff --git a/src/plugin/parse-html/ivy.xml b/src/plugin/parse-html/ivy.xmlindex 6a8dd3535..e8a61357a 100644--- a/src/plugin/parse-html/ivy.xml+++ b/src/plugin/parse-html/ivy.xml@@ -36,7 +36,7 @@</publications><dependencies>- <dependency org="org.ccil.cowan.tagsoup" name="tagsoup" rev="1.2"/>+ <dependency org="org.ccil.cowan.tagsoup" name="tagsoup" rev="1.2.1"/></dependencies></ivy-module>diff --git a/src/plugin/parse-html/plugin.xml b/src/plugin/parse-html/plugin.xmlindex d38377436..c154c53a7 100755--- a/src/plugin/parse-html/plugin.xml+++ b/src/plugin/parse-html/plugin.xml@@ -25,7 +25,7 @@<library name="parse-html.jar"><export name="*"/></library>- <library name="tagsoup-1.2.jar"/>+ <library name="tagsoup-1.2.1.jar"/></runtime><requires>diff --git a/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/DOMContentUtils.java b/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/DOMContentUtils.javaindex 6c7242684..aee8e4b58 100644--- a/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/DOMContentUtils.java+++ b/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/DOMContentUtils.java@@ -26,6 +26,7 @@ import java.util.Stack;import org.apache.nutch.parse.Outlink;import org.apache.nutch.util.NodeWalker;+import org.apache.nutch.util.URLUtil;import org.apache.hadoop.conf.Configuration;import org.w3c.dom.*;@@ -39,8 +40,6 @@ import org.w3c.dom.*;*/public class DOMContentUtils {- private boolean fixEmbeddedParams;-public static class LinkParams {public String elName;public String attrName;@@ -57,7 +56,7 @@ public class DOMContentUtils {}}- private HashMap linkParams = new HashMap();+ private HashMap<String,LinkParams> linkParams = new HashMap<String,LinkParams>();private Configuration conf;public DOMContentUtils(Configuration conf) {@@ -89,9 +88,6 @@ public class DOMContentUtils {if ( ! forceTags.contains(ignoreTags[i]) )linkParams.remove(ignoreTags[i]);}-- // https://issues.apache.org/jira/browse/NUTCH-1115- fixEmbeddedParams = conf.getBoolean("parser.fix.embeddedparams", true);}/**@@ -304,51 +300,6 @@ public class DOMContentUtils {return false;}- /**- * Handles cases where the url param information is encoded into the base- * url as opposed to the target.- * <p>- * If the taget contains params (i.e. ';xxxx') information then the target- * params information is assumed to be correct and any base params information- * is ignored. If the base contains params information but the tareget does- * not, then the params information is moved to the target allowing it to be- * correctly determined by the java.net.URL class.- *- * @param base The base URL.- * @param target The target path from the base URL.- *- * @return URL A URL with the params information correctly encoded.- *- * @throws MalformedURLException If the url is not a well formed URL.- */- private URL fixEmbeddedParams(URL base, String target)- throws MalformedURLException{-- // the target contains params information or the base doesn't then no- // conversion necessary, return regular URL- if (!fixEmbeddedParams || target.indexOf(';') >= 0 || base.toString().indexOf(';') == -1) {- return new URL(base, target);- }-- // get the base url and it params information- String baseURL = base.toString();- int startParams = baseURL.indexOf(';');- String params = baseURL.substring(startParams);-- // if the target has a query string then put the params information after- // any path but before the query string, otherwise just append to the path- int startQS = target.indexOf('?');- if (startQS >= 0) {- target = target.substring(0, startQS) + params +- target.substring(startQS);- }- else {- target += params;- }-- return new URL(base, target);- }-/*** This method finds all anchors below the supplied DOM* <code>node</code>, and creates appropriate {@link Outlink}@@ -363,7 +314,7 @@ public class DOMContentUtils {* nodes (this is a common DOM-fixup artifact, at least with* nekohtml).*/- public void getOutlinks(URL base, ArrayList outlinks,+ public void getOutlinks(URL base, ArrayList<Outlink> outlinks,Node node) {NodeWalker walker = new NodeWalker(node);@@ -434,8 +385,7 @@ public class DOMContentUtils {if (target != null && !noFollow && !post)try {- URL url = (base.toString().indexOf(';') > 0) ?- fixEmbeddedParams(base, target) : new URL(base, target);+ URL url = URLUtil.resolveURL(base, target);outlinks.add(new Outlink(url.toString(),linkText.toString().trim()));} catch (MalformedURLException e) {diff --git a/src/plugin/parse-html/src/test/org/apache/nutch/parse/html/TestDOMContentUtils.java b/src/plugin/parse-html/src/test/org/apache/nutch/parse/html/TestDOMContentUtils.javaindex fd73d2d1c..d99e13883 100644--- a/src/plugin/parse-html/src/test/org/apache/nutch/parse/html/TestDOMContentUtils.java+++ b/src/plugin/parse-html/src/test/org/apache/nutch/parse/html/TestDOMContentUtils.java@@ -294,11 +294,12 @@ public class TestDOMContentUtils extends TestCase {new Outlink("http://www.nutch.org/g;x?y#s", "anchor3")},{- new Outlink("http://www.nutch.org/g;something", "anchor1"),- new Outlink("http://www.nutch.org/g;something?y#s", "anchor2"),+ // this is tricky - see RFC3986 section 5.4.1 example 7+ new Outlink("http://www.nutch.org/g", "anchor1"),+ new Outlink("http://www.nutch.org/g?y#s", "anchor2"),new Outlink("http://www.nutch.org/;something?y=1", "anchor3"),new Outlink("http://www.nutch.org/;something?y=1#s", "anchor4"),- new Outlink("http://www.nutch.org/?y=1;somethingelse", "anchor5")+ new Outlink("http://www.nutch.org/;something?y=1;somethingelse", "anchor5")},{new Outlink("http://www.nutch.org/g", ""),diff --git a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/DOMContentUtils.java b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/DOMContentUtils.javaindex 9d84db553..2af17da0e 100644--- a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/DOMContentUtils.java+++ b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/DOMContentUtils.java@@ -26,6 +26,7 @@ import java.util.HashMap;import org.apache.hadoop.conf.Configuration;import org.apache.nutch.parse.Outlink;import org.apache.nutch.util.NodeWalker;+import org.apache.nutch.util.URLUtil;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;@@ -39,8 +40,6 @@ import org.w3c.dom.NodeList;*/class DOMContentUtils {- private boolean fixEmbeddedParams;-private static class LinkParams {private String elName;private String attrName;@@ -57,7 +56,7 @@ class DOMContentUtils {}}- private HashMap linkParams = new HashMap();+ private HashMap<String,LinkParams> linkParams = new HashMap<String,LinkParams>();private Configuration conf;DOMContentUtils(Configuration conf) {@@ -89,9 +88,6 @@ class DOMContentUtils {if ( ! forceTags.contains(ignoreTags[i]) )linkParams.remove(ignoreTags[i]);}-- // https://issues.apache.org/jira/browse/NUTCH-1115- fixEmbeddedParams = conf.getBoolean("parser.fix.embeddedparams", true);}/**@@ -304,51 +300,6 @@ class DOMContentUtils {return false;}- /**- * Handles cases where the url param information is encoded into the base- * url as opposed to the target.- * <p>- * If the taget contains params (i.e. ';xxxx') information then the target- * params information is assumed to be correct and any base params information- * is ignored. If the base contains params information but the tareget does- * not, then the params information is moved to the target allowing it to be- * correctly determined by the java.net.URL class.- *- * @param base The base URL.- * @param target The target path from the base URL.- *- * @return URL A URL with the params information correctly encoded.- *- * @throws MalformedURLException If the url is not a well formed URL.- */- private URL fixEmbeddedParams(URL base, String target)- throws MalformedURLException{-- // the target contains params information or the base doesn't then no- // conversion necessary, return regular URL- if (!fixEmbeddedParams || target.indexOf(';') >= 0 || base.toString().indexOf(';') == -1) {- return new URL(base, target);- }-- // get the base url and it params information- String baseURL = base.toString();- int startParams = baseURL.indexOf(';');- String params = baseURL.substring(startParams);-- // if the target has a query string then put the params information after- // any path but before the query string, otherwise just append to the path- int startQS = target.indexOf('?');- if (startQS >= 0) {- target = target.substring(0, startQS) + params +- target.substring(startQS);- }- else {- target += params;- }-- return new URL(base, target);- }-/*** This method finds all anchors below the supplied DOM* <code>node</code>, and creates appropriate {@link Outlink}@@ -363,7 +314,7 @@ class DOMContentUtils {* nodes (this is a common DOM-fixup artifact, at least with* nekohtml).*/- void getOutlinks(URL base, ArrayList outlinks,+ void getOutlinks(URL base, ArrayList<Outlink> outlinks,Node node) {NodeWalker walker = new NodeWalker(node);@@ -405,8 +356,7 @@ class DOMContentUtils {if (target != null && !noFollow && !post)try {- URL url = (base.toString().indexOf(';') > 0) ?- fixEmbeddedParams(base, target) : new URL(base, target);+ URL url = URLUtil.resolveURL(base, target);outlinks.add(new Outlink(url.toString(),linkText.toString().trim()));} catch (MalformedURLException e) {diff --git a/src/test/org/apache/nutch/util/TestURLUtil.java b/src/test/org/apache/nutch/util/TestURLUtil.javaindex b3ad4d241..94f2cabd8 100644--- a/src/test/org/apache/nutch/util/TestURLUtil.java+++ b/src/test/org/apache/nutch/util/TestURLUtil.java@@ -213,4 +213,50 @@ public class TestURLUtilassertEquals(aDotCom, URLUtil.chooseRepr(aDotCom, aSubDotCom, true));}+ // from RFC3986 section 5.4.1+ private static String baseString = "http://a/b/c/d;p?q";+ private static String[][] targets = new String[][] {+ // unknown protocol {"g:h" , "g:h"},+ {"g" , "http://a/b/c/g"},+ { "./g" , "http://a/b/c/g"},+ { "g/" , "http://a/b/c/g/"},+ { "/g" , "http://a/g"},+ { "//g" , "http://g"},+ { "?y" , "http://a/b/c/d;p?y"},+ { "g?y" , "http://a/b/c/g?y"},+ { "#s" , "http://a/b/c/d;p?q#s"},+ { "g#s" , "http://a/b/c/g#s"},+ { "g?y#s" , "http://a/b/c/g?y#s"},+ { ";x" , "http://a/b/c/;x"},+ { "g;x" , "http://a/b/c/g;x"},+ { "g;x?y#s" , "http://a/b/c/g;x?y#s"},+ { "" , "http://a/b/c/d;p?q"},+ { "." , "http://a/b/c/"},+ { "./" , "http://a/b/c/"},+ { ".." , "http://a/b/"},+ { "../" , "http://a/b/"},+ { "../g" , "http://a/b/g"},+ { "../.." , "http://a/"},+ { "../../" , "http://a/"},+ { "../../g" , "http://a/g"}+ };++ public void testResolveURL() throws Exception {+ // test NUTCH-436+ URL u436 = new URL("http://a/b/c/d;p?q#f");+ assertEquals("http://a/b/c/d;p?q#f", u436.toString());+ URL abs = URLUtil.resolveURL(u436, "?y");+ assertEquals("http://a/b/c/d;p?y", abs.toString());+ // test NUTCH-566+ URL u566 = new URL("http://www.fleurie.org/entreprise.asp");+ abs = URLUtil.resolveURL(u566, "?id_entrep=111");+ assertEquals("http://www.fleurie.org/entreprise.asp?id_entrep=111", abs.toString());+ URL base = new URL(baseString);+ assertEquals("base url parsing", baseString, base.toString());+ for (int i = 0; i < targets.length; i++) {+ URL u = URLUtil.resolveURL(base, targets[i][0]);+ assertEquals(targets[i][1], targets[i][1], u.toString());+ }+ }+}