aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeil Fuller <nfuller@google.com>2014-12-02 10:45:07 +0000
committerNeil Fuller <nfuller@google.com>2014-12-04 17:50:17 +0000
commit797fdc6cc6bf16372e9464f189b535148f944ce9 (patch)
tree9bdfaff8374e231be866357f2d2bad94607db31b
parent442180cdae6a99817e98724901cb5f5263025e64 (diff)
downloadandroid_external_okhttp-797fdc6cc6bf16372e9464f189b535148f944ce9.tar.gz
android_external_okhttp-797fdc6cc6bf16372e9464f189b535148f944ce9.tar.bz2
android_external_okhttp-797fdc6cc6bf16372e9464f189b535148f944ce9.zip
Avoid a reverse DNS-lookup for a numeric proxy address
When the proxy is specified as an IP address the RouteSelector would force a reverse-lookup. This can introduce extra latency when the DNS is slow or missing. It also then turns the name back into a set of IPs, which may result in the original IP address not being given priority. This is upstream commit: https://github.com/square/okhttp/commit/9acd5e7b54fb55f62a5dd16865c30787dfba4c8b Bug: 18327075 Change-Id: Idafd72f4d5b901e267081f924377db4ada9afe1b
-rw-r--r--okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/RouteSelectorTest.java18
-rw-r--r--okhttp/src/main/java/com/squareup/okhttp/internal/http/RouteSelector.java20
2 files changed, 37 insertions, 1 deletions
diff --git a/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/RouteSelectorTest.java b/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/RouteSelectorTest.java
index dd806c6..e4f5a5a 100644
--- a/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/RouteSelectorTest.java
+++ b/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/RouteSelectorTest.java
@@ -336,6 +336,24 @@ public final class RouteSelectorTest {
assertEquals(regularRoutes.size(), routesWithFailedRoute.size());
}
+ @Test public void getHostString() throws Exception {
+ // Name proxy specification.
+ InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("host", 1234);
+ assertEquals("host", RouteSelector.getHostString(socketAddress));
+ socketAddress = InetSocketAddress.createUnresolved("127.0.0.1", 1234);
+ assertEquals("127.0.0.1", RouteSelector.getHostString(socketAddress));
+
+ // InetAddress proxy specification.
+ socketAddress = new InetSocketAddress(InetAddress.getByName("localhost"), 1234);
+ assertEquals("127.0.0.1", RouteSelector.getHostString(socketAddress));
+ socketAddress = new InetSocketAddress(
+ InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }), 1234);
+ assertEquals("127.0.0.1", RouteSelector.getHostString(socketAddress));
+ socketAddress = new InetSocketAddress(
+ InetAddress.getByAddress("foobar", new byte[] { 127, 0, 0, 1 }), 1234);
+ assertEquals("127.0.0.1", RouteSelector.getHostString(socketAddress));
+ }
+
private void assertConnection(Connection connection, Address address, Proxy proxy,
InetAddress socketAddress, int socketPort) {
assertEquals(address, connection.getRoute().getAddress());
diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/RouteSelector.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/RouteSelector.java
index a5ffa77..876bac4 100644
--- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/RouteSelector.java
+++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/RouteSelector.java
@@ -197,7 +197,7 @@ public final class RouteSelector {
"Proxy.address() is not an " + "InetSocketAddress: " + proxyAddress.getClass());
}
InetSocketAddress proxySocketAddress = (InetSocketAddress) proxyAddress;
- socketHost = proxySocketAddress.getHostName();
+ socketHost = getHostString(proxySocketAddress);
socketPort = proxySocketAddress.getPort();
}
@@ -206,6 +206,24 @@ public final class RouteSelector {
nextSocketAddressIndex = 0;
}
+ /**
+ * Obtain a "host" from an {@link InetSocketAddress}. This returns a string containing either an
+ * actual host name or a numeric IP address.
+ */
+ // Visible for testing
+ static String getHostString(InetSocketAddress socketAddress) {
+ InetAddress address = socketAddress.getAddress();
+ if (address == null) {
+ // The InetSocketAddress was specified with a string (either a numeric IP or a host name). If
+ // it is a name, all IPs for that name should be tried. If it is an IP address, only that IP
+ // address should be tried.
+ return socketAddress.getHostName();
+ }
+ // The InetSocketAddress has a specific address: we should only try that address. Therefore we
+ // return the address and ignore any host name that may be available.
+ return address.getHostAddress();
+ }
+
/** Returns true if there's another socket address to try. */
private boolean hasNextInetSocketAddress() {
return socketAddresses != null;