Skip to content

Commit

Permalink
Fix zlib-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
PixeLInc committed Aug 13, 2020
1 parent f36997f commit 85e1cad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
17 changes: 14 additions & 3 deletions source/droid/gateway/compression.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,31 @@ class Decompressor {
}

class ZLibStream : Decompressor {
const ulong[] ZLIB_SUFFIX = [0x0, 0x0, 0xFF, 0xFF];
const ubyte[] ZLIB_SUFFIX = [0x0, 0x0, 0xFF, 0xFF];
UnCompress decompressor;

ubyte[] buffer;

this() {
decompressor = new UnCompress(HeaderFormat.deflate);
}

/*
* Reads a zlib stream from the websocket
* This will append the data to a buffer,
* returning nothing if the data is not a full zlib frame
* otherwise, returning the decompressed string.
*/
override string read(ubyte[] data) {
buffer ~= data;

if (data[$-4..$] != ZLIB_SUFFIX) {
throw new DroidException("ZLib-Stream compression enabled but invalid data was recieved!");
return "";
}

string decompressed = to!string(decompressor.uncompress(data));
string decompressed = to!string(decompressor.uncompress(buffer));
decompressor.flush();
buffer = null;

return decompressed;
}
Expand Down
3 changes: 3 additions & 0 deletions source/droid/gateway/gateway.d
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ final class Gateway
else
data = ws_.receiveText();

// The data isn't complete (not a full zlib message, or something borked)
if (data == "") return;

const packet = parseMessage(data);

auto opcodeHandler = packet.opcode in OPCODE_MAPPING;
Expand Down
1 change: 1 addition & 0 deletions source/droid/gateway/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public {
import droid.gateway.opcode;
import droid.gateway.packet;
import droid.gateway.gateway;
import droid.gateway.compression;
}

0 comments on commit 85e1cad

Please sign in to comment.