Skip to content

Commit

Permalink
[4.x] Cherry pick: fix DoH error when using ip address as hostname (#…
Browse files Browse the repository at this point in the history
…7648)

* Cherrypick: fix DoH error when using ip address as hostname (#7073)

(cherry picked from commit 631a29e)

Co-authored-by: Tao.Zang <zangtao.i@gmail.com>
  • Loading branch information
yschimke and TaoZang authored Jan 18, 2023
1 parent 949262e commit 8eddd1d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package okhttp3.internal.connection

import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Proxy
import java.net.SocketException
Expand All @@ -26,6 +27,7 @@ import okhttp3.Call
import okhttp3.EventListener
import okhttp3.HttpUrl
import okhttp3.Route
import okhttp3.internal.canParseAsIpAddress
import okhttp3.internal.immutableListOf
import okhttp3.internal.toImmutableList

Expand Down Expand Up @@ -158,16 +160,21 @@ class RouteSelector(
if (proxy.type() == Proxy.Type.SOCKS) {
mutableInetSocketAddresses += InetSocketAddress.createUnresolved(socketHost, socketPort)
} else {
eventListener.dnsStart(call, socketHost)
val addresses = if (socketHost.canParseAsIpAddress()) {
listOf(InetAddress.getByName(socketHost))
} else {
eventListener.dnsStart(call, socketHost)

// Try each address for best behavior in mixed IPv4/IPv6 environments.
val result = address.dns.lookup(socketHost)
if (result.isEmpty()) {
throw UnknownHostException("${address.dns} returned no addresses for $socketHost")
}

// Try each address for best behavior in mixed IPv4/IPv6 environments.
val addresses = address.dns.lookup(socketHost)
if (addresses.isEmpty()) {
throw UnknownHostException("${address.dns} returned no addresses for $socketHost")
eventListener.dnsEnd(call, socketHost, result)
result
}

eventListener.dnsEnd(call, socketHost, addresses)

for (inetAddress in addresses) {
mutableInetSocketAddresses += InetSocketAddress(inetAddress, socketPort)
}
Expand Down
21 changes: 21 additions & 0 deletions okhttp/src/test/java/okhttp3/EventListenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ public final class EventListenerTest {
"ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}

@Test public void successfulCallEventSequenceForIpAddress() throws IOException {
server.enqueue(new MockResponse()
.setBody("abc"));

String ipAddress = InetAddress.getLoopbackAddress().getHostAddress();

Call call = client.newCall(new Request.Builder()
.url(server.url("/").newBuilder().host(ipAddress).build())
.build());
Response response = call.execute();
assertThat(response.code()).isEqualTo(200);
assertThat(response.body().string()).isEqualTo("abc");
response.body().close();

assertThat(listener.recordedEventTypes()).containsExactly("CallStart",
"ProxySelectStart", "ProxySelectEnd",
"ConnectStart", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart",
"RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart",
"ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}

@Test public void successfulCallEventSequenceForEnqueue() throws Exception {
server.enqueue(new MockResponse()
.setBody("abc"));
Expand Down

0 comments on commit 8eddd1d

Please sign in to comment.