Apache Nutch is an extensible and scalable web crawler
Refactor robots.txt checking so that it's protocol independent.
Andrzej Bialeckifd8de622140b
diff --git a/src/java/org/apache/nutch/fetcher/Fetcher.java b/src/java/org/apache/nutch/fetcher/Fetcher.javaindex 38ef94412..a7ce26b6c 100644--- a/src/java/org/apache/nutch/fetcher/Fetcher.java+++ b/src/java/org/apache/nutch/fetcher/Fetcher.java@@ -434,8 +434,6 @@ public class Fetcher extends ToolBase implements MapRunnable {job.setInputPath(new Path(segment, CrawlDatum.GENERATE_DIR_NAME));job.setInputFormat(InputFormat.class);- job.setInputKeyClass(Text.class);- job.setInputValueClass(CrawlDatum.class);job.setMapRunnerClass(Fetcher.class);diff --git a/src/java/org/apache/nutch/protocol/EmptyRobotRules.java b/src/java/org/apache/nutch/protocol/EmptyRobotRules.javanew file mode 100644index 000000000..9086eb904--- /dev/null+++ b/src/java/org/apache/nutch/protocol/EmptyRobotRules.java@@ -0,0 +1,26 @@+/*+ * Created on Aug 4, 2006+ * Author: Andrzej Bialecki <[email protected]>+ *+ */+package org.apache.nutch.protocol;++import java.net.URL;++public class EmptyRobotRules implements RobotRules {++ public static final RobotRules RULES = new EmptyRobotRules();++ public long getCrawlDelay() {+ return -1;+ }++ public long getExpireTime() {+ return -1;+ }++ public boolean isAllowed(URL url) {+ return true;+ }++}diff --git a/src/java/org/apache/nutch/protocol/Protocol.java b/src/java/org/apache/nutch/protocol/Protocol.javaindex 1328531c3..911a5f4c0 100755--- a/src/java/org/apache/nutch/protocol/Protocol.java+++ b/src/java/org/apache/nutch/protocol/Protocol.java@@ -30,8 +30,34 @@ import org.apache.nutch.plugin.Pluggable;public interface Protocol extends Pluggable, Configurable {/** The name of the extension point. */public final static String X_POINT_ID = Protocol.class.getName();++ /**+ * Property name. If in the current configuration this property is set to+ * true, protocol implementations should handle "politeness" limits+ * internally. If this is set to false, it is assumed that these limits are+ * enforced elsewhere, and protocol implementations should not enforce them+ * internally.+ */+ public final static String CHECK_BLOCKING = "protocol.plugin.check.blocking";++ /**+ * Property name. If in the current configuration this property is set to+ * true, protocol implementations should handle robot exclusion rules+ * internally. If this is set to false, it is assumed that these limits are+ * enforced elsewhere, and protocol implementations should not enforce them+ * internally.+ */+ public final static String CHECK_ROBOTS = "protocol.plugin.check.robots";/** Returns the {@link Content} for a fetchlist entry.*/ProtocolOutput getProtocolOutput(Text url, CrawlDatum datum);++ /**+ * Retrieve robot rules applicable for this url.+ * @param url url to check+ * @param datum page datum+ * @return robot rules (specific for this url or default), never null+ */+ RobotRules getRobotRules(Text url, CrawlDatum datum);}diff --git a/src/java/org/apache/nutch/protocol/RobotRules.java b/src/java/org/apache/nutch/protocol/RobotRules.javanew file mode 100644index 000000000..e503298be--- /dev/null+++ b/src/java/org/apache/nutch/protocol/RobotRules.java@@ -0,0 +1,29 @@++package org.apache.nutch.protocol;++import java.net.URL;+++/**+ * This class holds the rules which were parsed from a robots.txt file, and can+ * test paths against those rules.+ */+public interface RobotRules {+ /**+ * Get expire time+ */+ public long getExpireTime();++ /**+ * Get Crawl-Delay, in milliseconds. This returns -1 if not set.+ */+ public long getCrawlDelay();++ /**+ * Returns <code>false</code> if the <code>robots.txt</code> file+ * prohibits us from accessing the given <code>url</code>, or+ * <code>true</code> otherwise.+ */+ public boolean isAllowed(URL url);++}diff --git a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.javaindex 889cee17b..8a594e00c 100644--- a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java+++ b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java@@ -36,6 +36,7 @@ import org.apache.nutch.protocol.Protocol;import org.apache.nutch.protocol.ProtocolException;import org.apache.nutch.protocol.ProtocolOutput;import org.apache.nutch.protocol.ProtocolStatus;+import org.apache.nutch.protocol.RobotRules;import org.apache.nutch.util.GZIPUtils;import org.apache.nutch.util.LogUtil;@@ -43,7 +44,6 @@ import org.apache.nutch.util.LogUtil;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.io.Text;-/*** @author Jérôme Charron*/@@ -130,6 +130,12 @@ public abstract class HttpBase implements Protocol {/** Skip page if Crawl-Delay longer than this value. */protected long maxCrawlDelay = -1L;+ /** Plugin should handle host blocking internally. */+ protected boolean checkBlocking = true;++ /** Plugin should handle robot rules checking internally. */+ protected boolean checkRobots = true;+/** Creates a new instance of HttpBase */public HttpBase() {this(null);@@ -161,6 +167,8 @@ public abstract class HttpBase implements Protocol {this.byIP = conf.getBoolean("fetcher.threads.per.host.by.ip", true);this.useHttp11 = conf.getBoolean("http.useHttp11", false);this.robots.setConf(conf);+ this.checkBlocking = conf.getBoolean(Protocol.CHECK_BLOCKING, true);+ this.checkRobots = conf.getBoolean(Protocol.CHECK_ROBOTS, true);logConf();}@@ -177,36 +185,40 @@ public abstract class HttpBase implements Protocol {try {URL u = new URL(urlString);- try {- if (!robots.isAllowed(this, u)) {- return new ProtocolOutput(null, new ProtocolStatus(ProtocolStatus.ROBOTS_DENIED, url));- }- } catch (Throwable e) {- // XXX Maybe bogus: assume this is allowed.- if (logger.isTraceEnabled()) {- logger.trace("Exception checking robot rules for " + url + ": " + e);+ if (checkRobots) {+ try {+ if (!robots.isAllowed(this, u)) {+ return new ProtocolOutput(null, new ProtocolStatus(ProtocolStatus.ROBOTS_DENIED, url));+ }+ } catch (Throwable e) {+ // XXX Maybe bogus: assume this is allowed.+ if (logger.isTraceEnabled()) {+ logger.trace("Exception checking robot rules for " + url + ": " + e);+ }}}long crawlDelay = robots.getCrawlDelay(this, u);long delay = crawlDelay > 0 ? crawlDelay : serverDelay;- if (maxCrawlDelay >= 0 && delay > maxCrawlDelay) {+ if (checkBlocking && maxCrawlDelay >= 0 && delay > maxCrawlDelay) {// skip this page, otherwise the thread would block for too long.LOGGER.info("Skipping: " + u + " exceeds fetcher.max.crawl.delay, max="+ (maxCrawlDelay / 1000) + ", Crawl-Delay=" + (delay / 1000));return new ProtocolOutput(null, ProtocolStatus.STATUS_WOULDBLOCK);}- String host;- try {- host = blockAddr(u, delay);- } catch (BlockedException be) {- return new ProtocolOutput(null, ProtocolStatus.STATUS_BLOCKED);+ String host = null;+ if (checkBlocking) {+ try {+ host = blockAddr(u, delay);+ } catch (BlockedException be) {+ return new ProtocolOutput(null, ProtocolStatus.STATUS_BLOCKED);+ }}Response response;try {response = getResponse(u, datum, false); // make a request} finally {- unblockAddr(host, delay);+ if (checkBlocking) unblockAddr(host, delay);}int code = response.getCode();@@ -456,8 +468,12 @@ public abstract class HttpBase implements Protocol {logger.info("http.timeout = " + timeout);logger.info("http.content.limit = " + maxContent);logger.info("http.agent = " + userAgent);- logger.info("fetcher.server.delay = " + serverDelay);- logger.info("http.max.delays = " + maxDelays);+ logger.info(Protocol.CHECK_BLOCKING + " = " + checkBlocking);+ logger.info(Protocol.CHECK_ROBOTS + " = " + checkRobots);+ if (checkBlocking) {+ logger.info("fetcher.server.delay = " + serverDelay);+ logger.info("http.max.delays = " + maxDelays);+ }}}@@ -531,4 +547,8 @@ public abstract class HttpBase implements Protocol {boolean followRedirects)throws ProtocolException, IOException;+ public RobotRules getRobotRules(Text url, CrawlDatum datum) {+ return robots.getRobotRulesSet(this, url);+ }+}diff --git a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.java b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.javaindex 2210f22ce..fa3da1939 100644--- a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.java+++ b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.java@@ -34,9 +34,11 @@ import org.apache.commons.logging.LogFactory;// Nutch importsimport org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configurable;+import org.apache.hadoop.io.Text;import org.apache.nutch.crawl.CrawlDatum;import org.apache.nutch.net.protocols.Response;import org.apache.nutch.protocol.ProtocolException;+import org.apache.nutch.protocol.RobotRules;/**@@ -70,7 +72,7 @@ public class RobotRulesParser implements Configurable {* This class holds the rules which were parsed from a robots.txt* file, and can test paths against those rules.*/- public static class RobotRuleSet {+ public static class RobotRuleSet implements RobotRules {ArrayList tmpEntries = new ArrayList();RobotsEntry[] entries = null;long expireTime;@@ -142,6 +144,19 @@ public class RobotRulesParser implements Configurable {this.crawlDelay = crawlDelay;}+ /**+ * Returns <code>false</code> if the <code>robots.txt</code> file+ * prohibits us from accessing the given <code>url</code>, or+ * <code>true</code> otherwise.+ */+ public boolean isAllowed(URL url) {+ String path = url.getPath(); // check rules+ if ((path == null) || "".equals(path)) {+ path= "/";+ }+ return isAllowed(path);+ }+/*** Returns <code>false</code> if the <code>robots.txt</code> file* prohibits us from accessing the given <code>path</code>, or@@ -154,7 +169,7 @@ public class RobotRulesParser implements Configurable {// just ignore it- we can still try to match// path prefixes}-+if (entries == null) {entries= new RobotsEntry[tmpEntries.size()];entries= (RobotsEntry[])@@ -415,6 +430,16 @@ public class RobotRulesParser implements Configurable {return rules;}+ public RobotRuleSet getRobotRulesSet(HttpBase http, Text url) {+ URL u = null;+ try {+ u = new URL(url.toString());+ } catch (Exception e) {+ return EMPTY_RULES;+ }+ return getRobotRulesSet(http, u);+ }+private RobotRuleSet getRobotRulesSet(HttpBase http, URL url) {String host = url.getHost().toLowerCase(); // normalize to lower casediff --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 165a08e1c..624551b88 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@@ -29,9 +29,11 @@ import org.apache.nutch.net.protocols.Response;import org.apache.hadoop.conf.Configuration;import org.apache.nutch.protocol.Content;+import org.apache.nutch.protocol.EmptyRobotRules;import org.apache.nutch.protocol.Protocol;import org.apache.nutch.protocol.ProtocolOutput;import org.apache.nutch.protocol.ProtocolStatus;+import org.apache.nutch.protocol.RobotRules;import java.net.URL;@@ -164,4 +166,8 @@ public class File implements Protocol {public Configuration getConf() {return this.conf;}++ public RobotRules getRobotRules(Text url, CrawlDatum datum) {+ return EmptyRobotRules.RULES;+ }}diff --git a/src/plugin/protocol-ftp/src/java/org/apache/nutch/protocol/ftp/Ftp.java b/src/plugin/protocol-ftp/src/java/org/apache/nutch/protocol/ftp/Ftp.javaindex b61cf9e10..519fbb078 100644--- a/src/plugin/protocol-ftp/src/java/org/apache/nutch/protocol/ftp/Ftp.java+++ b/src/plugin/protocol-ftp/src/java/org/apache/nutch/protocol/ftp/Ftp.java@@ -30,9 +30,11 @@ import org.apache.nutch.net.protocols.Response;import org.apache.hadoop.conf.Configuration;import org.apache.nutch.protocol.Content;+import org.apache.nutch.protocol.EmptyRobotRules;import org.apache.nutch.protocol.Protocol;import org.apache.nutch.protocol.ProtocolOutput;import org.apache.nutch.protocol.ProtocolStatus;+import org.apache.nutch.protocol.RobotRules;import java.net.URL;@@ -238,4 +240,8 @@ public class Ftp implements Protocol {return this.conf;}+ public RobotRules getRobotRules(Text url, CrawlDatum datum) {+ return EmptyRobotRules.RULES;+ }+}