Skip to content

Commit 137aba2

Browse files
committed
Merge pull request AsyncHttpClient#76 from micpe083/master
Please merge fix for AsyncHttpClient#75
2 parents 392dd48 + 76b5120 commit 137aba2

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,12 @@ public class NettyAsyncHttpProvider extends SimpleChannelUpstreamHandler impleme
140140
private final static String HTTPS = "https";
141141
private final static String HTTP = "http";
142142
private static final String WEBSOCKET = "ws";
143+
private static final String WEBSOCKET_SSL = "wss";
143144
private final static Logger log = LoggerFactory.getLogger(NettyAsyncHttpProvider.class);
144145
private final ClientBootstrap plainBootstrap;
145146
private final ClientBootstrap secureBootstrap;
146147
private final ClientBootstrap webSocketBootstrap;
148+
private final ClientBootstrap secureWebSocketBootstrap;
147149
private final static int MAX_BUFFERED_BYTES = 8192;
148150
private final AsyncHttpClientConfig config;
149151
private final AtomicBoolean isClose = new AtomicBoolean(false);
@@ -198,6 +200,7 @@ public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {
198200
plainBootstrap = new ClientBootstrap(socketChannelFactory);
199201
secureBootstrap = new ClientBootstrap(socketChannelFactory);
200202
webSocketBootstrap = new ClientBootstrap(socketChannelFactory);
203+
secureWebSocketBootstrap = new ClientBootstrap(socketChannelFactory);
201204
configureNetty();
202205

203206
this.config = config;
@@ -305,9 +308,30 @@ public ChannelPipeline getPipeline() throws Exception {
305308
}
306309
});
307310

311+
secureWebSocketBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
312+
313+
/* @Override */
314+
public ChannelPipeline getPipeline() throws Exception {
315+
ChannelPipeline pipeline = pipeline();
316+
317+
try {
318+
pipeline.addLast(SSL_HANDLER, new SslHandler(createSSLEngine()));
319+
} catch (Throwable ex) {
320+
abort(cl.future(), ex);
321+
}
322+
323+
pipeline.addLast("ws-decoder", new HttpResponseDecoder());
324+
pipeline.addLast("ws-encoder", new HttpRequestEncoder());
325+
pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this);
326+
327+
return pipeline;
328+
}
329+
});
330+
308331
if (asyncHttpProviderConfig != null) {
309332
for (Entry<String, Object> entry : asyncHttpProviderConfig.propertiesSet()) {
310333
secureBootstrap.setOption(entry.getKey(), entry.getValue());
334+
secureWebSocketBootstrap.setOption(entry.getKey(), entry.getValue());
311335
}
312336
}
313337
}
@@ -344,7 +368,7 @@ private Channel verifyChannelPipeline(Channel channel, String scheme) throws IOE
344368
channel.getPipeline().remove(SSL_HANDLER);
345369
} else if (channel.getPipeline().get(HTTP_HANDLER) != null && HTTP.equalsIgnoreCase(scheme)) {
346370
return channel;
347-
} else if (channel.getPipeline().get(SSL_HANDLER) == null && HTTPS.equalsIgnoreCase(scheme)) {
371+
} else if (channel.getPipeline().get(SSL_HANDLER) == null && isSecure(scheme)) {
348372
channel.getPipeline().addFirst(SSL_HANDLER, new SslHandler(createSSLEngine()));
349373
}
350374
return channel;
@@ -508,7 +532,7 @@ protected final static HttpRequest buildRequest(AsyncHttpClientConfig config, Re
508532
boolean allowConnect, ChannelBuffer buffer) throws IOException {
509533

510534
String method = request.getMethod();
511-
if (allowConnect && (isProxyServer(config, request) && HTTPS.equalsIgnoreCase(uri.getScheme()))) {
535+
if (allowConnect && (isProxyServer(config, request) && isSecure(uri))) {
512536
method = HttpMethod.CONNECT.toString();
513537
}
514538
return construct(config, request, new HttpMethod(method), uri, buffer);
@@ -542,7 +566,7 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
542566
}
543567
nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, m, path.toString());
544568
}
545-
boolean webSocket = uri.getScheme().equalsIgnoreCase(WEBSOCKET);
569+
boolean webSocket = isWebSocket(uri);
546570
if (webSocket) {
547571
nettyRequest.addHeader(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET);
548572
nettyRequest.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE);
@@ -762,7 +786,8 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
762786
/**
763787
* TODO: AHC-78: SSL + zero copy isn't supported by the MultiPart class and pretty complex to implements.
764788
*/
765-
if (uri.toString().startsWith(HTTPS)) {
789+
790+
if (isSecure(uri)) {
766791
ChannelBuffer b = ChannelBuffers.dynamicBuffer(lenght);
767792
mre.writeRequest(new ChannelBufferOutputStream(b));
768793
nettyRequest.setContent(b);
@@ -810,6 +835,7 @@ public void close() {
810835
plainBootstrap.releaseExternalResources();
811836
secureBootstrap.releaseExternalResources();
812837
webSocketBootstrap.releaseExternalResources();
838+
secureWebSocketBootstrap.releaseExternalResources();
813839
} catch (Throwable t) {
814840
log.warn("Unexpected error on close", t);
815841
}
@@ -872,7 +898,7 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
872898
bufferedBytes = f.getNettyRequest().getContent();
873899
}
874900

875-
boolean useSSl = uri.getScheme().compareToIgnoreCase(HTTPS) == 0 && proxyServer == null;
901+
boolean useSSl = isSecure(uri) && proxyServer == null;
876902
if (channel != null && channel.isOpen() && channel.isConnected()) {
877903
HttpRequest nettyRequest = buildRequest(config, request, uri, f == null ? false : f.isConnectAllowed(), bufferedBytes);
878904

@@ -946,7 +972,7 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
946972
}
947973

948974
ChannelFuture channelFuture;
949-
ClientBootstrap bootstrap = request.getUrl().startsWith(WEBSOCKET) ? webSocketBootstrap : (useSSl ? secureBootstrap : plainBootstrap);
975+
ClientBootstrap bootstrap = request.getUrl().startsWith(WEBSOCKET) ? (useSSl ? secureWebSocketBootstrap : webSocketBootstrap) : (useSSl ? secureBootstrap : plainBootstrap);
950976
bootstrap.setOption("connectTimeoutMillis", config.getConnectionTimeoutInMs());
951977

952978
// Do no enable this with win.
@@ -1294,7 +1320,7 @@ private void upgradeProtocol(ChannelPipeline p, String scheme) throws IOExceptio
12941320
p.remove(HTTP_HANDLER);
12951321
}
12961322

1297-
if (scheme.startsWith(HTTPS)) {
1323+
if (isSecure(scheme)) {
12981324
if (p.get(SSL_HANDLER) == null) {
12991325
p.addFirst(HTTP_HANDLER, new HttpClientCodec());
13001326
p.addFirst(SSL_HANDLER, new SslHandler(createSSLEngine()));
@@ -2372,5 +2398,20 @@ public void onClose(ChannelHandlerContext ctx, ChannelStateEvent e) {
23722398
}
23732399
}
23742400
}
2401+
2402+
private static boolean isWebSocket(URI uri)
2403+
{
2404+
return WEBSOCKET.equalsIgnoreCase(uri.getScheme()) || WEBSOCKET_SSL.equalsIgnoreCase(uri.getScheme());
2405+
}
2406+
2407+
private static boolean isSecure(String scheme)
2408+
{
2409+
return HTTPS.equalsIgnoreCase(scheme) || WEBSOCKET_SSL.equalsIgnoreCase(scheme);
2410+
}
2411+
2412+
private static boolean isSecure(URI uri)
2413+
{
2414+
return isSecure(uri.getScheme());
2415+
}
23752416
}
23762417

0 commit comments

Comments
 (0)