remove cookie option, and add support for handling headers

This commit is contained in:
Naoyuki Kanezawa
2014-02-01 03:38:10 +09:00
parent 84f97d8caf
commit 4e4d51bd04
5 changed files with 109 additions and 62 deletions

View File

@@ -76,6 +76,11 @@ public abstract class Socket extends Emitter {
public static final String EVENT_HEARTBEAT = "heartbeat";
public static final String EVENT_DATA = "data";
/**
* Called on a new transport is created.
*/
public static final String EVENT_TRANSPORT = "transport";
private static final Runnable noop = new Runnable() {
@Override
public void run() {}
@@ -99,7 +104,6 @@ public abstract class Socket extends Emitter {
private String hostname;
private String path;
private String timestampParam;
private String cookie;
private List<String> transports;
private List<String> upgrades;
private Map<String, String> query;
@@ -163,7 +167,6 @@ public abstract class Socket extends Emitter {
this.transports = new ArrayList<String>(Arrays.asList(opts.transports != null ?
opts.transports : new String[]{Polling.NAME, WebSocket.NAME}));
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
this.cookie = opts.cookie;
}
/**
@@ -201,7 +204,6 @@ public abstract class Socket extends Emitter {
opts.timestampRequests = this.timestampRequests;
opts.timestampParam = this.timestampParam;
opts.policyPort = this.policyPort;
opts.cookie = this.cookie;
if (WebSocket.NAME.equals(name)) {
return new WebSocket(opts);
@@ -223,6 +225,8 @@ public abstract class Socket extends Emitter {
this.transport = transport;
self.emit(EVENT_TRANSPORT, transport);
transport.on(Transport.EVENT_DRAIN, new Listener() {
@Override
public void call(Object... args) {

View File

@@ -23,6 +23,8 @@ public abstract class Transport extends Emitter {
public static final String EVENT_PACKET = "packet";
public static final String EVENT_DRAIN = "drain";
public static final String EVENT_ERROR = "error";
public static final String EVENT_REQUEST_HEADERS = "requestHeaders";
public static final String EVENT_RESPONSE_HEADERS = "responseHeaders";
public boolean writable;
public String name;
@@ -121,11 +123,6 @@ public abstract class Transport extends Emitter {
public static class Options {
/**
* Cookie value for handshake.
*/
public String cookie;
public String hostname;
public String path;
public String timestampParam;

View File

@@ -7,6 +7,8 @@ import com.github.nkzawa.engineio.client.EventThread;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
@@ -17,11 +19,9 @@ public class PollingXHR extends Polling {
private Request sendXhr;
private Request pollXhr;
private String cookie;
public PollingXHR(Options opts) {
super(opts);
this.cookie = opts.cookie;
}
protected Request request() {
@@ -33,8 +33,34 @@ public class PollingXHR extends Polling {
opts = new Request.Options();
}
opts.uri = this.uri();
opts.cookie = this.cookie;
return new Request(opts);
Request req = new Request(opts);
final PollingXHR self = this;
req.on(Request.EVENT_REQUEST_HEADERS, new Listener() {
@Override
public void call(Object... args) {
// Never execute asynchronously for support to modify headers.
@SuppressWarnings("unchecked")
Map<String, String> headers = args.length > 0 && args[0] instanceof Map ?
(Map<String, String>)args[0] : new HashMap<String, String>();
self.emit(EVENT_REQUEST_HEADERS, headers);
}
}).on(Request.EVENT_RESPONSE_HEADERS, new Listener() {
@Override
public void call(final Object... args) {
EventThread.exec(new Runnable() {
@Override
public void run() {
@SuppressWarnings("unchecked")
final Map<String, String> headers = args.length > 0 && args[0] instanceof Map ?
(Map<String, String>)args[0] : new HashMap<String, String>();
self.emit(EVENT_RESPONSE_HEADERS, headers);
}
});
}
});
return req;
}
protected void doWrite(String data, final Runnable fn) {
@@ -107,20 +133,20 @@ public class PollingXHR extends Polling {
public static final String EVENT_SUCCESS = "success";
public static final String EVENT_DATA = "data";
public static final String EVENT_ERROR = "error";
public static final String EVENT_REQUEST_HEADERS = "requestHeaders";
public static final String EVENT_RESPONSE_HEADERS = "responseHeaders";
private static final ExecutorService xhrService = Executors.newCachedThreadPool();
String method;
String uri;
String data;
String cookie;
HttpURLConnection xhr;
public Request(Options opts) {
this.method = opts.method != null ? opts.method : "GET";
this.uri = opts.uri;
this.data = opts.data;
this.cookie = opts.cookie;
}
public void create() {
@@ -135,13 +161,16 @@ public class PollingXHR extends Polling {
return;
}
Map<String, String> headers = new HashMap<String, String>();
if ("POST".equals(this.method)) {
xhr.setDoOutput(true);
xhr.setRequestProperty("Content-type", "text/plain;charset=UTF-8");
headers.put("Content-type", "text/plain;charset=UTF-8");
}
if (this.cookie != null) {
xhr.setRequestProperty("Cookie", this.cookie);
self.onRequestHeaders(headers);
for (Map.Entry<String, String> header : headers.entrySet()) {
xhr.setRequestProperty(header.getKey(), header.getValue());
}
logger.fine(String.format("sending xhr with url %s | data %s", this.uri, this.data));
@@ -159,6 +188,12 @@ public class PollingXHR extends Polling {
writer.flush();
}
Map<String, String> headers = new HashMap<String, String>();
for (String key : xhr.getHeaderFields().keySet()) {
headers.put(key, xhr.getHeaderField(key));
}
self.onResponseHeaders(headers);
StringBuilder data = null;
final int statusCode = xhr.getResponseCode();
@@ -205,6 +240,14 @@ public class PollingXHR extends Polling {
this.cleanup();
}
private void onRequestHeaders(Map<String, String> headers) {
this.emit(EVENT_REQUEST_HEADERS, headers);
}
private void onResponseHeaders(Map<String, String> headers) {
this.emit(EVENT_RESPONSE_HEADERS, headers);
}
private void cleanup() {
if (xhr != null) {
xhr.disconnect();
@@ -221,7 +264,6 @@ public class PollingXHR extends Polling {
public String uri;
public String method;
public String data;
public String cookie;
}
}
}