Skip to content

Commit debbfd5

Browse files
committed
Fixed nonProxyHosts handling for SSL hosts
1 parent 3ed7685 commit debbfd5

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,8 @@ private boolean sendAsGrizzlyRequest(final Request request,
855855
}
856856
}
857857
final ProxyServer proxy = getProxyServer(request);
858-
final boolean useProxy = (proxy != null);
858+
boolean avoidProxy = ProxyUtils.avoidProxy(proxy, request);
859+
final boolean useProxy = !(avoidProxy || proxy == null);
859860
if (useProxy) {
860861
if ((secure || httpCtx.isWSRequest) && !httpCtx.isTunnelEstablished(ctx.getConnection())) {
861862
secure = false;
@@ -903,16 +904,13 @@ private boolean sendAsGrizzlyRequest(final Request request,
903904
addCookies(request, requestPacket);
904905

905906
if (useProxy) {
906-
boolean avoidProxy = ProxyUtils.avoidProxy(proxy, request);
907-
if (!avoidProxy) {
908-
if (!requestPacket.getHeaders().contains(Header.ProxyConnection)) {
909-
requestPacket.setHeader(Header.ProxyConnection, "keep-alive");
910-
}
907+
if (!requestPacket.getHeaders().contains(Header.ProxyConnection)) {
908+
requestPacket.setHeader(Header.ProxyConnection, "keep-alive");
909+
}
911910

912-
if (proxy.getPrincipal() != null) {
913-
requestPacket.setHeader(Header.ProxyAuthorization,
914-
AuthenticatorUtils.computeBasicAuthentication(proxy));
915-
}
911+
if (proxy.getPrincipal() != null) {
912+
requestPacket.setHeader(Header.ProxyAuthorization,
913+
AuthenticatorUtils.computeBasicAuthentication(proxy));
916914
}
917915
}
918916
final AsyncHandler h = httpCtx.handler;

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,15 @@ public void operationComplete(ChannelFuture cf) {
578578
}
579579

580580
private static boolean isProxyServer(AsyncHttpClientConfig config, Request request) {
581-
return request.getProxyServer() != null || config.getProxyServer() != null;
581+
ProxyServer proxyServer = request.getProxyServer();
582+
if (proxyServer == null) {
583+
proxyServer = config.getProxyServer();
584+
}
585+
if (proxyServer == null) {
586+
return false;
587+
} else {
588+
return !ProxyUtils.avoidProxy(proxyServer, request);
589+
}
582590
}
583591

584592
protected final static HttpRequest buildRequest(AsyncHttpClientConfig config, Request request, URI uri,
@@ -951,7 +959,10 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
951959
bufferedBytes = f.getNettyRequest().getContent();
952960
}
953961

954-
boolean useSSl = isSecure(uri) && proxyServer == null;
962+
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, uri.getHost());
963+
boolean useProxy = !(avoidProxy || proxyServer == null);
964+
965+
boolean useSSl = isSecure(uri) && !useProxy;
955966
if (channel != null && channel.isOpen() && channel.isConnected()) {
956967
HttpRequest nettyRequest = buildRequest(config, request, uri, f == null ? false : f.isConnectAllowed(), bufferedBytes);
957968

@@ -1018,7 +1029,6 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
10181029
}
10191030

10201031
NettyConnectListener<T> c = new NettyConnectListener.Builder<T>(config, request, asyncHandler, f, this, bufferedBytes).build(uri);
1021-
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, uri.getHost());
10221032

10231033
if (useSSl) {
10241034
constructSSLPipeline(c);
@@ -1037,7 +1047,7 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
10371047
InetSocketAddress remoteAddress;
10381048
if (request.getInetAddress() != null) {
10391049
remoteAddress = new InetSocketAddress(request.getInetAddress(), AsyncHttpProviderUtils.getPort(uri));
1040-
} else if (proxyServer == null || avoidProxy) {
1050+
} else if (!useProxy) {
10411051
remoteAddress = new InetSocketAddress(AsyncHttpProviderUtils.getHost(uri), AsyncHttpProviderUtils.getPort(uri));
10421052
} else {
10431053
remoteAddress = new InetSocketAddress(proxyServer.getHost(), proxyServer.getPort());

src/test/java/com/ning/http/client/async/ProxyyTunnellingTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
import org.testng.annotations.BeforeClass;
2929
import org.testng.annotations.Test;
3030

31+
import javax.servlet.http.HttpServletResponse;
3132
import java.io.File;
3233
import java.io.IOException;
3334
import java.net.URL;
3435
import java.util.concurrent.ExecutionException;
3536
import java.util.concurrent.Future;
37+
import java.util.concurrent.TimeUnit;
3638
import java.util.concurrent.TimeoutException;
3739

3840
import static org.testng.Assert.assertEquals;
@@ -166,5 +168,20 @@ public void testSimpleAHCConfigProxy() throws IOException, InterruptedException,
166168

167169
client.close();
168170
}
171+
172+
@Test(groups = { "standalone", "default_provider" })
173+
public void testNonProxyHostsSsl() throws IOException, ExecutionException, TimeoutException, InterruptedException {
174+
AsyncHttpClient client = getAsyncHttpClient(null);
175+
176+
Response resp = client.prepareGet(getTargetUrl2()).setProxyServer(new ProxyServer("127.0.0.1", port1 - 1)
177+
.addNonProxyHost("127.0.0.1"))
178+
.execute().get(3, TimeUnit.SECONDS);
179+
180+
assertNotNull(resp);
181+
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
182+
assertEquals(resp.getHeader("X-pathInfo"), "/foo/test");
183+
184+
client.close();
185+
}
169186
}
170187

0 commit comments

Comments
 (0)