1919import org .asynchttpclient .Request ;
2020import org .glassfish .grizzly .CompletionHandler ;
2121import org .glassfish .grizzly .Connection ;
22+ import org .glassfish .grizzly .EmptyCompletionHandler ;
2223import org .glassfish .grizzly .Grizzly ;
2324import org .glassfish .grizzly .GrizzlyFuture ;
2425import org .glassfish .grizzly .attributes .Attribute ;
2526import org .glassfish .grizzly .connectionpool .EndpointKey ;
2627import org .glassfish .grizzly .filterchain .FilterChainBuilder ;
2728import org .glassfish .grizzly .impl .FutureImpl ;
29+ import org .glassfish .grizzly .ssl .SSLUtils ;
2830import org .glassfish .grizzly .utils .Futures ;
2931import org .glassfish .grizzly .utils .IdleTimeoutFilter ;
3032
33+ import javax .net .ssl .HostnameVerifier ;
34+ import javax .net .ssl .SSLSession ;
3135import java .io .IOException ;
36+ import java .net .ConnectException ;
3237import java .net .InetAddress ;
3338import java .net .InetSocketAddress ;
3439import java .net .SocketAddress ;
@@ -95,28 +100,29 @@ public void doTrackedConnection(final Request request,
95100 throws IOException {
96101 final EndpointKey <SocketAddress > key =
97102 getEndPointKey (request , requestFuture .getProxyServer ());
98-
103+ CompletionHandler <Connection > handler =
104+ wrapHandler (request , getVerifier (), connectHandler );
99105 if (asyncConnect ) {
100- connectionPool .take (key , connectHandler );
106+ connectionPool .take (key , handler );
101107 } else {
102108 IOException ioe = null ;
103109 GrizzlyFuture <Connection > future = connectionPool .take (key );
104110 try {
105111 // No explicit timeout when calling get() here as the Grizzly
106112 // endpoint pool will time it out based on the connect timeout
107113 // setting.
108- connectHandler .completed (future .get ());
114+ handler .completed (future .get ());
109115 } catch (CancellationException e ) {
110- connectHandler .cancelled ();
116+ handler .cancelled ();
111117 } catch (ExecutionException ee ) {
112118 final Throwable cause = ee .getCause ();
113119 if (cause instanceof ConnectionPool .MaxCapacityException ) {
114120 ioe = (IOException ) cause ;
115121 } else {
116- connectHandler .failed (ee .getCause ());
122+ handler .failed (ee .getCause ());
117123 }
118124 } catch (Exception ie ) {
119- connectHandler .failed (ie );
125+ handler .failed (ie );
120126 }
121127 if (ioe != null ) {
122128 throw ioe ;
@@ -136,6 +142,43 @@ public Connection obtainConnection(final Request request,
136142
137143 // --------------------------------------------------Package Private Methods
138144
145+ static CompletionHandler <Connection > wrapHandler (final Request request ,
146+ final HostnameVerifier verifier ,
147+ final CompletionHandler <Connection > delegate ) {
148+ final URI uri = request .getURI ();
149+ if (Utils .isSecure (uri ) && verifier != null ) {
150+ return new EmptyCompletionHandler <Connection >() {
151+ @ Override
152+ public void completed (Connection result ) {
153+ final String host = uri .getHost ();
154+ final SSLSession session = SSLUtils .getSSLEngine (result ).getSession ();
155+ if (!verifier .verify (host , session )) {
156+ failed (new ConnectException ("Host name verification failed for host " + host ));
157+ } else {
158+ delegate .completed (result );
159+ }
160+
161+ }
162+
163+ @ Override
164+ public void cancelled () {
165+ delegate .cancelled ();
166+ }
167+
168+ @ Override
169+ public void failed (Throwable throwable ) {
170+ delegate .failed (throwable );
171+ }
172+
173+ @ Override
174+ public void updated (Connection result ) {
175+ delegate .updated (result );
176+ }
177+ };
178+ }
179+ return delegate ;
180+ }
181+
139182
140183 static void markConnectionAsDoNotCache (final Connection c ) {
141184 DO_NOT_CACHE .set (c , Boolean .TRUE );
@@ -149,6 +192,10 @@ static boolean isConnectionCacheable(final Connection c) {
149192
150193 // --------------------------------------------------------- Private Methods
151194
195+ private HostnameVerifier getVerifier () {
196+ return provider .getClientConfig ().getHostnameVerifier ();
197+ }
198+
152199 private EndpointKey <SocketAddress > getEndPointKey (final Request request ,
153200 final ProxyServer proxyServer ) {
154201 final String stringKey = getPoolKey (request , proxyServer );
0 commit comments