improve ipv6 support, add tests

This commit is contained in:
nkzawa
2016-01-30 20:36:28 +09:00
parent a0aed87adc
commit 9363039a71
5 changed files with 148 additions and 33 deletions

View File

@@ -111,7 +111,7 @@ public class Socket extends Emitter {
private long pingInterval;
private long pingTimeout;
private String id;
private String hostname;
/*package*/ String hostname;
private String path;
private String timestampParam;
private List<String> transports;
@@ -171,29 +171,27 @@ public class Socket extends Emitter {
public Socket(Options opts) {
if (opts.host != null) {
boolean ipv6uri = opts.host.indexOf(']') != -1;
String[] pieces = ipv6uri ? opts.host.split("]:") : opts.host.split(":");
boolean ipv6 = (pieces.length > 2 || opts.host.indexOf("::") == -1);
String hostname = opts.host;
boolean ipv6 = hostname.split(":").length > 2;
if (ipv6) {
opts.hostname = opts.host;
} else {
opts.hostname = pieces[0];
if (ipv6uri) {
opts.hostname = opts.hostname.substring(1);
}
if (pieces.length > 1) {
opts.port = Integer.parseInt(pieces[pieces.length - 1]);
} else if (opts.port == -1) {
// if no port is specified manually, use the protocol default
opts.port = this.secure ? 443 : 80;
}
int start = hostname.indexOf('[');
if (start != -1) hostname = hostname.substring(start + 1);
int end = hostname.lastIndexOf(']');
if (end != -1) hostname = hostname.substring(0, end);
}
opts.hostname = hostname;
}
this.secure = opts.secure;
if (opts.port == -1) {
// if no port is specified manually, use the protocol default
opts.port = this.secure ? 443 : 80;
}
this.sslContext = opts.sslContext != null ? opts.sslContext : defaultSSLContext;
this.hostname = opts.hostname != null ? opts.hostname : "localhost";
this.port = opts.port != 0 ? opts.port : (this.secure ? 443 : 80);
this.port = opts.port;
this.query = opts.query != null ?
ParseQS.decode(opts.query) : new HashMap<String, String>();
this.upgrade = opts.upgrade;

View File

@@ -214,7 +214,8 @@ abstract public class Polling extends Transport {
_query = "?" + _query;
}
return schema + "://" + this.hostname + port + this.path + _query;
boolean ipv6 = this.hostname.contains(":");
return schema + "://" + (ipv6 ? "[" + this.hostname + "]" : this.hostname) + port + this.path + _query;
}
abstract protected void doWrite(byte[] data, Runnable fn);

View File

@@ -1,15 +1,22 @@
package io.socket.engineio.client.transports;
import io.socket.engineio.client.Transport;
import io.socket.engineio.parser.Packet;
import io.socket.engineio.parser.Parser;
import io.socket.parseqs.ParseQS;
import io.socket.thread.EventThread;
import io.socket.utf8.UTF8Exception;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.ws.WebSocketCall;
import okhttp3.ws.WebSocketListener;
import okio.Buffer;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
@@ -19,16 +26,6 @@ import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.net.ssl.SSLSocketFactory;
import io.socket.engineio.client.Transport;
import io.socket.engineio.parser.Packet;
import io.socket.engineio.parser.Parser;
import io.socket.parseqs.ParseQS;
import io.socket.thread.EventThread;
import io.socket.utf8.UTF8Exception;
import okio.Buffer;
import static okhttp3.ws.WebSocket.BINARY;
import static okhttp3.ws.WebSocket.TEXT;
@@ -222,6 +219,7 @@ public class WebSocket extends Transport {
_query = "?" + _query;
}
return schema + "://" + this.hostname + port + this.path + _query;
boolean ipv6 = this.hostname.contains(":");
return schema + "://" + (ipv6 ? "[" + this.hostname + "]" : this.hostname) + port + this.path + _query;
}
}