Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-3106 fix Issue with SSLHandshakeException in v1.20 using protocol-http plugin

Hanbing Luoaca19bbb7ca7
diff --git a/ivy/ivy.xml b/ivy/ivy.xml
index f402d8365..e3fc1c2fc 100644
--- a/ivy/ivy.xml
+++ b/ivy/ivy.xml
@@ -135,6 +135,7 @@
<dependency org="org.apache.httpcomponents" name="httpcore" rev="4.4.14" />
<dependency org="de.vandermeer" name="asciitable" rev="0.3.2" />
+ <dependency org="org.littleshoot" name="littleproxy" rev="1.1.2" conf="test->default"/>
<!--global exclusion -->
<exclude module="jmxtools" />
diff --git a/src/plugin/protocol-http/ivy.xml b/src/plugin/protocol-http/ivy.xml
index aaed5221e..29871da12 100644
--- a/src/plugin/protocol-http/ivy.xml
+++ b/src/plugin/protocol-http/ivy.xml
@@ -37,6 +37,8 @@
</publications>
<dependencies>
+ <dependency org="org.littleshoot" name="littleproxy" rev="1.1.2" />
+ <dependency org="com.google.guava" name="guava" rev="20.0" force="true" />
</dependencies>
</ivy-module>
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 48918dc51..d53ae823a 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
@@ -23,6 +23,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.net.InetSocketAddress;
+import java.net.Proxy;
import java.net.Socket;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@@ -119,20 +120,26 @@ public class HttpResponse implements Response {
Socket socket = null;
try {
- socket = new Socket(); // create the socket
+ boolean useProxy = http.useProxy(url);
+ if (useProxy) {
+ Proxy proxy = new Proxy(Proxy.Type.HTTP,
+ new InetSocketAddress(http.getProxyHost(), http.getProxyPort()));
+ socket = new Socket(proxy);
+ } else {
+ socket = new Socket();
+ }
socket.setSoTimeout(http.getTimeout());
// connect
- String sockHost = http.useProxy(url) ? http.getProxyHost() : host;
int sockPort = http.useProxy(url) ? http.getProxyPort() : port;
- InetSocketAddress sockAddr = new InetSocketAddress(sockHost, sockPort);
+ InetSocketAddress sockAddr = new InetSocketAddress(host, port);
socket.connect(sockAddr, http.getTimeout());
if (scheme == Scheme.HTTPS) {
SSLSocket sslsocket = null;
try {
- sslsocket = getSSLSocket(socket, sockHost, sockPort);
+ sslsocket = getSSLSocket(socket, host, port);
sslsocket.startHandshake();
} catch (Exception e) {
Http.LOG.debug("SSL connection to {} failed with: {}", url,
diff --git a/src/plugin/protocol-http/src/test/org/apache/nutch/protocol/http/TestProtocolHttpByProxy.java b/src/plugin/protocol-http/src/test/org/apache/nutch/protocol/http/TestProtocolHttpByProxy.java
new file mode 100644
index 000000000..16d354818
--- /dev/null
+++ b/src/plugin/protocol-http/src/test/org/apache/nutch/protocol/http/TestProtocolHttpByProxy.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nutch.protocol.http;
+
+import org.apache.hadoop.io.Text;
+import org.apache.nutch.crawl.CrawlDatum;
+import org.apache.nutch.protocol.AbstractHttpProtocolPluginTest;
+import org.apache.nutch.protocol.Content;
+import org.apache.nutch.protocol.ProtocolOutput;
+import org.apache.nutch.protocol.ProtocolStatus;
+import org.junit.Before;
+import org.junit.Test;
+import org.littleshoot.proxy.HttpProxyServer;
+import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test cases for protocol-http by proxy
+ */
+public class TestProtocolHttpByProxy extends AbstractHttpProtocolPluginTest {
+
+ public static final String PROXY_HOST = "localhost";
+ public static final Integer PROXY_PORT = 8888;
+
+ public static final String TARGET_HOST = "www.baidu.com";
+ public static final Integer TARGET_PORT = 443;
+
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ conf.set("http.proxy.host", PROXY_HOST);
+ conf.set("http.proxy.port", PROXY_PORT.toString());
+ http.setConf(conf);
+
+ HttpProxyServer server = DefaultHttpProxyServer.bootstrap()
+ .withPort(PROXY_PORT).start();
+ }
+
+ @Override
+ protected String getPluginClassName() {
+ return "org.apache.nutch.protocol.http.Http";
+ }
+
+ @Test
+ public void testRequestByProxy() throws Exception {
+ Http httpObj = new Http();
+ httpObj.setConf(conf);
+
+ String url = "https://" + TARGET_HOST;
+ ProtocolOutput out = httpObj.getProtocolOutput(new Text(url),
+ new CrawlDatum());
+ assertNotNull(out);
+
+ ProtocolStatus status = out.getStatus();
+ assertNotNull(status);
+ assertTrue(status.isSuccess());
+
+ Content content = out.getContent();
+ assertNotNull(content);
+ assertTrue(content.toString().length() > 250);
+ }
+}