Skip to content

Commit 7c4ab47

Browse files
author
Stephane Landelle
committed
Port #250 to master, close #202
1 parent b3c118e commit 7c4ab47

File tree

9 files changed

+94
-71
lines changed

9 files changed

+94
-71
lines changed

api/src/main/java/com/ning/http/client/providers/jdk/JDKAsyncHttpProvider.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler,
136136
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, request);
137137
if (!avoidProxy && (proxyServer != null || realm != null)) {
138138
try {
139-
/*Proxy proxy =*/ configureProxyAndAuth(proxyServer, realm);
139+
configureProxyAndAuth(proxyServer, realm);
140140
} catch (AuthenticationException e) {
141141
throw new IOException(e.getMessage());
142142
}
@@ -163,11 +163,10 @@ public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler,
163163
}
164164

165165
private HttpURLConnection createUrlConnection(Request request) throws IOException {
166-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
166+
ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
167167
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
168-
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, request);
169168
Proxy proxy = null;
170-
if (!avoidProxy && proxyServer != null || realm != null) {
169+
if (proxyServer != null || realm != null) {
171170
try {
172171
proxy = configureProxyAndAuth(proxyServer, realm);
173172
} catch (AuthenticationException e) {
@@ -496,9 +495,8 @@ private void configure(URI uri, HttpURLConnection urlConnection, Request request
496495

497496
String ka = config.getAllowPoolingConnection() ? "keep-alive" : "close";
498497
urlConnection.setRequestProperty("Connection", ka);
499-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
500-
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, uri.getHost());
501-
if (!avoidProxy) {
498+
ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
499+
if (proxyServer != null) {
502500
urlConnection.setRequestProperty("Proxy-Connection", ka);
503501
if (proxyServer.getPrincipal() != null) {
504502
urlConnection.setRequestProperty("Proxy-Authorization", AuthenticatorUtils.computeBasicAuthentication(proxyServer));

api/src/main/java/com/ning/http/util/ProxyUtils.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414

1515
import static com.ning.http.util.MiscUtil.isNonEmpty;
1616

17+
import java.util.List;
18+
import java.util.Properties;
19+
20+
import com.ning.http.client.AsyncHttpClientConfig;
1721
import com.ning.http.client.ProxyServer;
1822
import com.ning.http.client.ProxyServer.Protocol;
1923
import com.ning.http.client.Request;
2024

21-
import java.util.List;
22-
import java.util.Properties;
23-
2425
/**
2526
* Utilities for Proxy handling.
2627
*
@@ -60,6 +61,19 @@ public class ProxyUtils {
6061
*/
6162
public static final String PROXY_PASSWORD = PROPERTY_PREFIX + "password";
6263

64+
/**
65+
* @param config the global config
66+
* @param request the request
67+
* @return the proxy server to be used for this request (can be null)
68+
*/
69+
public static ProxyServer getProxyServer(AsyncHttpClientConfig config, Request request) {
70+
ProxyServer proxyServer = request.getProxyServer();
71+
if (proxyServer == null) {
72+
proxyServer = config.getProxyServer();
73+
}
74+
return ProxyUtils.avoidProxy(proxyServer, request) ? null : proxyServer;
75+
}
76+
6377
/**
6478
* Checks whether proxy should be used according to nonProxyHosts settings of it, or we want to go directly to
6579
* target host. If <code>null</code> proxy is passed in, this method returns true -- since there is NO proxy, we

api/src/test/java/com/ning/http/client/async/ProxyTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ public void testNonProxyHosts() throws IOException, ExecutionException, TimeoutE
125125
}
126126
}
127127

128+
@Test(groups = { "standalone", "default_provider" })
129+
public void testNonProxyHostIssue202() throws IOException, ExecutionException, TimeoutException, InterruptedException {
130+
AsyncHttpClient client = getAsyncHttpClient(null);
131+
try {
132+
String target = "http://127.0.0.1:" + port1 + "/";
133+
Future<Response> f = client.prepareGet(target).setProxyServer(new ProxyServer("127.0.0.1", port1 - 1).addNonProxyHost("127.0.0.1")).execute();
134+
Response resp = f.get(3, TimeUnit.SECONDS);
135+
assertNotNull(resp);
136+
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
137+
assertEquals(resp.getHeader("target"), "/");
138+
} finally {
139+
client.close();
140+
}
141+
}
142+
128143
@Test(groups = { "standalone", "default_provider" })
129144
public void testProxyProperties() throws IOException, ExecutionException, TimeoutException, InterruptedException {
130145
Properties originalProps = System.getProperties();

providers/apache/src/main/java/com/ning/http/client/providers/apache/ApacheAsyncHttpProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,8 @@ private HttpMethodBase createMethod(HttpClient client, Request request) throws I
341341
throw new IllegalStateException(String.format("Invalid Method", methodName));
342342
}
343343

344-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
345-
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, request);
346-
if (!avoidProxy) {
344+
ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
345+
if (proxyServer != null) {
347346

348347
if (proxyServer.getPrincipal() != null) {
349348
Credentials defaultcreds = new UsernamePasswordCredentials(proxyServer.getPrincipal(), proxyServer.getPassword());

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

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ public GrizzlyAsyncHttpProvider(final AsyncHttpClientConfig clientConfig) {
201201
public <T> ListenableFuture<T> execute(final Request request,
202202
final AsyncHandler<T> handler) throws IOException {
203203

204-
final GrizzlyResponseFuture<T> future =
205-
new GrizzlyResponseFuture<T>(this, request, handler);
204+
final ProxyServer proxy = ProxyUtils.getProxyServer(clientConfig, request);
205+
final GrizzlyResponseFuture<T> future = new GrizzlyResponseFuture<T>(this, request, handler, proxy);
206206
future.setDelegate(SafeFutureImpl.<T>create());
207207
final CompletionHandler<Connection> connectHandler = new CompletionHandler<Connection>() {
208208
@Override
@@ -862,8 +862,8 @@ private boolean sendAsGrizzlyRequest(final Request request,
862862
builder.header(Header.Host, uri.getHost() + ':' + uri.getPort());
863863
}
864864
}
865-
final ProxyServer proxy = getProxyServer(request);
866-
final boolean useProxy = (proxy != null);
865+
final ProxyServer proxy = ProxyUtils.getProxyServer(config, request);
866+
final boolean useProxy = proxy != null;
867867
if (useProxy) {
868868
if ((secure || httpCtx.isWSRequest) && !httpCtx.isTunnelEstablished(ctx.getConnection())) {
869869
ctx.notifyDownstream(new SwitchingSSLFilter.SSLSwitchingEvent(false, ctx.getConnection()));
@@ -959,18 +959,6 @@ private void convertToUpgradeRequest(final HttpTransactionContext ctx) {
959959
ctx.requestUrl = sb.toString();
960960
}
961961

962-
963-
private ProxyServer getProxyServer(Request request) {
964-
965-
ProxyServer proxyServer = request.getProxyServer();
966-
if (proxyServer == null) {
967-
proxyServer = config.getProxyServer();
968-
}
969-
return proxyServer;
970-
971-
}
972-
973-
974962
private void addHeaders(final Request request,
975963
final HttpRequestPacket requestPacket) {
976964

@@ -2436,11 +2424,9 @@ Connection obtainConnection(final Request request,
24362424
final GrizzlyResponseFuture requestFuture)
24372425
throws IOException, ExecutionException, InterruptedException, TimeoutException {
24382426

2439-
final Connection c = (obtainConnection0(request,
2440-
requestFuture));
2427+
final Connection c = obtainConnection0(request, requestFuture, requestFuture.getProxyServer());
24412428
DO_NOT_CACHE.set(c, Boolean.TRUE);
24422429
return c;
2443-
24442430
}
24452431

24462432
void doAsyncConnect(final Request request,
@@ -2466,14 +2452,11 @@ void doAsyncConnect(final Request request,
24662452
}
24672453

24682454
private Connection obtainConnection0(final Request request,
2469-
final GrizzlyResponseFuture requestFuture)
2455+
final GrizzlyResponseFuture requestFuture,
2456+
final ProxyServer proxy)
24702457
throws IOException, ExecutionException, InterruptedException, TimeoutException {
24712458

24722459
final URI uri = request.getURI();
2473-
ProxyServer proxy = getProxyServer(request);
2474-
if (ProxyUtils.avoidProxy(proxy, request)) {
2475-
proxy = null;
2476-
}
24772460
String host = ((proxy != null) ? proxy.getHost() : uri.getHost());
24782461
int port = ((proxy != null) ? proxy.getPort() : uri.getPort());
24792462
int cTimeout = provider.clientConfig.getConnectionTimeoutInMs();

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.ning.http.client.providers.grizzly;
1515

1616
import com.ning.http.client.AsyncHandler;
17+
import com.ning.http.client.ProxyServer;
1718
import com.ning.http.client.Request;
1819
import com.ning.http.client.listenable.AbstractListenableFuture;
1920

@@ -41,7 +42,7 @@ public class GrizzlyResponseFuture<V> extends AbstractListenableFuture<V> {
4142
private final AsyncHandler handler;
4243
private final GrizzlyAsyncHttpProvider provider;
4344
private final Request request;
44-
45+
private final ProxyServer proxyServer;
4546
private Connection connection;
4647

4748
FutureImpl<V> delegate;
@@ -52,12 +53,13 @@ public class GrizzlyResponseFuture<V> extends AbstractListenableFuture<V> {
5253

5354
GrizzlyResponseFuture(final GrizzlyAsyncHttpProvider provider,
5455
final Request request,
55-
final AsyncHandler handler) {
56+
final AsyncHandler handler,
57+
final ProxyServer proxyServer) {
5658

5759
this.provider = provider;
5860
this.request = request;
5961
this.handler = handler;
60-
62+
this.proxyServer = proxyServer;
6163
}
6264

6365

@@ -204,4 +206,7 @@ private void closeConnection() {
204206

205207
}
206208

209+
public ProxyServer getProxyServer() {
210+
return proxyServer;
211+
}
207212
}

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -559,18 +559,14 @@ public void operationComplete(ChannelFuture cf) {
559559

560560
}
561561

562-
private static boolean isProxyServer(AsyncHttpClientConfig config, Request request) {
563-
return request.getProxyServer() != null || config.getProxyServer() != null;
564-
}
565-
566562
protected final static HttpRequest buildRequest(AsyncHttpClientConfig config, Request request, URI uri,
567-
boolean allowConnect, ChannelBuffer buffer) throws IOException {
563+
boolean allowConnect, ChannelBuffer buffer, ProxyServer proxyServer) throws IOException {
568564

569565
String method = request.getMethod();
570-
if (allowConnect && (isProxyServer(config, request) && isSecure(uri))) {
566+
if (allowConnect && proxyServer != null && isSecure(uri)) {
571567
method = HttpMethod.CONNECT.toString();
572568
}
573-
return construct(config, request, new HttpMethod(method), uri, buffer);
569+
return construct(config, request, new HttpMethod(method), uri, buffer, proxyServer);
574570
}
575571

576572
private static SpnegoEngine getSpnegoEngine() {
@@ -583,7 +579,8 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
583579
Request request,
584580
HttpMethod m,
585581
URI uri,
586-
ChannelBuffer buffer) throws IOException {
582+
ChannelBuffer buffer,
583+
ProxyServer proxyServer) throws IOException {
587584

588585
String host = AsyncHttpProviderUtils.getHost(uri);
589586
boolean webSocket = isWebSocket(uri);
@@ -597,7 +594,7 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
597594
nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_0, m, AsyncHttpProviderUtils.getAuthority(uri));
598595
} else {
599596
String path = null;
600-
if (isProxyServer(config, request))
597+
if (proxyServer != null)
601598
path = uri.toString();
602599
else if (uri.getRawQuery() != null)
603600
path = uri.getRawPath() + "?" + uri.getRawQuery();
@@ -648,7 +645,6 @@ else if (uri.getRawQuery() != null)
648645
nettyRequest.addHeader(HttpHeaders.Names.PROXY_AUTHORIZATION, auth.get(0));
649646
}
650647
}
651-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
652648
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
653649

654650
if (realm != null && realm.getUsePreemptiveAuth()) {
@@ -712,8 +708,7 @@ else if (uri.getRawQuery() != null)
712708
nettyRequest.setHeader(HttpHeaders.Names.CONNECTION, "keep-alive");
713709
}
714710

715-
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, request);
716-
if (!avoidProxy) {
711+
if (proxyServer != null) {
717712
if (!request.getHeaders().containsKey("Proxy-Connection")) {
718713
nettyRequest.setHeader("Proxy-Connection", "keep-alive");
719714
}
@@ -914,7 +909,7 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
914909
throw new IOException("WebSocket method must be a GET");
915910
}
916911

917-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
912+
ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
918913
URI uri;
919914
if (useRawUrl) {
920915
uri = request.getRawURI();
@@ -939,12 +934,12 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
939934

940935
boolean useSSl = isSecure(uri) && proxyServer == null;
941936
if (channel != null && channel.isOpen() && channel.isConnected()) {
942-
HttpRequest nettyRequest = buildRequest(config, request, uri, f == null ? false : f.isConnectAllowed(), bufferedBytes);
937+
HttpRequest nettyRequest = buildRequest(config, request, uri, f == null ? false : f.isConnectAllowed(), bufferedBytes, proxyServer);
943938

944939
if (f == null) {
945-
f = newFuture(uri, request, asyncHandler, nettyRequest, config, this);
940+
f = newFuture(uri, request, asyncHandler, nettyRequest, config, this, proxyServer);
946941
} else {
947-
nettyRequest = buildRequest(config, request, uri, f.isConnectAllowed(), bufferedBytes);
942+
nettyRequest = buildRequest(config, request, uri, f.isConnectAllowed(), bufferedBytes, proxyServer);
948943
f.setNettyRequest(nettyRequest);
949944
}
950945
f.setState(NettyResponseFuture.STATE.POOLED);
@@ -1676,10 +1671,11 @@ public static <T> NettyResponseFuture<T> newFuture(URI uri,
16761671
AsyncHandler<T> asyncHandler,
16771672
HttpRequest nettyRequest,
16781673
AsyncHttpClientConfig config,
1679-
NettyAsyncHttpProvider provider) {
1674+
NettyAsyncHttpProvider provider,
1675+
ProxyServer proxyServer) {
16801676

16811677
NettyResponseFuture<T> f = new NettyResponseFuture<T>(uri, request, asyncHandler, nettyRequest,
1682-
requestTimeout(config, request.getPerRequestConfig()), config.getIdleConnectionTimeoutInMs(), provider, request.getConnectionPoolKeyStrategy());
1678+
requestTimeout(config, request.getPerRequestConfig()), config.getIdleConnectionTimeoutInMs(), provider, request.getConnectionPoolKeyStrategy(), proxyServer);
16831679

16841680
if (request.getHeaders().getFirstValue("Expect") != null
16851681
&& request.getHeaders().getFirstValue("Expect").equalsIgnoreCase("100-Continue")) {
@@ -2092,6 +2088,7 @@ public void handle(final ChannelHandlerContext ctx, final MessageEvent e) throws
20922088
HttpRequest nettyRequest = future.getNettyRequest();
20932089
AsyncHandler handler = future.getAsyncHandler();
20942090
Request request = future.getRequest();
2091+
ProxyServer proxyServer = future.getProxyServer();
20952092
HttpResponse response = null;
20962093
try {
20972094
if (e.getMessage() instanceof HttpResponse) {
@@ -2141,7 +2138,6 @@ public void handle(final ChannelHandlerContext ctx, final MessageEvent e) throws
21412138
}
21422139

21432140
Realm newRealm = null;
2144-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
21452141
final FluentCaseInsensitiveStringsMap headers = request.getHeaders();
21462142
final RequestBuilder builder = new RequestBuilder(future.getRequest());
21472143

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
*/
1717
package com.ning.http.client.providers.netty;
1818

19-
import com.ning.http.client.AsyncHandler;
20-
import com.ning.http.client.AsyncHttpClientConfig;
21-
import com.ning.http.client.Request;
19+
import java.io.IOException;
20+
import java.net.ConnectException;
21+
import java.net.URI;
22+
import java.nio.channels.ClosedChannelException;
23+
import java.util.concurrent.atomic.AtomicBoolean;
24+
25+
import javax.net.ssl.HostnameVerifier;
26+
2227
import org.jboss.netty.buffer.ChannelBuffer;
2328
import org.jboss.netty.channel.Channel;
2429
import org.jboss.netty.channel.ChannelFuture;
@@ -28,12 +33,11 @@
2833
import org.slf4j.Logger;
2934
import org.slf4j.LoggerFactory;
3035

31-
import javax.net.ssl.HostnameVerifier;
32-
import java.io.IOException;
33-
import java.net.ConnectException;
34-
import java.net.URI;
35-
import java.nio.channels.ClosedChannelException;
36-
import java.util.concurrent.atomic.AtomicBoolean;
36+
import com.ning.http.client.AsyncHandler;
37+
import com.ning.http.client.AsyncHttpClientConfig;
38+
import com.ning.http.client.ProxyServer;
39+
import com.ning.http.client.Request;
40+
import com.ning.http.util.ProxyUtils;
3741

3842

3943
/**
@@ -135,9 +139,10 @@ public Builder(AsyncHttpClientConfig config, Request request, AsyncHandler<T> as
135139
}
136140

137141
public NettyConnectListener<T> build(final URI uri) throws IOException {
138-
HttpRequest nettyRequest = NettyAsyncHttpProvider.buildRequest(config, request, uri, true, buffer);
142+
ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
143+
HttpRequest nettyRequest = NettyAsyncHttpProvider.buildRequest(config, request, uri, true, buffer, proxyServer);
139144
if (future == null) {
140-
future = NettyAsyncHttpProvider.newFuture(uri, request, asyncHandler, nettyRequest, config, provider);
145+
future = NettyAsyncHttpProvider.newFuture(uri, request, asyncHandler, nettyRequest, config, provider, proxyServer);
141146
} else {
142147
future.setNettyRequest(nettyRequest);
143148
future.setRequest(request);

0 commit comments

Comments
 (0)