Skip to content

Commit c3879c8

Browse files
committed
No need to verify the channel, HTTP connections are always upgraded after CONNECT and only offered then
1 parent 7e98e01 commit c3879c8

File tree

3 files changed

+26
-66
lines changed

3 files changed

+26
-66
lines changed

client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -448,28 +448,6 @@ public SslHandler addSslHandler(ChannelPipeline pipeline, Uri uri, String virtua
448448
return sslHandler;
449449
}
450450

451-
/**
452-
* Always make sure the channel who got cached supports the proper protocol.
453-
* It could only occurs when a HttpMethod. CONNECT is used against a proxy
454-
* that requires upgrading from http to https.
455-
*/
456-
/**
457-
* @param pipeline the pipeline
458-
* @param uri the uri
459-
* @param virtualHost the virtual host
460-
*/
461-
public void verifyChannelPipeline(ChannelPipeline pipeline, Uri uri, String virtualHost) {
462-
463-
boolean sslHandlerConfigured = isSslHandlerConfigured(pipeline);
464-
465-
if (uri.isSecured()) {
466-
if (!sslHandlerConfigured)
467-
addSslHandler(pipeline, uri, virtualHost);
468-
469-
} else if (sslHandlerConfigured)
470-
pipeline.remove(SSL_HANDLER);
471-
}
472-
473451
public Bootstrap getBootstrap(Uri uri, ProxyServer proxy) {
474452
return uri.isWebSocket() && proxy == null ? wsBootstrap : httpBootstrap;
475453
}

client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ private <T> ListenableFuture<T> sendRequestWithCertainForceConnect(//
121121

122122
NettyResponseFuture<T> newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, forceConnect);
123123

124-
Channel channel = getCachedChannel(future, request, proxyServer, asyncHandler);
124+
Channel channel = getOpenChannel(future, request, proxyServer, asyncHandler);
125125

126126
if (Channels.isChannelValid(channel))
127-
return sendRequestWithCachedChannel(request, proxyServer, newFuture, asyncHandler, channel);
127+
return sendRequestWithOpenChannel(request, proxyServer, newFuture, asyncHandler, channel);
128128
else
129129
return sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler, reclaimCache);
130130
}
@@ -144,15 +144,15 @@ private <T> ListenableFuture<T> sendRequestThroughSslProxy(//
144144

145145
NettyResponseFuture<T> newFuture = null;
146146
for (int i = 0; i < 3; i++) {
147-
Channel channel = getCachedChannel(future, request, proxyServer, asyncHandler);
147+
Channel channel = getOpenChannel(future, request, proxyServer, asyncHandler);
148148
if (Channels.isChannelValid(channel))
149149
if (newFuture == null)
150150
newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, false);
151151

152152
if (Channels.isChannelValid(channel))
153153
// if the channel is still active, we can use it, otherwise try
154154
// gain
155-
return sendRequestWithCachedChannel(request, proxyServer, newFuture, asyncHandler, channel);
155+
return sendRequestWithOpenChannel(request, proxyServer, newFuture, asyncHandler, channel);
156156
else
157157
// pool is empty
158158
break;
@@ -195,15 +195,15 @@ private <T> NettyResponseFuture<T> newNettyRequestAndResponseFuture(final Reques
195195
}
196196
}
197197

198-
private Channel getCachedChannel(NettyResponseFuture<?> future, Request request, ProxyServer proxyServer, AsyncHandler<?> asyncHandler) {
198+
private Channel getOpenChannel(NettyResponseFuture<?> future, Request request, ProxyServer proxyServer, AsyncHandler<?> asyncHandler) {
199199

200200
if (future != null && future.reuseChannel() && Channels.isChannelValid(future.channel()))
201201
return future.channel();
202202
else
203-
return pollAndVerifyCachedChannel(request, proxyServer, asyncHandler);
203+
return pollPooledChannel(request, proxyServer, asyncHandler);
204204
}
205205

206-
private <T> ListenableFuture<T> sendRequestWithCachedChannel(Request request, ProxyServer proxy, NettyResponseFuture<T> future, AsyncHandler<T> asyncHandler, Channel channel) {
206+
private <T> ListenableFuture<T> sendRequestWithOpenChannel(Request request, ProxyServer proxy, NettyResponseFuture<T> future, AsyncHandler<T> asyncHandler, Channel channel) {
207207

208208
if (asyncHandler instanceof AsyncHandlerExtensions)
209209
AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionPooled(channel);
@@ -442,7 +442,7 @@ else if (!request.getMethod().equals(HttpMethod.GET.name()))
442442
}
443443
}
444444

445-
private Channel pollAndVerifyCachedChannel(Request request, ProxyServer proxy, AsyncHandler<?> asyncHandler) {
445+
private Channel pollPooledChannel(Request request, ProxyServer proxy, AsyncHandler<?> asyncHandler) {
446446

447447
if (asyncHandler instanceof AsyncHandlerExtensions)
448448
AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionPool();
@@ -453,12 +453,6 @@ private Channel pollAndVerifyCachedChannel(Request request, ProxyServer proxy, A
453453

454454
if (channel != null) {
455455
LOGGER.debug("Using cached Channel {}\n for uri {}\n", channel, uri);
456-
457-
try {
458-
channelManager.verifyChannelPipeline(channel.pipeline(), uri, virtualHost);
459-
} catch (Exception ex) {
460-
LOGGER.debug(ex.getMessage(), ex);
461-
}
462456
}
463457
return channel;
464458
}

client/src/test/java/org/asynchttpclient/proxy/HttpsProxyTest.java

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919

2020
import java.io.IOException;
2121
import java.util.concurrent.ExecutionException;
22-
import java.util.concurrent.Future;
2322
import java.util.concurrent.TimeoutException;
2423

2524
import org.asynchttpclient.AbstractBasicTest;
26-
import org.asynchttpclient.AsyncCompletionHandlerBase;
2725
import org.asynchttpclient.AsyncHttpClient;
2826
import org.asynchttpclient.AsyncHttpClientConfig;
2927
import org.asynchttpclient.RequestBuilder;
@@ -69,52 +67,42 @@ public void tearDownGlobal() throws Exception {
6967
server2.stop();
7068
}
7169

72-
@Test(groups = "online")
70+
@Test(groups = "standalone")
7371
public void testRequestProxy() throws IOException, InterruptedException, ExecutionException, TimeoutException {
7472

7573
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setFollowRedirect(true).setAcceptAnyCertificate(true))) {
7674
RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServer("127.0.0.1", port1));
77-
Future<Response> responseFuture = asyncHttpClient.executeRequest(rb.build(), new AsyncCompletionHandlerBase() {
78-
79-
public void onThrowable(Throwable t) {
80-
t.printStackTrace();
81-
logger.debug(t.getMessage(), t);
82-
}
83-
84-
@Override
85-
public Response onCompleted(Response response) throws Exception {
86-
return response;
87-
}
88-
});
89-
Response r = responseFuture.get();
75+
Response r = asyncHttpClient.executeRequest(rb.build()).get();
9076
assertEquals(r.getStatusCode(), 200);
9177
assertEquals(r.getHeader("X-Connection"), HttpHeaders.Values.KEEP_ALIVE);
9278
}
9379
}
9480

95-
@Test(groups = "online")
81+
@Test(groups = "standalone")
9682
public void testConfigProxy() throws IOException, InterruptedException, ExecutionException, TimeoutException {
9783
AsyncHttpClientConfig config = config()//
9884
.setFollowRedirect(true)//
9985
.setProxyServer(proxyServer("127.0.0.1", port1).build())//
10086
.setAcceptAnyCertificate(true)//
10187
.build();
10288
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) {
103-
Future<Response> responseFuture = asyncHttpClient.executeRequest(get(getTargetUrl2()), new AsyncCompletionHandlerBase() {
104-
105-
public void onThrowable(Throwable t) {
106-
t.printStackTrace();
107-
logger.debug(t.getMessage(), t);
108-
}
109-
110-
@Override
111-
public Response onCompleted(Response response) throws Exception {
112-
return response;
113-
}
114-
});
115-
Response r = responseFuture.get();
89+
Response r = asyncHttpClient.executeRequest(get(getTargetUrl2())).get();
11690
assertEquals(r.getStatusCode(), 200);
11791
assertEquals(r.getHeader("X-Connection"), HttpHeaders.Values.KEEP_ALIVE);
11892
}
11993
}
94+
95+
@Test(groups = "standalone")
96+
public void testPooledConnectionsWithProxy() throws IOException, InterruptedException, ExecutionException, TimeoutException {
97+
98+
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setFollowRedirect(true).setAcceptAnyCertificate(true).setKeepAlive(true))) {
99+
RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServer("127.0.0.1", port1));
100+
101+
Response r1 = asyncHttpClient.executeRequest(rb.build()).get();
102+
assertEquals(r1.getStatusCode(), 200);
103+
104+
Response r2 = asyncHttpClient.executeRequest(rb.build()).get();
105+
assertEquals(r2.getStatusCode(), 200);
106+
}
107+
}
120108
}

0 commit comments

Comments
 (0)