Apache Nutch is an extensible and scalable web crawler
Refactor HTTP plugins so that both support gzip encoding. Add appropriate headers in protocol-httpclient so that it prefers this encoding.
Andrzej Bialecki94efb56f1084
diff --git a/conf/nutch-default.xml b/conf/nutch-default.xmlindex df60bf932..d72956a55 100644--- a/conf/nutch-default.xml+++ b/conf/nutch-default.xml@@ -132,6 +132,14 @@trying to fetch a page.</description></property>+<property>+ <name>http.useHttp11</name>+ <value>false</value>+ <description>NOTE: at the moment this works only for protocol-httpclient.+ If true, use HTTP 1.1, if false use HTTP 1.0 .+ </description>+</property>+<!-- FTP properties --><property>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 652f5756e..8df566990 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@@ -27,13 +27,13 @@ import java.util.logging.Logger;// Nutch importsimport org.apache.nutch.crawl.CrawlDatum;-import org.apache.nutch.metadata.Metadata;import org.apache.nutch.net.protocols.Response;import org.apache.nutch.protocol.Content;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.util.GZIPUtils;// Hadoop importsimport org.apache.hadoop.conf.Configuration;@@ -120,6 +120,8 @@ public abstract class HttpBase implements Protocol {/** Do we block by IP addresses or by hostnames? */private boolean byIP = true;+ /** Do we use HTTP/1.1? */+ protected boolean useHttp11 = false;/** Creates a new instance of HttpBase */public HttpBase() {@@ -149,6 +151,7 @@ public abstract class HttpBase implements Protocol {this.serverDelay = (long) (conf.getFloat("fetcher.server.delay", 1.0f) * 1000);// backward-compatible default settingthis.byIP = conf.getBoolean("fetcher.threads.per.host.by.ip", true);+ this.useHttp11 = conf.getBoolean("http.http11", false);this.robots.setConf(conf);logConf();}@@ -285,8 +288,11 @@ public abstract class HttpBase implements Protocol {public String getUserAgent() {return userAgent;}--++ public boolean getUseHttp11() {+ return useHttp11;+ }+private String blockAddr(URL url) throws ProtocolException {String host;@@ -428,6 +434,21 @@ public abstract class HttpBase implements Protocol {logger.info("http.max.delays = " + maxDelays);}+ public byte[] processGzipEncoded(byte[] compressed, URL url) throws IOException {+ LOGGER.fine("uncompressing....");++ byte[] content = GZIPUtils.unzipBestEffort(compressed, getMaxContent());++ if (content == null)+ throw new IOException("unzipBestEffort returned null");++ if (LOGGER.isLoggable(Level.FINE))+ LOGGER.fine("fetched " + compressed.length+ + " bytes of compressed content (expanded to "+ + content.length + " bytes) from " + url);+ return content;+ }+protected static void main(HttpBase http, String[] args) throws Exception {boolean verbose = false;String url = null;@@ -475,5 +496,5 @@ public abstract class HttpBase implements Protocol {CrawlDatum datum,boolean followRedirects)throws ProtocolException, IOException;-+}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.javaindex 5c5bebaed..9867ec96d 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@@ -26,9 +26,6 @@ import java.io.PushbackInputStream;import java.net.InetSocketAddress;import java.net.Socket;import java.net.URL;-import java.util.Map;-import java.util.TreeMap;-import java.util.Date;import java.util.logging.Level;// Nutch imports@@ -38,7 +35,6 @@ import org.apache.nutch.net.protocols.Response;import org.apache.nutch.protocol.ProtocolException;import org.apache.nutch.protocol.http.api.HttpBase;import org.apache.nutch.protocol.http.api.HttpException;-import org.apache.nutch.util.GZIPUtils;/** An HTTP response. */@@ -150,18 +146,7 @@ public class HttpResponse implements Response {String contentEncoding = getHeader(Response.CONTENT_ENCODING);if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) {- Http.LOG.fine("uncompressing....");- byte[] compressed = content;-- content = GZIPUtils.unzipBestEffort(compressed, http.getMaxContent());-- if (content == null)- throw new HttpException("unzipBestEffort returned null");-- if (Http.LOG.isLoggable(Level.FINE))- Http.LOG.fine("fetched " + compressed.length- + " bytes of compressed content (expanded to "- + content.length + " bytes) from " + url);+ content = http.processGzipEncoded(content, url);} else {if (Http.LOG.isLoggable(Level.FINE))Http.LOG.fine("fetched " + content.length + " bytes from " + url);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 fb944a760..bf9a24372 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@@ -124,6 +124,8 @@ public class Http extends HttpBase {// prefer understandable formatsheaders.add(new Header("Accept","text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));+ // accept gzipped content+ headers.add(new Header("Accept-Encoding", "x-gzip, gzip"));hostConf.getParams().setParameter("http.default-headers", headers);if (useProxy) {hostConf.setProxy(proxyHost, proxyPort);diff --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 9f9710d99..f3a253013 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@@ -70,8 +70,11 @@ public class HttpResponse implements Response {get.setFollowRedirects(followRedirects);get.setRequestHeader("User-Agent", http.getUserAgent());HttpMethodParams params = get.getParams();- // some servers cannot digest the new protocol- params.setVersion(HttpVersion.HTTP_1_0);+ if (http.getUseHttp11()) {+ params.setVersion(HttpVersion.HTTP_1_1);+ } else {+ params.setVersion(HttpVersion.HTTP_1_0);+ }params.makeLenient();params.setContentCharset("UTF-8");params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);@@ -110,6 +113,13 @@ public class HttpResponse implements Response {if (code == 200) throw new IOException(e.toString());// for codes other than 200 OK, we are fine with empty content}+ if (content != null) {+ // check if we have to uncompress it+ String contentEncoding = headers.get(Response.CONTENT_ENCODING);+ if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) {+ content = http.processGzipEncoded(content, url);+ }+ }} catch (org.apache.commons.httpclient.ProtocolException pe) {pe.printStackTrace();get.releaseConnection();