diff --git a/httpclient/src/main/java-deprecated/org/apache/http/conn/ssl/SSLSocketFactory.java b/httpclient/src/main/java-deprecated/org/apache/http/conn/ssl/SSLSocketFactory.java index 21f625169..ecf2e9b60 100644 --- a/httpclient/src/main/java-deprecated/org/apache/http/conn/ssl/SSLSocketFactory.java +++ b/httpclient/src/main/java-deprecated/org/apache/http/conn/ssl/SSLSocketFactory.java @@ -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; } } diff --git a/httpclient/src/main/java/org/apache/http/conn/ssl/SSLConnectionSocketFactory.java b/httpclient/src/main/java/org/apache/http/conn/ssl/SSLConnectionSocketFactory.java index 3e2dd618c..a82deaf67 100644 --- a/httpclient/src/main/java/org/apache/http/conn/ssl/SSLConnectionSocketFactory.java +++ b/httpclient/src/main/java/org/apache/http/conn/ssl/SSLConnectionSocketFactory.java @@ -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; } } diff --git a/httpclient/src/test/java/org/apache/http/conn/ssl/TestSSLSocketFactory.java b/httpclient/src/test/java/org/apache/http/conn/ssl/TestSSLSocketFactory.java index d5df79e8d..7258c840c 100644 --- a/httpclient/src/test/java/org/apache/http/conn/ssl/TestSSLSocketFactory.java +++ b/httpclient/src/test/java/org/apache/http/conn/ssl/TestSSLSocketFactory.java @@ -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()); + } + }