Skip to content

Commit d7f0eb6

Browse files
committed
Introduce AddressNameResolver, close AsyncHttpClient#859
1 parent 2c1474c commit d7f0eb6

File tree

7 files changed

+104
-23
lines changed

7 files changed

+104
-23
lines changed

src/main/java/com/ning/http/client/AsyncHandlerExtensions.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package com.ning.http.client;
1414

15+
import java.net.InetAddress;
16+
1517
/**
1618
* This interface hosts new low level callback methods on {@link AsyncHandler}.
1719
* For now, those methods are in a dedicated interface in order not to break the existing API,
@@ -62,8 +64,10 @@ public interface AsyncHandlerExtensions {
6264

6365
/**
6466
* Notify the callback after DNS resolution has completed.
67+
*
68+
* @param address the resolved address
6569
*/
66-
void onDnsResolved();
70+
void onDnsResolved(InetAddress address);
6771

6872
/**
6973
* Notify the callback when the SSL handshake performed to establish an HTTPS connection has been completed.

src/main/java/com/ning/http/client/ConnectionPoolPartitioning.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
/*
2-
* Copyright 2010 Ning, Inc.
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
33
*
4-
* Ning licenses this file to you under the Apache License, version 2.0
5-
* (the "License"); you may not use this file except in compliance with the
6-
* License. You may obtain a copy of the License at:
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12-
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13-
* License for the specific language governing permissions and limitations
14-
* under the License.
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1512
*/
1613
package com.ning.http.client;
1714

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2015 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.client;
14+
15+
import java.net.InetAddress;
16+
import java.net.UnknownHostException;
17+
18+
public interface NameResolver {
19+
20+
InetAddress resolve(String name) throws UnknownHostException;
21+
22+
public enum JdkNameResolver implements NameResolver {
23+
24+
INSTANCE;
25+
26+
@Override
27+
public InetAddress resolve(String name) throws UnknownHostException {
28+
return InetAddress.getByName(name);
29+
}
30+
}
31+
}

src/main/java/com/ning/http/client/Request.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,6 @@ public interface Request {
190190
String getBodyEncoding();
191191

192192
ConnectionPoolPartitioning getConnectionPoolPartitioning();
193+
194+
NameResolver getNameResolver();
193195
}

src/main/java/com/ning/http/client/RequestBuilderBase.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private static final class RequestImpl implements Request {
6969
private long rangeOffset;
7070
public String charset;
7171
private ConnectionPoolPartitioning connectionPoolPartitioning = ConnectionPoolPartitioning.PerHostConnectionPoolPartitioning.INSTANCE;
72+
private NameResolver nameResolver = NameResolver.JdkNameResolver.INSTANCE;
7273
private List<Param> queryParams;
7374

7475
public RequestImpl() {
@@ -99,105 +100,135 @@ public RequestImpl(Request prototype) {
99100
this.rangeOffset = prototype.getRangeOffset();
100101
this.charset = prototype.getBodyEncoding();
101102
this.connectionPoolPartitioning = prototype.getConnectionPoolPartitioning();
103+
this.nameResolver = prototype.getNameResolver();
102104
}
103105
}
104106

107+
@Override
105108
public String getMethod() {
106109
return method;
107110
}
108111

112+
@Override
109113
public InetAddress getInetAddress() {
110114
return address;
111115
}
112116

117+
@Override
113118
public InetAddress getLocalAddress() {
114119
return localAddress;
115120
}
116121

122+
@Override
117123
public Uri getUri() {
118124
return uri;
119125
}
120126

127+
@Override
121128
public String getUrl() {
122129
return uri.toUrl();
123130
}
124131

132+
@Override
125133
public FluentCaseInsensitiveStringsMap getHeaders() {
126134
return headers;
127135
}
128136

137+
@Override
129138
public Collection<Cookie> getCookies() {
130139
return cookies != null ? Collections.unmodifiableCollection(cookies) : Collections.<Cookie> emptyList();
131140
}
132141

142+
@Override
133143
public byte[] getByteData() {
134144
return byteData;
135145
}
136146

147+
@Override
137148
public List<byte[]> getCompositeByteData() {
138149
return compositeByteData;
139150
}
140151

152+
@Override
141153
public String getStringData() {
142154
return stringData;
143155
}
144156

157+
@Override
145158
public InputStream getStreamData() {
146159
return streamData;
147160
}
148161

162+
@Override
149163
public BodyGenerator getBodyGenerator() {
150164
return bodyGenerator;
151165
}
152166

167+
@Override
153168
public long getContentLength() {
154169
return length;
155170
}
156171

172+
@Override
157173
public List<Param> getFormParams() {
158174
return formParams != null ? formParams : Collections.<Param> emptyList();
159175
}
160176

177+
@Override
161178
public List<Part> getParts() {
162179
return parts != null ? parts : Collections.<Part> emptyList();
163180
}
164181

182+
@Override
165183
public String getVirtualHost() {
166184
return virtualHost;
167185
}
168186

187+
@Override
169188
public ProxyServer getProxyServer() {
170189
return proxyServer;
171190
}
172191

192+
@Override
173193
public Realm getRealm() {
174194
return realm;
175195
}
176196

197+
@Override
177198
public File getFile() {
178199
return file;
179200
}
180201

202+
@Override
181203
public Boolean getFollowRedirect() {
182204
return followRedirects;
183205
}
184206

207+
@Override
185208
public int getRequestTimeout() {
186209
return requestTimeout;
187210
}
188211

212+
@Override
189213
public long getRangeOffset() {
190214
return rangeOffset;
191215
}
192216

217+
@Override
193218
public String getBodyEncoding() {
194219
return charset;
195220
}
196221

222+
@Override
197223
public ConnectionPoolPartitioning getConnectionPoolPartitioning() {
198224
return connectionPoolPartitioning;
199225
}
200226

227+
@Override
228+
public NameResolver getNameResolver() {
229+
return nameResolver;
230+
}
231+
201232
@Override
202233
public List<Param> getQueryParams() {
203234
if (queryParams == null)
@@ -539,6 +570,11 @@ public T setConnectionPoolKeyStrategy(ConnectionPoolPartitioning connectionPoolK
539570
return derived.cast(this);
540571
}
541572

573+
public T setNameResolver(NameResolver nameResolver) {
574+
request.nameResolver = nameResolver;
575+
return derived.cast(this);
576+
}
577+
542578
public T setSignatureCalculator(SignatureCalculator signatureCalculator) {
543579
this.signatureCalculator = signatureCalculator;
544580
return derived.cast(this);

src/main/java/com/ning/http/client/providers/netty/request/NettyRequestSender.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import static com.ning.http.client.providers.netty.util.HttpUtils.WEBSOCKET;
1616
import static com.ning.http.client.providers.netty.util.HttpUtils.isSecure;
1717
import static com.ning.http.client.providers.netty.util.HttpUtils.useProxyConnect;
18+
import static com.ning.http.util.AsyncHttpProviderUtils.REMOTELY_CLOSED_EXCEPTION;
1819
import static com.ning.http.util.AsyncHttpProviderUtils.getDefaultPort;
1920
import static com.ning.http.util.AsyncHttpProviderUtils.requestTimeout;
20-
import static com.ning.http.util.AsyncHttpProviderUtils.REMOTELY_CLOSED_EXCEPTION;
2121
import static com.ning.http.util.ProxyUtils.avoidProxy;
2222
import static com.ning.http.util.ProxyUtils.getProxyServer;
2323

@@ -57,7 +57,9 @@
5757
import com.ning.http.client.ws.WebSocketUpgradeHandler;
5858

5959
import java.io.IOException;
60+
import java.net.InetAddress;
6061
import java.net.InetSocketAddress;
62+
import java.net.UnknownHostException;
6163
import java.util.Map;
6264
import java.util.concurrent.TimeUnit;
6365
import java.util.concurrent.atomic.AtomicBoolean;
@@ -347,22 +349,30 @@ public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) {
347349
}
348350
}
349351

350-
private InetSocketAddress remoteAddress(Request request, Uri uri, ProxyServer proxy, boolean useProxy) {
351-
if (request.getInetAddress() != null)
352-
return new InetSocketAddress(request.getInetAddress(), getDefaultPort(uri));
352+
private InetSocketAddress remoteAddress(Request request, Uri uri, ProxyServer proxy, boolean useProxy) throws UnknownHostException {
353353

354-
else if (!useProxy || avoidProxy(proxy, uri.getHost()))
355-
return new InetSocketAddress(uri.getHost(), getDefaultPort(uri));
354+
InetAddress address;
355+
int port = getDefaultPort(uri);
356356

357-
else
358-
return new InetSocketAddress(proxy.getHost(), proxy.getPort());
357+
if (request.getInetAddress() != null) {
358+
address = request.getInetAddress();
359+
360+
} else if (!useProxy || avoidProxy(proxy, uri.getHost())) {
361+
address = request.getNameResolver().resolve(uri.getHost());
362+
363+
} else {
364+
address = request.getNameResolver().resolve(proxy.getHost());
365+
port = proxy.getPort();
366+
}
367+
368+
return new InetSocketAddress(address, port);
359369
}
360370

361-
private ChannelFuture connect(Request request, Uri uri, ProxyServer proxy, boolean useProxy, ClientBootstrap bootstrap, AsyncHandler<?> asyncHandler) {
371+
private ChannelFuture connect(Request request, Uri uri, ProxyServer proxy, boolean useProxy, ClientBootstrap bootstrap, AsyncHandler<?> asyncHandler) throws UnknownHostException {
362372
InetSocketAddress remoteAddress = remoteAddress(request, uri, proxy, useProxy);
363373

364374
if (asyncHandler instanceof AsyncHandlerExtensions)
365-
AsyncHandlerExtensions.class.cast(asyncHandler).onDnsResolved();
375+
AsyncHandlerExtensions.class.cast(asyncHandler).onDnsResolved(remoteAddress.getAddress());
366376

367377
if (request.getLocalAddress() != null)
368378
return bootstrap.connect(remoteAddress, new InetSocketAddress(request.getLocalAddress(), 0));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.ning.http.client.HttpResponseStatus;
2222
import com.ning.http.client.Response;
2323

24+
import java.net.InetAddress;
2425
import java.util.Queue;
2526
import java.util.concurrent.ConcurrentLinkedQueue;
2627
import java.util.concurrent.CountDownLatch;
@@ -101,7 +102,7 @@ public void onRetry() {
101102
}
102103

103104
@Override
104-
public void onDnsResolved() {
105+
public void onDnsResolved(InetAddress address) {
105106
firedEvents.add("DnsResolved");
106107
}
107108

0 commit comments

Comments
 (0)