Skip to content

Commit 632c697

Browse files
author
Stephane Landelle
committed
Changes from Alexei
1 parent 0628194 commit 632c697

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.asynchttpclient.providers.grizzly.filters.AsyncHttpClientFilter;
3030
import org.asynchttpclient.providers.grizzly.filters.AsyncSpdyClientEventFilter;
3131
import org.asynchttpclient.providers.grizzly.filters.ClientEncodingFilter;
32+
import org.asynchttpclient.providers.grizzly.filters.HostPortAwareSSLEngineConfigurator;
3233
import org.asynchttpclient.providers.grizzly.filters.SwitchingSSLFilter;
3334
import org.asynchttpclient.util.AsyncHttpProviderUtils;
3435
import org.asynchttpclient.util.ProxyUtils;
@@ -257,7 +258,7 @@ public void onTimeout(Connection connection) {
257258
throw new IllegalStateException(e);
258259
}
259260
}
260-
final SSLEngineConfigurator configurator = new SSLEngineConfigurator(context, true, false, false);
261+
final SSLEngineConfigurator configurator = new HostPortAwareSSLEngineConfigurator(context, true, false, false);
261262
final SwitchingSSLFilter filter = new SwitchingSSLFilter(configurator);
262263
secure.add(filter);
263264
GrizzlyAsyncHttpProviderConfig providerConfig = (GrizzlyAsyncHttpProviderConfig) clientConfig.getAsyncHttpProviderConfig();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. 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+
package org.asynchttpclient.providers.grizzly.filters;
14+
15+
import javax.net.ssl.SSLContext;
16+
import javax.net.ssl.SSLEngine;
17+
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
18+
19+
public class HostPortAwareSSLEngineConfigurator extends SSLEngineConfigurator {
20+
21+
public HostPortAwareSSLEngineConfigurator(final SSLContext context, final boolean clientMode, final boolean needClientAuth,
22+
final boolean wantClientAuth) {
23+
super(context, clientMode, needClientAuth, wantClientAuth);
24+
}
25+
26+
/**
27+
* Create and configure {@link SSLEngine} using this context configuration
28+
* using advisory peer information.
29+
* <P>
30+
* Applications using this factory method are providing hints
31+
* for an internal session reuse strategy.
32+
* <P>
33+
* Some cipher suites (such as Kerberos) require remote hostname
34+
* information, in which case peerHost needs to be specified.
35+
*
36+
* @param peerHost the non-authoritative name of the host
37+
* @param peerPort the non-authoritative port
38+
*
39+
* @return {@link SSLEngine}.
40+
*/
41+
public SSLEngine createSSLEngine(final String peerHost, final int peerPort) {
42+
final SSLEngine sslEngine = getSslContext().createSSLEngine(peerHost, peerPort);
43+
configure(sslEngine);
44+
45+
return sslEngine;
46+
}
47+
}

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/filters/SwitchingSSLFilter.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.asynchttpclient.providers.grizzly.filters;
1515

1616
import org.asynchttpclient.providers.grizzly.filters.events.SSLSwitchingEvent;
17+
import org.glassfish.grizzly.CompletionHandler;
1718
import org.glassfish.grizzly.Connection;
1819
import org.glassfish.grizzly.EmptyCompletionHandler;
1920
import org.glassfish.grizzly.Grizzly;
@@ -23,13 +24,16 @@
2324
import org.glassfish.grizzly.filterchain.FilterChainContext;
2425
import org.glassfish.grizzly.filterchain.FilterChainEvent;
2526
import org.glassfish.grizzly.filterchain.NextAction;
27+
import org.glassfish.grizzly.ssl.SSLConnectionContext;
2628
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
2729
import org.glassfish.grizzly.ssl.SSLFilter;
30+
import org.glassfish.grizzly.ssl.SSLUtils;
2831

2932
import javax.net.ssl.SSLEngine;
3033
import javax.net.ssl.SSLHandshakeException;
3134

3235
import java.io.IOException;
36+
import java.net.InetSocketAddress;
3337

3438
/**
3539
* SSL Filter that may be present within the FilterChain and may be
@@ -143,6 +147,28 @@ public static Throwable getHandshakeError(final Connection c) {
143147
return HANDSHAKE_ERROR.remove(c);
144148
}
145149

150+
@Override
151+
protected void handshake(final Connection<?> connection,
152+
final CompletionHandler<SSLEngine> completionHandler,
153+
final Object dstAddress, SSLEngineConfigurator sslEngineConfigurator,
154+
final FilterChainContext context) throws IOException {
155+
156+
SSLEngine sslEngine = SSLUtils.getSSLEngine(connection);
157+
if (sslEngine == null) {
158+
InetSocketAddress peerAddress = (InetSocketAddress) connection.getPeerAddress();
159+
String host = peerAddress.getHostString();
160+
int port = peerAddress.getPort();
161+
sslEngine = ((HostPortAwareSSLEngineConfigurator) sslEngineConfigurator)
162+
.createSSLEngine(host, port);
163+
final SSLConnectionContext sslCtx = new SSLConnectionContext(connection);
164+
sslCtx.configure(sslEngine);
165+
sslCtx.attach();
166+
}
167+
168+
super.handshake(connection, completionHandler, dstAddress, sslEngineConfigurator,
169+
context);
170+
}
171+
146172
// --------------------------------------------------------- Private Methods
147173

148174
private static boolean isSecure(final Connection c) {

0 commit comments

Comments
 (0)