compatible with engine.io-client 1.4.0 and engine.io-parser 1.1.0

This commit is contained in:
Naoyuki Kanezawa
2014-09-21 20:04:56 +09:00
parent 0609ab3818
commit cf68b944d0
5 changed files with 77 additions and 45 deletions

View File

@@ -208,6 +208,16 @@ public class Socket extends Emitter {
String transportName; String transportName;
if (Socket.this.rememberUpgrade && Socket.priorWebsocketSuccess && Socket.this.transports.contains(WebSocket.NAME)) { if (Socket.this.rememberUpgrade && Socket.priorWebsocketSuccess && Socket.this.transports.contains(WebSocket.NAME)) {
transportName = WebSocket.NAME; transportName = WebSocket.NAME;
} else if (0 == Socket.this.transports.size()) {
// Emit error on next tick so it can be listened to
final Socket self = Socket.this;
EventThread.nextTick(new Runnable() {
@Override
public void run() {
self.emit(Socket.EVENT_ERROR, new EngineIOException("No transports available"));
}
});
return;
} else { } else {
transportName = Socket.this.transports.get(0); transportName = Socket.this.transports.get(0);
} }

View File

@@ -142,7 +142,10 @@ public class PollingXHR extends Polling {
private String method; private String method;
private String uri; private String uri;
// data is always a binary
private byte[] data; private byte[] data;
private SSLContext sslContext; private SSLContext sslContext;
private HttpURLConnection xhr; private HttpURLConnection xhr;
@@ -186,8 +189,6 @@ public class PollingXHR extends Polling {
@Override @Override
public void run() { public void run() {
OutputStream output = null; OutputStream output = null;
InputStream input = null;
BufferedReader reader = null;
try { try {
if (self.data != null) { if (self.data != null) {
xhr.setFixedLengthStreamingMode(self.data.length); xhr.setFixedLengthStreamingMode(self.data.length);
@@ -210,33 +211,7 @@ public class PollingXHR extends Polling {
final int statusCode = xhr.getResponseCode(); final int statusCode = xhr.getResponseCode();
if (HttpURLConnection.HTTP_OK == statusCode) { if (HttpURLConnection.HTTP_OK == statusCode) {
String contentType = xhr.getContentType(); self.onLoad();
if ("application/octet-stream".equalsIgnoreCase(contentType)) {
input = new BufferedInputStream(xhr.getInputStream());
List<byte[]> buffers = new ArrayList<byte[]>();
int capacity = 0;
int len = 0;
byte[] buffer = new byte[1024];
while ((len = input.read(buffer)) > 0) {
byte[] _buffer = new byte[len];
System.arraycopy(buffer, 0, _buffer, 0, len);
buffers.add(_buffer);
capacity += len;
}
ByteBuffer data = ByteBuffer.allocate(capacity);
for (byte[] b : buffers) {
data.put(b);
}
self.onData(data.array());
} else {
String line;
StringBuilder data = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(xhr.getInputStream()));
while ((line = reader.readLine()) != null) {
data.append(line);
}
self.onData(data.toString());
}
} else { } else {
self.onError(new IOException(Integer.toString(statusCode))); self.onError(new IOException(Integer.toString(statusCode)));
} }
@@ -246,12 +221,6 @@ public class PollingXHR extends Polling {
try { try {
if (output != null) output.close(); if (output != null) output.close();
} catch (IOException e) {} } catch (IOException e) {}
try {
if (input != null) input.close();
} catch (IOException e) {}
try {
if (reader != null) reader.close();
} catch (IOException e) {}
} }
} }
}).start(); }).start();
@@ -294,6 +263,48 @@ public class PollingXHR extends Polling {
xhr = null; xhr = null;
} }
private void onLoad() {
InputStream input = null;
BufferedReader reader = null;
String contentType = xhr.getContentType();
try {
if ("application/octet-stream".equalsIgnoreCase(contentType)) {
input = new BufferedInputStream(this.xhr.getInputStream());
List<byte[]> buffers = new ArrayList<byte[]>();
int capacity = 0;
int len = 0;
byte[] buffer = new byte[1024];
while ((len = input.read(buffer)) > 0) {
byte[] _buffer = new byte[len];
System.arraycopy(buffer, 0, _buffer, 0, len);
buffers.add(_buffer);
capacity += len;
}
ByteBuffer data = ByteBuffer.allocate(capacity);
for (byte[] b : buffers) {
data.put(b);
}
this.onData(data.array());
} else {
String line;
StringBuilder data = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(xhr.getInputStream()));
while ((line = reader.readLine()) != null) {
data.append(line);
}
this.onData(data.toString());
}
} catch (IOException e) {
this.onError(e);
} finally {
try {
if (input != null) input.close();
} catch (IOException e) {}
try {
if (reader != null) reader.close();
} catch (IOException e) {}
}
}
public void abort() { public void abort() {
this.cleanup(); this.cleanup();
} }

View File

@@ -39,6 +39,10 @@ public class Parser {
private Parser() {} private Parser() {}
public static void encodePacket(Packet packet, EncodeCallback callback) { public static void encodePacket(Packet packet, EncodeCallback callback) {
encodePacket(packet, false, callback);
}
public static void encodePacket(Packet packet, boolean utf8encode, EncodeCallback callback) {
if (packet.data instanceof byte[]) { if (packet.data instanceof byte[]) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Packet<byte[]> _packet = packet; Packet<byte[]> _packet = packet;
@@ -51,7 +55,7 @@ public class Parser {
String encoded = String.valueOf(packets.get(packet.type)); String encoded = String.valueOf(packets.get(packet.type));
if (null != packet.data) { if (null != packet.data) {
encoded += UTF8.encode(String.valueOf(packet.data)); encoded += utf8encode ? UTF8.encode(String.valueOf(packet.data)) : String.valueOf(packet.data);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -68,17 +72,24 @@ public class Parser {
} }
public static Packet<String> decodePacket(String data) { public static Packet<String> decodePacket(String data) {
return decodePacket(data, false);
}
public static Packet<String> decodePacket(String data, boolean utf8decode) {
int type; int type;
try { try {
type = Character.getNumericValue(data.charAt(0)); type = Character.getNumericValue(data.charAt(0));
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
type = -1; type = -1;
} }
if (utf8decode) {
try { try {
data = UTF8.decode(data); data = UTF8.decode(data);
} catch (UTF8Exception e) { } catch (UTF8Exception e) {
return err; return err;
} }
}
if (type < 0 || type >= packetslist.size()) { if (type < 0 || type >= packetslist.size()) {
return err; return err;
@@ -107,7 +118,7 @@ public class Parser {
final ArrayList<byte[]> results = new ArrayList<byte[]>(packets.length); final ArrayList<byte[]> results = new ArrayList<byte[]>(packets.length);
for (Packet packet : packets) { for (Packet packet : packets) {
encodePacket(packet, new EncodeCallback() { encodePacket(packet, true, new EncodeCallback() {
@Override @Override
public void call(Object packet) { public void call(Object packet) {
if (packet instanceof String) { if (packet instanceof String) {
@@ -168,7 +179,7 @@ public class Parser {
} }
if (msg.length() != 0) { if (msg.length() != 0) {
Packet<String> packet = decodePacket(msg); Packet<String> packet = decodePacket(msg, true);
if (err.type.equals(packet.type) && err.data.equals(packet.data)) { if (err.type.equals(packet.type) && err.data.equals(packet.data)) {
callback.call(err, 0, 1); callback.call(err, 0, 1);
return; return;
@@ -237,7 +248,7 @@ public class Parser {
if (buffer instanceof String) { if (buffer instanceof String) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
DecodePayloadCallback<String> _callback = callback; DecodePayloadCallback<String> _callback = callback;
_callback.call(decodePacket((String)buffer), i, total); _callback.call(decodePacket((String)buffer, true), i, total);
} else if (buffer instanceof byte[]) { } else if (buffer instanceof byte[]) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
DecodePayloadCallback<byte[]> _callback = callback; DecodePayloadCallback<byte[]> _callback = callback;

View File

@@ -171,7 +171,7 @@ public class ParserTest {
@Test @Test
public void decodeInvalidUTF8() { public void decodeInvalidUTF8() {
Packet<String> p = decodePacket("4\uffff"); Packet<String> p = decodePacket("4\uffff", true);
assertThat(p.type, is(Packet.ERROR)); assertThat(p.type, is(Packet.ERROR));
assertThat(p.data, is(ERROR_DATA)); assertThat(p.data, is(ERROR_DATA));
} }

View File

@@ -3,6 +3,6 @@
"version": "0.0.0", "version": "0.0.0",
"private": true, "private": true,
"dependencies": { "dependencies": {
"engine.io": "1.3.1" "engine.io": "1.4.0"
} }
} }