Added Compression class with gzip and deflate support.

Added IOStreamInflate and IOStreamString.
Added support for compressed response in HTTP requests.

--HG--
branch : dev
This commit is contained in:
Martín Lucas Golini
2019-05-01 22:00:40 -03:00
parent 73c60719a4
commit 70dd741491
14 changed files with 783 additions and 163 deletions

View File

@@ -57,8 +57,9 @@ class EE_API Http : NonCopyable {
** @param validateCertificate Enables certificate validation for https request
** @param validateHostname Enables hostname validation for https request
** @param followRedirect Allow follor redirects to the request.
** @param compressedResponse Set if the requested response should be compressed ( if available )
*/
Request(const std::string& uri = "/", Method method = Get, const std::string& body = "", bool validateCertificate = true, bool validateHostname = true, bool followRedirect = true);
Request(const std::string& uri = "/", Method method = Get, const std::string& body = "", bool validateCertificate = true, bool validateHostname = true, bool followRedirect = true, bool compressedResponse = false);
/** @brief Set the value of a field
** The field is created if it doesn't exist. The name of
@@ -159,6 +160,15 @@ class EE_API Http : NonCopyable {
/** @return True if the current request was cancelled */
const bool& isCancelled() const;
/** @return If requests a compressed response */
const bool& isCompressedResponse() const;
/** Set to request a compressed response from the server
** The returned response will be automatically decompressed
** by the client.
*/
void setCompressedResponse(const bool& compressedResponse);
private:
friend class Http;
@@ -184,6 +194,7 @@ class EE_API Http : NonCopyable {
bool mValidateCertificate; ///< Validates the SSL certificate in case of an HTTPS request
bool mValidateHostname; ///< Validates the hostname in case of an HTTPS request
bool mFollowRedirect; ///< Follows redirect response codes
bool mCompressedResponse; ///< Request comrpessed response
mutable bool mCancel; ///< Cancel state of current request
ProgressCallback mProgressCallback; ///< Progress callback
unsigned int mMaxRedirections; ///< Maximun number of redirections allowed

View File

@@ -25,6 +25,7 @@
#include <eepp/system/packmanager.hpp>
#include <eepp/system/threadlocal.hpp>
#include <eepp/system/threadlocalptr.hpp>
#include <eepp/system/compression.hpp>
#include <eepp/system/base64.hpp>
#include <eepp/system/md5.hpp>
#include <eepp/system/translator.hpp>
@@ -32,6 +33,8 @@
#include <eepp/system/iostreamfile.hpp>
#include <eepp/system/iostreamzip.hpp>
#include <eepp/system/iostreampak.hpp>
#include <eepp/system/iostreamstring.hpp>
#include <eepp/system/iostreaminflate.hpp>
#include <eepp/system/virtualfilesystem.hpp>
#endif

View File

@@ -0,0 +1,55 @@
#ifndef EE_SYSTEM_COMPRESSION_HPP
#define EE_SYSTEM_COMPRESSION_HPP
#include <eepp/config.hpp>
#include <eepp/system/iostream.hpp>
namespace EE { namespace System {
class Compression {
public:
enum Mode {
MODE_DEFLATE,
MODE_GZIP
};
enum Status {
OK = 0,
ERRNO = -1,
STREAM_ERROR = -2,
DATA_ERROR = -3,
MEM_ERROR = -4,
BUF_ERROR = -5,
VERSION_ERROR = -6
};
struct ZlibConfig {
int level= -1;
};
struct GzipConfig {
int level = -1;
};
struct Config {
Config() {}
ZlibConfig zlib;
GzipConfig gzip;
};
static Status compress(Uint8* dst, Uint64 dstMaxSize, const Uint8* src, Uint64 srcSize, Mode mode = MODE_DEFLATE, const Config& config = Config());
static Status compress(IOStream& dst, IOStream& src, Mode mode = MODE_DEFLATE, const Config& config = Config());
static int getMaxCompressedBufferSize(Uint64 srcSize, Mode mode = MODE_DEFLATE, const Config& config = Config());
static Status decompress(Uint8* dst, Uint64 dstMaxSize, const Uint8* src, Uint64 srcSize, Mode mode = MODE_DEFLATE);
static Status decompress(IOStream& dst, IOStream& src, Mode mode = MODE_DEFLATE);
static std::size_t getModeDefaultChunkSize( const Mode& mode );
};
}}
#endif // EE_SYSTEM_COMPRESSION_HPP

View File

@@ -0,0 +1,48 @@
#ifndef EE_SYSTEM_IOSTREAMINFLATE_HPP
#define EE_SYSTEM_IOSTREAMINFLATE_HPP
#include <eepp/system/iostream.hpp>
#include <eepp/system/safedatapointer.hpp>
#include <eepp/system/compression.hpp>
namespace EE { namespace System {
struct LocalStreamData;
/** @brief Implementation of a inflating stream */
class EE_API IOStreamInflate : public IOStream {
public:
static IOStreamInflate * New( IOStream& inOutStream, Compression::Mode mode );
/** @brief Use a stream as a input or output buffer
** @param inOutStream Stream where the results will ve loaded or saved.
** It must be used only for reading or writing, can't mix both calls.
** @param mode Compression/Decompression method used
*/
IOStreamInflate( IOStream& inOutStream, Compression::Mode mode );
virtual ~IOStreamInflate();
ios_size read( char * data, ios_size size );
ios_size write( const char * data, ios_size size );
ios_size seek( ios_size position );
ios_size tell();
ios_size getSize();
bool isOpen();
const Compression::Mode& getMode() const;
protected:
IOStream& mStream;
Compression::Mode mMode;
SafeDataPointer mBuffer;
LocalStreamData * mLocalStream;
};
}}
#endif // EE_SYSTEM_IOSTREAMINFLATE_HPP

View File

@@ -0,0 +1,44 @@
#ifndef EE_SYSTEM_IOSTREAMSTRING_HPP
#define EE_SYSTEM_IOSTREAMSTRING_HPP
#include <eepp/system/iostream.hpp>
#include <cstring>
namespace EE { namespace System {
/** @brief Implementation of a memory stream file using an std::string as a container */
class EE_API IOStreamString : public IOStream {
public:
IOStreamString();
ios_size read( char * data, ios_size size );
ios_size write( const char * data, ios_size size );
ios_size write( const std::string& string );
ios_size seek( ios_size position );
ios_size tell();
ios_size getSize();
bool isOpen();
void clear();
/** @return Pointer to the current position in the stream */
const char * getPositionPointer();
/** @return The pointer to the beggining of the stream */
const char * getStreamPointer() const;
const std::string& getStream() const;
protected:
std::string mStream;
ios_size mPos;
};
}}
#endif // EE_SYSTEM_IOSTREAMSTRING_HPP