Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-208 http: proxy exception list:

Lewis John McGibbney3999d0f3fccc
diff --git a/CHANGES.txt b/CHANGES.txt
index 4f518ccd6..ec2ac14ab 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -2,6 +2,8 @@ Nutch Change Log
Nutch Current Development 1.11-SNAPSHOT
+* NUTCH-208 http: proxy exception list: (Matthias Günter, siren, markus, lewismc)
+
* NUTCH-2007 add test libs to classpath of bin/nutch junit (snagel)
* NUTCH-1995 Add support for wildcard to http.robot.rules.whitelist (totaro)
diff --git a/conf/nutch-default.xml b/conf/nutch-default.xml
index a84d6ea26..e8ccf42d1 100644
--- a/conf/nutch-default.xml
+++ b/conf/nutch-default.xml
@@ -277,6 +277,13 @@
</description>
</property>
+<property>
+ <name>http.proxy.exception.list</name>
+ <value></value>
+ <description>A comma separated list of URL's and hosts that don't use the proxy
+ (e.g. intranets). Example: www.apache.org</description>
+</property>
+
<property>
<name>http.verbose</name>
<value>false</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.java
index d16ba1f24..5181ddae8 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
@@ -21,6 +21,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
+import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
@@ -67,6 +68,9 @@ public abstract class HttpBase implements Protocol {
/** The proxy port. */
protected int proxyPort = 8080;
+
+ /** The proxy exception list. */
+ protected HashMap proxyException = new HashMap();
/** Indicates if a proxy is used */
protected boolean useProxy = false;
@@ -135,6 +139,7 @@ public abstract class HttpBase implements Protocol {
this.conf = conf;
this.proxyHost = conf.get("http.proxy.host");
this.proxyPort = conf.getInt("http.proxy.port", 8080);
+ this.proxyException = arrayToMap(conf.getStrings("http.proxy.exception.list"));
this.useProxy = (proxyHost != null && proxyHost.length() > 0);
this.timeout = conf.getInt("http.timeout", 10000);
this.maxContent = conf.getInt("http.content.limit", 64 * 1024);
@@ -340,7 +345,12 @@ public abstract class HttpBase implements Protocol {
return proxyPort;
}
- public boolean useProxy() {
+ public boolean useProxy(URL url) {
+ if (!useProxy){
+ return false;
+ } else if (proxyException.get(url.getHost())!=null){
+ return false;
+ }
return useProxy;
}
@@ -434,6 +444,7 @@ public abstract class HttpBase implements Protocol {
if (logger.isInfoEnabled()) {
logger.info("http.proxy.host = " + proxyHost);
logger.info("http.proxy.port = " + proxyPort);
+ logger.info("http.proxy.exception.list = " + useProxy);
logger.info("http.timeout = " + timeout);
logger.info("http.content.limit = " + maxContent);
logger.info("http.agent = " + userAgent);
@@ -547,4 +558,22 @@ public abstract class HttpBase implements Protocol {
public BaseRobotRules getRobotRules(Text url, CrawlDatum datum) {
return robots.getRobotRulesSet(this, url);
}
+
+ /**
+ * Transforming a String[] into a HashMap for faster searching
+ * @param input String[]
+ * @return a new HashMap
+ */
+ private HashMap arrayToMap(String[]input){
+ if (input==null ||input.length==0) {
+ return new HashMap();
+ }
+ HashMap hm=new HashMap();
+ for (int i=0;i<input.length;i++){
+ if (!"".equals(input[i].trim())){
+ hm.put(input[i],input[i]);
+ }
+ }
+ return hm;
+ }
}
diff --git a/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/HttpResponse.java b/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/HttpResponse.java
index 331f2a352..f2b18b0ec 100644
--- a/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/HttpResponse.java
+++ b/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/HttpResponse.java
@@ -117,8 +117,8 @@ public class HttpResponse implements Response {
socket.setSoTimeout(http.getTimeout());
// connect
- String sockHost = http.useProxy() ? http.getProxyHost() : host;
- int sockPort = http.useProxy() ? http.getProxyPort() : port;
+ String sockHost = http.useProxy(url) ? http.getProxyHost() : host;
+ int sockPort = http.useProxy(url) ? http.getProxyPort() : port;
InetSocketAddress sockAddr = new InetSocketAddress(sockHost, sockPort);
socket.connect(sockAddr, http.getTimeout());
@@ -158,7 +158,7 @@ public class HttpResponse implements Response {
OutputStream req = socket.getOutputStream();
StringBuffer reqStr = new StringBuffer("GET ");
- if (http.useProxy()) {
+ if (http.useProxy(url)) {
reqStr.append(url.getProtocol() + "://" + host + portString + path);
} else {
reqStr.append(path);
@@ -329,7 +329,6 @@ public class HttpResponse implements Response {
* @throws HttpException
* @throws IOException
*/
- @SuppressWarnings("unused")
private void readChunkedContent(PushbackInputStream in, StringBuffer line)
throws HttpException, IOException {
boolean doneChunks = false;
diff --git a/src/plugin/protocol-selenium/src/java/org/apache/nutch/protocol/selenium/HttpResponse.java b/src/plugin/protocol-selenium/src/java/org/apache/nutch/protocol/selenium/HttpResponse.java
index 1c4f31127..681e838a2 100644
--- a/src/plugin/protocol-selenium/src/java/org/apache/nutch/protocol/selenium/HttpResponse.java
+++ b/src/plugin/protocol-selenium/src/java/org/apache/nutch/protocol/selenium/HttpResponse.java
@@ -90,8 +90,8 @@ public class HttpResponse implements Response {
socket.setSoTimeout(http.getTimeout());
// connect
- String sockHost = http.useProxy() ? http.getProxyHost() : host;
- int sockPort = http.useProxy() ? http.getProxyPort() : port;
+ String sockHost = http.useProxy(url) ? http.getProxyHost() : host;
+ int sockPort = http.useProxy(url) ? http.getProxyPort() : port;
InetSocketAddress sockAddr = new InetSocketAddress(sockHost, sockPort);
socket.connect(sockAddr, http.getTimeout());
@@ -99,7 +99,7 @@ public class HttpResponse implements Response {
OutputStream req = socket.getOutputStream();
StringBuffer reqStr = new StringBuffer("GET ");
- if (http.useProxy()) {
+ if (http.useProxy(url)) {
reqStr.append(url.getProtocol() + "://" + host + portString + path);
} else {
reqStr.append(path);