Browse Source

\n and encoding optimization. #205

master
Nikita 11 years ago
parent
commit
e8e0492857
  1. 11
      src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java
  2. 10
      src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java

11
src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java

@ -54,12 +54,19 @@ public class PacketDecoder {
if (jsonIndex != null) {
// skip "d="
startPos = 2;
/**
* double escaping is required for escaped new lines because unescaping of new lines can be done safely on server-side
* (c) socket.io.js
*
* @see https://github.com/Automattic/socket.io-client/blob/1.3.3/socket.io.js#L2682
*/
packet = packet.replace("\\\\n", "\\n");
}
int slashesCount = packet.split("\\\\\\\\\\\\n").length - 1;
int splitIndex = packet.indexOf(":");
String len = packet.substring(startPos, splitIndex);
Integer length = Integer.valueOf(len) + slashesCount;
Integer length = Integer.valueOf(len);
packet = packet.substring(splitIndex+1, splitIndex+length+1);
packet = new String(packet.getBytes(CharsetUtil.ISO_8859_1), CharsetUtil.UTF_8);

10
src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java

@ -74,11 +74,6 @@ public class PacketEncoder {
encodePacket(packet, packetBuf, allocator, true);
int packetSize = packetBuf.writerIndex();
if (jsonpMode) {
int count = count(packetBuf, Unpooled.copiedBuffer("\\\\\\n", CharsetUtil.UTF_8));
packetSize -= count;
}
buf.writeBytes(toChars(packetSize));
buf.writeBytes(B64_DELIMITER);
buf.writeBytes(packetBuf);
@ -101,11 +96,10 @@ public class PacketEncoder {
out.writeBytes(toChars(jsonpIndex));
out.writeBytes(JSONP_START);
String packet = buf.toString(CharsetUtil.UTF_8);
String packet = buf.toString(CharsetUtil.ISO_8859_1);
buf.release();
// TODO optimize
packet = packet.replace("\\", "\\\\").replace("'", "\\'").replace("\\\\\\\\\\n", "\\\\\\n");
packet = new String(packet.getBytes(CharsetUtil.UTF_8), CharsetUtil.ISO_8859_1);
packet = packet.replace("\\", "\\\\").replace("'", "\\'");
out.writeBytes(packet.getBytes(CharsetUtil.UTF_8));
out.writeBytes(JSONP_END);

Loading…
Cancel
Save