Apache Nutch is an extensible and scalable web crawler
NUTCH-546 - file URL are filtered out by the crawler.
Tacettin Guney9396ecfed994
diff --git a/CHANGES.txt b/CHANGES.txtindex 42b2d2091..38a53537e 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -131,6 +131,8 @@ Unreleased changes (1.0-dev)44. NUTCH-550 - Parse fails if db.max.outlinks.per.page is -1. (dogacan)+45. NUTCH-546 - file URL are filtered out by the crawler. (dogacan)+Release 0.9 - 2007-04-021. Changed log4j confiquration to log to stdout on commandlinediff --git a/src/java/org/apache/nutch/crawl/Injector.java b/src/java/org/apache/nutch/crawl/Injector.javaindex bdb03bfe7..6956b8b03 100644--- a/src/java/org/apache/nutch/crawl/Injector.java+++ b/src/java/org/apache/nutch/crawl/Injector.java@@ -52,7 +52,6 @@ public class Injector extends ToolBase {private URLFilters filters;private ScoringFilters scfilters;private long curTime;- final UrlValidator validator = UrlValidator.get();public void configure(JobConf job) {this.jobConf = job;@@ -73,10 +72,7 @@ public class Injector extends ToolBase {String url = value.toString(); // value is line of text// System.out.println("url: " +url);try {- url = urlNormalizers.normalize(url, URLNormalizers.SCOPE_INJECT); // normalize the url- if (!validator.isValid(url)) {- return;- }+ url = urlNormalizers.normalize(url, URLNormalizers.SCOPE_INJECT);url = filters.filter(url); // filter the url} catch (Exception e) {if (LOG.isWarnEnabled()) { LOG.warn("Skipping " +url+":"+e); }diff --git a/src/java/org/apache/nutch/parse/ParseOutputFormat.java b/src/java/org/apache/nutch/parse/ParseOutputFormat.javaindex 990f9da79..6d6e60dbe 100644--- a/src/java/org/apache/nutch/parse/ParseOutputFormat.java+++ b/src/java/org/apache/nutch/parse/ParseOutputFormat.java@@ -81,7 +81,6 @@ public class ParseOutputFormat implements OutputFormat {this.filters = new URLFilters(job);this.scfilters = new ScoringFilters(job);- final UrlValidator validator = UrlValidator.get();final int interval = job.getInt("db.fetch.interval.default", 2592000);final boolean ignoreExternalLinks = job.getBoolean("db.ignore.external.links", false);int maxOutlinksPerPage = job.getInt("db.max.outlinks.per.page", 100);@@ -166,9 +165,6 @@ public class ParseOutputFormat implements OutputFormat {continue; // skip it}}- if (!validator.isValid(toUrl)) {- continue;- }try {// normalizing here is not necessary since outlinks// are already normalized in Outlink's constructordiff --git a/src/plugin/build.xml b/src/plugin/build.xmlindex 9e5bd4e89..17ac22553 100755--- a/src/plugin/build.xml+++ b/src/plugin/build.xml@@ -73,6 +73,7 @@<ant dir="urlfilter-prefix" target="deploy"/><ant dir="urlfilter-regex" target="deploy"/><ant dir="urlfilter-suffix" target="deploy"/>+ <ant dir="urlfilter-validator" target="deploy"/><ant dir="urlnormalizer-basic" target="deploy"/><ant dir="urlnormalizer-pass" target="deploy"/><ant dir="urlnormalizer-regex" target="deploy"/>@@ -164,6 +165,7 @@<ant dir="urlfilter-prefix" target="clean"/><ant dir="urlfilter-regex" target="clean"/><ant dir="urlfilter-suffix" target="clean"/>+ <ant dir="urlfilter-validator" target="clean"/><ant dir="urlnormalizer-basic" target="clean"/><ant dir="urlnormalizer-pass" target="clean"/><ant dir="urlnormalizer-regex" target="clean"/>diff --git a/src/plugin/urlfilter-validator/build.xml b/src/plugin/urlfilter-validator/build.xmlnew file mode 100644index 000000000..4de9292ae--- /dev/null+++ b/src/plugin/urlfilter-validator/build.xml@@ -0,0 +1,22 @@+<?xml version="1.0"?>+<!--+ Licensed to the Apache Software Foundation (ASF) under one or more+ contributor license agreements. See the NOTICE file distributed with+ this work for additional information regarding copyright ownership.+ The ASF licenses this file to You under the Apache License, Version 2.0+ (the "License"); you may not use this file except in compliance with+ the License. You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+-->+<project name="urlfilter-validator" default="jar-core">++ <import file="../build-plugin.xml"/>++</project>diff --git a/src/plugin/urlfilter-validator/plugin.xml b/src/plugin/urlfilter-validator/plugin.xmlnew file mode 100644index 000000000..413b2883f--- /dev/null+++ b/src/plugin/urlfilter-validator/plugin.xml@@ -0,0 +1,41 @@+<?xml version="1.0" encoding="UTF-8"?>+<!--+ Licensed to the Apache Software Foundation (ASF) under one or more+ contributor license agreements. See the NOTICE file distributed with+ this work for additional information regarding copyright ownership.+ The ASF licenses this file to You under the Apache License, Version 2.0+ (the "License"); you may not use this file except in compliance with+ the License. You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+-->+<plugin+ id="urlfilter-validator"+ name="URL Validator"+ version="1.0.0"+ provider-name="nutch.org">++ <runtime>+ <library name="urlfilter-validator.jar">+ <export name="*"/>+ </library>+ </runtime>++ <requires>+ <import plugin="nutch-extensionpoints"/>+ </requires>++ <extension id="org.apache.nutch.net.urlfilter.validator"+ name="Nutch URL Validatorr"+ point="org.apache.nutch.net.URLFilter">+ <implementation id="URLValidator"+ class="org.apache.nutch.urlfilter.validator.UrlValidator"/>+ </extension>++</plugin>diff --git a/src/java/org/apache/nutch/net/UrlValidator.java b/src/plugin/urlfilter-validator/src/java/org/apache/nutch/urlfilter/validator/UrlValidator.javasimilarity index 84%rename from src/java/org/apache/nutch/net/UrlValidator.javarename to src/plugin/urlfilter-validator/src/java/org/apache/nutch/urlfilter/validator/UrlValidator.javaindex 176e88a2d..8acb79c14 100644--- a/src/java/org/apache/nutch/net/UrlValidator.java+++ b/src/plugin/urlfilter-validator/src/java/org/apache/nutch/urlfilter/validator/UrlValidator.java@@ -14,17 +14,21 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.nutch.net;+package org.apache.nutch.urlfilter.validator;import java.util.regex.Matcher;import java.util.regex.Pattern;+import org.apache.hadoop.conf.Configuration;+import org.apache.nutch.net.URLFilter;+/*** <p>Validates URLs.</p>*- * <p>Originally based in on php script by Debbie Dyer, validation.php v1.2b, Date: 03/07/02,- * http://javascript.internet.com. However, this validation now bears little resemblance- * to the php original.</p>+ * <p>Originally based in on php script by Debbie Dyer, validation.php v1.2b,+ * Date: 03/07/02,+ * http://javascript.internet.com. However, this validation now bears little+ * resemblance to the php original.</p>* <pre>* Example of usage:* UrlValidator urlValidator = UrlValidator.get();@@ -36,16 +40,16 @@ import java.util.regex.Pattern;** prints out "url is valid"* </pre>- *+ ** <p>Based on UrlValidator code from Apache commons-validator.</p>** @see* <a href='http://www.ietf.org/rfc/rfc2396.txt' >* Uniform Resource Identifiers (URI): Generic Syntax* </a>- *+ **/-public class UrlValidator {+public class UrlValidator implements URLFilter {private static final String ALPHA_CHARS = "a-zA-Z";@@ -66,7 +70,8 @@ public class UrlValidator {* This expression derived/taken from the BNF for URI (RFC2396).*/private static final Pattern URL_PATTERN =- Pattern.compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");+ Pattern.compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)" ++ "(\\?([^#]*))?(#(.*))?");/*** Schema/Protocol (ie. http:, ftp:, file:, etc).@@ -85,7 +90,7 @@ public class UrlValidator {/*** Protocol (ie. http:, ftp:,https:).*/- private static final Pattern SCHEME_PATTERN =+ private static final Pattern SCHEME_PATTERN =Pattern.compile("^[" + SCHEME_CHARS + "]+");private static final Pattern AUTHORITY_PATTERN =@@ -100,12 +105,12 @@ public class UrlValidator {*/private static final int PARSE_AUTHORITY_EXTRA = 3;- private static final Pattern PATH_PATTERN =+ private static final Pattern PATH_PATTERN =Pattern.compile("^(/[-\\w:@&?=+,.!/~*'%$_;\\(\\)]*)?$");private static final Pattern QUERY_PATTERN = Pattern.compile("^(.*)$");- private static final Pattern LEGAL_ASCII_PATTERN =+ private static final Pattern LEGAL_ASCII_PATTERN =Pattern.compile("^[\\x21-\\x7E]+$");private static final Pattern IP_V4_DOMAIN_PATTERN =@@ -114,32 +119,40 @@ public class UrlValidator {private static final Pattern DOMAIN_PATTERN =Pattern.compile("^" + ATOM + "(\\." + ATOM + ")*$");- private static final Pattern PORT_PATTERN =+ private static final Pattern PORT_PATTERN =Pattern.compile("^:(\\d{1,5})$");- private static final Pattern ATOM_PATTERN =+ private static final Pattern ATOM_PATTERN =Pattern.compile("(" + ATOM + ")");- private static final Pattern ALPHA_PATTERN =+ private static final Pattern ALPHA_PATTERN =Pattern.compile("^[" + ALPHA_CHARS + "]");-- private static final UrlValidator VALIDATOR = new UrlValidator();- private UrlValidator() {+ private Configuration conf;++ @Override+ public String filter(String urlString) {+ return isValid(urlString) ? urlString : null;+ }++ @Override+ public Configuration getConf() {+ return conf;}-- public static UrlValidator get() {- return VALIDATOR;++ @Override+ public void setConf(Configuration conf) {+ this.conf = conf;}/*** <p>Checks if a field has a valid url address.</p>*- * @param value The value validation is being performed on. A <code>null</code>- * value is considered invalid.+ * @param value The value validation is being performed on.+ * A <code>null</code> value is considered invalid.* @return true if the url is valid.*/- public boolean isValid(String value) {+ private boolean isValid(String value) {if (value == null) {return false;}@@ -177,11 +190,11 @@ public class UrlValidator {* Validate scheme. If schemes[] was initialized to a non null,* then only those scheme's are allowed. Note this is slightly different* than for the constructor.- * @param scheme The scheme to validate. A <code>null</code> value is considered- * invalid.+ * @param scheme The scheme to validate. A <code>null</code> value is+ * considered invalid.* @return true if valid.*/- protected boolean isValidScheme(String scheme) {+ private boolean isValidScheme(String scheme) {if (scheme == null) {return false;}@@ -190,12 +203,13 @@ public class UrlValidator {}/**- * Returns true if the authority is properly formatted. An authority is the combination- * of hostname and port. A <code>null</code> authority value is considered invalid.+ * Returns true if the authority is properly formatted. An authority is+ * the combination of hostname and port. A <code>null</code> authority+ * value is considered invalid.* @param authority Authority value to validate.* @return true if authority (hostname and port) is valid.*/- protected boolean isValidAuthority(String authority) {+ private boolean isValidAuthority(String authority) {if (authority == null) {return false;}@@ -253,7 +267,7 @@ public class UrlValidator {while (atomMatcher.find()) {domainSegment[segCount] = atomMatcher.group();segLen = domainSegment[segCount].length() + 1;- hostIP = (segLen >= hostIP.length()) ? ""+ hostIP = (segLen >= hostIP.length()) ? "": hostIP.substring(segLen);segCount++;}@@ -289,7 +303,7 @@ public class UrlValidator {}/**- * <p>Checks if the field isn't null and length of the field is greater+ * <p>Checks if the field isn't null and length of the field is greater* than zero not including whitespace.</p>** @param value The value validation is being performed on.@@ -300,11 +314,12 @@ public class UrlValidator {}/**- * Returns true if the path is valid. A <code>null</code> value is considered invalid.+ * Returns true if the path is valid. A <code>null</code> value is+ * considered invalid.* @param path Path value to validate.* @return true if path is valid.*/- protected boolean isValidPath(String path) {+ private boolean isValidPath(String path) {if (path == null) {return false;}@@ -316,16 +331,17 @@ public class UrlValidator {int slash2Count = countToken("//", path);int slashCount = countToken("/", path);int dot2Count = countToken("..", path);-+return (dot2Count <= 0) || ((slashCount - slash2Count - 1) > dot2Count);}/**- * Returns true if the query is null or it's a properly formatted query string.+ * Returns true if the query is null or it's a properly formatted query+ * string.* @param query Query value to validate.* @return true if query is valid.*/- protected boolean isValidQuery(String query) {+ private boolean isValidQuery(String query) {if (query == null) {return true;}@@ -339,7 +355,7 @@ public class UrlValidator {* @param target Target value to count tokens in.* @return the number of tokens.*/- protected int countToken(String token, String target) {+ private int countToken(String token, String target) {int tokenIndex = 0;int count = 0;while (tokenIndex != -1) {@@ -351,4 +367,5 @@ public class UrlValidator {}return count;}+}diff --git a/src/plugin/urlfilter-validator/src/java/org/apache/nutch/urlfilter/validator/package.html b/src/plugin/urlfilter-validator/src/java/org/apache/nutch/urlfilter/validator/package.htmlnew file mode 100644index 000000000..845696fc7--- /dev/null+++ b/src/plugin/urlfilter-validator/src/java/org/apache/nutch/urlfilter/validator/package.html@@ -0,0 +1,9 @@+<html>+<body>+<p>A url filter plugin that validates given urls.</p>+<p>This plugin runs a series of tests for the given url to make sure that given+url is valid and 'fetchable'.</p>+<p>Note: This plugin should <b>only</b> be used for web-related protocols such+as http, https and ftp.</p>+</body>+</html>