Apache Nutch is an extensible and scalable web crawler
NUTCH-2589 HTML redirections are not followed when using parse-tika - extract meta-refresh redirects from DOM tree normalized by Tika - add unit test to check whether meta-refresh redirects are extracted and parse status holds the redirect target
Sebastian Nagel107b364b7f99
diff --git a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/HTMLMetaProcessor.java b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/HTMLMetaProcessor.javaindex e5be8855d..9c96bd5f6 100644--- a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/HTMLMetaProcessor.java+++ b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/HTMLMetaProcessor.java@@ -120,6 +120,9 @@ public class HTMLMetaProcessor {if (content.contains("no-cache")) {metaTags.setNoCache();}+ } else if (name.equals("refresh")) {+ String content = contentNode.getNodeValue().toLowerCase();+ setRefresh(metaTags, content, currURL);} else if (name.equals("content-location")) {String urlString = contentNode.getNodeValue();URL url = null;@@ -148,54 +151,7 @@ public class HTMLMetaProcessor {if (index >= 0)metaTags.setNoCache();} else if ("refresh".equals(name)) {- int idx = content.indexOf(';');- String time = null;- if (idx == -1) { // just the refresh time- time = content;- } else- time = content.substring(0, idx);- try {- metaTags.setRefreshTime(Integer.parseInt(time));- // skip this if we couldn't parse the time- metaTags.setRefresh(true);- } catch (Exception e) {- ;- }- URL refreshUrl = null;- if (metaTags.getRefresh() && idx != -1) { // set the URL- idx = content.toLowerCase().indexOf("url=");- if (idx == -1) { // assume a mis-formatted entry with just the- // url- idx = content.indexOf(';') + 1;- } else- idx += 4;- if (idx != -1) {- String url = content.substring(idx);- try {- refreshUrl = new URL(url);- } catch (Exception e) {- // XXX according to the spec, this has to be an absolute- // XXX url. However, many websites use relative URLs and- // XXX expect browsers to handle that.- // XXX Unfortunately, in some cases this may create a- // XXX infinitely recursive paths (a crawler trap)...- // if (!url.startsWith("/")) url = "/" + url;- try {- refreshUrl = new URL(currURL, url);- } catch (Exception e1) {- refreshUrl = null;- }- }- }- }- if (metaTags.getRefresh()) {- if (refreshUrl == null) {- // apparently only refresh time was present. set the URL- // to the same URL.- refreshUrl = currURL;- }- metaTags.setRefreshHref(refreshUrl);- }+ setRefresh(metaTags, content, currURL);}}}@@ -234,4 +190,56 @@ public class HTMLMetaProcessor {}}+ private static void setRefresh(HTMLMetaTags metaTags, String content,+ URL currURL) {+ int idx = content.indexOf(';');+ String time = null;+ if (idx == -1) { // just the refresh time+ time = content;+ } else+ time = content.substring(0, idx);+ try {+ metaTags.setRefreshTime(Integer.parseInt(time));+ // skip this if we couldn't parse the time+ metaTags.setRefresh(true);+ } catch (Exception e) {+ ;+ }+ URL refreshUrl = null;+ if (metaTags.getRefresh() && idx != -1) { // set the URL+ idx = content.toLowerCase().indexOf("url=");+ if (idx == -1) { // assume a mis-formatted entry with just the+ // url+ idx = content.indexOf(';') + 1;+ } else+ idx += 4;+ if (idx != -1) {+ String url = content.substring(idx);+ try {+ refreshUrl = new URL(url);+ } catch (Exception e) {+ // XXX according to the spec, this has to be an absolute+ // XXX url. However, many websites use relative URLs and+ // XXX expect browsers to handle that.+ // XXX Unfortunately, in some cases this may create a+ // XXX infinitely recursive paths (a crawler trap)...+ // if (!url.startsWith("/")) url = "/" + url;+ try {+ refreshUrl = new URL(currURL, url);+ } catch (Exception e1) {+ refreshUrl = null;+ }+ }+ }+ }+ if (metaTags.getRefresh()) {+ if (refreshUrl == null) {+ // apparently only refresh time was present. set the URL+ // to the same URL.+ refreshUrl = currURL;+ }+ metaTags.setRefreshHref(refreshUrl);+ }+ }+}diff --git a/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestRobotsMetaProcessor.java b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestRobotsMetaProcessor.javaindex 3c7b25418..8a949a65f 100644--- a/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestRobotsMetaProcessor.java+++ b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestRobotsMetaProcessor.java@@ -23,8 +23,8 @@ import org.apache.hadoop.conf.Configuration;import org.apache.html.dom.HTMLDocumentImpl;import org.apache.nutch.metadata.Metadata;import org.apache.nutch.parse.HTMLMetaTags;+import org.apache.nutch.parse.Parse;import org.apache.nutch.protocol.Content;-import org.apache.nutch.util.DomUtil;import org.apache.nutch.util.NutchConfiguration;import org.junit.Assert;import org.junit.Test;@@ -85,6 +85,9 @@ public class TestRobotsMetaProcessor {+ "<base href=\"http://www.nutch.org/base/\">" + "</head><body>"+ " some text" + "</body></html>",+ "<html><head><title>Meta-refresh redirect</title>"+ + "<meta http-equiv=\"refresh\" content=\"0; url=http://example.com/\"></head><body> "+ + "Test meta-refresh redirect." + "</body></html>",};public static final boolean[][] answers = { //@@ -97,6 +100,7 @@ public class TestRobotsMetaProcessor {{ false, true, false }, // index,nofollow{ false, false, false }, // index,follow{ false, false, false }, // missing!+ { false, false, false }, // NUTCH-2589: test for meta-refresh redirects};private URL[][] currURLsAndAnswers;@@ -119,7 +123,8 @@ public class TestRobotsMetaProcessor {{ new URL("http://www.nutch.org/foo/"),new URL("http://www.nutch.org/") },{ new URL("http://www.nutch.org"),- new URL("http://www.nutch.org/base/") } };+ new URL("http://www.nutch.org/base/") },+ { new URL("http://www.nutch.org"), null } };} catch (Exception e) {Assert.assertTrue("couldn't make test URLs!", false);}@@ -130,11 +135,13 @@ public class TestRobotsMetaProcessor {HTMLDocumentImpl doc = new HTMLDocumentImpl();doc.setErrorChecking(false);DocumentFragment root = doc.createDocumentFragment();- Content content = new Content("http://www.nutch.org",- "http://www.nutch.org", bytes, "text/html", new Metadata(), conf);+ String url = "http://www.nutch.org";+ Content content = new Content(url,+ url, bytes, "text/html", new Metadata(), conf);+ Parse parse = null;try {- parser.getParse(content, doc, root);+ parse = parser.getParse(content, doc, root).get(url);} catch (Exception e) {e.printStackTrace();}@@ -156,6 +163,17 @@ public class TestRobotsMetaProcessor {|| ((robotsMeta.getBaseHref() != null) && robotsMeta.getBaseHref().equals(currURLsAndAnswers[i][1])));+ if (tests[i].contains("meta-refresh redirect")) {+ // test for NUTCH-2589+ URL metaRefreshUrl = robotsMeta.getRefreshHref();+ Assert.assertNotNull("failed to get meta-refresh redirect",+ metaRefreshUrl);+ Assert.assertEquals("failed to get meta-refresh redirect",+ "http://example.com/", metaRefreshUrl.toString());+ Assert.assertEquals(+ "failed to add meta-refresh redirect to parse status",+ "http://example.com/", parse.getData().getStatus().getArgs()[0]);+ }}}