Apache Nutch is an extensible and scalable web crawler
NUTCH-1483 (including NUTCH-1879, NUTCH-1880, NUTCH-1885) fix errors related to protocol-file
Sebastian Nageleec91cf69c23
diff --git a/CHANGES.txt b/CHANGES.txtindex 3f6e4442b..bd1947045 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,14 @@ Nutch Change LogNutch Current Development 1.10-SNAPSHOT+* NUTCH-1483 Can't crawl filesystem with protocol-file plugin (Rogério Pereira Araújo, Mengying Wang, snagel)++* NUTCH-1885 Protocol-file should treat symbolic links as redirects (Mengying Wang, snagel)++* NUTCH-1880 URLUtil should not add additional slashes for file URLs (snagel)++* NUTCH-1879 Regex URL normalizer should remove multiple slashes after file: protocol (snagel)+* NUTCH-1883 bin/crawl: use function to run bin/nutch and check exit value (snagel)* NUTCH-1865 Enable use of SNAPSHOT's with Nutch Ivy dependency management (lewismc)diff --git a/conf/nutch-default.xml b/conf/nutch-default.xmlindex 908354d3f..eda74ef04 100644--- a/conf/nutch-default.xml+++ b/conf/nutch-default.xml@@ -55,6 +55,19 @@crawled.</description></property>+<property>+ <name>file.crawl.redirect_noncanonical</name>+ <value>true</value>+ <description>+ If true, protocol-file treats non-canonical file names as+ redirects and does not canonicalize file names internally. A file+ name containing symbolic links as path elements is then not+ resolved and "fetched" but recorded as redirect with the+ canonical name (all links on path are resolved) as redirect+ target.+ </description>+</property>+<property><name>file.content.ignored</name><value>true</value>diff --git a/conf/regex-normalize.xml.template b/conf/regex-normalize.xml.templateindex d14174eaa..ec60c1081 100644--- a/conf/regex-normalize.xml.template+++ b/conf/regex-normalize.xml.template@@ -63,7 +63,15 @@<substitution></substitution></regex>-<!-- removes duplicate slashes -->+<!-- normalize file:/// protocol prefix: -->+<!-- keep one single slash (NUTCH-1483) -->+<regex>+ <pattern>^file://+</pattern>+ <substitution>file:/</substitution>+</regex>++<!-- removes duplicate slashes but -->+<!-- * allow 2 slashes after colon ':' (indicating protocol) --><regex><pattern>(?<!:)/{2,}</pattern><substitution>/</substitution>diff --git a/src/java/org/apache/nutch/util/URLUtil.java b/src/java/org/apache/nutch/util/URLUtil.javaindex 1a59b2382..68b792e29 100644--- a/src/java/org/apache/nutch/util/URLUtil.java+++ b/src/java/org/apache/nutch/util/URLUtil.java@@ -425,9 +425,15 @@ public class URLUtil {public static String toASCII(String url) {try {URL u = new URL(url);+ String host = u.getHost();+ if (host == null || host.isEmpty()) {+ // no host name => no punycoded domain name+ // also do not add additional slashes for file: URLs (NUTCH-1880)+ return url;+ }URI p = new URI(u.getProtocol(),u.getUserInfo(),- IDN.toASCII(u.getHost()),+ IDN.toASCII(host),u.getPort(),u.getPath(),u.getQuery(),@@ -443,6 +449,12 @@ public class URLUtil {public static String toUNICODE(String url) {try {URL u = new URL(url);+ String host = u.getHost();+ if (host == null || host.isEmpty()) {+ // no host name => no punycoded domain name+ // also do not add additional slashes for file: URLs (NUTCH-1880)+ return url;+ }StringBuilder sb = new StringBuilder();sb.append(u.getProtocol());sb.append("://");@@ -450,7 +462,7 @@ public class URLUtil {sb.append(u.getUserInfo());sb.append('@');}- sb.append(IDN.toUnicode(u.getHost()));+ sb.append(IDN.toUnicode(host));if (u.getPort() != -1) {sb.append(':');sb.append(u.getPort());diff --git a/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/File.java b/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/File.javaindex 5a908b1dd..522445593 100644--- a/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/File.java+++ b/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/File.java@@ -53,6 +53,12 @@ public class File implements Protocol {int maxContentLength;boolean crawlParents;+ /**+ * if true return a redirect for symbolic links and do not resolve the links+ * internally+ */+ boolean symlinksAsRedirects = true;+private Configuration conf;public File() {}@@ -64,6 +70,8 @@ public class File implements Protocol {this.conf = conf;this.maxContentLength = conf.getInt("file.content.limit", 64 * 1024);this.crawlParents = conf.getBoolean("file.crawl.parent", true);+ this.symlinksAsRedirects = conf.getBoolean(+ "file.crawl.redirect_noncanonical", true);}/**@@ -115,13 +123,19 @@ public class File implements Protocol {return new ProtocolOutput(response.toContent(), ProtocolStatus.STATUS_NOTFOUND);} else if (code >= 300 && code < 400) { // handle redirect- if (redirects == MAX_REDIRECTS)- throw new FileException("Too many redirects: " + url);u = new URL(response.getHeader("Location"));- redirects++;if (LOG.isTraceEnabled()) {LOG.trace("redirect to " + u);}+ if (symlinksAsRedirects) {+ return new ProtocolOutput(response.toContent(), new ProtocolStatus(+ ProtocolStatus.MOVED, u));+ } else if (redirects == MAX_REDIRECTS) {+ LOG.trace("Too many redirects: {}", url);+ return new ProtocolOutput(response.toContent(), new ProtocolStatus(+ ProtocolStatus.REDIR_EXCEEDED, u));+ }+ redirects++;} else { // convert to exceptionthrow new FileError(code);@@ -172,13 +186,21 @@ public class File implements Protocol {// set log level//LOG.setLevel(Level.parse((new String(logLevel)).toUpperCase()));- Content content = file.getProtocolOutput(new Text(urlString), new CrawlDatum()).getContent();+ ProtocolOutput output = file.getProtocolOutput(new Text(urlString), new CrawlDatum());+ Content content = output.getContent();+ System.err.println("URL: " + content.getUrl());+ System.err.println("Status: " + output.getStatus());System.err.println("Content-Type: " + content.getContentType());System.err.println("Content-Length: " +content.getMetadata().get(Response.CONTENT_LENGTH));System.err.println("Last-Modified: " +content.getMetadata().get(Response.LAST_MODIFIED));+ String redirectLocation = content.getMetadata().get("Location");+ if (redirectLocation != null) {+ System.err.println("Location: " + redirectLocation);+ }+if (dumpContent) {System.out.print(new String(content.getContent()));}diff --git a/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.test b/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.testindex dbb08b568..7867ad80e 100644--- a/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.test+++ b/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.test@@ -75,3 +75,10 @@ http://www.foo.com/foo.html?&x=y http://www.foo.com/foo.html?x=yhttp://www.foo.com/foo.html?x=y&&&z=a http://www.foo.com/foo.html?x=y&z=ahttp://www.foo.com/foo.html? http://www.foo.com/foo.html+# remove double slashes but keep 2 slashes after protocol+http://www.foo.com//path//foo.html http://www.foo.com/path/foo.html+https://www.foo.com//path//foo.html https://www.foo.com/path/foo.html++# normalize file: protocol prefix (keep one slash)+file:///path//foo.html file:/path/foo.html+file:/path//foo.html file:/path/foo.htmldiff --git a/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.xml b/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.xmlindex 69a8eba26..4d6eabcf2 100644--- a/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.xml+++ b/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.xml@@ -48,5 +48,19 @@<substitution></substitution></regex>+<!-- normalize file:/// protocol prefix: -->+<!-- keep one single slash (NUTCH-1483) -->+<regex>+ <pattern>^file://+</pattern>+ <substitution>file:/</substitution>+</regex>++<!-- removes duplicate slashes but -->+<!-- * allow 2 slashes after colon ':' (indicating protocol) -->+<regex>+ <pattern>(?<!:)/{2,}</pattern>+ <substitution>/</substitution>+</regex>+</regex-normalize>diff --git a/src/test/org/apache/nutch/util/TestURLUtil.java b/src/test/org/apache/nutch/util/TestURLUtil.javaindex 5307dedd7..fbfcddeb5 100644--- a/src/test/org/apache/nutch/util/TestURLUtil.java+++ b/src/test/org/apache/nutch/util/TestURLUtil.java@@ -275,4 +275,11 @@ public class TestURLUtil {URLUtil.toASCII("http://www.medizin.uni-tübingen.de:8080/search.php?q=abc#p1"));}+ @Test+ public void testFileProtocol() throws Exception {+ // keep one single slash NUTCH-XXX+ Assert.assertEquals("file:/path/file.html", URLUtil.toASCII("file:/path/file.html"));+ Assert.assertEquals("file:/path/file.html", URLUtil.toUNICODE("file:/path/file.html"));+ }+}