Skip to content

Commit 93b8b7b

Browse files
committed
1 parent 5179c5e commit 93b8b7b

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/ConnectionManager.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@
1919
import org.asynchttpclient.Request;
2020
import org.glassfish.grizzly.CompletionHandler;
2121
import org.glassfish.grizzly.Connection;
22+
import org.glassfish.grizzly.EmptyCompletionHandler;
2223
import org.glassfish.grizzly.Grizzly;
2324
import org.glassfish.grizzly.GrizzlyFuture;
2425
import org.glassfish.grizzly.attributes.Attribute;
2526
import org.glassfish.grizzly.connectionpool.EndpointKey;
2627
import org.glassfish.grizzly.filterchain.FilterChainBuilder;
2728
import org.glassfish.grizzly.impl.FutureImpl;
29+
import org.glassfish.grizzly.ssl.SSLUtils;
2830
import org.glassfish.grizzly.utils.Futures;
2931
import org.glassfish.grizzly.utils.IdleTimeoutFilter;
3032

33+
import javax.net.ssl.HostnameVerifier;
34+
import javax.net.ssl.SSLSession;
3135
import java.io.IOException;
36+
import java.net.ConnectException;
3237
import java.net.InetAddress;
3338
import java.net.InetSocketAddress;
3439
import 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);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
14+
package org.asynchttpclient.providers.grizzly;
15+
16+
import org.asynchttpclient.AsyncHttpClient;
17+
import org.asynchttpclient.AsyncHttpClientConfig;
18+
import org.asynchttpclient.async.HostnameVerifierTest;
19+
20+
public class GrizzlyHostnameVerifierTest extends HostnameVerifierTest {
21+
22+
@Override
23+
public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
24+
return GrizzlyProviderUtil.grizzlyProvider(config);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)