19
19
import org .asynchttpclient .Request ;
20
20
import org .glassfish .grizzly .CompletionHandler ;
21
21
import org .glassfish .grizzly .Connection ;
22
+ import org .glassfish .grizzly .EmptyCompletionHandler ;
22
23
import org .glassfish .grizzly .Grizzly ;
23
24
import org .glassfish .grizzly .GrizzlyFuture ;
24
25
import org .glassfish .grizzly .attributes .Attribute ;
25
26
import org .glassfish .grizzly .connectionpool .EndpointKey ;
26
27
import org .glassfish .grizzly .filterchain .FilterChainBuilder ;
27
28
import org .glassfish .grizzly .impl .FutureImpl ;
29
+ import org .glassfish .grizzly .ssl .SSLUtils ;
28
30
import org .glassfish .grizzly .utils .Futures ;
29
31
import org .glassfish .grizzly .utils .IdleTimeoutFilter ;
30
32
33
+ import javax .net .ssl .HostnameVerifier ;
34
+ import javax .net .ssl .SSLSession ;
31
35
import java .io .IOException ;
36
+ import java .net .ConnectException ;
32
37
import java .net .InetAddress ;
33
38
import java .net .InetSocketAddress ;
34
39
import java .net .SocketAddress ;
@@ -95,28 +100,29 @@ public void doTrackedConnection(final Request request,
95
100
throws IOException {
96
101
final EndpointKey <SocketAddress > key =
97
102
getEndPointKey (request , requestFuture .getProxyServer ());
98
-
103
+ CompletionHandler <Connection > handler =
104
+ wrapHandler (request , getVerifier (), connectHandler );
99
105
if (asyncConnect ) {
100
- connectionPool .take (key , connectHandler );
106
+ connectionPool .take (key , handler );
101
107
} else {
102
108
IOException ioe = null ;
103
109
GrizzlyFuture <Connection > future = connectionPool .take (key );
104
110
try {
105
111
// No explicit timeout when calling get() here as the Grizzly
106
112
// endpoint pool will time it out based on the connect timeout
107
113
// setting.
108
- connectHandler .completed (future .get ());
114
+ handler .completed (future .get ());
109
115
} catch (CancellationException e ) {
110
- connectHandler .cancelled ();
116
+ handler .cancelled ();
111
117
} catch (ExecutionException ee ) {
112
118
final Throwable cause = ee .getCause ();
113
119
if (cause instanceof ConnectionPool .MaxCapacityException ) {
114
120
ioe = (IOException ) cause ;
115
121
} else {
116
- connectHandler .failed (ee .getCause ());
122
+ handler .failed (ee .getCause ());
117
123
}
118
124
} catch (Exception ie ) {
119
- connectHandler .failed (ie );
125
+ handler .failed (ie );
120
126
}
121
127
if (ioe != null ) {
122
128
throw ioe ;
@@ -136,6 +142,43 @@ public Connection obtainConnection(final Request request,
136
142
137
143
// --------------------------------------------------Package Private Methods
138
144
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
+
139
182
140
183
static void markConnectionAsDoNotCache (final Connection c ) {
141
184
DO_NOT_CACHE .set (c , Boolean .TRUE );
@@ -149,6 +192,10 @@ static boolean isConnectionCacheable(final Connection c) {
149
192
150
193
// --------------------------------------------------------- Private Methods
151
194
195
+ private HostnameVerifier getVerifier () {
196
+ return provider .getClientConfig ().getHostnameVerifier ();
197
+ }
198
+
152
199
private EndpointKey <SocketAddress > getEndPointKey (final Request request ,
153
200
final ProxyServer proxyServer ) {
154
201
final String stringKey = getPoolKey (request , proxyServer );
0 commit comments