Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2557 protocol-http fails to follow redirections when HTTP response body is invalid (patch contributed by Gerard Bouchar) - catch exceptions while reading payload - if response code is not "200 OK": ignore exception but reset content

Sebastian Nageld163512d5d2e
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 1092d576f..b5b7dca65 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
@@ -272,30 +272,46 @@ public class HttpResponse implements Response {
haveSeenNonContinueStatus = code != 100; // 100 is "Continue"
}
- String transferEncoding = getHeader(Response.TRANSFER_ENCODING);
- if (transferEncoding != null && "chunked"
- .equalsIgnoreCase(transferEncoding.trim())) {
- readChunkedContent(in, line);
- } else {
- readPlainContent(in);
- }
+ try {
+ String transferEncoding = getHeader(Response.TRANSFER_ENCODING);
+ if (transferEncoding != null
+ && "chunked".equalsIgnoreCase(transferEncoding.trim())) {
+ readChunkedContent(in, line);
+ } else {
+ readPlainContent(in);
+ }
- String contentEncoding = getHeader(Response.CONTENT_ENCODING);
- if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) {
- content = http.processGzipEncoded(content, url);
- } else if ("deflate".equals(contentEncoding)) {
- content = http.processDeflateEncoded(content, url);
- } else {
- // store the headers verbatim only if the response was not compressed
- // as the content length reported does not match otherwise
- if (httpHeaders != null) {
- headers.add(Response.RESPONSE_HEADERS, httpHeaders.toString());
+ String contentEncoding = getHeader(Response.CONTENT_ENCODING);
+ if ("gzip".equals(contentEncoding)
+ || "x-gzip".equals(contentEncoding)) {
+ content = http.processGzipEncoded(content, url);
+ } else if ("deflate".equals(contentEncoding)) {
+ content = http.processDeflateEncoded(content, url);
+ } else {
+ // store the headers verbatim only if the response was not compressed
+ // as the content length reported does not match otherwise
+ if (httpHeaders != null) {
+ headers.add(Response.RESPONSE_HEADERS, httpHeaders.toString());
+ }
+ if (Http.LOG.isTraceEnabled()) {
+ Http.LOG.trace("fetched " + content.length + " bytes from " + url);
+ }
}
- if (Http.LOG.isTraceEnabled()) {
- Http.LOG.trace("fetched " + content.length + " bytes from " + url);
+ } catch (IOException | HttpException e) {
+ // Headers parsing went fine, but an error occurred while trying to read
+ // the body of the request (the body may be malformed)
+ if (code != 200) {
+ Http.LOG.warn(
+ "Ignored exception while reading payload of response with status code "
+ + code + ":",
+ e);
+ content = null;
+ } else {
+ // If the page is a "200 OK" response, we do not want to go further
+ // with processing the invalid payload.
+ throw e;
}
}
-
} finally {
if (socket != null)
socket.close();
diff --git a/src/plugin/protocol-http/src/test/org/apache/nutch/protocol/http/TestBadServerResponses.java b/src/plugin/protocol-http/src/test/org/apache/nutch/protocol/http/TestBadServerResponses.java
index bfc39c25c..77bc70157 100644
--- a/src/plugin/protocol-http/src/test/org/apache/nutch/protocol/http/TestBadServerResponses.java
+++ b/src/plugin/protocol-http/src/test/org/apache/nutch/protocol/http/TestBadServerResponses.java
@@ -18,6 +18,7 @@
package org.apache.nutch.protocol.http;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import java.io.BufferedReader;
import java.io.InputStreamReader;
@@ -200,6 +201,21 @@ public class TestBadServerResponses {
fetchPage("/", 200);
}
+ /**
+ * NUTCH-2557 protocol-http fails to follow redirections when an HTTP response
+ * body is invalid
+ */
+ @Test
+ public void testIgnoreErrorInRedirectPayload() throws Exception {
+ setUp();
+ launchServer("HTTP/1.1 302 Found\r\nLocation: http://example.com/\r\n"
+ + "Transfer-Encoding: chunked\r\n\r\nNot a valid chunk.");
+ Response fetched = fetchPage("/", 302);
+ assertNotNull("No redirect Location.", fetched.getHeader("Location"));
+ assertEquals("Wrong redirect Location.", "http://example.com/",
+ fetched.getHeader("Location"));
+ }
+
/**
* NUTCH-2558 protocol-http cannot handle a missing HTTP status line
*/