Skip to content

Commit 5a7880d

Browse files
wsargentStephane Landelle
authored andcommitted
Add host and port to SSLEngineFactory, change the providers to accept host and port
1 parent 0347bec commit 5a7880d

File tree

6 files changed

+43
-15
lines changed

6 files changed

+43
-15
lines changed

api/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import java.io.IOException;
3232
import java.io.InputStream;
33+
import java.security.GeneralSecurityException;
3334
import java.util.Collections;
3435
import java.util.LinkedList;
3536
import java.util.List;
@@ -351,6 +352,16 @@ public SSLEngine newSSLEngine() {
351352
return null;
352353
}
353354
}
355+
356+
public SSLEngine newSSLEngine(String peerHost, int peerPort) throws GeneralSecurityException {
357+
if (sslContext != null) {
358+
SSLEngine sslEngine = sslContext.createSSLEngine(peerHost, peerPort);
359+
sslEngine.setUseClientMode(true);
360+
return sslEngine;
361+
} else {
362+
return null;
363+
}
364+
}
354365
};
355366
}
356367
return sslEngineFactory;

api/src/main/java/org/asynchttpclient/SSLEngineFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ public interface SSLEngineFactory {
3030
* @throws GeneralSecurityException if the SSLEngine cannot be created
3131
*/
3232
SSLEngine newSSLEngine() throws GeneralSecurityException;
33+
34+
/**
35+
* Creates new {@link SSLEngine}.
36+
*
37+
* @param peerHost the hostname of the peer the engine is connecting to.
38+
* @param peerPort the port of the peer the engine is connecting to.
39+
* @return new engine
40+
* @throws GeneralSecurityException if the SSLEngine cannot be created
41+
*/
42+
SSLEngine newSSLEngine(String peerHost, int peerPort) throws GeneralSecurityException;
3343
}

api/src/main/java/org/asynchttpclient/util/SslUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public static SslUtils getInstance() {
3333
return SingletonHolder.instance;
3434
}
3535

36-
public SSLEngine getSSLEngine() throws GeneralSecurityException, IOException {
36+
public SSLEngine getSSLEngine(String peerHost, int peerPort) throws GeneralSecurityException, IOException {
3737
SSLEngine engine = null;
3838

3939
SSLContext context = getSSLContext();
4040
if (context != null) {
41-
engine = context.createSSLEngine();
41+
engine = context.createSSLEngine(peerHost, peerPort);
4242
engine.setUseClientMode(true);
4343
}
4444

providers/netty/src/main/java/org/asynchttpclient/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.slf4j.LoggerFactory;
2727

2828
import java.io.IOException;
29+
import java.net.URI;
2930
import java.util.concurrent.atomic.AtomicBoolean;
3031

3132
public class NettyAsyncHttpProvider implements AsyncHttpProvider {
@@ -47,7 +48,6 @@ public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {
4748

4849
channels = new Channels(config, nettyConfig);
4950
requestSender = new NettyRequestSender(closed, config, nettyConfig, channels);
50-
channels.configureProcessor(requestSender, closed);
5151
}
5252

5353
@Override
@@ -72,6 +72,9 @@ public void close() {
7272

7373
@Override
7474
public <T> ListenableFuture<T> execute(Request request, final AsyncHandler<T> asyncHandler) throws IOException {
75+
final URI uri = request.getURI();
76+
channels.configureProcessor(requestSender, closed, uri.getHost(), uri.getPort());
77+
7578
return requestSender.sendRequest(request, asyncHandler, null, false);
7679
}
7780
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/channel/Channels.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,18 @@ private Timer newNettyTimer() {
201201
return nettyTimer;
202202
}
203203

204-
private SSLEngine createSSLEngine() throws IOException, GeneralSecurityException {
205-
SSLEngine sslEngine = config.getSSLEngineFactory().newSSLEngine();
204+
private SSLEngine createSSLEngine(String peerHost, int peerPort) throws IOException, GeneralSecurityException {
205+
SSLEngine sslEngine = config.getSSLEngineFactory().newSSLEngine(peerHost, peerPort);
206206
if (sslEngine == null) {
207-
sslEngine = SslUtils.getInstance().getSSLEngine();
207+
sslEngine = SslUtils.getInstance().getSSLEngine(peerHost, peerPort);
208208
}
209209
return sslEngine;
210210
}
211211

212-
public void configureProcessor(NettyRequestSender requestSender, AtomicBoolean closed) {
212+
public void configureProcessor(final NettyRequestSender requestSender,//
213+
final AtomicBoolean closed,//
214+
final String peerHost,//
215+
final int peerPort) {
213216

214217
final Processor httpProcessor = newHttpProcessor(config, nettyProviderConfig, requestSender, this, closed);
215218
wsProcessor = newWsProcessor(config, nettyProviderConfig, requestSender, this, closed);
@@ -249,7 +252,7 @@ protected void initChannel(Channel ch) throws Exception {
249252
@Override
250253
protected void initChannel(Channel ch) throws Exception {
251254

252-
SSLEngine sslEngine = createSSLEngine();
255+
SSLEngine sslEngine = createSSLEngine(peerHost, peerPort);
253256
SslHandler sslHandler = new SslHandler(sslEngine);
254257
if (handshakeTimeoutInMillis > 0)
255258
sslHandler.setHandshakeTimeoutMillis(handshakeTimeoutInMillis);
@@ -275,7 +278,7 @@ protected void initChannel(Channel ch) throws Exception {
275278
@Override
276279
protected void initChannel(Channel ch) throws Exception {
277280
ch.pipeline()//
278-
.addLast(SSL_HANDLER, new SslHandler(createSSLEngine()))//
281+
.addLast(SSL_HANDLER, new SslHandler(createSSLEngine(peerHost, peerPort)))//
279282
.addLast(HTTP_HANDLER, newHttpClientCodec())//
280283
.addLast(WS_PROCESSOR, wsProcessor);
281284

@@ -313,15 +316,15 @@ public void close() {
313316
* Always make sure the channel who got cached support the proper protocol. It could only occurs when a HttpMethod.
314317
* CONNECT is used against a proxy that requires upgrading from http to https.
315318
*/
316-
public void verifyChannelPipeline(ChannelPipeline pipeline, String scheme) throws IOException, GeneralSecurityException {
319+
public void verifyChannelPipeline(ChannelPipeline pipeline, String scheme, String peerHost, int peerPort) throws IOException, GeneralSecurityException {
317320

318321
boolean isSecure = isSecure(scheme);
319322
if (pipeline.get(SSL_HANDLER) != null) {
320323
if (!isSecure)
321324
pipeline.remove(SSL_HANDLER);
322325

323326
} else if (isSecure)
324-
pipeline.addFirst(SSL_HANDLER, new SslHandler(createSSLEngine()));
327+
pipeline.addFirst(SSL_HANDLER, new SslHandler(createSSLEngine(peerHost, peerPort)));
325328
}
326329

327330
protected HttpClientCodec newHttpClientCodec() {
@@ -337,15 +340,15 @@ protected HttpClientCodec newHttpClientCodec() {
337340
}
338341
}
339342

340-
public void upgradeProtocol(ChannelPipeline p, String scheme) throws IOException, GeneralSecurityException {
343+
public void upgradeProtocol(ChannelPipeline p, String scheme, String peerHost, int peerPort) throws IOException, GeneralSecurityException {
341344
if (p.get(HTTP_HANDLER) != null) {
342345
p.remove(HTTP_HANDLER);
343346
}
344347

345348
if (isSecure(scheme)) {
346349
if (p.get(SSL_HANDLER) == null) {
347350
p.addFirst(HTTP_HANDLER, newHttpClientCodec());
348-
p.addFirst(SSL_HANDLER, new SslHandler(createSSLEngine()));
351+
p.addFirst(SSL_HANDLER, new SslHandler(createSSLEngine(peerHost, peerPort)));
349352
} else {
350353
p.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec());
351354
}
@@ -371,7 +374,7 @@ public Channel pollAndVerifyCachedChannel(URI uri, ProxyServer proxy, Connection
371374
LOGGER.debug("Using cached Channel {}\n for uri {}\n", channel, uri);
372375

373376
try {
374-
verifyChannelPipeline(channel.pipeline(), uri.getScheme());
377+
verifyChannelPipeline(channel.pipeline(), uri.getScheme(), uri.getHost(), uri.getPort());
375378
} catch (Exception ex) {
376379
LOGGER.debug(ex.getMessage(), ex);
377380
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/handler/HttpProtocol.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ private boolean handleConnectOKAndExit(int statusCode, Realm realm, final Reques
320320

321321
try {
322322
LOGGER.debug("Connecting to proxy {} for scheme {}", proxyServer, request.getUrl());
323-
channels.upgradeProtocol(channel.pipeline(), request.getURI().getScheme());
323+
URI uri = request.getURI();
324+
channels.upgradeProtocol(channel.pipeline(), uri.getScheme(), uri.getHost(), uri.getPort());
324325
} catch (Throwable ex) {
325326
channels.abort(future, ex);
326327
}

0 commit comments

Comments
 (0)