Browse Source

\n handling fixed. #205

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

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

@ -56,9 +56,10 @@ public class PacketDecoder {
startPos = 2; startPos = 2;
} }
int slashesCount = packet.split("\\\\\\\\\\\\n").length - 1;
int splitIndex = packet.indexOf(":"); int splitIndex = packet.indexOf(":");
String len = packet.substring(startPos, splitIndex); String len = packet.substring(startPos, splitIndex);
Integer length = Integer.valueOf(len);
Integer length = Integer.valueOf(len) + slashesCount;
packet = packet.substring(splitIndex+1, splitIndex+length+1); packet = packet.substring(splitIndex+1, splitIndex+length+1);
packet = new String(packet.getBytes(CharsetUtil.ISO_8859_1), CharsetUtil.UTF_8); packet = new String(packet.getBytes(CharsetUtil.ISO_8859_1), CharsetUtil.UTF_8);

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

@ -74,6 +74,10 @@ public class PacketEncoder {
encodePacket(packet, packetBuf, allocator, true); encodePacket(packet, packetBuf, allocator, true);
int packetSize = packetBuf.writerIndex(); int packetSize = packetBuf.writerIndex();
if (jsonpMode) {
int count = count(packetBuf, Unpooled.copiedBuffer("\\\\\\n", CharsetUtil.UTF_8));
packetSize -= count;
}
buf.writeBytes(toChars(packetSize)); buf.writeBytes(toChars(packetSize));
buf.writeBytes(B64_DELIMITER); buf.writeBytes(B64_DELIMITER);
@ -100,7 +104,7 @@ public class PacketEncoder {
String packet = buf.toString(CharsetUtil.UTF_8); String packet = buf.toString(CharsetUtil.UTF_8);
buf.release(); buf.release();
// TODO optimize // TODO optimize
packet = packet.replace("\\", "\\\\").replace("'", "\\'");
packet = packet.replace("\\", "\\\\").replace("'", "\\'").replace("\\\\\\\\\\n", "\\\\\\n");
packet = new String(packet.getBytes(CharsetUtil.UTF_8), CharsetUtil.ISO_8859_1); packet = new String(packet.getBytes(CharsetUtil.UTF_8), CharsetUtil.ISO_8859_1);
out.writeBytes(packet.getBytes(CharsetUtil.UTF_8)); out.writeBytes(packet.getBytes(CharsetUtil.UTF_8));
@ -315,6 +319,16 @@ public class PacketEncoder {
} }
} }
private int count(ByteBuf buffer, ByteBuf searchValue) {
int count = 0;
for (int i = 0; i < buffer.readableBytes(); i++) {
if (isValueFound(buffer, i, searchValue)) {
count++;
}
}
return count;
}
public static int find(ByteBuf buffer, ByteBuf searchValue) { public static int find(ByteBuf buffer, ByteBuf searchValue) {
for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) { for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) {
if (isValueFound(buffer, i, searchValue)) { if (isValueFound(buffer, i, searchValue)) {

Loading…
Cancel
Save