Skip to content

Commit

Permalink
Create SSL socket in a proper way (fixes #323)
Browse files Browse the repository at this point in the history
  • Loading branch information
igr committed Aug 3, 2016
1 parent 6797275 commit c3b3b50
Showing 1 changed file with 23 additions and 5 deletions.
Expand Up @@ -116,9 +116,10 @@ protected Socket createSocket(String host, int port, int connectionTimeout) thro
* Creates a SSL socket. Enables default secure enabled protocols if specified..
*/
protected SSLSocket createSSLSocket(String host, int port, int connectionTimeout) throws IOException {
SocketFactory socketFactory;
SSLSocketFactory socketFactory;

try {
socketFactory = getSSLSocketFactory(proxy);
socketFactory = (SSLSocketFactory) getSSLSocketFactory(proxy);
}
catch (Exception ex) {
if (ex instanceof IOException) {
Expand All @@ -135,9 +136,26 @@ protected SSLSocket createSSLSocket(String host, int port, int connectionTimeout
}
else {
// creates unconnected socket
sslSocket = (SSLSocket) socketFactory.createSocket();

sslSocket.connect(new InetSocketAddress(host, port), connectionTimeout);
// unfortunately, this does not work always

// sslSocket = (SSLSocket) socketFactory.createSocket();
// sslSocket.connect(new InetSocketAddress(host, port), connectionTimeout);

//
// Note: SSLSocketFactory has several create() methods.
// Those that take arguments all connect immediately
// and have no options for specifying a connection timeout.
//
// So, we have to create a socket and connect it (with a
// connection timeout), then have the SSLSocketFactory wrap
// the already-connected socket.
//
Socket sock = new Socket();
//sock.setSoTimeout(readTimeout);
sock.connect(new InetSocketAddress(host, port), connectionTimeout);

// wrap plain socket in an SSL socket
sslSocket = (SSLSocket)socketFactory.createSocket(sock, host, port, true);
}

String enabledProtocols = JoddHttp.defaultSecureEnabledProtocols;
Expand Down

0 comments on commit c3b3b50

Please sign in to comment.