From c965ab9c9c785ba3ce119ba9370d668f79d60227 Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 9 Feb 2015 20:23:58 +0300 Subject: [PATCH] Polling encoding fixed. #207 --- .../socketio/handler/EncoderHandler.java | 6 ++- .../socketio/protocol/PacketEncoder.java | 40 +++++++++---------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java b/src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java index 71fd72d3..c67ab67c 100644 --- a/src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java +++ b/src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java @@ -251,7 +251,11 @@ private void handleHTTP(OutPacketMessage msg, ChannelHandlerContext ctx) throws if (b64 != null && b64) { Integer jsonpIndex = ctx.channel().attr(EncoderHandler.JSONP_INDEX).get(); encoder.encodeJsonP(jsonpIndex, queue, out, ctx.alloc(), 50); - sendMessage(msg, channel, out, "application/javascript"); + String type = "application/javascript"; + if (jsonpIndex == null) { + type = "text/plain"; + } + sendMessage(msg, channel, out, type); } else { encoder.encodePackets(queue, out, ctx.alloc(), 50); sendMessage(msg, channel, out, "application/octet-stream"); diff --git a/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java b/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java index 59aa26d9..37fb96af 100644 --- a/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java +++ b/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java @@ -27,7 +27,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Queue; -import java.util.regex.Pattern; import com.corundumstudio.socketio.Configuration; @@ -58,10 +57,7 @@ public ByteBuf allocateBuffer(ByteBufAllocator allocator) { public void encodeJsonP(Integer jsonpIndex, Queue packets, ByteBuf out, ByteBufAllocator allocator, int limit) throws IOException { boolean jsonpMode = jsonpIndex != null; - ByteBuf buf = out; - if (jsonpMode) { - buf = allocateBuffer(allocator); - } + ByteBuf buf = allocateBuffer(allocator); int i = 0; while (true) { @@ -95,17 +91,31 @@ public void encodeJsonP(Integer jsonpIndex, Queue packets, ByteBuf out, out.writeBytes(JSONP_HEAD); out.writeBytes(toChars(jsonpIndex)); out.writeBytes(JSONP_START); + } - String packet = buf.toString(CharsetUtil.ISO_8859_1); - buf.release(); - // TODO optimize - packet = packet.replace("\\", "\\\\").replace("'", "\\'"); - out.writeBytes(packet.getBytes(CharsetUtil.UTF_8)); + processUtf8(buf, out, jsonpMode); + buf.release(); + if (jsonpMode) { out.writeBytes(JSONP_END); } } + private void processUtf8(ByteBuf in, ByteBuf out, boolean jsonpMode) { + while (in.isReadable()) { + short value = (short) (in.readByte() & 0xFF); + if (value >>> 7 == 0) { + if (jsonpMode && (value == '\\' || value == '\'')) { + out.writeByte('\\'); + } + out.writeByte(value); + } else { + out.writeByte(((value >>> 6) | 0xC0)); + out.writeByte(((value & 0x3F) | 0x80)); + } + } + } + public void encodePackets(Queue packets, ByteBuf buffer, ByteBufAllocator allocator, int limit) throws IOException { int i = 0; while (true) { @@ -313,16 +323,6 @@ public void encodePacket(Packet packet, ByteBuf buffer, ByteBufAllocator allocat } } - 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) { for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) { if (isValueFound(buffer, i, searchValue)) {