diff options
author | Loïc Hoguin <[email protected]> | 2025-01-22 10:44:44 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2025-01-22 10:44:44 +0100 |
commit | 144c7eebe2eff19ed5ddc156a6d02941ce0aaee1 (patch) | |
tree | 80e62a2c1f97a8f7ded9448b94780ad2f3f3fd27 /src | |
parent | 6ae8206fe7576f5cf10d2e1c69b238e4e7864c0f (diff) | |
download | cowlib-144c7eebe2eff19ed5ddc156a6d02941ce0aaee1.tar.gz cowlib-144c7eebe2eff19ed5ddc156a6d02941ce0aaee1.tar.bz2 cowlib-144c7eebe2eff19ed5ddc156a6d02941ce0aaee1.zip |
Fix compression of zero-byte Websocket frames
An empty deflate block must be inserted
when zlib returns nothing.
Diffstat (limited to 'src')
-rw-r--r-- | src/cow_ws.erl | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/cow_ws.erl b/src/cow_ws.erl index 47c9e89..aa5a08a 100644 --- a/src/cow_ws.erl +++ b/src/cow_ws.erl @@ -859,13 +859,22 @@ payload_length(Payload) -> end. deflate_frame(Payload, Deflate, TakeOver) -> + %% Compress all the octets of the payload of the message using DEFLATE. Deflated = iolist_to_binary(zlib:deflate(Deflate, Payload, sync)), case TakeOver of no_takeover -> zlib:deflateReset(Deflate); takeover -> ok end, + %% Remove 4 octets (that are 0x00 0x00 0xff 0xff) from the tail end. Len = byte_size(Deflated) - 4, case Deflated of - << Body:Len/binary, 0:8, 0:8, 255:8, 255:8 >> -> Body; - _ -> Deflated + << Body:Len/binary, 0:8, 0:8, 255:8, 255:8 >> -> + Body; + <<>> -> + %% If the resulting data does not end with an empty DEFLATE block + %% with no compression (the "BTYPE" bits are set to 00), append an + %% empty DEFLATE block with no compression to the tail end. + <<0>>; + _ -> + Deflated end. |