diff --git a/source/net/yacy/cora/protocol/http/HTTPClient.java b/source/net/yacy/cora/protocol/http/HTTPClient.java index 693e74b95..0b7b8c0ca 100644 --- a/source/net/yacy/cora/protocol/http/HTTPClient.java +++ b/source/net/yacy/cora/protocol/http/HTTPClient.java @@ -1,5 +1,5 @@ /** - * Client + * HTTPClient * Copyright 2010 by Sebastian Gaebel * First released 01.07.2010 at http://yacy.net * @@ -29,11 +29,19 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; import java.util.LinkedHashMap; import java.util.Set; import java.util.Map.Entry; import java.util.concurrent.TimeUnit; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + import net.yacy.cora.protocol.ConnectionInfo; import org.apache.http.Header; @@ -41,7 +49,6 @@ import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; -import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; @@ -104,6 +111,9 @@ public class HTTPClient { } public static void setDefaultUserAgent(final String defaultAgent) { + if (httpClient == null) { + initConnectionManager(); + } HttpProtocolParams.setUserAgent(httpClient.getParams(), defaultAgent); } @@ -152,7 +162,7 @@ public class HTTPClient { // Create and initialize scheme registry final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); - schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); + schemeRegistry.register(new Scheme("https", getSSLSocketFactory(), 443)); ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry); @@ -465,7 +475,7 @@ public class HTTPClient { try { // execute the method httpResponse = httpClient.execute(httpUriRequest, httpContext); - } catch (ClientProtocolException e) { + } catch (Exception e) { ConnectionInfo.removeConnection(httpUriRequest.hashCode()); httpUriRequest.abort(); throw new IOException("Client can't execute: " + e.getMessage()); @@ -539,6 +549,40 @@ public class HTTPClient { public static String getSystemOST() { return systemOST; } + + private static SSLSocketFactory getSSLSocketFactory() { + final TrustManager trustManager = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) + throws CertificateException { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) + throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + }; + SSLContext sslContext = null; + try { + sslContext = SSLContext.getInstance("TLS"); + sslContext.init(null, new TrustManager[] { trustManager }, null); + } catch (NoSuchAlgorithmException e) { + // should not happen + // e.printStackTrace(); + } catch (KeyManagementException e) { + // should not happen + // e.printStackTrace(); + } + + final SSLSocketFactory sslSF = new SSLSocketFactory(sslContext); + sslSF.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + return sslSF; + } /** * testing diff --git a/source/net/yacy/cora/protocol/http/ProxySettings.java b/source/net/yacy/cora/protocol/http/ProxySettings.java index 3d6e729ce..493ec95cb 100644 --- a/source/net/yacy/cora/protocol/http/ProxySettings.java +++ b/source/net/yacy/cora/protocol/http/ProxySettings.java @@ -23,8 +23,8 @@ package net.yacy.cora.protocol.http; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import org.apache.commons.httpclient.HostConfiguration; -import org.apache.commons.httpclient.HttpClient; +//import org.apache.commons.httpclient.HostConfiguration; +//import org.apache.commons.httpclient.HttpClient; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; @@ -46,18 +46,18 @@ public final class ProxySettings { public static final Map allowProxy = new ConcurrentHashMap(); public static final Map disallowProxy = new ConcurrentHashMap(); - /** - * produce a HostConfiguration (apache object) with the proxy access information included - * @param apacheHttpClient - * @return a host configuration with proxy config if the proxy shall be used; a cloned configuration otherwise - */ - public static HostConfiguration getProxyHostConfig(HttpClient apacheHttpClient) { - final HostConfiguration hostConfig; - if (!use) return null; - hostConfig = new HostConfiguration(apacheHttpClient.getHostConfiguration()); - hostConfig.setProxy(host, port); - return hostConfig; - } +// /** +// * produce a HostConfiguration (apache object) with the proxy access information included +// * @param apacheHttpClient +// * @return a host configuration with proxy config if the proxy shall be used; a cloned configuration otherwise +// */ +// public static HostConfiguration getProxyHostConfig(HttpClient apacheHttpClient) { +// final HostConfiguration hostConfig; +// if (!use) return null; +// hostConfig = new HostConfiguration(apacheHttpClient.getHostConfiguration()); +// hostConfig.setProxy(host, port); +// return hostConfig; +// } /** *