Apache Nutch is an extensible and scalable web crawler
NUTCH-1941 Optional rolling http.agent.names
Sebastian Nagel78f192dec11e
diff --git a/CHANGES.txt b/CHANGES.txtindex eb88b3888..123bf8467 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogNutch Current Development 1.10-SNAPSHOT+* NUTCH-1941 Optional rolling http.agent.name's (Asitang Mishra, lewismc via snagel)+* NUTCH-1959 Improving CommonCrawlFormat implementations (Giuseppe Totaro via mattmann)* NUTCH-1974 keyPrefix option for CommonCrawlDataDumper tool (Giuseppe Totaro via mattmann)diff --git a/conf/nutch-default.xml b/conf/nutch-default.xmlindex 6a8ab093d..76d070cc3 100644--- a/conf/nutch-default.xml+++ b/conf/nutch-default.xml@@ -160,6 +160,26 @@header.</description></property>+<property>+ <name>http.agent.rotate</name>+ <value>false</value>+ <description>+ If true, instead of http.agent.name, alternating agent names are+ chosen from a list provided via http.agent.rotate.file.+ </description>+</property>++<property>+ <name>http.agent.rotate.file</name>+ <value>agents.txt</value>+ <description>+ File containing alternative user agent names to be used instead of+ http.agent.name on a rotating basis if http.agent.rotate is true.+ Each line of the file should contain exactly one agent+ specification including name, version, description, URL, etc.+ </description>+</property>+<property><name>http.agent.host</name><value></value>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 846be0eef..d16ba1f24 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@@ -17,12 +17,15 @@package org.apache.nutch.protocol.http.api;// JDK imports+import java.io.BufferedReader;import java.io.IOException;+import java.io.Reader;import java.net.URL;+import java.util.ArrayList;import java.util.Arrays;import java.util.HashSet;import java.util.Set;-+import java.util.concurrent.ThreadLocalRandom;// Logging importsimport org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -37,6 +40,7 @@ import org.apache.nutch.protocol.ProtocolOutput;import org.apache.nutch.protocol.ProtocolStatus;import org.apache.nutch.util.GZIPUtils;import org.apache.nutch.util.DeflateUtils;+import org.apache.hadoop.util.StringUtils;// Hadoop importsimport org.apache.hadoop.conf.Configuration;@@ -56,6 +60,8 @@ public abstract class HttpBase implements Protocol {private HttpRobotRulesParser robots = null;+ private ArrayList<String> userAgentNames = null;+/** The proxy hostname. */protected String proxyHost = null;@@ -143,6 +149,45 @@ public abstract class HttpBase implements Protocol {this.enableIfModifiedsinceHeader = conf.getBoolean("http.enable.if.modified.since.header", true);this.robots.setConf(conf);+ // NUTCH-1941: read list of alternating agent names+ if (conf.getBoolean("http.agent.rotate", false)) {+ String agentsFile = conf.get("http.agent.rotate.file", "agents.txt");+ BufferedReader br = null;+ try {+ Reader reader = conf.getConfResourceAsReader(agentsFile);+ br = new BufferedReader(reader);+ userAgentNames = new ArrayList<String>();+ String word = "";+ while ((word = br.readLine()) != null) {+ if (!word.trim().isEmpty())+ userAgentNames.add(word.trim());+ }++ if (userAgentNames.size() == 0) {+ logger.warn("Empty list of user agents in http.agent.rotate.file {}",+ agentsFile);+ userAgentNames = null;+ }++ } catch (Exception e) {+ logger.warn("Failed to read http.agent.rotate.file {}: {}", agentsFile,+ StringUtils.stringifyException(e));+ userAgentNames = null;+ } finally {+ if (br != null) {+ try {+ br.close();+ } catch (IOException e) {+ // ignore+ }+ }+ }+ if (userAgentNames == null) {+ logger+ .warn("Falling back to fixed user agent set via property http.agent.name");+ }+ }+String[] protocols = conf.getStrings("http.tls.supported.protocols","TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3");String[] ciphers = conf.getStrings("http.tls.supported.cipher.suites",@@ -312,6 +357,9 @@ public abstract class HttpBase implements Protocol {}public String getUserAgent() {+ if (userAgentNames!=null) {+ return userAgentNames.get(ThreadLocalRandom.current().nextInt(userAgentNames.size()-1));+ }return userAgent;}diff --git a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.javaindex 20cbaf8f8..2371ee762 100644--- a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java+++ b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java@@ -203,7 +203,7 @@ public class Http extends HttpBase {HostConfiguration hostConf = client.getHostConfiguration();ArrayList<Header> headers = new ArrayList<Header>();// Set the User Agent in the header- headers.add(new Header("User-Agent", userAgent));+ //headers.add(new Header("User-Agent", userAgent)); //NUTCH-1941// prefer Englishheaders.add(new Header("Accept-Language", acceptLanguage));// prefer UTF-8diff --git a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/HttpResponse.java b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/HttpResponse.javaindex e6a4c35db..f074af27d 100644--- a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/HttpResponse.java+++ b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/HttpResponse.java@@ -29,6 +29,8 @@ import org.apache.commons.httpclient.cookie.CookiePolicy;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.params.HttpMethodParams;import org.apache.commons.httpclient.HttpException;+import org.apache.commons.httpclient.HttpClient;+// Nutch importsimport org.apache.nutch.crawl.CrawlDatum;@@ -95,7 +97,9 @@ public class HttpResponse implements Response {// XXX little danger in retrying...// params.setParameter(HttpMethodParams.RETRY_HANDLER, null);try {- code = Http.getClient().executeMethod(get);+ HttpClient client = Http.getClient();+ client.getParams().setParameter("http.useragent", http.getUserAgent()); // NUTCH-1941+ code = client.executeMethod(get);Header[] heads = get.getResponseHeaders();