Skip to content

Commit a6c2537

Browse files
author
Stephane Landelle
committed
Merge pull request AsyncHttpClient#250 from AsyncHttpClient/non-proxy-hosts-refactoring
Refactor non proxy hosts handling, fix AsyncHttpClient#202 in 1.7.x
2 parents ce7b467 + 6c8c031 commit a6c2537

File tree

9 files changed

+81
-88
lines changed

9 files changed

+81
-88
lines changed

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
@@ -340,9 +340,8 @@ private HttpMethodBase createMethod(HttpClient client, Request request) throws I
340340
throw new IllegalStateException(String.format("Invalid Method", methodName));
341341
}
342342

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

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

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

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

203-
final GrizzlyResponseFuture<T> future =
204-
new GrizzlyResponseFuture<T>(this, request, handler);
203+
final ProxyServer proxy = ProxyUtils.getProxyServer(clientConfig, request);
204+
final GrizzlyResponseFuture<T> future = new GrizzlyResponseFuture<T>(this, request, handler, proxy);
205205
future.setDelegate(SafeFutureImpl.<T>create());
206206
final CompletionHandler<Connection> connectHandler = new CompletionHandler<Connection>() {
207207
@Override
@@ -816,24 +816,6 @@ public NextAction handleEvent(final FilterChainContext ctx,
816816

817817
}
818818

819-
// @Override
820-
// public NextAction handleRead(FilterChainContext ctx) throws IOException {
821-
// Object message = ctx.getMessage();
822-
// if (HttpPacket.isHttp(message)) {
823-
// final HttpPacket packet = (HttpPacket) message;
824-
// HttpResponsePacket responsePacket;
825-
// if (HttpContent.isContent(packet)) {
826-
// responsePacket = (HttpResponsePacket) ((HttpContent) packet).getHttpHeader();
827-
// } else {
828-
// responsePacket = (HttpResponsePacket) packet;
829-
// }
830-
// if (HttpStatus.SWITCHING_PROTOCOLS_101.statusMatches(responsePacket.getStatus())) {
831-
// return ctx.getStopAction();
832-
// }
833-
// }
834-
// return super.handleRead(ctx);
835-
// }
836-
837819
// ----------------------------------------------------- Private Methods
838820

839821

@@ -861,9 +843,8 @@ private boolean sendAsGrizzlyRequest(final Request request,
861843
builder.header(Header.Host, uri.getHost() + ':' + uri.getPort());
862844
}
863845
}
864-
final ProxyServer proxy = getProxyServer(request);
865-
boolean avoidProxy = ProxyUtils.avoidProxy(proxy, request);
866-
final boolean useProxy = !(avoidProxy || proxy == null);
846+
final ProxyServer proxy = ProxyUtils.getProxyServer(config, request);
847+
final boolean useProxy = proxy != null;
867848
if (useProxy) {
868849
if ((secure || httpCtx.isWSRequest) && !httpCtx.isTunnelEstablished(ctx.getConnection())) {
869850
secure = false;
@@ -956,18 +937,6 @@ private void convertToUpgradeRequest(final HttpTransactionContext ctx) {
956937
ctx.requestUrl = sb.toString();
957938
}
958939

959-
960-
private ProxyServer getProxyServer(Request request) {
961-
962-
ProxyServer proxyServer = request.getProxyServer();
963-
if (proxyServer == null) {
964-
proxyServer = config.getProxyServer();
965-
}
966-
return proxyServer;
967-
968-
}
969-
970-
971940
private void addHeaders(final Request request,
972941
final HttpRequestPacket requestPacket) {
973942

@@ -2329,8 +2298,7 @@ Connection obtainConnection(final Request request,
23292298
final GrizzlyResponseFuture requestFuture)
23302299
throws IOException, ExecutionException, InterruptedException, TimeoutException {
23312300

2332-
final Connection c = (obtainConnection0(request,
2333-
requestFuture));
2301+
final Connection c = obtainConnection0(request, requestFuture);
23342302
DO_NOT_CACHE.set(c, Boolean.TRUE);
23352303
return c;
23362304

@@ -2341,10 +2309,7 @@ void doAsyncConnect(final Request request,
23412309
final CompletionHandler<Connection> connectHandler)
23422310
throws IOException, ExecutionException, InterruptedException {
23432311

2344-
ProxyServer proxy = getProxyServer(request);
2345-
if (ProxyUtils.avoidProxy(proxy, request)) {
2346-
proxy = null;
2347-
}
2312+
ProxyServer proxy = requestFuture.getProxy();
23482313
final URI uri = request.getURI();
23492314
String host = ((proxy != null) ? proxy.getHost() : uri.getHost());
23502315
int port = ((proxy != null) ? proxy.getPort() : uri.getPort());
@@ -2363,10 +2328,7 @@ private Connection obtainConnection0(final Request request,
23632328
throws IOException, ExecutionException, InterruptedException, TimeoutException {
23642329

23652330
final URI uri = request.getURI();
2366-
ProxyServer proxy = getProxyServer(request);
2367-
if (ProxyUtils.avoidProxy(proxy, request)) {
2368-
proxy = null;
2369-
}
2331+
final ProxyServer proxy = requestFuture.getProxy();
23702332
String host = ((proxy != null) ? proxy.getHost() : uri.getHost());
23712333
int port = ((proxy != null) ? proxy.getPort() : uri.getPort());
23722334
int cTimeout = provider.clientConfig.getConnectionTimeoutInMs();

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

@@ -42,7 +43,7 @@ public class GrizzlyResponseFuture<V> extends AbstractListenableFuture<V> {
4243
private final AsyncHandler handler;
4344
private final GrizzlyAsyncHttpProvider provider;
4445
private final Request request;
45-
46+
private final ProxyServer proxy;
4647
private Connection connection;
4748

4849
FutureImpl<V> delegate;
@@ -53,12 +54,13 @@ public class GrizzlyResponseFuture<V> extends AbstractListenableFuture<V> {
5354

5455
GrizzlyResponseFuture(final GrizzlyAsyncHttpProvider provider,
5556
final Request request,
56-
final AsyncHandler handler) {
57+
final AsyncHandler handler,
58+
final ProxyServer proxy) {
5759

5860
this.provider = provider;
5961
this.request = request;
6062
this.handler = handler;
61-
63+
this.proxy = proxy;
6264
}
6365

6466

@@ -208,4 +210,7 @@ private void closeConnection() {
208210

209211
}
210212

213+
public ProxyServer getProxy() {
214+
return proxy;
215+
}
211216
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,10 @@ public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler,
131131
throw new IOException(String.format("Too many connections %s", config.getMaxTotalConnections()));
132132
}
133133

134-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
134+
ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
135135
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
136-
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, request);
137136
Proxy proxy = null;
138-
if (!avoidProxy && (proxyServer != null || realm != null)) {
137+
if (proxyServer != null || realm != null) {
139138
try {
140139
proxy = configureProxyAndAuth(proxyServer, realm);
141140
} catch (AuthenticationException e) {
@@ -497,7 +496,7 @@ private void configure(URI uri, HttpURLConnection urlConnection, Request request
497496

498497
String ka = config.getAllowPoolingConnection() ? "keep-alive" : "close";
499498
urlConnection.setRequestProperty("Connection", ka);
500-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
499+
ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
501500
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, uri.getHost());
502501
if (!avoidProxy) {
503502
urlConnection.setRequestProperty("Proxy-Connection", ka);

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

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -595,26 +595,14 @@ public void operationComplete(ChannelFuture cf) {
595595

596596
}
597597

598-
private static boolean isProxyServer(AsyncHttpClientConfig config, Request request) {
599-
ProxyServer proxyServer = request.getProxyServer();
600-
if (proxyServer == null) {
601-
proxyServer = config.getProxyServer();
602-
}
603-
if (proxyServer == null) {
604-
return false;
605-
} else {
606-
return !ProxyUtils.avoidProxy(proxyServer, request);
607-
}
608-
}
609-
610598
protected final static HttpRequest buildRequest(AsyncHttpClientConfig config, Request request, URI uri,
611-
boolean allowConnect, ChannelBuffer buffer) throws IOException {
599+
boolean allowConnect, ChannelBuffer buffer, ProxyServer proxyServer) throws IOException {
612600

613601
String method = request.getMethod();
614-
if (allowConnect && (isProxyServer(config, request) && isSecure(uri))) {
602+
if (allowConnect && proxyServer != null && isSecure(uri)) {
615603
method = HttpMethod.CONNECT.toString();
616604
}
617-
return construct(config, request, new HttpMethod(method), uri, buffer);
605+
return construct(config, request, new HttpMethod(method), uri, buffer, proxyServer);
618606
}
619607

620608
private static SpnegoEngine getSpnegoEngine() {
@@ -627,7 +615,8 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
627615
Request request,
628616
HttpMethod m,
629617
URI uri,
630-
ChannelBuffer buffer) throws IOException {
618+
ChannelBuffer buffer,
619+
ProxyServer proxyServer) throws IOException {
631620

632621
String host = AsyncHttpProviderUtils.getHost(uri);
633622

@@ -640,7 +629,7 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
640629
nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_0, m, AsyncHttpProviderUtils.getAuthority(uri));
641630
} else {
642631
String path = null;
643-
if (isProxyServer(config, request))
632+
if (proxyServer != null)
644633
path = uri.toString();
645634
else if (uri.getRawQuery() != null)
646635
path = uri.getRawPath() + "?" + uri.getRawQuery();
@@ -690,7 +679,6 @@ else if (uri.getRawQuery() != null)
690679
nettyRequest.addHeader(HttpHeaders.Names.PROXY_AUTHORIZATION, auth.get(0));
691680
}
692681
}
693-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
694682
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
695683

696684
if (realm != null && realm.getUsePreemptiveAuth()) {
@@ -754,8 +742,7 @@ else if (uri.getRawQuery() != null)
754742
nettyRequest.setHeader(HttpHeaders.Names.CONNECTION, "keep-alive");
755743
}
756744

757-
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, request);
758-
if (!avoidProxy) {
745+
if (proxyServer != null) {
759746
if (!request.getHeaders().containsKey("Proxy-Connection")) {
760747
nettyRequest.setHeader("Proxy-Connection", "keep-alive");
761748
}
@@ -958,7 +945,9 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
958945
throw new IOException("WebSocket method must be a GET");
959946
}
960947

961-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
948+
ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
949+
boolean useProxy = proxyServer != null;
950+
962951
URI uri;
963952
if (useRawUrl) {
964953
uri = request.getRawURI();
@@ -981,17 +970,14 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
981970
bufferedBytes = f.getNettyRequest().getContent();
982971
}
983972

984-
boolean avoidProxy = ProxyUtils.avoidProxy(proxyServer, uri.getHost());
985-
boolean useProxy = !(avoidProxy || proxyServer == null);
986-
987973
boolean useSSl = isSecure(uri) && !useProxy;
988974
if (channel != null && channel.isOpen() && channel.isConnected()) {
989-
HttpRequest nettyRequest = buildRequest(config, request, uri, f == null ? false : f.isConnectAllowed(), bufferedBytes);
975+
HttpRequest nettyRequest = buildRequest(config, request, uri, f == null ? false : f.isConnectAllowed(), bufferedBytes, proxyServer);
990976

991977
if (f == null) {
992-
f = newFuture(uri, request, asyncHandler, nettyRequest, config, this);
978+
f = newFuture(uri, request, asyncHandler, nettyRequest, config, this, proxyServer);
993979
} else {
994-
nettyRequest = buildRequest(config, request, uri, f.isConnectAllowed(), bufferedBytes);
980+
nettyRequest = buildRequest(config, request, uri, f.isConnectAllowed(), bufferedBytes, proxyServer);
995981
f.setNettyRequest(nettyRequest);
996982
}
997983
f.setState(NettyResponseFuture.STATE.POOLED);
@@ -1729,10 +1715,11 @@ public static <T> NettyResponseFuture<T> newFuture(URI uri,
17291715
AsyncHandler<T> asyncHandler,
17301716
HttpRequest nettyRequest,
17311717
AsyncHttpClientConfig config,
1732-
NettyAsyncHttpProvider provider) {
1718+
NettyAsyncHttpProvider provider,
1719+
ProxyServer proxyServer) {
17331720

17341721
NettyResponseFuture<T> f = new NettyResponseFuture<T>(uri, request, asyncHandler, nettyRequest,
1735-
requestTimeout(config, request.getPerRequestConfig()), config.getIdleConnectionTimeoutInMs(), provider, request.getConnectionPoolKeyStrategy());
1722+
requestTimeout(config, request.getPerRequestConfig()), config.getIdleConnectionTimeoutInMs(), provider, request.getConnectionPoolKeyStrategy(), proxyServer);
17361723

17371724
if (request.getHeaders().getFirstValue("Expect") != null
17381725
&& request.getHeaders().getFirstValue("Expect").equalsIgnoreCase("100-Continue")) {
@@ -2145,6 +2132,7 @@ public void handle(final ChannelHandlerContext ctx, final MessageEvent e) throws
21452132
HttpRequest nettyRequest = future.getNettyRequest();
21462133
AsyncHandler handler = future.getAsyncHandler();
21472134
Request request = future.getRequest();
2135+
ProxyServer proxyServer = future.getProxyServer();
21482136
HttpResponse response = null;
21492137
try {
21502138
if (e.getMessage() instanceof HttpResponse) {
@@ -2194,7 +2182,6 @@ public void handle(final ChannelHandlerContext ctx, final MessageEvent e) throws
21942182
}
21952183

21962184
Realm newRealm = null;
2197-
ProxyServer proxyServer = request.getProxyServer() != null ? request.getProxyServer() : config.getProxyServer();
21982185
final FluentCaseInsensitiveStringsMap headers = request.getHeaders();
21992186
final RequestBuilder builder = new RequestBuilder(future.getRequest());
22002187

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
import com.ning.http.client.AsyncHandler;
2020
import com.ning.http.client.AsyncHttpClientConfig;
21+
import com.ning.http.client.ProxyServer;
2122
import com.ning.http.client.Request;
2223
import com.ning.http.util.AllowAllHostnameVerifier;
24+
import com.ning.http.util.ProxyUtils;
25+
2326
import org.jboss.netty.buffer.ChannelBuffer;
2427
import org.jboss.netty.channel.Channel;
2528
import org.jboss.netty.channel.ChannelFuture;
@@ -137,9 +140,10 @@ public Builder(AsyncHttpClientConfig config, Request request, AsyncHandler<T> as
137140
}
138141

139142
public NettyConnectListener<T> build(final URI uri) throws IOException {
140-
HttpRequest nettyRequest = NettyAsyncHttpProvider.buildRequest(config, request, uri, true, buffer);
143+
ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
144+
HttpRequest nettyRequest = NettyAsyncHttpProvider.buildRequest(config, request, uri, true, buffer, proxyServer);
141145
if (future == null) {
142-
future = NettyAsyncHttpProvider.newFuture(uri, request, asyncHandler, nettyRequest, config, provider);
146+
future = NettyAsyncHttpProvider.newFuture(uri, request, asyncHandler, nettyRequest, config, provider, proxyServer);
143147
} else {
144148
future.setNettyRequest(nettyRequest);
145149
future.setRequest(request);

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.ning.http.client.AsyncHandler;
1919
import com.ning.http.client.ConnectionPoolKeyStrategy;
20+
import com.ning.http.client.ProxyServer;
2021
import com.ning.http.client.Request;
2122
import com.ning.http.client.listenable.AbstractListenableFuture;
2223
import org.jboss.netty.channel.Channel;
@@ -87,6 +88,7 @@ enum STATE {
8788
private final AtomicBoolean throwableCalled = new AtomicBoolean(false);
8889
private boolean allowConnect = false;
8990
private final ConnectionPoolKeyStrategy connectionPoolKeyStrategy;
91+
private final ProxyServer proxyServer;
9092

9193
public NettyResponseFuture(URI uri,
9294
Request request,
@@ -95,7 +97,8 @@ public NettyResponseFuture(URI uri,
9597
int responseTimeoutInMs,
9698
int idleConnectionTimeoutInMs,
9799
NettyAsyncHttpProvider asyncHttpProvider,
98-
ConnectionPoolKeyStrategy connectionPoolKeyStrategy) {
100+
ConnectionPoolKeyStrategy connectionPoolKeyStrategy,
101+
ProxyServer proxyServer) {
99102

100103
this.asyncHandler = asyncHandler;
101104
this.responseTimeoutInMs = responseTimeoutInMs;
@@ -105,6 +108,7 @@ public NettyResponseFuture(URI uri,
105108
this.uri = uri;
106109
this.asyncHttpProvider = asyncHttpProvider;
107110
this.connectionPoolKeyStrategy = connectionPoolKeyStrategy;
111+
this.proxyServer = proxyServer;
108112

109113
if (System.getProperty(MAX_RETRY) != null) {
110114
maxRetry = Integer.valueOf(System.getProperty(MAX_RETRY));
@@ -127,6 +131,10 @@ public ConnectionPoolKeyStrategy getConnectionPoolKeyStrategy() {
127131
return connectionPoolKeyStrategy;
128132
}
129133

134+
public ProxyServer getProxyServer() {
135+
return proxyServer;
136+
}
137+
130138
/**
131139
* {@inheritDoc}
132140
*/

0 commit comments

Comments
 (0)