Apache Nutch is an extensible and scalable web crawler
NUTCH-2555 URL normalization problem: path not starting with a '/' For URLs with query and an empty path (http://example.com?a=1): - fix urlnormalizer-basic to add the missing slash (http://example.com/?a=1) - fix protocol-http to send a correct "GET /?a=1 ..." request
Sebastian Nagel6239655b6fd9
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 c0a3c01b0..a4b6275b1 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@@ -90,7 +90,10 @@ public class HttpResponse implements Response {Http.LOG.trace("fetching " + url);}- String path = "".equals(url.getFile()) ? "/" : url.getFile();+ String path = url.getFile();+ if (!path.startsWith("/")) {+ path = "/" + path;+ }// some servers will redirect a request with a host line like// "Host: <hostname>:80" to "http://<hpstname>/<orig_path>"- theydiff --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 a09de6079..10de27c9e 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@@ -32,6 +32,8 @@ import java.net.ServerSocket;import java.net.Socket;import java.net.URL;import java.nio.charset.StandardCharsets;+import java.util.regex.Matcher;+import java.util.regex.Pattern;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.io.Text;@@ -86,6 +88,7 @@ public class TestBadServerResponses {private void runServer(int port, String response) throws Exception {server = new ServerSocket();server.bind(new InetSocketAddress("127.0.0.1", port));+ Pattern requestPattern = Pattern.compile("(?i)^GET\\s+(\\S+)");while (true) {LOG.info("Listening on port {}", port);Socket socket = server.accept();@@ -102,6 +105,13 @@ public class TestBadServerResponses {if (line.trim().isEmpty()) {break;}+ Matcher m = requestPattern.matcher(line);+ if (m.find()) {+ LOG.info("Requested {}", m.group(1));+ if (!m.group(1).startsWith("/")) {+ response = "HTTP/1.1 400 Bad request\r\n\r\n";+ }+ }}LOG.info("Response: {}",response.substring(0, Math.min(1024, response.length())));@@ -139,8 +149,6 @@ public class TestBadServerResponses {LOG.info("Fetching {}", url);CrawlDatum crawlDatum = new CrawlDatum();Response response = http.getResponse(url, crawlDatum, true);- ProtocolOutput out = http.getProtocolOutput(new Text(url.toString()),- crawlDatum);assertEquals("HTTP Status Code for " + url, expectedCode,response.getCode());return response;@@ -154,6 +162,16 @@ public class TestBadServerResponses {fetchPage("/", 200);}+ /**+ * NUTCH-2555 URL normalization problem: path not starting with a '/'+ */+ @Test+ public void testRequestNotStartingWithSlash() throws Exception {+ setUp();+ launchServer(responseHeader + simpleContent);+ fetchPage("?171", 200);+ }+/*** NUTCH-2562 protocol-http fails to read large chunked HTTP responses,* NUTCH-2575 protocol-http does not respect the maximum content-size fordiff --git a/src/plugin/urlnormalizer-basic/src/java/org/apache/nutch/net/urlnormalizer/basic/BasicURLNormalizer.java b/src/plugin/urlnormalizer-basic/src/java/org/apache/nutch/net/urlnormalizer/basic/BasicURLNormalizer.javaindex b6033aef4..d2cd0cec6 100644--- a/src/plugin/urlnormalizer-basic/src/java/org/apache/nutch/net/urlnormalizer/basic/BasicURLNormalizer.java+++ b/src/plugin/urlnormalizer-basic/src/java/org/apache/nutch/net/urlnormalizer/basic/BasicURLNormalizer.java@@ -132,21 +132,25 @@ public class BasicURLNormalizer extends Configured implements URLNormalizer {changed = true;}- if (file == null || "".equals(file)) { // add a slash+ if (file == null || "".equals(file)) {file = "/";changed = true;+ } else if (!file.startsWith("/")) {+ file = "/" + file;+ changed = true;+ } else {+ // check for unnecessary use of "/../", "/./", and "//"+ String file2 = getFileWithNormalizedPath(url);+ if (!file.equals(file2)) {+ changed = true;+ file = file2;+ }}if (url.getRef() != null) { // remove the refchanged = true;}- // check for unnecessary use of "/../", "/./", and "//"- String file2 = getFileWithNormalizedPath(url);- if (!file.equals(file2)) {- changed = true;- file = file2;- }}// properly encode characters in path/file using percent-encoding@@ -191,6 +195,8 @@ public class BasicURLNormalizer extends Configured implements URLNormalizer {// if path is empty return a single slashif (file.isEmpty()) {file = "/";+ } else if (!file.startsWith("/")) {+ file = "/" + file;}return file;diff --git a/src/plugin/urlnormalizer-basic/src/test/org/apache/nutch/net/urlnormalizer/basic/TestBasicURLNormalizer.java b/src/plugin/urlnormalizer-basic/src/test/org/apache/nutch/net/urlnormalizer/basic/TestBasicURLNormalizer.javaindex 5cefbf384..a656a7aa4 100644--- a/src/plugin/urlnormalizer-basic/src/test/org/apache/nutch/net/urlnormalizer/basic/TestBasicURLNormalizer.java+++ b/src/plugin/urlnormalizer-basic/src/test/org/apache/nutch/net/urlnormalizer/basic/TestBasicURLNormalizer.java@@ -170,6 +170,8 @@ public class TestBasicURLNormalizer {normalizeTest("http:", "http:/");normalizeTest("http:////", "http:/");normalizeTest("http:///////", "http:/");+ // NUTCH-2555 path must start with '/'+ normalizeTest("http://example.com?a=1", "http://example.com/?a=1");}@Test