fix #3 enable to set SSLContext as an option

This commit is contained in:
Naoyuki Kanezawa
2014-07-07 22:41:59 +09:00
parent b1e43ba1b2
commit fe2fd4413a
13 changed files with 208 additions and 36 deletions

View File

@@ -10,6 +10,7 @@ import com.github.nkzawa.parseqs.ParseQS;
import com.github.nkzawa.thread.EventThread;
import org.json.JSONException;
import javax.net.ssl.SSLContext;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
@@ -117,6 +118,7 @@ public class Socket extends Emitter {
/*package*/ Transport transport;
private Future pingTimeoutTimer;
private Future pingIntervalTimer;
private SSLContext sslContext;
private ReadyState readyState;
private ScheduledExecutorService heartbeatScheduler = Executors.newSingleThreadScheduledExecutor();
@@ -165,6 +167,7 @@ public class Socket extends Emitter {
}
this.secure = opts.secure;
this.sslContext = opts.sslContext;
this.hostname = opts.hostname != null ? opts.hostname : "localhost";
this.port = opts.port != 0 ? opts.port : (this.secure ? 443 : 80);
this.query = opts.query != null ?
@@ -211,6 +214,7 @@ public class Socket extends Emitter {
}
Transport.Options opts = new Transport.Options();
opts.sslContext = this.sslContext;
opts.hostname = this.hostname;
opts.port = this.port;
opts.secure = this.secure;

View File

@@ -6,6 +6,7 @@ import com.github.nkzawa.engineio.parser.Packet;
import com.github.nkzawa.engineio.parser.Parser;
import com.github.nkzawa.thread.EventThread;
import javax.net.ssl.SSLContext;
import java.util.Map;
public abstract class Transport extends Emitter {
@@ -39,6 +40,7 @@ public abstract class Transport extends Emitter {
protected String path;
protected String hostname;
protected String timestampParam;
protected SSLContext sslContext;
protected Socket socket;
protected ReadyState readyState;
@@ -51,6 +53,7 @@ public abstract class Transport extends Emitter {
this.query = opts.query;
this.timestampParam = opts.timestampParam;
this.timestampRequests = opts.timestampRequests;
this.sslContext = opts.sslContext;
this.socket = opts.socket;
}
@@ -140,6 +143,7 @@ public abstract class Transport extends Emitter {
public int port;
public int policyPort;
public Map<String, String> query;
public SSLContext sslContext;
protected Socket socket;
}
}

View File

@@ -4,6 +4,8 @@ package com.github.nkzawa.engineio.client.transports;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.thread.EventThread;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
@@ -36,6 +38,7 @@ public class PollingXHR extends Polling {
opts = new Request.Options();
}
opts.uri = this.uri();
opts.sslContext = this.sslContext;
Request req = new Request(opts);
@@ -141,15 +144,17 @@ public class PollingXHR extends Polling {
private static final ExecutorService xhrService = Executors.newCachedThreadPool();
String method;
String uri;
byte[] data;
HttpURLConnection xhr;
private String method;
private String uri;
private byte[] data;
private SSLContext sslContext;
private HttpURLConnection xhr;
public Request(Options opts) {
this.method = opts.method != null ? opts.method : "GET";
this.uri = opts.uri;
this.data = opts.data;
this.sslContext = opts.sslContext;
}
public void create() {
@@ -164,6 +169,10 @@ public class PollingXHR extends Polling {
return;
}
if (xhr instanceof HttpsURLConnection && this.sslContext != null) {
((HttpsURLConnection)xhr).setSSLSocketFactory(this.sslContext.getSocketFactory());
}
Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
if ("POST".equals(this.method)) {
@@ -293,6 +302,7 @@ public class PollingXHR extends Polling {
public String uri;
public String method;
public byte[] data;
public SSLContext sslContext;
}
}
}

View File

@@ -6,6 +6,7 @@ import com.github.nkzawa.engineio.parser.Packet;
import com.github.nkzawa.engineio.parser.Parser;
import com.github.nkzawa.parseqs.ParseQS;
import com.github.nkzawa.thread.EventThread;
import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;
@@ -93,6 +94,9 @@ public class WebSocket extends Transport {
});
}
};
if (this.sslContext != null) {
this.ws.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(this.sslContext));
}
this.ws.connect();
} catch (URISyntaxException e) {
throw new RuntimeException(e);