Skip to content

Commit aed562b

Browse files
committed
1 parent 8c7e310 commit aed562b

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@
8989
import org.jboss.netty.handler.codec.http.HttpHeaders;
9090
import org.jboss.netty.handler.codec.http.HttpMethod;
9191
import org.jboss.netty.handler.codec.http.HttpRequest;
92-
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
9392
import org.jboss.netty.handler.codec.http.HttpResponse;
94-
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
9593
import org.jboss.netty.handler.codec.http.HttpVersion;
9694
import org.jboss.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
9795
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
@@ -166,8 +164,11 @@ public class NettyAsyncHttpProvider extends SimpleChannelUpstreamHandler impleme
166164
static {
167165
REMOTELY_CLOSED_EXCEPTION.setStackTrace(new StackTraceElement[0]);
168166
}
169-
private final static String HTTP_HANDLER = "httpHandler";
170-
protected final static String SSL_HANDLER = "sslHandler";
167+
public final static String HTTP_HANDLER = "httpHandler";
168+
public final static String SSL_HANDLER = "sslHandler";
169+
public final static String HTTP_PROCESSOR = "httpProcessor";
170+
public final static String WS_PROCESSOR = "wsProcessor";
171+
171172
private final static String HTTPS = "https";
172173
private final static String HTTP = "http";
173174
private static final String WEBSOCKET = "ws";
@@ -318,7 +319,7 @@ public ChannelPipeline getPipeline() throws Exception {
318319
pipeline.addLast("inflater", new HttpContentDecompressor());
319320
}
320321
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
321-
pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this);
322+
pipeline.addLast(HTTP_PROCESSOR, NettyAsyncHttpProvider.this);
322323
return pipeline;
323324
}
324325
});
@@ -338,9 +339,8 @@ public ChannelPipeline getPipeline() throws Exception {
338339
/* @Override */
339340
public ChannelPipeline getPipeline() throws Exception {
340341
ChannelPipeline pipeline = pipeline();
341-
pipeline.addLast("http-decoder", new HttpResponseDecoder());
342-
pipeline.addLast("http-encoder", new HttpRequestEncoder());
343-
pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this);
342+
pipeline.addLast(HTTP_HANDLER, createHttpClientCodec());
343+
pipeline.addLast(WS_PROCESSOR, NettyAsyncHttpProvider.this);
344344
return pipeline;
345345
}
346346
});
@@ -378,7 +378,7 @@ public ChannelPipeline getPipeline() throws Exception {
378378
pipeline.addLast("inflater", new HttpContentDecompressor());
379379
}
380380
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
381-
pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this);
381+
pipeline.addLast(HTTP_PROCESSOR, NettyAsyncHttpProvider.this);
382382
return pipeline;
383383
}
384384
});
@@ -395,9 +395,8 @@ public ChannelPipeline getPipeline() throws Exception {
395395
abort(cl.future(), ex);
396396
}
397397

398-
pipeline.addLast("http-decoder", new HttpResponseDecoder());
399-
pipeline.addLast("http-encoder", new HttpRequestEncoder());
400-
pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this);
398+
pipeline.addLast(HTTP_HANDLER, createHttpsClientCodec());
399+
pipeline.addLast(WS_PROCESSOR, NettyAsyncHttpProvider.this);
401400

402401
return pipeline;
403402
}
@@ -665,8 +664,8 @@ else if (uri.getRawQuery() != null)
665664
path = uri.getRawPath();
666665
nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, m, path);
667666
}
668-
boolean webSocket = isWebSocket(uri);
669-
if (webSocket) {
667+
boolean webSocket = isWebSocket(uri.getScheme());
668+
if (!m.equals(HttpMethod.CONNECT) && webSocket) {
670669
nettyRequest.addHeader(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET);
671670
nettyRequest.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE);
672671
nettyRequest.addHeader(HttpHeaders.Names.ORIGIN, "http://" + uri.getHost() + ":" + uri.getPort());
@@ -979,7 +978,9 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
979978
}
980979

981980
ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
982-
boolean useProxy = proxyServer != null;
981+
982+
boolean resultOfAConnect = f != null && f.getNettyRequest() != null && f.getNettyRequest().getMethod().equals(HttpMethod.CONNECT);
983+
boolean useProxy = proxyServer != null && !resultOfAConnect;
983984

984985
URI uri;
985986
if (useRawUrl) {
@@ -1059,7 +1060,8 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
10591060
}
10601061

10611062
ChannelFuture channelFuture;
1062-
ClientBootstrap bootstrap = request.getUrl().startsWith(WEBSOCKET) ? (useSSl ? secureWebSocketBootstrap : webSocketBootstrap) : (useSSl ? secureBootstrap : plainBootstrap);
1063+
ClientBootstrap bootstrap = (request.getUrl().startsWith(WEBSOCKET) && !useProxy) ?
1064+
(useSSl ? secureWebSocketBootstrap : webSocketBootstrap) : (useSSl ? secureBootstrap : plainBootstrap);
10631065
bootstrap.setOption("connectTimeoutMillis", config.getConnectionTimeoutInMs());
10641066

10651067
// Do no enable this with win.
@@ -1204,7 +1206,7 @@ public void messageReceived(final ChannelHandlerContext ctx, MessageEvent e) thr
12041206
return;
12051207
}
12061208

1207-
Protocol p = (ctx.getPipeline().get(HttpClientCodec.class) != null ? httpProtocol : webSocketProtocol);
1209+
Protocol p = (ctx.getPipeline().get(HTTP_PROCESSOR) != null ? httpProtocol : webSocketProtocol);
12081210
p.handle(ctx, e);
12091211
}
12101212

@@ -1409,6 +1411,10 @@ private void upgradeProtocol(ChannelPipeline p, String scheme) throws IOExceptio
14091411
} else {
14101412
p.addFirst(HTTP_HANDLER, createHttpClientCodec());
14111413
}
1414+
1415+
if (isWebSocket(scheme)) {
1416+
p.replace(HTTP_PROCESSOR, WS_PROCESSOR, NettyAsyncHttpProvider.this);
1417+
}
14121418
}
14131419

14141420
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
@@ -2315,8 +2321,8 @@ public void handle(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
23152321
return;
23162322
}
23172323

2318-
ctx.getPipeline().replace("http-encoder", "ws-encoder", new WebSocket08FrameEncoder(true));
2319-
ctx.getPipeline().get(HttpResponseDecoder.class).replace("ws-decoder", new WebSocket08FrameDecoder(false, false));
2324+
ctx.getPipeline().replace(HTTP_HANDLER, "ws-encoder", new WebSocket08FrameEncoder(true));
2325+
ctx.getPipeline().addBefore(WS_PROCESSOR, "ws-decoder", new WebSocket08FrameDecoder(false, false));
23202326

23212327
invokeOnSucces(ctx, h);
23222328
future.done();
@@ -2437,8 +2443,8 @@ public Timeout newTimeoutInMs(TimerTask task, long delayInMs) {
24372443
return hashedWheelTimer.newTimeout(task, delayInMs, TimeUnit.MILLISECONDS);
24382444
}
24392445

2440-
private static boolean isWebSocket(URI uri) {
2441-
return WEBSOCKET.equalsIgnoreCase(uri.getScheme()) || WEBSOCKET_SSL.equalsIgnoreCase(uri.getScheme());
2446+
private static boolean isWebSocket(String scheme) {
2447+
return WEBSOCKET.equalsIgnoreCase(scheme) || WEBSOCKET_SSL.equalsIgnoreCase(scheme);
24422448
}
24432449

24442450
private static boolean isSecure(String scheme) {

0 commit comments

Comments
 (0)