Ensure X509HostnameVerifier is never null

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1528447 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-10-02 12:41:38 +00:00
parent 01bf4cdbb9
commit d9e2140b12
3 changed files with 30 additions and 24 deletions

View File

@ -361,11 +361,10 @@ public class SSLSocketFactory implements LayeredConnectionSocketFactory, SchemeL
final String[] supportedProtocols,
final String[] supportedCipherSuites,
final X509HostnameVerifier hostnameVerifier) {
Args.notNull(socketfactory, "SSL socket factory");
this.socketfactory = socketfactory;
this.socketfactory = Args.notNull(socketfactory, "SSL socket factory");
this.supportedProtocols = supportedProtocols;
this.supportedCipherSuites = supportedCipherSuites;
this.hostnameVerifier = hostnameVerifier;
this.hostnameVerifier = hostnameVerifier != null ? hostnameVerifier : BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
this.nameResolver = null;
}
@ -556,15 +555,13 @@ public class SSLSocketFactory implements LayeredConnectionSocketFactory, SchemeL
}
private void verifyHostname(final SSLSocket sslsock, final String hostname) throws IOException {
if (this.hostnameVerifier != null) {
try {
this.hostnameVerifier.verify(hostname, sslsock);
// verifyHostName() didn't blowup - good!
} catch (final IOException iox) {
// close the socket before re-throwing the exception
try { sslsock.close(); } catch (final Exception x) { /*ignore*/ }
throw iox;
}
try {
this.hostnameVerifier.verify(hostname, sslsock);
// verifyHostName() didn't blowup - good!
} catch (final IOException iox) {
// close the socket before re-throwing the exception
try { sslsock.close(); } catch (final Exception x) { /*ignore*/ }
throw iox;
}
}

View File

@ -201,11 +201,10 @@ public class SSLConnectionSocketFactory implements LayeredConnectionSocketFactor
final String[] supportedProtocols,
final String[] supportedCipherSuites,
final X509HostnameVerifier hostnameVerifier) {
Args.notNull(socketfactory, "SSL socket factory");
this.socketfactory = socketfactory;
this.socketfactory = Args.notNull(socketfactory, "SSL socket factory");
this.supportedProtocols = supportedProtocols;
this.supportedCipherSuites = supportedCipherSuites;
this.hostnameVerifier = hostnameVerifier;
this.hostnameVerifier = hostnameVerifier != null ? hostnameVerifier : BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
}
/**
@ -283,16 +282,18 @@ public class SSLConnectionSocketFactory implements LayeredConnectionSocketFactor
return sslsock;
}
X509HostnameVerifier getHostnameVerifier() {
return this.hostnameVerifier;
}
private void verifyHostname(final SSLSocket sslsock, final String hostname) throws IOException {
if (this.hostnameVerifier != null) {
try {
this.hostnameVerifier.verify(hostname, sslsock);
// verifyHostName() didn't blowup - good!
} catch (final IOException iox) {
// close the socket before re-throwing the exception
try { sslsock.close(); } catch (final Exception x) { /*ignore*/ }
throw iox;
}
try {
this.hostnameVerifier.verify(hostname, sslsock);
// verifyHostName() didn't blowup - good!
} catch (final IOException iox) {
// close the socket before re-throwing the exception
try { sslsock.close(); } catch (final Exception x) { /*ignore*/ }
throw iox;
}
}

View File

@ -300,4 +300,12 @@ public class TestSSLSocketFactory extends LocalServerTestBase {
socketFactory.connectSocket(0, socket, host, remoteAddress, null, context);
}
@Test
public void testDefaultHostnameVerifier() throws Exception {
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
SSLContexts.createDefault(),
null);
Assert.assertNotNull(socketFactory.getHostnameVerifier());
}
}