1
0
mirror of https://github.com/square/okhttp.git synced 2025-11-23 06:42:24 +03:00

Handle localhost as ipv6 without bracket quoting (#5297)

* Handle ipv6 hostnames without bracket quoting

* simplify

* Add ipv4 localhost test

* Update RecordedRequest.kt
This commit is contained in:
Yuri Schimke
2019-07-24 04:15:53 +01:00
committed by Jesse Wilson
parent 076e976c10
commit 24c7d54ecb
2 changed files with 32 additions and 11 deletions

View File

@@ -101,7 +101,11 @@ class RecordedRequest(
val inetAddress = socket.localAddress
var hostname = inetAddress.hostName
if (inetAddress is Inet6Address) {
if (inetAddress is Inet6Address && hostname.contains(':')) {
// hostname is likely some form representing the IPv6 bytes
// 2001:0db8:85a3:0000:0000:8a2e:0370:7334
// 2001:db8:85a3::8a2e:370:7334
// ::1
hostname = "[$hostname]"
}

View File

@@ -16,7 +16,6 @@
package okhttp3.mockwebserver;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
@@ -30,8 +29,9 @@ import org.junit.rules.Timeout;
import static org.assertj.core.api.Assertions.assertThat;
@SuppressWarnings("ConstantConditions")
public class RecordedRequestTest {
Headers headers = Util.EMPTY_HEADERS;
private Headers headers = Util.EMPTY_HEADERS;
private static class FakeSocket extends Socket {
private final InetAddress localAddress;
@@ -39,15 +39,12 @@ public class RecordedRequestTest {
private final InetAddress remoteAddress;
private final int localPort;
private FakeSocket(int localPort) {
this(Inet4Address.getLoopbackAddress(), localPort);
}
private FakeSocket(InetAddress inetAddress, int localPort) {
this(inetAddress, localPort, inetAddress, 1234);
}
private FakeSocket(InetAddress localAddress, int localPort, InetAddress remoteAddress, int remotePort) {
private FakeSocket(InetAddress localAddress, int localPort, InetAddress remoteAddress,
int remotePort) {
this.localAddress = localAddress;
this.localPort = localPort;
this.remoteAddress = remoteAddress;
@@ -102,4 +99,24 @@ public class RecordedRequestTest {
assertThat(request.getRequestUrl().toString()).isEqualTo("http://127.0.0.1/");
}
@Test public void testLocalhostIpv6() throws UnknownHostException {
Socket socket = new FakeSocket(InetAddress.getByAddress("localhost",
new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}), 80);
RecordedRequest request = new RecordedRequest("GET / HTTP/1.1", headers,
Collections.emptyList(), 0, new Buffer(), 0, socket);
assertThat(request.getRequestUrl().toString()).isEqualTo("http://localhost/");
}
@Test public void testLocalhostIpv4() throws UnknownHostException {
Socket socket =
new FakeSocket(InetAddress.getByAddress("localhost", new byte[] {127, 0, 0, 1}), 80);
RecordedRequest request = new RecordedRequest("GET / HTTP/1.1", headers,
Collections.emptyList(), 0, new Buffer(), 0, socket);
assertThat(request.getRequestUrl().toString()).isEqualTo("http://localhost/");
}
}