added support for gzip compression
new config option: compression_encoding (integer) 1 - use deflate if available (or raw deflate for Internet Explorer) or don't compress 2 - use gzip if available or don't compress 10 - prefer deflate -- use deflate (or raw deflate for IE) if both deflate and gzip are available 20 - prefer gzip -- use gzip if both deflate and gzip are available default: 20 git-svn-id: svn://ttmath.org/publicrep/winix/trunk@727 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008-2010, Tomasz Sowa
|
||||
* Copyright (c) 2008-2011, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
@@ -15,9 +15,13 @@
|
||||
|
||||
Compress::Compress()
|
||||
{
|
||||
buffer = 0;
|
||||
buffer_max_len = 65536; // 64KB
|
||||
buffer = 0;
|
||||
buffer_max_len = 65536; // 64KB
|
||||
ready_for_compress = false;
|
||||
compress_level = 6;
|
||||
raw_deflate_inited = false;
|
||||
deflate_inited = false;
|
||||
gzip_inited = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +29,15 @@ Compress::~Compress()
|
||||
{
|
||||
if( buffer )
|
||||
delete [] buffer;
|
||||
|
||||
if( raw_deflate_inited )
|
||||
deflateEnd(&strm_raw_deflate);
|
||||
|
||||
if( deflate_inited )
|
||||
deflateEnd(&strm_deflate);
|
||||
|
||||
if( gzip_inited )
|
||||
deflateEnd(&strm_gzip);
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +46,6 @@ bool Compress::AllocateMemory()
|
||||
if( buffer )
|
||||
delete [] buffer;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
buffer = new char[buffer_max_len];
|
||||
@@ -41,7 +53,6 @@ bool Compress::AllocateMemory()
|
||||
catch(const std::bad_alloc &)
|
||||
{
|
||||
log << log1 << "Compress: can't allocate memory" << logend;
|
||||
|
||||
buffer = 0;
|
||||
return false;
|
||||
}
|
||||
@@ -50,39 +61,93 @@ return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Compress::InitRawDeflate()
|
||||
{
|
||||
raw_deflate_inited = false;
|
||||
|
||||
strm_raw_deflate.next_in = 0;
|
||||
strm_raw_deflate.zalloc = Z_NULL;
|
||||
strm_raw_deflate.zfree = Z_NULL;
|
||||
strm_raw_deflate.opaque = Z_NULL;
|
||||
|
||||
int ret = deflateInit2(&strm_raw_deflate, compress_level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
|
||||
|
||||
if( ret != Z_OK )
|
||||
log << log1 << "Compress: problem with deflateInit2() for raw deflate" << logend;
|
||||
else
|
||||
raw_deflate_inited = true;
|
||||
|
||||
return ret == Z_OK;
|
||||
}
|
||||
|
||||
|
||||
bool Compress::InitDeflate()
|
||||
{
|
||||
deflate_inited = false;
|
||||
|
||||
strm_deflate.next_in = 0;
|
||||
strm_deflate.zalloc = Z_NULL;
|
||||
strm_deflate.zfree = Z_NULL;
|
||||
strm_deflate.opaque = Z_NULL;
|
||||
|
||||
int ret = deflateInit2(&strm_deflate, compress_level, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
|
||||
|
||||
if( ret != Z_OK )
|
||||
log << log1 << "Compress: problem with deflateInit2() for deflate" << logend;
|
||||
else
|
||||
deflate_inited = true;
|
||||
|
||||
return ret == Z_OK;
|
||||
}
|
||||
|
||||
|
||||
bool Compress::InitGzip()
|
||||
{
|
||||
gzip_inited = false;
|
||||
|
||||
strm_gzip.next_in = 0;
|
||||
strm_gzip.zalloc = Z_NULL;
|
||||
strm_gzip.zfree = Z_NULL;
|
||||
strm_gzip.opaque = Z_NULL;
|
||||
|
||||
int ret = deflateInit2(&strm_gzip, compress_level, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
|
||||
|
||||
if( ret != Z_OK )
|
||||
log << log1 << "Compress: problem with deflateInit2() for gzip" << logend;
|
||||
else
|
||||
gzip_inited = true;
|
||||
|
||||
return ret == Z_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
return:
|
||||
0 - ok;
|
||||
1 - can't allocate memory
|
||||
100 - unknown
|
||||
0 - ok
|
||||
1 - can't allocate memory
|
||||
100 - unknown error
|
||||
*/
|
||||
int Compress::Init(int compress_level)
|
||||
int Compress::Init(int compress_level_)
|
||||
{
|
||||
compress_level = compress_level_;
|
||||
|
||||
if( buffer == 0 )
|
||||
if( !AllocateMemory() )
|
||||
return 1;
|
||||
|
||||
strm.zalloc = Z_NULL;
|
||||
strm.zfree = Z_NULL;
|
||||
strm.opaque = Z_NULL;
|
||||
int ret = deflateInit(&strm, compress_level);
|
||||
|
||||
if( ret != Z_OK )
|
||||
log << log1 << "Compress: problem with deflateInit()" << logend;
|
||||
|
||||
if( ret == Z_MEM_ERROR )
|
||||
return 1;
|
||||
|
||||
if( ret != Z_OK )
|
||||
return 100;
|
||||
|
||||
ready_for_compress = true;
|
||||
if( InitRawDeflate() && InitDeflate() && InitGzip() )
|
||||
ready_for_compress = true;
|
||||
else
|
||||
return 100;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int Compress::MakeCompress(const char * source, size_t source_len, FCGX_Stream * out_stream)
|
||||
|
||||
int Compress::MakeCompress(z_stream & strm, const char * source, size_t source_len, FCGX_Stream * out_stream, int encoding)
|
||||
{
|
||||
int ret, flush;
|
||||
size_t have;
|
||||
@@ -133,6 +198,62 @@ return 0;
|
||||
|
||||
|
||||
|
||||
z_stream * Compress::SelectStream(int encoding)
|
||||
{
|
||||
z_stream * pstrm;
|
||||
|
||||
if( encoding == 0 )
|
||||
pstrm = &strm_raw_deflate;
|
||||
else
|
||||
if( encoding == 1 )
|
||||
pstrm = &strm_deflate;
|
||||
else
|
||||
pstrm = &strm_gzip;
|
||||
|
||||
return pstrm;
|
||||
}
|
||||
|
||||
|
||||
void Compress::ResetStream(z_stream * pstrm, int encoding)
|
||||
{
|
||||
if( deflateReset(pstrm) != Z_OK )
|
||||
{
|
||||
log << log1 << "Compress: problem with deflateReset()" << logend;
|
||||
|
||||
deflateEnd(pstrm);
|
||||
|
||||
if( encoding == 0 )
|
||||
InitRawDeflate();
|
||||
else
|
||||
if( encoding == 1 )
|
||||
InitDeflate();
|
||||
else
|
||||
InitGzip();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Compress::PutLog(size_t source_len, int encoding)
|
||||
{
|
||||
double ratio = 100.0 - (double(last_out_size) / double(source_len) * 100.0);
|
||||
char buffer[30];
|
||||
sprintf(buffer, "%.1f", ratio);
|
||||
|
||||
log << log2 << "Compress: ";
|
||||
|
||||
if( encoding == 0 )
|
||||
log << "raw deflate";
|
||||
else
|
||||
if( encoding == 1 )
|
||||
log << "deflate";
|
||||
else
|
||||
log << "gzip";
|
||||
|
||||
log << ", original size: " << source_len << ", size after compressing: "
|
||||
<< (int)last_out_size << ", ratio: " << buffer << "%" << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
return:
|
||||
@@ -142,10 +263,11 @@ return 0;
|
||||
3 - not inited (use Init() first)
|
||||
100 - unknown
|
||||
*/
|
||||
int Compress::CompressAndPut(const char * source, size_t source_len, FCGX_Stream * out_stream, int level)
|
||||
int Compress::CompressAndPut(const char * source, size_t source_len, FCGX_Stream * out_stream, int encoding)
|
||||
{
|
||||
int ret;
|
||||
|
||||
z_stream * pstrm;
|
||||
|
||||
last_out_size = 0;
|
||||
|
||||
if( !ready_for_compress )
|
||||
@@ -156,17 +278,11 @@ int ret;
|
||||
|
||||
if( source_len == 0 )
|
||||
return 0;
|
||||
|
||||
ret = MakeCompress(source, source_len, out_stream);
|
||||
|
||||
if( deflateReset(&strm) != Z_OK )
|
||||
log << log1 << "Compress: problem with deflateReset()" << logend;
|
||||
|
||||
double ratio = 100.0 - (double(last_out_size) / double(source_len) * 100.0);
|
||||
char buffer[30];
|
||||
sprintf(buffer, "%.1f", ratio);
|
||||
|
||||
log << log2 << "Compress: original size: " << source_len << ", compress size: " << (int)last_out_size << ", ratio: " << buffer << "%" << logend;
|
||||
pstrm = SelectStream(encoding);
|
||||
ret = MakeCompress(*pstrm, source, source_len, out_stream, encoding);
|
||||
ResetStream(pstrm, encoding);
|
||||
PutLog(source_len, encoding);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
Reference in New Issue
Block a user