Skip to content

Commit

Permalink
Merge pull request #1007 from nicsor/master
Browse files Browse the repository at this point in the history
fix: treat attachments as binary by default
  • Loading branch information
mrniko authored Nov 1, 2024
2 parents 3593135 + 5e56faa commit f4a793f
Showing 1 changed file with 37 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,13 @@ private String readString(ByteBuf frame, int size) {
}

private Packet decode(ClientHead head, ByteBuf frame) throws IOException {
if ((frame.getByte(0) == 'b' && frame.getByte(1) == '4')
|| frame.getByte(0) == 4 || frame.getByte(0) == 1) {
return parseBinary(head, frame);

Packet lastPacket = head.getLastBinaryPacket();
// Assume attachments follow.
if (lastPacket != null) {
if (lastPacket.hasAttachments() && !lastPacket.isAttachmentsLoaded()) {
return addAttachment(head, frame, lastPacket);
}
}

final int separatorPos = frame.bytesBefore((byte) 0x1E);
Expand Down Expand Up @@ -215,63 +219,41 @@ private void parseHeader(ByteBuf frame, Packet packet, PacketType innerType) {
}
}

private Packet parseBinary(ClientHead head, ByteBuf frame) throws IOException {
if (frame.getByte(0) == 1) {
frame.readByte();
int headEndIndex = frame.bytesBefore((byte)-1);
int len = (int) readLong(frame, headEndIndex);
ByteBuf oldFrame = frame;
frame = frame.slice(oldFrame.readerIndex() + 1, len);
oldFrame.readerIndex(oldFrame.readerIndex() + 1 + len);
}

if (frame.getByte(0) == 'b' && frame.getByte(1) == '4') {
frame.readShort();
} else if (frame.getByte(0) == 4) {
frame.readByte();
}

Packet binaryPacket = head.getLastBinaryPacket();
if (binaryPacket != null) {
if (frame.getByte(0) == 'b' && frame.getByte(1) == '4') {
binaryPacket.addAttachment(Unpooled.copiedBuffer(frame));
} else {
ByteBuf attachBuf = Base64.encode(frame);
binaryPacket.addAttachment(Unpooled.copiedBuffer(attachBuf));
attachBuf.release();
}
frame.skipBytes(frame.readableBytes());

if (binaryPacket.isAttachmentsLoaded()) {
LinkedList<ByteBuf> slices = new LinkedList<ByteBuf>();
ByteBuf source = binaryPacket.getDataSource();
for (int i = 0; i < binaryPacket.getAttachments().size(); i++) {
ByteBuf attachment = binaryPacket.getAttachments().get(i);
ByteBuf scanValue = Unpooled.copiedBuffer("{\"_placeholder\":true,\"num\":" + i + "}", CharsetUtil.UTF_8);
int pos = PacketEncoder.find(source, scanValue);
private Packet addAttachment(ClientHead head, ByteBuf frame, Packet binaryPacket) throws IOException {
ByteBuf attachBuf = Base64.encode(frame);
binaryPacket.addAttachment(Unpooled.copiedBuffer(attachBuf));
attachBuf.release();
frame.skipBytes(frame.readableBytes());

if (binaryPacket.isAttachmentsLoaded()) {
LinkedList<ByteBuf> slices = new LinkedList<ByteBuf>();
ByteBuf source = binaryPacket.getDataSource();
for (int i = 0; i < binaryPacket.getAttachments().size(); i++) {
ByteBuf attachment = binaryPacket.getAttachments().get(i);
ByteBuf scanValue = Unpooled.copiedBuffer("{\"_placeholder\":true,\"num\":" + i + "}", CharsetUtil.UTF_8);
int pos = PacketEncoder.find(source, scanValue);
if (pos == -1) {
scanValue = Unpooled.copiedBuffer("{\"num\":" + i + ",\"_placeholder\":true}", CharsetUtil.UTF_8);
pos = PacketEncoder.find(source, scanValue);
if (pos == -1) {
scanValue = Unpooled.copiedBuffer("{\"num\":" + i + ",\"_placeholder\":true}", CharsetUtil.UTF_8);
pos = PacketEncoder.find(source, scanValue);
if (pos == -1) {
throw new IllegalStateException("Can't find attachment by index: " + i + " in packet source");
}
throw new IllegalStateException("Can't find attachment by index: " + i + " in packet source");
}

ByteBuf prefixBuf = source.slice(source.readerIndex(), pos - source.readerIndex());
slices.add(prefixBuf);
slices.add(QUOTES);
slices.add(attachment);
slices.add(QUOTES);

source.readerIndex(pos + scanValue.readableBytes());
}
slices.add(source.slice());

ByteBuf compositeBuf = Unpooled.wrappedBuffer(slices.toArray(new ByteBuf[0]));
parseBody(head, compositeBuf, binaryPacket);
head.setLastBinaryPacket(null);
return binaryPacket;
ByteBuf prefixBuf = source.slice(source.readerIndex(), pos - source.readerIndex());
slices.add(prefixBuf);
slices.add(QUOTES);
slices.add(attachment);
slices.add(QUOTES);

source.readerIndex(pos + scanValue.readableBytes());
}
slices.add(source.slice());

ByteBuf compositeBuf = Unpooled.wrappedBuffer(slices.toArray(new ByteBuf[0]));
parseBody(head, compositeBuf, binaryPacket);
head.setLastBinaryPacket(null);
return binaryPacket;
}
return new Packet(PacketType.MESSAGE, head.getEngineIOVersion());
}
Expand Down

0 comments on commit f4a793f

Please sign in to comment.