... migrating to HttpComponents-Client-4.x ...

ssl-stuff: accept almost everything

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7097 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
sixcooler 14 years ago
parent d8f52c5b9c
commit ca0a03e9ea

@ -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

@ -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<String, Object> allowProxy = new ConcurrentHashMap<String, Object>();
public static final Map<String, Object> disallowProxy = new ConcurrentHashMap<String, Object>();
/**
* 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;
// }
/**
*

Loading…
Cancel
Save