Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2729 protocol-okhttp: fix marking of truncated content - avoid integer overflow if http.content.limit == -1

Sebastian Nagel5c45172bc86f
diff --git a/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttpResponse.java b/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttpResponse.java
index f611095cb..5aa70a1bb 100644
--- a/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttpResponse.java
+++ b/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttpResponse.java
@@ -168,9 +168,10 @@ public class OkHttpResponse implements Response {
bytesRequested += Math.min(bufferGrowStepBytes,
/*
* request one byte more than required to reliably detect truncated
- * content
+ * content, but beware of integer overflows
*/
- (1 + maxContentBytes - bytesRequested));
+ (maxContentBytes == Integer.MAX_VALUE ? maxContentBytes
+ : (1 + maxContentBytes)) - bytesRequested);
boolean success = false;
try {
success = source.request(bytesRequested);
diff --git a/src/plugin/protocol-okhttp/src/test/org/apache/nutch/protocol/okhttp/TestBadServerResponses.java b/src/plugin/protocol-okhttp/src/test/org/apache/nutch/protocol/okhttp/TestBadServerResponses.java
index 2f6fd27af..94912e430 100644
--- a/src/plugin/protocol-okhttp/src/test/org/apache/nutch/protocol/okhttp/TestBadServerResponses.java
+++ b/src/plugin/protocol-okhttp/src/test/org/apache/nutch/protocol/okhttp/TestBadServerResponses.java
@@ -383,7 +383,7 @@ public class TestBadServerResponses {
Response.TruncatedContentReason.LENGTH.toString().toLowerCase(),
fetched.getContent().getMetadata().get(Response.TRUNCATED_CONTENT_REASON));
}
- server.close();
+ server.close(); // need to close server before next loop iteration
}
}
@@ -428,8 +428,33 @@ public class TestBadServerResponses {
Response.TruncatedContentReason.LENGTH.toString().toLowerCase(),
fetched.getContent().getMetadata().get(Response.TRUNCATED_CONTENT_REASON));
}
- server.close();
+ server.close(); // need to close server before next loop iteration
}
}
+ @Test
+ public void testNoContentLimit() throws Exception {
+ setUp();
+ conf.setInt("http.content.limit", -1);
+ http.setConf(conf);
+ StringBuilder response = new StringBuilder();
+ response.append(responseHeader);
+ // Even 128 kB content shouldn't cause any truncation because
+ // http.content.limit == -1
+ int kB = 128;
+ response.append("Content-Type: text/plain\r\nContent-Length: " + (kB * 1024)
+ + "\r\n\r\n");
+ for (int i = 0; i < kB; i++) {
+ for (int j = 0; j < 16; j++) {
+ // 16 chunks a 64 bytes = 1 kB
+ response.append(
+ "abcdefghijklmnopqurstuvxyz0123456789-ABCDEFGHIJKLMNOPQURSTUVXYZ\n");
+ }
+ }
+ launchServer(response.toString());
+ ProtocolOutput fetched = fetchPage("/", 200);
+ assertEquals("Content truncated although http.content.limit == -1",
+ (kB * 1024), fetched.getContent().getContent().length);
+ }
+
}