From 9dcc2538ae01491e79a516f298f8bd128292ce0c Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 23 Mar 2020 18:46:30 -0700 Subject: [PATCH] (websocket) reset per-message deflate codec everytime we connect to a server/client --- docs/CHANGELOG.md | 4 ++++ ixwebsocket/IXWebSocketHandshake.cpp | 6 +++--- ixwebsocket/IXWebSocketHandshake.h | 4 ++-- ixwebsocket/IXWebSocketPerMessageDeflate.h | 2 ++ ixwebsocket/IXWebSocketTransport.cpp | 6 ++++-- ixwebsocket/IXWebSocketTransport.h | 2 +- ixwebsocket/IXWebSocketVersion.h | 2 +- 7 files changed, 17 insertions(+), 9 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 2884d627..8c066b5d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All changes to this project will be documented in this file. +## [9.0.0] - 2020-03-23 + +(websocket) reset per-message deflate codec everytime we connect to a server/client + ## [8.3.4] - 2020-03-23 (websocket) fix #167, a long standing issue with sending empty messages with per-message deflate extension (and hopefully other zlib bug) diff --git a/ixwebsocket/IXWebSocketHandshake.cpp b/ixwebsocket/IXWebSocketHandshake.cpp index 1d384627..392dba72 100644 --- a/ixwebsocket/IXWebSocketHandshake.cpp +++ b/ixwebsocket/IXWebSocketHandshake.cpp @@ -22,7 +22,7 @@ namespace ix WebSocketHandshake::WebSocketHandshake( std::atomic& requestInitCancellation, std::shared_ptr socket, - WebSocketPerMessageDeflate& perMessageDeflate, + WebSocketPerMessageDeflatePtr& perMessageDeflate, WebSocketPerMessageDeflateOptions& perMessageDeflateOptions, std::atomic& enablePerMessageDeflate) : _requestInitCancellation(requestInitCancellation) @@ -230,7 +230,7 @@ namespace ix _enablePerMessageDeflate = false; } // Otherwise try to initialize the deflate engine (zlib) - else if (!_perMessageDeflate.init(webSocketPerMessageDeflateOptions)) + else if (!_perMessageDeflate->init(webSocketPerMessageDeflateOptions)) { return WebSocketInitResult( false, 0, "Failed to initialize per message deflate engine"); @@ -341,7 +341,7 @@ namespace ix { _enablePerMessageDeflate = true; - if (!_perMessageDeflate.init(webSocketPerMessageDeflateOptions)) + if (!_perMessageDeflate->init(webSocketPerMessageDeflateOptions)) { return WebSocketInitResult( false, 0, "Failed to initialize per message deflate engine"); diff --git a/ixwebsocket/IXWebSocketHandshake.h b/ixwebsocket/IXWebSocketHandshake.h index f440429a..62090bc3 100644 --- a/ixwebsocket/IXWebSocketHandshake.h +++ b/ixwebsocket/IXWebSocketHandshake.h @@ -24,7 +24,7 @@ namespace ix public: WebSocketHandshake(std::atomic& requestInitCancellation, std::shared_ptr _socket, - WebSocketPerMessageDeflate& perMessageDeflate, + WebSocketPerMessageDeflatePtr& perMessageDeflate, WebSocketPerMessageDeflateOptions& perMessageDeflateOptions, std::atomic& enablePerMessageDeflate); @@ -47,7 +47,7 @@ namespace ix std::atomic& _requestInitCancellation; std::shared_ptr _socket; - WebSocketPerMessageDeflate& _perMessageDeflate; + WebSocketPerMessageDeflatePtr& _perMessageDeflate; WebSocketPerMessageDeflateOptions& _perMessageDeflateOptions; std::atomic& _enablePerMessageDeflate; }; diff --git a/ixwebsocket/IXWebSocketPerMessageDeflate.h b/ixwebsocket/IXWebSocketPerMessageDeflate.h index 512868ec..9177409d 100644 --- a/ixwebsocket/IXWebSocketPerMessageDeflate.h +++ b/ixwebsocket/IXWebSocketPerMessageDeflate.h @@ -57,4 +57,6 @@ namespace ix std::unique_ptr _compressor; std::unique_ptr _decompressor; }; + + using WebSocketPerMessageDeflatePtr = std::unique_ptr; } // namespace ix diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 728f2f98..04e38ac9 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -117,6 +117,7 @@ namespace ix std::string errorMsg; bool tls = protocol == "wss"; _socket = createSocket(tls, -1, errorMsg, _socketTLSOptions); + _perMessageDeflate = std::make_unique(); if (!_socket) { @@ -149,6 +150,7 @@ namespace ix _blockingSend = true; _socket = socket; + _perMessageDeflate = std::make_unique(); WebSocketHandshake webSocketHandshake(_requestInitCancellation, _socket, @@ -711,7 +713,7 @@ namespace ix if (compressedMessage && messageKind != MessageKind::FRAGMENT) { std::string decompressedMessage; - bool success = _perMessageDeflate.decompress(message, decompressedMessage); + bool success = _perMessageDeflate->decompress(message, decompressedMessage); if (messageKind == MessageKind::MSG_TEXT && !validateUtf8(decompressedMessage)) { @@ -765,7 +767,7 @@ namespace ix if (compress) { - if (!_perMessageDeflate.compress(message, compressedMessage)) + if (!_perMessageDeflate->compress(message, compressedMessage)) { bool success = false; compressionError = true; diff --git a/ixwebsocket/IXWebSocketTransport.h b/ixwebsocket/IXWebSocketTransport.h index 64c18f54..5813e0dd 100644 --- a/ixwebsocket/IXWebSocketTransport.h +++ b/ixwebsocket/IXWebSocketTransport.h @@ -185,7 +185,7 @@ namespace ix mutable std::mutex _closeDataMutex; // Data used for Per Message Deflate compression (with zlib) - WebSocketPerMessageDeflate _perMessageDeflate; + WebSocketPerMessageDeflatePtr _perMessageDeflate; WebSocketPerMessageDeflateOptions _perMessageDeflateOptions; std::atomic _enablePerMessageDeflate; diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index 9a004a53..099b5051 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "8.3.4" +#define IX_WEBSOCKET_VERSION "9.0.0"