summaryrefslogtreecommitdiff
path: root/gnu/java/net/protocol
diff options
context:
space:
mode:
authorChris Burdess <dog@bluezoo.org>2004-12-06 09:55:22 +0000
committerChris Burdess <dog@bluezoo.org>2004-12-06 09:55:22 +0000
commit5114d7ac01e55a3aaa5b2b558844d7d7ca32ccc7 (patch)
tree128c7cc06d36fd36b49269320ae84081471ff17b /gnu/java/net/protocol
parent97c3292d9ed402035dfa8c20eef939b7480a89b5 (diff)
downloadclasspath-5114d7ac01e55a3aaa5b2b558844d7d7ca32ccc7.tar.gz
2004-12-05 Chris Burdess <dog@gnu.org>
* gnu/java/net/protocol/http/HTTPConnection.java, gnu/java/net/protocol/http/HTTPURLConnection.java: HTTPURLConnection now derives from HttpsURLConnection.
Diffstat (limited to 'gnu/java/net/protocol')
-rw-r--r--gnu/java/net/protocol/http/HTTPConnection.java72
-rw-r--r--gnu/java/net/protocol/http/HTTPURLConnection.java74
2 files changed, 134 insertions, 12 deletions
diff --git a/gnu/java/net/protocol/http/HTTPConnection.java b/gnu/java/net/protocol/http/HTTPConnection.java
index 62518cd2e..42ca4bd22 100644
--- a/gnu/java/net/protocol/http/HTTPConnection.java
+++ b/gnu/java/net/protocol/http/HTTPConnection.java
@@ -38,6 +38,7 @@ exception statement from your version. */
package gnu.java.net.protocol.http;
+import gnu.classpath.Configuration;
import gnu.java.net.protocol.http.event.ConnectionEvent;
import gnu.java.net.protocol.http.event.ConnectionListener;
import gnu.java.net.protocol.http.event.RequestEvent;
@@ -55,9 +56,11 @@ import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.net.SocketFactory;
+import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
@@ -87,7 +90,9 @@ public class HTTPConnection
{
try
{
- StringBuffer buf = new StringBuffer("inetlib/1.1 (");
+ StringBuffer buf = new StringBuffer("classpath/");
+ buf.append(Configuration.CLASSPATH_VERSION);
+ buf.append(" (");
buf.append(System.getProperty("os.name"));
buf.append("; ");
buf.append(System.getProperty("os.arch"));
@@ -149,6 +154,7 @@ public class HTTPConnection
private final List connectionListeners;
private final List requestListeners;
+ private final List handshakeCompletedListeners;
/**
* The socket this connection communicates on.
@@ -156,6 +162,11 @@ public class HTTPConnection
protected Socket socket;
/**
+ * The SSL socket factory to use.
+ */
+ private SSLSocketFactory sslSocketFactory;
+
+ /**
* The socket input stream.
*/
protected InputStream in;
@@ -246,8 +257,9 @@ public class HTTPConnection
this.connectionTimeout = connectionTimeout;
this.timeout = timeout;
majorVersion = minorVersion = 1;
- connectionListeners = Collections.synchronizedList(new ArrayList(4));
- requestListeners = Collections.synchronizedList(new ArrayList(4));
+ connectionListeners = new ArrayList(4);
+ requestListeners = new ArrayList(4);
+ handshakeCompletedListeners = new ArrayList(2);
}
/**
@@ -434,17 +446,27 @@ public class HTTPConnection
{
try
{
- TrustManager tm = new EmptyX509TrustManager();
- SSLContext context = SSLContext.getInstance("SSL");
- TrustManager[] trust = new TrustManager[] { tm };
- context.init(null, trust, null);
- SSLSocketFactory factory = context.getSocketFactory();
+ SSLSocketFactory factory = getSSLSocketFactory();
SSLSocket ss =
(SSLSocket) factory.createSocket(socket, connectHostname,
connectPort, true);
String[] protocols = { "TLSv1", "SSLv3" };
ss.setEnabledProtocols(protocols);
ss.setUseClientMode(true);
+ synchronized (handshakeCompletedListeners)
+ {
+ if (!handshakeCompletedListeners.isEmpty())
+ {
+ for (Iterator i =
+ handshakeCompletedListeners.iterator();
+ i.hasNext(); )
+ {
+ HandshakeCompletedListener l =
+ (HandshakeCompletedListener) i.next();
+ ss.addHandshakeCompletedListener(l);
+ }
+ }
+ }
ss.startHandshake();
socket = ss;
}
@@ -461,6 +483,25 @@ public class HTTPConnection
return socket;
}
+ SSLSocketFactory getSSLSocketFactory()
+ throws GeneralSecurityException
+ {
+ if (sslSocketFactory == null)
+ {
+ TrustManager tm = new EmptyX509TrustManager();
+ SSLContext context = SSLContext.getInstance("SSL");
+ TrustManager[] trust = new TrustManager[] { tm };
+ context.init(null, trust, null);
+ sslSocketFactory = context.getSocketFactory();
+ }
+ return sslSocketFactory;
+ }
+
+ void setSSLSocketFactory(SSLSocketFactory factory)
+ {
+ sslSocketFactory = factory;
+ }
+
protected InputStream getInputStream()
throws IOException
{
@@ -634,5 +675,20 @@ public class HTTPConnection
}
}
+ void addHandshakeCompletedListener(HandshakeCompletedListener l)
+ {
+ synchronized (handshakeCompletedListeners)
+ {
+ handshakeCompletedListeners.add(l);
+ }
+ }
+ void removeHandshakeCompletedListener(HandshakeCompletedListener l)
+ {
+ synchronized (handshakeCompletedListeners)
+ {
+ handshakeCompletedListeners.remove(l);
+ }
+ }
+
}
diff --git a/gnu/java/net/protocol/http/HTTPURLConnection.java b/gnu/java/net/protocol/http/HTTPURLConnection.java
index 3a3930292..7995bc213 100644
--- a/gnu/java/net/protocol/http/HTTPURLConnection.java
+++ b/gnu/java/net/protocol/http/HTTPURLConnection.java
@@ -43,16 +43,22 @@ import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
-import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
+import java.security.cert.Certificate;
import java.util.Date;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
+import javax.net.ssl.HandshakeCompletedEvent;
+import javax.net.ssl.HandshakeCompletedListener;
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLPeerUnverifiedException;
+import javax.net.ssl.SSLSocketFactory;
/**
* A URLConnection that uses the HTTPConnection class.
@@ -60,7 +66,8 @@ import java.util.Map;
* @author Chris Burdess (dog@gnu.org)
*/
public class HTTPURLConnection
- extends HttpURLConnection
+ extends HttpsURLConnection
+ implements HandshakeCompletedListener
{
/*
@@ -79,11 +86,14 @@ public class HTTPURLConnection
private Response response;
private ByteArrayInputStream responseSink;
+ private HandshakeCompletedEvent handshakeEvent;
+
/**
* Constructor.
* @param url the URL
*/
public HTTPURLConnection(URL url)
+ throws IOException
{
super(url);
requestHeaders = new Headers();
@@ -97,10 +107,18 @@ public class HTTPURLConnection
public Object run()
{
proxyHostname = System.getProperty("http.proxyHost");
- if (proxyHostname != null)
+ if (proxyHostname != null && proxyHostname.length() > 0)
{
String port = System.getProperty("http.proxyPort");
- proxyPort = (port != null) ? Integer.parseInt (port) : -1;
+ if (port != null && port.length() > 0)
+ {
+ proxyPort = Integer.parseInt (port);
+ }
+ else
+ {
+ proxyHostname = null;
+ proxyPort = -1;
+ }
}
return null;
}
@@ -145,6 +163,17 @@ public class HTTPURLConnection
if (connection == null)
{
connection = new HTTPConnection(host, port, secure);
+ if (secure)
+ {
+ SSLSocketFactory factory = getSSLSocketFactory();
+ HostnameVerifier verifier = getHostnameVerifier();
+ if (factory != null)
+ {
+ connection.setSSLSocketFactory(factory);
+ }
+ connection.addHandshakeCompletedListener(this);
+ // TODO verifier
+ }
}
if (proxyHostname != null)
{
@@ -515,5 +544,42 @@ public class HTTPURLConnection
return response.getMessage();
}
+ // -- HTTPS specific --
+
+ public String getCipherSuite()
+ {
+ if (!connected)
+ {
+ throw new IllegalStateException("not connected");
+ }
+ return handshakeEvent.getCipherSuite();
+ }
+
+ public Certificate[] getLocalCertificates()
+ {
+ if (!connected)
+ {
+ throw new IllegalStateException("not connected");
+ }
+ return handshakeEvent.getLocalCertificates();
+ }
+
+ public Certificate[] getServerCertificates()
+ throws SSLPeerUnverifiedException
+ {
+ if (!connected)
+ {
+ throw new IllegalStateException("not connected");
+ }
+ return handshakeEvent.getPeerCertificates();
+ }
+
+ // HandshakeCompletedListener
+
+ public void handshakeCompleted(HandshakeCompletedEvent event)
+ {
+ handshakeEvent = event;
+ }
+
}