2018-11-10 03:23:49 +01:00
|
|
|
/*
|
2018-11-12 18:00:55 +01:00
|
|
|
* Copyright (c) 2015, Peter Thorson. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of the WebSocket++ Project nor the
|
|
|
|
* names of its contributors may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2018-11-10 03:23:49 +01:00
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
2018-11-12 18:00:55 +01:00
|
|
|
*
|
|
|
|
* Adapted from websocketpp/extensions/permessage_deflate/enabled.hpp
|
|
|
|
* (same license as MZ: https://opensource.org/licenses/BSD-3-Clause)
|
2018-11-10 03:23:49 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "zlib.h"
|
|
|
|
#include <string>
|
2018-11-15 00:52:28 +01:00
|
|
|
#include <memory>
|
2018-11-10 03:23:49 +01:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
class WebSocketPerMessageDeflateOptions;
|
|
|
|
|
|
|
|
class WebSocketPerMessageDeflateCompressor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebSocketPerMessageDeflateCompressor();
|
|
|
|
~WebSocketPerMessageDeflateCompressor();
|
2018-11-12 18:00:55 +01:00
|
|
|
|
|
|
|
bool init(uint8_t deflateBits, bool clientNoContextTakeOver);
|
2018-11-10 03:23:49 +01:00
|
|
|
bool compress(const std::string& in, std::string& out);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool endsWith(const std::string& value, const std::string& ending);
|
|
|
|
|
|
|
|
int _flush;
|
|
|
|
size_t _compressBufferSize;
|
|
|
|
std::unique_ptr<unsigned char[]> _compressBuffer;
|
|
|
|
z_stream _deflateState;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WebSocketPerMessageDeflateDecompressor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebSocketPerMessageDeflateDecompressor();
|
|
|
|
~WebSocketPerMessageDeflateDecompressor();
|
2018-11-12 18:00:55 +01:00
|
|
|
|
|
|
|
bool init(uint8_t inflateBits, bool clientNoContextTakeOver);
|
2018-11-10 03:23:49 +01:00
|
|
|
bool decompress(const std::string& in, std::string& out);
|
|
|
|
|
|
|
|
private:
|
|
|
|
int _flush;
|
|
|
|
size_t _compressBufferSize;
|
|
|
|
std::unique_ptr<unsigned char[]> _compressBuffer;
|
|
|
|
z_stream _inflateState;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WebSocketPerMessageDeflate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebSocketPerMessageDeflate();
|
2018-11-12 18:00:55 +01:00
|
|
|
~WebSocketPerMessageDeflate();
|
2018-11-10 03:23:49 +01:00
|
|
|
|
|
|
|
bool init(const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions);
|
|
|
|
bool compress(const std::string& in, std::string& out);
|
|
|
|
bool decompress(const std::string& in, std::string& out);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<WebSocketPerMessageDeflateCompressor> _compressor;
|
|
|
|
std::shared_ptr<WebSocketPerMessageDeflateDecompressor> _decompressor;
|
|
|
|
};
|
|
|
|
}
|