Apache Nutch is an extensible and scalable web crawler
NUTCH-2558 protocol-http cannot handle a missing HTTP status line NUTCH-2561 protocol-http can be made to read arbitrarily large HTTP responses - if parsing HTTP status line fails: log warning, push back input, assume status 200 OK (patch contributed by Gerard Bouchar) - limit max. length of HTTP header lines - 2 kB for status line - Http.BUFFER_SIZE (8 kB) for HTTP header field lines - throw exception if header line is longer than limit - fix encoding when pushing back content not accepted as HTTP header - push back also line separator (do not change binary content)
Sebastian Nagel146a76ce000f
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 92430ba11..1092d576f 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@@ -18,7 +18,6 @@ package org.apache.nutch.protocol.http;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;-import java.io.EOFException;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;@@ -26,6 +25,7 @@ import java.io.PushbackInputStream;import java.net.InetSocketAddress;import java.net.Socket;import java.net.URL;+import java.nio.charset.StandardCharsets;import java.util.Arrays;import java.util.HashSet;import java.util.Set;@@ -243,6 +243,7 @@ public class HttpResponse implements Response {Http.BUFFER_SIZE), Http.BUFFER_SIZE);StringBuffer line = new StringBuffer();+ StringBuffer lineSeparator = new StringBuffer();// store the http headers verbatimif (http.isStoreHttpHeaders()) {@@ -254,7 +255,16 @@ public class HttpResponse implements Response {boolean haveSeenNonContinueStatus = false;while (!haveSeenNonContinueStatus) {// parse status code line- this.code = parseStatusLine(in, line);+ try {+ this.code = parseStatusLine(in, line, lineSeparator);+ } catch(HttpException e) {+ Http.LOG.warn("Missing or invalid HTTP status line", e);+ Http.LOG.warn("No HTTP header, assuming HTTP/0.9 for {}", getUrl());+ this.code = 200;+ in.unread(lineSeparator.toString().getBytes(StandardCharsets.ISO_8859_1));+ in.unread(line.toString().getBytes(StandardCharsets.ISO_8859_1));+ break;+ }if (httpHeaders != null)httpHeaders.append(line).append("\n");// parse headers@@ -496,9 +506,9 @@ public class HttpResponse implements Response {}- private int parseStatusLine(PushbackInputStream in, StringBuffer line)- throws IOException, HttpException {- readLine(in, line, false);+ private int parseStatusLine(PushbackInputStream in, StringBuffer line,+ StringBuffer lineSeparator) throws IOException, HttpException {+ readLine(in, line, false, 2048, lineSeparator);int codeStart = line.indexOf(" ");int codeEnd;@@ -514,7 +524,7 @@ public class HttpResponse implements Response {try {return Integer.parseInt(line.substring(codeStart + 1, codeEnd));} catch (NumberFormatException e) {- throw new HttpException("bad status line '" + line + "'" + e.getMessage(), e);+ throw new HttpException("Bad status line, no HTTP response code: " + line, e);}}@@ -553,7 +563,7 @@ public class HttpResponse implements Response {(pos = line.indexOf("<HTML")) != -1) || ((pos = line.indexOf("<html"))!= -1)) {- in.unread(line.substring(pos).getBytes("UTF-8"));+ in.unread(line.substring(pos).getBytes(StandardCharsets.ISO_8859_1));line.setLength(pos);try {@@ -576,14 +586,31 @@ public class HttpResponse implements Response {private static int readLine(PushbackInputStream in, StringBuffer line,boolean allowContinuedLine) throws IOException {+ return readLine(in, line, allowContinuedLine, Http.BUFFER_SIZE, null);+ }++ private static int readLine(PushbackInputStream in, StringBuffer line,+ boolean allowContinuedLine, int maxBytes, StringBuffer lineSeparator) throws IOException {line.setLength(0);- for (int c = in.read(); c != -1; c = in.read()) {+ int bytesRead = 0;+ for (int c = in.read(); c != -1+ && bytesRead < maxBytes; c = in.read(), bytesRead++) {switch (c) {case '\r':+ if (lineSeparator != null) {+ lineSeparator.append((char) c);+ }if (peek(in) == '\n') {in.read();+ if (lineSeparator != null) {+ lineSeparator.append((char) c);+ }}+ // fall-throughcase '\n':+ if (lineSeparator != null) {+ lineSeparator.append((char) c);+ }if (line.length() > 0) {// at EOL -- check for continued line if the current// (possibly continued) line wasn't blank@@ -592,6 +619,9 @@ public class HttpResponse implements Response {case ' ':case '\t': // line is continuedin.read();+ if (lineSeparator != null) {+ lineSeparator.replace(0, lineSeparator.length(), "");+ }continue;}}@@ -600,7 +630,11 @@ public class HttpResponse implements Response {line.append((char) c);}}- throw new EOFException();+ if (bytesRead >= maxBytes) {+ throw new IOException("Line exceeds max. buffer size: "+ + line.substring(0, Math.min(32, line.length())));+ }+ return line.length();}private static int peek(PushbackInputStream in) throws IOException {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.javaindex 1f9606777..844ee90c3 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@@ -19,11 +19,8 @@ package org.apache.nutch.protocol.http;import static org.junit.Assert.assertEquals;-import java.io.BufferedOutputStream;import java.io.BufferedReader;-import java.io.IOException;import java.io.InputStreamReader;-import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.lang.invoke.MethodHandles;@@ -36,11 +33,8 @@ import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.hadoop.conf.Configuration;-import org.apache.hadoop.io.Text;import org.apache.nutch.crawl.CrawlDatum;import org.apache.nutch.net.protocols.Response;-import org.apache.nutch.protocol.Content;-import org.apache.nutch.protocol.ProtocolOutput;import org.junit.After;import org.junit.Test;import org.slf4j.Logger;@@ -192,8 +186,50 @@ public class TestBadServerResponses {setUp();launchServer("HTTP/1.1 200: OK\r\n" + simpleContent);fetchPage("/", 200);+ }++ /**+ * NUTCH-2558 protocol-http cannot handle a missing HTTP status line+ */+ @Test+ public void testNoStatusLine() throws Exception {+ setUp();+ String text = "This is a text containing non-ASCII characters: \u00e4\u00f6\u00fc\u00df";+ launchServer(text);+ Response fetched = fetchPage("/", 200);+ assertEquals("Wrong text returned for response with no status line.", text,+ new String(fetched.getContent(), StandardCharsets.UTF_8));server.close();- launchServer("HTTP/1.1 \u0966\u0967\u0968: OK\r\n" + simpleContent);+ text = "<!DOCTYPE html>\n<html>\n<head>\n"+ + "<title>Testing no HTTP header èéâ</title>\n"+ + "<meta charset=\"utf-8\">\n"+ + "</head>\n<body>This is a text containing non-ASCII characters:"+ + "\u00e4\u00f6\u00fc\u00df</body>\n</html";+ launchServer(text);+ fetched = fetchPage("/", 200);+ assertEquals("Wrong text returned for response with no status line.", text,+ new String(fetched.getContent(), StandardCharsets.UTF_8));+ }++ /**+ * NUTCH-2561 protocol-http can be made to read arbitrarily large HTTP+ * responses+ */+ @Test(expected = Exception.class)+ public void testOverlongHeader() throws Exception {+ setUp();+ StringBuilder response = new StringBuilder();+ response.append(responseHeader);+ for (int i = 0; i < 80; i++) {+ response.append("X-Custom-Header-");+ for (int j = 0; j < 10000; j++) {+ response.append('x');+ }+ response.append(": hello\r\n");+ }+ response.append("\r\n" + simpleContent);+ launchServer(response.toString());+ // should throw exception because of overlong headerfetchPage("/", 200);}