added: acceptbaseparser.h acceptbaseparser.cpp

class AcceptBaseParser for parsing http accept* headers
added: acceptencodingparser.h
       class AcceptEncodingParser for parsing HTTP_ACCEPT_ENCODING header
added: compresion only when HTTP_ACCEPT_ENCODING has 'deflate'
       and the browser is not the Internet Explorer


git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@515 e52654a7-88a9-db11-a3e9-0013d4bc506e
pull/3/head
Tomasz Sowa 14 years ago
parent 85b678a8fb
commit 7db71d43e0

@ -5,6 +5,7 @@ cat.o: ../templates/patterncacher.h ../app/templates.h ../core/thread.h
cat.o: ../core/request.h ../core/requesttypes.h ../core/session.h
cat.o: ../core/done.h ../core/item.h ../core/error.h ../core/log.h
cat.o: ../core/user.h ../core/function.h ../core/thread.h ../core/compress.h
cat.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
cat.o: ../core/error.h
content.o: content.h ../core/item.h ../templates/templates.h
content.o: ../../ezc/src/ezc.h ../templates/patterncacher.h
@ -12,6 +13,7 @@ content.o: ../app/templates.h ../core/thread.h ../core/request.h
content.o: ../core/requesttypes.h ../core/session.h ../core/done.h
content.o: ../core/item.h ../core/error.h ../core/log.h ../core/user.h
content.o: ../core/function.h ../core/thread.h ../core/compress.h
content.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
content.o: ../core/error.h ../core/db.h ../core/group.h
content.o: ../core/dircontainer.h ../core/ugcontainer.h ../core/data.h
content.o: ../core/dirs.h ../core/users.h ../core/groups.h
@ -23,6 +25,7 @@ createthread.o: ../app/templates.h ../core/thread.h ../core/request.h
createthread.o: ../core/requesttypes.h ../core/session.h ../core/done.h
createthread.o: ../core/item.h ../core/error.h ../core/log.h ../core/user.h
createthread.o: ../core/function.h ../core/thread.h ../core/compress.h
createthread.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
createthread.o: ../core/error.h ../core/db.h ../core/group.h
createthread.o: ../core/dircontainer.h ../core/ugcontainer.h ../core/mount.h
createthread.o: ../core/data.h ../core/dirs.h ../core/users.h
@ -34,6 +37,7 @@ default.o: ../app/templates.h ../core/thread.h ../core/request.h
default.o: ../core/requesttypes.h ../core/session.h ../core/done.h
default.o: ../core/item.h ../core/error.h ../core/log.h ../core/user.h
default.o: ../core/function.h ../core/thread.h ../core/compress.h
default.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
default.o: ../core/error.h ../core/db.h ../core/group.h
default.o: ../core/dircontainer.h ../core/ugcontainer.h ../core/data.h
default.o: ../core/dirs.h ../core/users.h ../core/groups.h
@ -44,12 +48,13 @@ emacs.o: ../../ezc/src/ezc.h ../templates/patterncacher.h ../app/templates.h
emacs.o: ../core/thread.h ../core/request.h ../core/requesttypes.h
emacs.o: ../core/session.h ../core/done.h ../core/item.h ../core/error.h
emacs.o: ../core/log.h ../core/user.h ../core/function.h ../core/thread.h
emacs.o: ../core/compress.h ../core/error.h ../core/db.h ../core/group.h
emacs.o: ../core/dircontainer.h ../core/ugcontainer.h ../core/data.h
emacs.o: ../core/dirs.h ../core/users.h ../core/groups.h ../core/functions.h
emacs.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h
emacs.o: ../core/notify.h ../templatesnotify/templatesnotify.h
emacs.o: ../core/mount.h
emacs.o: ../core/compress.h ../core/acceptencodingparser.h
emacs.o: ../core/acceptbaseparser.h ../core/error.h ../core/db.h
emacs.o: ../core/group.h ../core/dircontainer.h ../core/ugcontainer.h
emacs.o: ../core/data.h ../core/dirs.h ../core/users.h ../core/groups.h
emacs.o: ../core/functions.h ../core/lastcontainer.h ../core/mounts.h
emacs.o: ../core/mount.h ../core/notify.h
emacs.o: ../templatesnotify/templatesnotify.h ../core/mount.h
last.o: content.h ../core/item.h ../templates/templates.h ../../ezc/src/ezc.h
last.o: ../templates/patterncacher.h ../app/templates.h ../core/thread.h
login.o: content.h ../core/item.h ../templates/templates.h
@ -57,16 +62,19 @@ login.o: ../../ezc/src/ezc.h ../templates/patterncacher.h ../app/templates.h
login.o: ../core/thread.h ../core/request.h ../core/requesttypes.h
login.o: ../core/session.h ../core/done.h ../core/item.h ../core/error.h
login.o: ../core/log.h ../core/user.h ../core/function.h ../core/thread.h
login.o: ../core/compress.h ../core/error.h ../core/db.h ../core/group.h
login.o: ../core/dircontainer.h ../core/ugcontainer.h ../core/data.h
login.o: ../core/dirs.h ../core/users.h ../core/groups.h ../core/functions.h
login.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h
login.o: ../core/compress.h ../core/acceptencodingparser.h
login.o: ../core/acceptbaseparser.h ../core/error.h ../core/db.h
login.o: ../core/group.h ../core/dircontainer.h ../core/ugcontainer.h
login.o: ../core/data.h ../core/dirs.h ../core/users.h ../core/groups.h
login.o: ../core/functions.h ../core/lastcontainer.h ../core/mounts.h
login.o: ../core/mount.h
logout.o: content.h ../core/item.h ../templates/templates.h
logout.o: ../../ezc/src/ezc.h ../templates/patterncacher.h ../app/templates.h
logout.o: ../core/thread.h ../core/request.h ../core/requesttypes.h
logout.o: ../core/session.h ../core/done.h ../core/item.h ../core/error.h
logout.o: ../core/log.h ../core/user.h ../core/function.h ../core/thread.h
logout.o: ../core/compress.h ../core/data.h ../core/dirs.h
logout.o: ../core/compress.h ../core/acceptencodingparser.h
logout.o: ../core/acceptbaseparser.h ../core/data.h ../core/dirs.h
logout.o: ../core/dircontainer.h ../core/users.h ../core/ugcontainer.h
logout.o: ../core/groups.h ../core/group.h ../core/functions.h
logout.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h
@ -75,29 +83,32 @@ ls.o: ../templates/patterncacher.h ../app/templates.h ../core/thread.h
ls.o: ../core/request.h ../core/requesttypes.h ../core/session.h
ls.o: ../core/done.h ../core/item.h ../core/error.h ../core/log.h
ls.o: ../core/user.h ../core/function.h ../core/thread.h ../core/compress.h
ls.o: ../core/db.h ../core/group.h ../core/dircontainer.h
ls.o: ../core/ugcontainer.h
ls.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h ../core/db.h
ls.o: ../core/group.h ../core/dircontainer.h ../core/ugcontainer.h
mkdir.o: content.h ../core/item.h ../templates/templates.h
mkdir.o: ../../ezc/src/ezc.h ../templates/patterncacher.h ../app/templates.h
mkdir.o: ../core/thread.h ../core/request.h ../core/requesttypes.h
mkdir.o: ../core/session.h ../core/done.h ../core/item.h ../core/error.h
mkdir.o: ../core/log.h ../core/user.h ../core/function.h ../core/thread.h
mkdir.o: ../core/compress.h ../core/error.h ../core/db.h ../core/group.h
mkdir.o: ../core/dircontainer.h ../core/ugcontainer.h ../core/data.h
mkdir.o: ../core/dirs.h ../core/users.h ../core/groups.h ../core/functions.h
mkdir.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h
mkdir.o: ../core/notify.h ../templatesnotify/templatesnotify.h
mkdir.o: ../core/mount.h
mkdir.o: ../core/compress.h ../core/acceptencodingparser.h
mkdir.o: ../core/acceptbaseparser.h ../core/error.h ../core/db.h
mkdir.o: ../core/group.h ../core/dircontainer.h ../core/ugcontainer.h
mkdir.o: ../core/data.h ../core/dirs.h ../core/users.h ../core/groups.h
mkdir.o: ../core/functions.h ../core/lastcontainer.h ../core/mounts.h
mkdir.o: ../core/mount.h ../core/notify.h
mkdir.o: ../templatesnotify/templatesnotify.h ../core/mount.h
node.o: content.h ../core/item.h ../templates/templates.h ../../ezc/src/ezc.h
node.o: ../templates/patterncacher.h ../app/templates.h ../core/thread.h
node.o: ../core/request.h ../core/requesttypes.h ../core/session.h
node.o: ../core/done.h ../core/item.h ../core/error.h ../core/log.h
node.o: ../core/user.h ../core/function.h ../core/thread.h ../core/compress.h
node.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
priv.o: content.h ../core/item.h ../templates/templates.h ../../ezc/src/ezc.h
priv.o: ../templates/patterncacher.h ../app/templates.h ../core/thread.h
priv.o: ../core/request.h ../core/requesttypes.h ../core/session.h
priv.o: ../core/done.h ../core/item.h ../core/error.h ../core/log.h
priv.o: ../core/user.h ../core/function.h ../core/thread.h ../core/compress.h
priv.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
priv.o: ../core/error.h ../core/db.h ../core/group.h ../core/dircontainer.h
priv.o: ../core/ugcontainer.h ../core/data.h ../core/dirs.h ../core/users.h
priv.o: ../core/groups.h ../core/functions.h ../core/lastcontainer.h
@ -107,6 +118,7 @@ rm.o: ../templates/patterncacher.h ../app/templates.h ../core/thread.h
rm.o: ../core/request.h ../core/requesttypes.h ../core/session.h
rm.o: ../core/done.h ../core/item.h ../core/error.h ../core/log.h
rm.o: ../core/user.h ../core/function.h ../core/thread.h ../core/compress.h
rm.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
rm.o: ../core/error.h ../core/db.h ../core/group.h ../core/dircontainer.h
rm.o: ../core/ugcontainer.h ../core/data.h ../core/dirs.h ../core/users.h
rm.o: ../core/groups.h ../core/functions.h ../core/lastcontainer.h
@ -116,13 +128,15 @@ run.o: ../templates/patterncacher.h ../app/templates.h ../core/thread.h
run.o: ../core/request.h ../core/requesttypes.h ../core/session.h
run.o: ../core/done.h ../core/item.h ../core/error.h ../core/log.h
run.o: ../core/user.h ../core/function.h ../core/thread.h ../core/compress.h
run.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
run.o: ../core/error.h
thread.o: content.h ../core/item.h ../templates/templates.h
thread.o: ../../ezc/src/ezc.h ../templates/patterncacher.h ../app/templates.h
thread.o: ../core/thread.h ../core/request.h ../core/requesttypes.h
thread.o: ../core/session.h ../core/done.h ../core/item.h ../core/error.h
thread.o: ../core/log.h ../core/user.h ../core/function.h ../core/thread.h
thread.o: ../core/compress.h ../core/db.h ../core/group.h
thread.o: ../core/compress.h ../core/acceptencodingparser.h
thread.o: ../core/acceptbaseparser.h ../core/db.h ../core/group.h
thread.o: ../core/dircontainer.h ../core/ugcontainer.h ../core/data.h
thread.o: ../core/dirs.h ../core/users.h ../core/groups.h ../core/functions.h
thread.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h

@ -1,5 +1,6 @@
# DO NOT DELETE
acceptbaseparser.o: acceptbaseparser.h
compress.o: compress.h log.h
config.o: config.h ../confparser/confparser.h log.h data.h dirs.h item.h
config.o: dircontainer.h users.h user.h ugcontainer.h groups.h group.h
@ -23,7 +24,8 @@ functionparser.o: functionparser.h requesttypes.h ../app/content.h log.h
functionparser.o: item.h error.h data.h dirs.h dircontainer.h users.h user.h
functionparser.o: ugcontainer.h groups.h group.h functions.h function.h
functionparser.o: lastcontainer.h mounts.h mount.h db.h thread.h request.h
functionparser.o: session.h done.h compress.h
functionparser.o: session.h done.h compress.h acceptencodingparser.h
functionparser.o: acceptbaseparser.h
functions.o: functions.h function.h item.h
groups.o: groups.h group.h ugcontainer.h log.h db.h item.h user.h thread.h
groups.o: error.h dircontainer.h
@ -37,7 +39,8 @@ main.o: sessionmanager.h sessioncontainer.h session.h done.h item.h error.h
main.o: log.h user.h functionparser.h requesttypes.h ../app/content.h data.h
main.o: dirs.h dircontainer.h users.h ugcontainer.h groups.h group.h
main.o: functions.h function.h lastcontainer.h mounts.h mount.h request.h
main.o: thread.h compress.h db.h config.h ../confparser/confparser.h notify.h
main.o: thread.h compress.h acceptencodingparser.h acceptbaseparser.h db.h
main.o: config.h ../confparser/confparser.h notify.h
main.o: ../templatesnotify/templatesnotify.h ../core/mount.h
misc.o: misc.h item.h log.h
mount.o: mount.h
@ -47,17 +50,19 @@ mountparser.o: functions.h function.h lastcontainer.h mounts.h
mounts.o: mounts.h mount.h data.h dirs.h item.h dircontainer.h users.h user.h
mounts.o: ugcontainer.h log.h groups.h group.h functions.h function.h
mounts.o: lastcontainer.h request.h requesttypes.h session.h done.h error.h
mounts.o: thread.h compress.h mountparser.h
mounts.o: thread.h compress.h acceptencodingparser.h acceptbaseparser.h
mounts.o: mountparser.h
notify.o: log.h notify.h ../templatesnotify/templatesnotify.h ../core/mount.h
notify.o: ../../ezc/src/ezc.h data.h dirs.h item.h dircontainer.h users.h
notify.o: user.h ugcontainer.h groups.h group.h functions.h function.h
notify.o: lastcontainer.h mounts.h mount.h misc.h request.h requesttypes.h
notify.o: session.h done.h error.h thread.h compress.h
notify.o: session.h done.h error.h thread.h compress.h acceptencodingparser.h
notify.o: acceptbaseparser.h
request.o: request.h requesttypes.h session.h done.h item.h error.h log.h
request.o: user.h function.h thread.h compress.h getparser.h
request.o: httpsimpleparser.h postparser.h cookieparser.h data.h dirs.h
request.o: dircontainer.h users.h ugcontainer.h groups.h group.h functions.h
request.o: lastcontainer.h mounts.h mount.h
request.o: user.h function.h thread.h compress.h acceptencodingparser.h
request.o: acceptbaseparser.h getparser.h httpsimpleparser.h postparser.h
request.o: cookieparser.h data.h dirs.h dircontainer.h users.h ugcontainer.h
request.o: groups.h group.h functions.h lastcontainer.h mounts.h mount.h
requestcontroller.o: requestcontroller.h ../content/content.h ../core/item.h
requestcontroller.o: ../templates/templates.h ../../ezc/src/ezc.h
requestcontroller.o: ../templates/patterncacher.h ../app/templates.h
@ -66,7 +71,8 @@ requestcontroller.o: session.h done.h item.h error.h log.h user.h
requestcontroller.o: functionparser.h requesttypes.h ../app/content.h data.h
requestcontroller.o: dirs.h dircontainer.h users.h ugcontainer.h groups.h
requestcontroller.o: group.h functions.h function.h lastcontainer.h mounts.h
requestcontroller.o: mount.h request.h thread.h compress.h postparser.h
requestcontroller.o: mount.h request.h thread.h compress.h
requestcontroller.o: acceptencodingparser.h acceptbaseparser.h postparser.h
requestcontroller.o: httpsimpleparser.h cookieparser.h notify.h
requestcontroller.o: ../templatesnotify/templatesnotify.h ../core/mount.h
session.o: session.h done.h item.h error.h log.h user.h
@ -76,7 +82,8 @@ sessioncontainer.o: groups.h group.h functions.h function.h lastcontainer.h
sessioncontainer.o: mounts.h mount.h
sessionmanager.o: sessionmanager.h sessioncontainer.h session.h done.h item.h
sessionmanager.o: error.h log.h user.h request.h requesttypes.h function.h
sessionmanager.o: thread.h compress.h data.h dirs.h dircontainer.h users.h
sessionmanager.o: thread.h compress.h acceptencodingparser.h
sessionmanager.o: acceptbaseparser.h data.h dirs.h dircontainer.h users.h
sessionmanager.o: ugcontainer.h groups.h group.h functions.h lastcontainer.h
sessionmanager.o: mounts.h mount.h
users.o: users.h user.h ugcontainer.h log.h db.h item.h group.h thread.h

@ -1 +1 @@
o = compress.o config.o data.o db.o db_itemcolumns.o dircontainer.o dirs.o done.o error.o function.o functioncodeparser.o functionparser.o functions.o groups.o httpsimpleparser.o lastcontainer.o log.o main.o misc.o mount.o mountparser.o mounts.o notify.o request.o requestcontroller.o session.o sessioncontainer.o sessionmanager.o users.o
o = acceptbaseparser.o compress.o config.o data.o db.o db_itemcolumns.o dircontainer.o dirs.o done.o error.o function.o functioncodeparser.o functionparser.o functions.o groups.o httpsimpleparser.o lastcontainer.o log.o main.o misc.o mount.o mountparser.o mounts.o notify.o request.o requestcontroller.o session.o sessioncontainer.o sessionmanager.o users.o

@ -0,0 +1,113 @@
/*
* This file is a part of CMSLU -- Content Management System like Unix
* and is not publicly distributed
*
* Copyright (c) 2008-2009, Tomasz Sowa
* All rights reserved.
*
*/
#include "acceptbaseparser.h"
bool AcceptBaseParser::IsWhite(int c)
{
if( c==' ' || c=='\t' )
return true;
return false;
}
void AcceptBaseParser::SkipWhite()
{
while( IsWhite(*text) )
++text;
}
void AcceptBaseParser::RemoveWhiteFromEnd(std::string & str)
{
if( str.empty() )
return;
size_t i = str.size() - 1;
for( ; i!=0 && IsWhite(str[i]) ; --i);
if( !IsWhite(str[i]) )
++i;
if( i < str.size() )
str.erase(i); // erasing until the end of the string
}
void AcceptBaseParser::ReadParameter()
{
param.clear();
SkipWhite();
while( *text!=0 && *text!=',' && *text!=';' )
{
param += *text;
++text;
}
RemoveWhiteFromEnd(param);
}
void AcceptBaseParser::ReadQ()
{
q = 1.0;
SkipWhite();
if( *text != ';' )
return;
++text; // skipping a semicolon
while( *text!=0 && *text!=',' && *text!='=' )
// skipping until ',' or '='
++text;
if( *text==0 || *text==',' )
return;
++text; // skipping '='
SkipWhite();
q = strtod(text, (char**)&text);
}
void AcceptBaseParser::SkipParam()
{
SkipWhite();
if( *text == ',' )
++text;
}
void AcceptBaseParser::Parse(const char * str)
{
text = str;
Init();
while( *text != 0 )
{
ReadParameter();
ReadQ();
SkipParam();
Param(param, q);
}
}

@ -0,0 +1,47 @@
/*
* This file is a part of CMSLU -- Content Management System like Unix
* and is not publicly distributed
*
* Copyright (c) 2008-2009, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfilecmslucoreacceptbaseparser
#define headerfilecmslucoreacceptbaseparser
#include <string>
// sample (you must create your own class derived from this one):
// object.Parse(" text/html ; , ; q = 45, application / xhtml+xml ; q = 0.4 , application/xml ; q = 0.9 , */* ; q = 0.8 ");
class AcceptBaseParser
{
public:
void Parse(const char * str);
private:
virtual void Init() {} ;
virtual void Param(const std::string & param, double q) = 0;
bool IsWhite(int c);
void SkipWhite();
void RemoveWhiteFromEnd(std::string & str);
void ReadParameter();
void ReadQ();
void SkipParam();
const char * text;
std::string param;
double q;
};
#endif

@ -0,0 +1,49 @@
/*
* This file is a part of CMSLU -- Content Management System like Unix
* and is not publicly distributed
*
* Copyright (c) 2008-2009, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfilecmslucoreacceptencodingparser
#define headerfilecmslucoreacceptencodingparser
#include "acceptbaseparser.h"
#include "log.h"
class AcceptEncodingParser : public AcceptBaseParser
{
public:
bool AcceptDeflate()
{
return accept_deflate;
}
private:
void Init()
{
accept_deflate = false;
}
void Param(const std::string & param, double q)
{
if( param=="deflate" && q!=0 )
{
accept_deflate = true;
log << log3 << "AEP: accept deflate" << logend;
}
}
bool accept_deflate;
};
#endif

@ -7,166 +7,169 @@
*
*/
#include "compress.h"
#include "log.h"
Compress::Compress()
{
buffer = 0;
buffer_max_len = 65536; // 64KB
ready_for_compress = false;
}
Compress::~Compress()
{
if( buffer )
delete [] buffer;
}
bool Compress::AllocateMemory()
{
if( buffer )
delete [] buffer;
try
{
buffer = new char[buffer_max_len];
}
catch(const std::bad_alloc &)
{
log << log1 << "Compress: can't allocate memory" << logend;
buffer = 0;
return false;
}
return true;
}
/*
return:
0 - ok;
1 - can't allocate memory
100 - unknown
*/
int Compress::Init(int 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;
return 0;
}
int Compress::MakeCompress(const char * source, size_t source_len, FCGX_Stream * out_stream)
{
int ret, flush;
size_t have;
do
{
strm.avail_in = (source_len > buffer_max_len) ? buffer_max_len : source_len;
source_len -= strm.avail_in;
flush = (source_len == 0) ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = (Bytef*)source;
source += strm.avail_in;
do
{
strm.avail_out = buffer_max_len;
strm.next_out = (Bytef*)buffer;
ret = deflate(&strm, flush);
if( ret == Z_STREAM_ERROR || ret == Z_BUF_ERROR )
{
log << log1 << "Compress: problem with deflate()" << logend;
return 2;
}
have = buffer_max_len - strm.avail_out;
last_out_size += have;
FCGX_PutStr(buffer, have, out_stream);
}
while( strm.avail_out == 0 );
if( strm.avail_in != 0 )
{
log << log1 << "Compress: problem with deflate() - not all input is used" << logend;
return 2;
}
}
while( flush != Z_FINISH );
if( ret != Z_STREAM_END )
{
log << log1 << "Compress: problem with deflate() - stream not complete" << logend;
return 2;
}
return 0;
}
/*
return:
0 - ok;
1 - can't allocate memory
2 - error during compressing
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 ret;
last_out_size = 0;
if( !ready_for_compress )
{
log << log1 << "Compress: not ready yet" << logend;
return 3;
}
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 = (double(last_out_size) / double(source_len) * 100.0);
log << log2 << "Compress: original size: " << source_len << ", compress size: " << (int)last_out_size << ", ratio: " << ratio << "%" << logend;
return ret;
}
#include "compress.h"
#include "log.h"
Compress::Compress()
{
buffer = 0;
buffer_max_len = 65536; // 64KB
ready_for_compress = false;
}
Compress::~Compress()
{
if( buffer )
delete [] buffer;
}
bool Compress::AllocateMemory()
{
if( buffer )
delete [] buffer;
try
{
buffer = new char[buffer_max_len];
}
catch(const std::bad_alloc &)
{
log << log1 << "Compress: can't allocate memory" << logend;
buffer = 0;
return false;
}
return true;
}
/*
return:
0 - ok;
1 - can't allocate memory
100 - unknown
*/
int Compress::Init(int 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;
return 0;
}
int Compress::MakeCompress(const char * source, size_t source_len, FCGX_Stream * out_stream)
{
int ret, flush;
size_t have;
do
{
strm.avail_in = (source_len > buffer_max_len) ? buffer_max_len : source_len;
source_len -= strm.avail_in;
flush = (source_len == 0) ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = (Bytef*)source;
source += strm.avail_in;
do
{
strm.avail_out = buffer_max_len;
strm.next_out = (Bytef*)buffer;
ret = deflate(&strm, flush);
if( ret == Z_STREAM_ERROR || ret == Z_BUF_ERROR )
{
log << log1 << "Compress: problem with deflate()" << logend;
return 2;
}
have = buffer_max_len - strm.avail_out;
last_out_size += have;
FCGX_PutStr(buffer, have, out_stream);
}
while( strm.avail_out == 0 );
if( strm.avail_in != 0 )
{
log << log1 << "Compress: problem with deflate() - not all input is used" << logend;
return 2;
}
}
while( flush != Z_FINISH );
if( ret != Z_STREAM_END )
{
log << log1 << "Compress: problem with deflate() - stream not complete" << logend;
return 2;
}
return 0;
}
/*
return:
0 - ok;
1 - can't allocate memory
2 - error during compressing
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 ret;
last_out_size = 0;
if( !ready_for_compress )
{
log << log1 << "Compress: not ready yet" << logend;
return 3;
}
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;
return ret;
}

@ -7,37 +7,37 @@
*
*/
#ifndef headerfilecmslucorecompress
#define headerfilecmslucorecompress
#include <cstring>
#include <fcgiapp.h>
#include <zlib.h>
class Compress
{
public:
Compress();
~Compress();
int Init(int compress_level = 6);
int CompressAndPut(const char * source, size_t source_len, FCGX_Stream * out_stream, int level = 6);
size_t last_out_size;
private:
bool AllocateMemory();
int MakeCompress(const char * source, size_t source_len, FCGX_Stream * out_stream);
size_t buffer_max_len;
char * buffer;
z_stream strm;
bool ready_for_compress;
};
#endif
#ifndef headerfilecmslucorecompress
#define headerfilecmslucorecompress
#include <cstring>
#include <fcgiapp.h>
#include <zlib.h>
class Compress
{
public:
Compress();
~Compress();
int Init(int compress_level = 6);
int CompressAndPut(const char * source, size_t source_len, FCGX_Stream * out_stream, int level = 6);
size_t last_out_size;
private:
bool AllocateMemory();
int MakeCompress(const char * source, size_t source_len, FCGX_Stream * out_stream);
size_t buffer_max_len;
char * buffer;
z_stream strm;
bool ready_for_compress;
};
#endif

@ -51,7 +51,8 @@ void Request::Clear()
env_http_cookie = &char_empty;
env_remote_addr = &char_empty;
env_http_host = &char_empty;
env_http_user_agent = &char_empty;
env_http_accept_encoding = &char_empty;
session = 0;
@ -79,6 +80,7 @@ void Request::Clear()
notify_code = 0;
browser_msie = false;
}
@ -248,6 +250,19 @@ void Request::ReadEnvVariables()
env_remote_addr = SetEnvVar("REMOTE_ADDR");
env_http_host = SetEnvVar("HTTP_HOST");
env_http_user_agent = SetEnvVar("HTTP_USER_AGENT");
env_http_accept_encoding = SetEnvVar("HTTP_ACCEPT_ENCODING");
}
void Request::CheckIE()
{
char * msie = strstr(env_http_user_agent, "MSIE");
if( msie )
browser_msie = true;
else
browser_msie = false;
}
@ -266,12 +281,8 @@ void Request::CheckMethod()
void Request::ReadParameters()
{
// !!some parameters (get) we have always
// if( method == get )
{
GetParser get_parser(env_request_uri, get_table);
get_parser.Parse();
}
GetParser get_parser(env_request_uri, get_table);
get_parser.Parse();
if( method == post )
{
@ -281,6 +292,8 @@ void Request::ReadParameters()
CookieParser cookie_parser(env_http_cookie, cookie_table);
cookie_parser.Parse();
accept_encoding_parser.Parse(env_http_accept_encoding);
}
@ -300,6 +313,7 @@ void Request::Read()
StandardLog();
CheckMethod();
ReadParameters();
CheckIE();
}
@ -319,7 +333,9 @@ void Request::SendAll()
FCGX_PutS("Content-Type: Text/Html\r\n", out);
}
if( data.compression )
bool compressing = data.compression && !browser_msie && accept_encoding_parser.AcceptDeflate();
if( compressing )
FCGX_PutS("Content-Encoding: deflate\r\n", out);
FCGX_PutS(headers.str().c_str(), out);
@ -333,11 +349,15 @@ void Request::SendAll()
const std::string & source = page.str();
if( !data.compression || compress.CompressAndPut(source.c_str(), source.length(), out) != 0 )
if( compressing )
{
compress.CompressAndPut(source.c_str(), source.length(), out);
}
else
{
// there were problems during compressing, or we don't use compression at all
FCGX_PutS(source.c_str(), out);
}
const std::string & d = debug.str();

@ -22,7 +22,7 @@
#include "function.h"
#include "thread.h"
#include "compress.h"
#include "acceptencodingparser.h"
struct Request
@ -58,7 +58,12 @@ struct Request
const char * env_remote_addr;
const char * env_http_host;
const char * env_http_user_agent;
const char * env_http_accept_encoding;
// true if the browser is Microsoft Internet Explorer
bool browser_msie;
// current session
// is set after calling session_manager.SetSession()
Session * session;
@ -175,6 +180,9 @@ struct Request
bool CanUseBBCode(long user_id);
private:
void CheckIE();
// used to set some env_* variables into it, when the server didn't set that variable
// it contains '\0'
const char char_empty;
@ -184,7 +192,8 @@ private:
void StandardLog();
Compress compress;
AcceptEncodingParser accept_encoding_parser;
};

@ -4,7 +4,8 @@ dir.o: templates.h ../../ezc/src/ezc.h patterncacher.h ../core/item.h
dir.o: ../app/templates.h ../core/request.h ../core/requesttypes.h
dir.o: ../core/session.h ../core/done.h ../core/item.h ../core/error.h
dir.o: ../core/log.h ../core/user.h ../core/function.h ../core/thread.h
dir.o: ../core/compress.h ../core/data.h ../core/dirs.h
dir.o: ../core/compress.h ../core/acceptencodingparser.h
dir.o: ../core/acceptbaseparser.h ../core/data.h ../core/dirs.h
dir.o: ../core/dircontainer.h ../core/users.h ../core/ugcontainer.h
dir.o: ../core/groups.h ../core/group.h ../core/functions.h
dir.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h ../core/db.h
@ -16,12 +17,15 @@ doc.o: ../core/ugcontainer.h ../core/log.h ../core/groups.h ../core/group.h
doc.o: ../core/functions.h ../core/function.h ../core/lastcontainer.h
doc.o: ../core/mounts.h ../core/mount.h ../core/request.h
doc.o: ../core/requesttypes.h ../core/session.h ../core/done.h
doc.o: ../core/error.h ../core/thread.h ../core/compress.h ../core/misc.h
doc.o: ../core/error.h ../core/thread.h ../core/compress.h
doc.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
doc.o: ../core/misc.h
done.o: templates.h ../../ezc/src/ezc.h patterncacher.h ../core/item.h
done.o: ../app/templates.h ../core/request.h ../core/requesttypes.h
done.o: ../core/session.h ../core/done.h ../core/item.h ../core/error.h
done.o: ../core/log.h ../core/user.h ../core/function.h ../core/thread.h
done.o: ../core/compress.h ../core/misc.h
done.o: ../core/compress.h ../core/acceptencodingparser.h
done.o: ../core/acceptbaseparser.h ../core/misc.h
item.o: templates.h ../../ezc/src/ezc.h patterncacher.h ../core/item.h
item.o: ../app/templates.h ../core/data.h ../core/dirs.h ../core/item.h
item.o: ../core/dircontainer.h ../core/users.h ../core/user.h
@ -29,7 +33,9 @@ item.o: ../core/ugcontainer.h ../core/log.h ../core/groups.h ../core/group.h
item.o: ../core/functions.h ../core/function.h ../core/lastcontainer.h
item.o: ../core/mounts.h ../core/mount.h ../core/request.h
item.o: ../core/requesttypes.h ../core/session.h ../core/done.h
item.o: ../core/error.h ../core/thread.h ../core/compress.h ../core/misc.h
item.o: ../core/error.h ../core/thread.h ../core/compress.h
item.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
item.o: ../core/misc.h
last.o: templates.h ../../ezc/src/ezc.h patterncacher.h ../core/item.h
last.o: ../app/templates.h ../core/lastcontainer.h ../core/data.h
last.o: ../core/dirs.h ../core/item.h ../core/dircontainer.h ../core/users.h
@ -38,6 +44,7 @@ last.o: ../core/group.h ../core/functions.h ../core/function.h
last.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h
last.o: ../core/request.h ../core/requesttypes.h ../core/session.h
last.o: ../core/done.h ../core/error.h ../core/thread.h ../core/compress.h
last.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
last.o: ../core/misc.h
mount.o: templates.h ../../ezc/src/ezc.h patterncacher.h ../core/item.h
mount.o: ../app/templates.h ../core/data.h ../core/dirs.h ../core/item.h
@ -59,7 +66,9 @@ priv.o: ../core/ugcontainer.h ../core/log.h ../core/groups.h ../core/group.h
priv.o: ../core/functions.h ../core/function.h ../core/lastcontainer.h
priv.o: ../core/mounts.h ../core/mount.h ../core/request.h
priv.o: ../core/requesttypes.h ../core/session.h ../core/done.h
priv.o: ../core/error.h ../core/thread.h ../core/compress.h ../core/misc.h
priv.o: ../core/error.h ../core/thread.h ../core/compress.h
priv.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
priv.o: ../core/misc.h
sys.o: templates.h ../../ezc/src/ezc.h patterncacher.h ../core/item.h
sys.o: ../app/templates.h ../core/data.h ../core/dirs.h ../core/item.h
sys.o: ../core/dircontainer.h ../core/users.h ../core/user.h
@ -67,7 +76,9 @@ sys.o: ../core/ugcontainer.h ../core/log.h ../core/groups.h ../core/group.h
sys.o: ../core/functions.h ../core/function.h ../core/lastcontainer.h
sys.o: ../core/mounts.h ../core/mount.h ../core/request.h
sys.o: ../core/requesttypes.h ../core/session.h ../core/done.h
sys.o: ../core/error.h ../core/thread.h ../core/compress.h ../core/misc.h
sys.o: ../core/error.h ../core/thread.h ../core/compress.h
sys.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
sys.o: ../core/misc.h
templates.o: templates.h ../../ezc/src/ezc.h patterncacher.h ../core/item.h
templates.o: ../app/templates.h ../core/data.h ../core/dirs.h ../core/item.h
templates.o: ../core/dircontainer.h ../core/users.h ../core/user.h
@ -76,7 +87,8 @@ templates.o: ../core/group.h ../core/functions.h ../core/function.h
templates.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h
templates.o: ../core/request.h ../core/requesttypes.h ../core/session.h
templates.o: ../core/done.h ../core/error.h ../core/thread.h
templates.o: ../core/compress.h ../core/misc.h
templates.o: ../core/compress.h ../core/acceptencodingparser.h
templates.o: ../core/acceptbaseparser.h ../core/misc.h
thread.o: templates.h ../../ezc/src/ezc.h patterncacher.h ../core/item.h
thread.o: ../app/templates.h ../core/data.h ../core/dirs.h ../core/item.h
thread.o: ../core/dircontainer.h ../core/users.h ../core/user.h
@ -85,6 +97,7 @@ thread.o: ../core/group.h ../core/functions.h ../core/function.h
thread.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h
thread.o: ../core/request.h ../core/requesttypes.h ../core/session.h
thread.o: ../core/done.h ../core/error.h ../core/thread.h ../core/compress.h
thread.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
thread.o: ../core/misc.h
user.o: templates.h ../../ezc/src/ezc.h patterncacher.h ../core/item.h
user.o: ../app/templates.h ../core/data.h ../core/dirs.h ../core/item.h
@ -93,7 +106,9 @@ user.o: ../core/ugcontainer.h ../core/log.h ../core/groups.h ../core/group.h
user.o: ../core/functions.h ../core/function.h ../core/lastcontainer.h
user.o: ../core/mounts.h ../core/mount.h ../core/request.h
user.o: ../core/requesttypes.h ../core/session.h ../core/done.h
user.o: ../core/error.h ../core/thread.h ../core/compress.h ../core/misc.h
user.o: ../core/error.h ../core/thread.h ../core/compress.h
user.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
user.o: ../core/misc.h
who.o: templates.h ../../ezc/src/ezc.h patterncacher.h ../core/item.h
who.o: ../app/templates.h ../core/sessioncontainer.h ../core/session.h
who.o: ../core/done.h ../core/item.h ../core/error.h ../core/log.h
@ -105,4 +120,5 @@ who.o: ../core/dircontainer.h ../core/users.h ../core/ugcontainer.h
who.o: ../core/groups.h ../core/group.h ../core/functions.h
who.o: ../core/function.h ../core/lastcontainer.h ../core/mounts.h
who.o: ../core/mount.h ../core/request.h ../core/thread.h ../core/compress.h
who.o: ../core/acceptencodingparser.h ../core/acceptbaseparser.h
who.o: ../core/misc.h

Loading…
Cancel
Save