Apache Nutch is an extensible and scalable web crawler
[NUTCH-3025^Curlfilter-fast to filter based on the length of the URL
Julien Nioched8e66ce87328
diff --git a/src/plugin/urlfilter-fast/README.md b/src/plugin/urlfilter-fast/README.mdindex 2e5860575..e6205fc16 100644--- a/src/plugin/urlfilter-fast/README.md+++ b/src/plugin/urlfilter-fast/README.md@@ -73,3 +73,8 @@ the end of the line.The rules file is defined via the property `urlfilter.fast.file`,the default name is `fast-urlfilter.txt`.++In addition to this, the filter checks that the length of the path element of the URL and its query+done not exceed the values set in the properties `urlfilter.fast.url.path.max.length` and+`urlfilter.fast.url.query.max.length` if set.+diff --git a/src/plugin/urlfilter-fast/src/java/org/apache/nutch/urlfilter/fast/FastURLFilter.java b/src/plugin/urlfilter-fast/src/java/org/apache/nutch/urlfilter/fast/FastURLFilter.javaindex ffcd0138a..6761d5ef7 100644--- a/src/plugin/urlfilter-fast/src/java/org/apache/nutch/urlfilter/fast/FastURLFilter.java+++ b/src/plugin/urlfilter-fast/src/java/org/apache/nutch/urlfilter/fast/FastURLFilter.java@@ -97,9 +97,17 @@ public class FastURLFilter implements URLFilter {private Configuration conf;public static final String URLFILTER_FAST_FILE = "urlfilter.fast.file";+ public static final String URLFILTER_FAST_PATH_MAX_LENGTH = "urlfilter.fast.url.path.max.length";+ public static final String URLFILTER_FAST_QUERY_MAX_LENGTH = "urlfilter.fast.url.query.max.length";+private Multimap<String, Rule> hostRules = LinkedHashMultimap.create();private Multimap<String, Rule> domainRules = LinkedHashMultimap.create();+ /** Max allowed size of the path of a URL **/+ private int maxLengthPath = -1;+ /** Max allowed size of the query of a URL **/+ private int maxLengthQuery = -1;+private static final Pattern CATCH_ALL_RULE = Pattern.compile("^\\s*DenyPath(?:Query)?\\s+\\.[*?]\\s*$");@@ -112,6 +120,8 @@ public class FastURLFilter implements URLFilter {@Overridepublic void setConf(Configuration conf) {this.conf = conf;+ maxLengthPath = conf.getInt(URLFILTER_FAST_PATH_MAX_LENGTH, -1);+ maxLengthQuery = conf.getInt(URLFILTER_FAST_QUERY_MAX_LENGTH, -1);try {reloadRules();} catch (Exception e) {@@ -137,6 +147,22 @@ public class FastURLFilter implements URLFilter {e.getMessage());return null;}++ final String path = u.getPath();+ if (maxLengthPath != -1 && path.length() > maxLengthPath)+ {+ LOG.debug("Rejected {} as path length {} is greater than {}", url,+ path.length(), maxLengthPath);+ return null;+ }++ final String query = u.getQuery();+ if (maxLengthQuery != -1 && query != null && query.length() > maxLengthQuery)+ {+ LOG.debug("Rejected {} as query length {} is greater than {}", url,+ query.length(), maxLengthQuery);+ return null;+ }String hostname = u.getHost();diff --git a/src/plugin/urlfilter-fast/src/test/org/apache/nutch/urlfilter/fast/TestFastURLFilter.java b/src/plugin/urlfilter-fast/src/test/org/apache/nutch/urlfilter/fast/TestFastURLFilter.javaindex 8e01d8d3c..0b31a5ad1 100644--- a/src/plugin/urlfilter-fast/src/test/org/apache/nutch/urlfilter/fast/TestFastURLFilter.java+++ b/src/plugin/urlfilter-fast/src/test/org/apache/nutch/urlfilter/fast/TestFastURLFilter.java@@ -16,10 +16,12 @@*/package org.apache.nutch.urlfilter.fast;+import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.Reader;+import org.apache.hadoop.conf.Configuration;import org.apache.nutch.net.URLFilter;import org.apache.nutch.urlfilter.api.RegexURLFilterBaseTest;import org.junit.Assert;@@ -53,4 +55,24 @@ public class TestFastURLFilter extends RegexURLFilterBaseTest {bench(800, "fast-urlfilter-benchmark.txt", "Benchmarks.urls");}+ public void lengthQueryAndPath() throws FileNotFoundException {+ URLFilter filter = getURLFilter(new FileReader(SAMPLES + SEPARATOR + "fast-urlfilter-test.txt"));+ Configuration conf = new Configuration();+ conf.setInt(FastURLFilter.URLFILTER_FAST_PATH_MAX_LENGTH, 50);+ conf.setInt(FastURLFilter.URLFILTER_FAST_QUERY_MAX_LENGTH, 50);+ filter.setConf(conf);++ StringBuilder url = new StringBuilder("http://nutch.apache.org/");+ for (int i = 0; i < 50; i++) {+ url.append(i);+ }+ Assert.assertEquals(null, filter.filter(url.toString()));++ url = new StringBuilder("http://nutch.apache.org/path?");+ for (int i = 0; i < 50; i++) {+ url.append(i);+ }++ Assert.assertEquals(null, filter.filter(url.toString()));+ }}