moved winix directories to winixd subdirectory
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@1027 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
169
winixd/Makefile
Normal file
169
winixd/Makefile
Normal file
@@ -0,0 +1,169 @@
|
||||
# Makefile for GNU make
|
||||
|
||||
include Makefile.dep
|
||||
|
||||
ifeq ($(CXX), g++)
|
||||
CXX = g++-4.8
|
||||
endif
|
||||
|
||||
ifndef CXX
|
||||
ifeq ($(OSTYPE), FreeBSD)
|
||||
CXX = clang++
|
||||
else
|
||||
CXX = g++-4.8
|
||||
endif
|
||||
endif
|
||||
|
||||
# CXX = g++-4.8
|
||||
|
||||
ifndef CXXFLAGS
|
||||
CXXFLAGS = -Wall -O0 -g -fPIC -pthread -std=c++11 -I/usr/local/include -I/usr/include/postgresql -DEZC_USE_WINIX_LOGGER -DEZC_HAS_SPECIAL_STREAM
|
||||
endif
|
||||
|
||||
ifndef AR
|
||||
AR = ar
|
||||
endif
|
||||
|
||||
ifndef CXXWINIXINCLUDEFLAGS
|
||||
# these flags are used for compiling and making dependencies (make depend)
|
||||
# it's better to not using any system directories like -I/usr/local/include here
|
||||
CXXWINIXINCLUDEFLAGS = -I../../winix -I../../../winix -I../../ezc/src -I../../../ezc/src -I../../tito/src -I../../../tito/src -I../../pikotools -I../../../pikotools
|
||||
endif
|
||||
|
||||
ifndef LDFLAGS
|
||||
LDFLAGS = -L/usr/local/lib
|
||||
endif
|
||||
|
||||
# this macro is used in ezc's Makefile
|
||||
EZC_USE_WINIX_LOGGER = yes
|
||||
|
||||
|
||||
# for make install
|
||||
winix_install_dir = /usr/local/winix
|
||||
|
||||
|
||||
export CXX
|
||||
export CXXFLAGS
|
||||
export CXXWINIXINCLUDEFLAGS
|
||||
export LDFLAGS
|
||||
export EZC_USE_WINIX_LOGGER
|
||||
|
||||
|
||||
all: winix.so plugins winix
|
||||
|
||||
|
||||
|
||||
winix.so: $(winix.src.files)
|
||||
@cd core ; $(MAKE) -e
|
||||
@cd db ; $(MAKE) -e
|
||||
@cd functions ; $(MAKE) -e
|
||||
@cd notify ; $(MAKE) -e
|
||||
@cd templates ; $(MAKE) -e
|
||||
@cd ../ezc/src ; $(MAKE) -e
|
||||
@cd ../tito/src ; $(MAKE) -e
|
||||
@cd ../pikotools ; $(MAKE) -e
|
||||
$(CXX) -shared -rdynamic -Wl,-whole-archive -o winix.so $(CXXFLAGS) $(CXXWINIXINCLUDEFLAGS) core/*.o db/*.o functions/*.o templates/*.o notify/*.o ../ezc/src/ezc.a ../tito/src/tito.a ../pikotools/utf8/utf8.a ../pikotools/space/space.a ../pikotools/mainparser/mainparser.a ../pikotools/date/date.a $(LDFLAGS) -lfcgi -lpq -lz -lpthread -lcurl -Wl,-no-whole-archive
|
||||
|
||||
|
||||
winix: winix.so $(winix.src.files)
|
||||
@cd main ; $(MAKE) -e
|
||||
$(CXX) -o winix $(CXXFLAGS) $(LDFLAGS) main/*.o winix.so -lfcgi
|
||||
|
||||
|
||||
|
||||
plugins: FORCE
|
||||
@cd plugins/stats ; $(MAKE) -e
|
||||
@cd plugins/thread ; $(MAKE) -e
|
||||
@cd plugins/ticket ; $(MAKE) -e
|
||||
@cd plugins/gallery ; $(MAKE) -e
|
||||
@cd plugins/group ; $(MAKE) -e
|
||||
@cd plugins/menu ; $(MAKE) -e
|
||||
@cd plugins/export ; $(MAKE) -e
|
||||
@cd plugins/mailregister ; $(MAKE) -e
|
||||
|
||||
|
||||
FORCE:
|
||||
|
||||
|
||||
clean:
|
||||
@cd core ; $(MAKE) -e clean
|
||||
@cd db ; $(MAKE) -e clean
|
||||
@cd functions ; $(MAKE) -e clean
|
||||
@cd templates ; $(MAKE) -e clean
|
||||
@cd notify ; $(MAKE) -e clean
|
||||
@cd plugins/stats ; $(MAKE) -e clean
|
||||
@cd plugins/thread ; $(MAKE) -e clean
|
||||
@cd plugins/ticket ; $(MAKE) -e clean
|
||||
@cd plugins/gallery ; $(MAKE) -e clean
|
||||
@cd plugins/group ; $(MAKE) -e clean
|
||||
@cd plugins/menu ; $(MAKE) -e clean
|
||||
@cd plugins/export ; $(MAKE) -e clean
|
||||
@cd plugins/mailregister ; $(MAKE) -e clean
|
||||
@cd ../ezc/src ; $(MAKE) -e clean
|
||||
@cd ../tito/src ; $(MAKE) -e clean
|
||||
@cd ../pikotools ; $(MAKE) -e clean
|
||||
@cd main ; $(MAKE) -e clean
|
||||
rm -f winix.so
|
||||
rm -f winix
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
depend:
|
||||
@cd core ; $(MAKE) -e depend
|
||||
@cd db ; $(MAKE) -e depend
|
||||
@cd functions ; $(MAKE) -e depend
|
||||
@cd templates ; $(MAKE) -e depend
|
||||
@cd notify ; $(MAKE) -e depend
|
||||
@cd plugins/stats ; $(MAKE) -e depend
|
||||
@cd plugins/thread ; $(MAKE) -e depend
|
||||
@cd plugins/ticket ; $(MAKE) -e depend
|
||||
@cd plugins/gallery ; $(MAKE) -e depend
|
||||
@cd plugins/group ; $(MAKE) -e depend
|
||||
@cd plugins/menu ; $(MAKE) -e depend
|
||||
@cd plugins/export ; $(MAKE) -e depend
|
||||
@cd plugins/mailregister ; $(MAKE) -e depend
|
||||
@cd ../ezc/src ; $(MAKE) -e depend
|
||||
@cd ../tito/src ; $(MAKE) -e depend
|
||||
@cd ../pikotools ; $(MAKE) -e depend
|
||||
@cd main ; $(MAKE) -e depend
|
||||
echo -n "winix.src.files = " > Makefile.dep
|
||||
find -E . -type f -regex ".*\.h|.*\.cpp" | xargs -I foo echo -n foo " " >> Makefile.dep
|
||||
find -E ../ezc/src -type f -regex ".*\.h|.*\.cpp" | xargs -I foo echo -n foo " " >> Makefile.dep
|
||||
find -E ../tito/src -type f -regex ".*\.h|.*\.cpp" | xargs -I foo echo -n foo " " >> Makefile.dep
|
||||
find -E ../pikotools -type f -regex ".*\.h|.*\.cpp" | xargs -I foo echo -n foo " " >> Makefile.dep
|
||||
|
||||
|
||||
|
||||
install: all
|
||||
# installing binaries
|
||||
rm -Rf $(winix_install_dir)/bin
|
||||
mkdir -p $(winix_install_dir)/bin
|
||||
cp winix $(winix_install_dir)/bin
|
||||
cp winix.so $(winix_install_dir)/bin
|
||||
# etc configs
|
||||
rm -Rf $(winix_install_dir)/etc
|
||||
mkdir -p $(winix_install_dir)/etc
|
||||
cp -rf etc/* $(winix_install_dir)/etc/
|
||||
# html templates
|
||||
rm -Rf $(winix_install_dir)/html
|
||||
mkdir -p $(winix_install_dir)/html
|
||||
cp -rf html/* $(winix_install_dir)/html/
|
||||
# txt templates
|
||||
rm -Rf $(winix_install_dir)/txt
|
||||
mkdir -p $(winix_install_dir)/txt
|
||||
cp -rf txt/* $(winix_install_dir)/txt/
|
||||
# locales
|
||||
rm -Rf $(winix_install_dir)/locale
|
||||
mkdir -p $(winix_install_dir)/locale
|
||||
cp -rf locale/* $(winix_install_dir)/locale/
|
||||
# plugins
|
||||
rm -Rf $(winix_install_dir)/plugins
|
||||
mkdir -p $(winix_install_dir)/plugins
|
||||
find plugins/ -name "*.so" | xargs -I foo cp foo $(winix_install_dir)/plugins/
|
||||
# deleting subversion directories
|
||||
find $(winix_install_dir) -type d -name ".svn" | xargs -I foo rm -fr foo
|
||||
# removing privileges for others
|
||||
find $(winix_install_dir) -exec chmod o-r,o-x,o-w "{}" "+"
|
||||
|
||||
1
winixd/Makefile.dep
Normal file
1
winixd/Makefile.dep
Normal file
File diff suppressed because one or more lines are too long
19
winixd/core/Makefile
Normal file
19
winixd/core/Makefile
Normal file
@@ -0,0 +1,19 @@
|
||||
include Makefile.o.dep
|
||||
|
||||
|
||||
all: $(o)
|
||||
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) -c $(CXXFLAGS) $(CXXWINIXINCLUDEFLAGS) $<
|
||||
|
||||
|
||||
depend:
|
||||
makedepend -Y. $(CXXWINIXINCLUDEFLAGS) -f- *.cpp > Makefile.dep
|
||||
echo -n "o = " > Makefile.o.dep
|
||||
ls -1 *.cpp | xargs -I foo echo -n foo " " | sed -E "s/([^\.]*)\.cpp[ ]/\1\.o/g" >> Makefile.o.dep
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
||||
|
||||
include Makefile.dep
|
||||
1171
winixd/core/Makefile.dep
Normal file
1171
winixd/core/Makefile.dep
Normal file
File diff suppressed because it is too large
Load Diff
1
winixd/core/Makefile.o.dep
Normal file
1
winixd/core/Makefile.o.dep
Normal file
@@ -0,0 +1 @@
|
||||
o = acceptbaseparser.o app.o basethread.o bbcodeparser.o compress.o config.o crypt.o dircontainer.o dirs.o groups.o htmlfilter.o httpsimpleparser.o image.o ipbancontainer.o item.o job.o lastcontainer.o loadavg.o lock.o log.o misc.o mount.o mountparser.o mounts.o plugin.o plugindata.o postmultiparser.o rebus.o request.o run.o session.o sessioncontainer.o sessionidmanager.o sessionmanager.o sessionparser.o slog.o synchro.o system.o threadmanager.o timezone.o timezones.o user.o users.o
|
||||
138
winixd/core/acceptbaseparser.cpp
Normal file
138
winixd/core/acceptbaseparser.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <wchar.h>
|
||||
#include "acceptbaseparser.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
bool AcceptBaseParser::IsWhite(int c)
|
||||
{
|
||||
if( c==' ' || c=='\t' )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void AcceptBaseParser::SkipWhite()
|
||||
{
|
||||
while( IsWhite(*text) )
|
||||
++text;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void AcceptBaseParser::ReadParameter()
|
||||
{
|
||||
param.clear();
|
||||
SkipWhite();
|
||||
|
||||
while( *text!=0 && *text!=',' && *text!=';' )
|
||||
{
|
||||
param += *text;
|
||||
++text;
|
||||
}
|
||||
|
||||
TrimWhite(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 = wcstod(text, (wchar_t**)&text);
|
||||
}
|
||||
|
||||
|
||||
void AcceptBaseParser::SkipParam()
|
||||
{
|
||||
SkipWhite();
|
||||
|
||||
if( *text == ',' )
|
||||
++text;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AcceptBaseParser::Parse(const wchar_t * str)
|
||||
{
|
||||
text = str;
|
||||
Init();
|
||||
|
||||
while( *text != 0 )
|
||||
{
|
||||
ReadParameter();
|
||||
ReadQ();
|
||||
SkipParam();
|
||||
Param(param, q);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AcceptBaseParser::Parse(const std::wstring & str)
|
||||
{
|
||||
Parse(str.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
80
winixd/core/acceptbaseparser.h
Normal file
80
winixd/core/acceptbaseparser.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_acceptbaseparser
|
||||
#define headerfile_winix_core_acceptbaseparser
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
// sample (you must create your own class derived from this one):
|
||||
// object.Parse(L" text/html ; , ; q = 45, application / xhtml+xml ; q = 0.4 , application/xml ; q = 0.9 , */* ; q = 0.8 ");
|
||||
class AcceptBaseParser
|
||||
{
|
||||
public:
|
||||
|
||||
void Parse(const wchar_t * str);
|
||||
void Parse(const std::wstring & str);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
virtual void Init() {} ;
|
||||
virtual void Param(const std::wstring & param, double q) = 0;
|
||||
|
||||
bool IsWhite(int c);
|
||||
void SkipWhite();
|
||||
void ReadParameter();
|
||||
void ReadQ();
|
||||
void SkipParam();
|
||||
|
||||
|
||||
const wchar_t * text;
|
||||
std::wstring param;
|
||||
double q;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
124
winixd/core/acceptencodingparser.h
Normal file
124
winixd/core/acceptencodingparser.h
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_acceptencodingparser
|
||||
#define headerfile_winix_core_acceptencodingparser
|
||||
|
||||
#include "acceptbaseparser.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class AcceptEncodingParser : public AcceptBaseParser
|
||||
{
|
||||
public:
|
||||
|
||||
bool AcceptDeflate()
|
||||
{
|
||||
return accept_deflate;
|
||||
}
|
||||
|
||||
|
||||
bool AcceptGzip()
|
||||
{
|
||||
return accept_gzip;
|
||||
}
|
||||
|
||||
|
||||
void ParseAndLog(const wchar_t * str)
|
||||
{
|
||||
Parse(str);
|
||||
|
||||
if( accept_deflate || accept_gzip )
|
||||
{
|
||||
log << log3 << "AEP: ";
|
||||
|
||||
if( accept_deflate )
|
||||
{
|
||||
log << "accept deflate";
|
||||
|
||||
if( accept_gzip )
|
||||
log << ", ";
|
||||
}
|
||||
|
||||
if( accept_gzip )
|
||||
log << "accept gzip";
|
||||
|
||||
log << logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ParseAndLog(const std::wstring & str)
|
||||
{
|
||||
ParseAndLog(str.c_str());
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
void Init()
|
||||
{
|
||||
accept_deflate = false;
|
||||
accept_gzip = false;
|
||||
}
|
||||
|
||||
|
||||
void Param(const std::wstring & param, double q)
|
||||
{
|
||||
if( param == L"deflate" && q!=0.0 )
|
||||
{
|
||||
accept_deflate = true;
|
||||
}
|
||||
|
||||
if( param == L"gzip" && q!=0.0 )
|
||||
{
|
||||
accept_gzip = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool accept_deflate;
|
||||
bool accept_gzip;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
1948
winixd/core/app.cpp
Normal file
1948
winixd/core/app.cpp
Normal file
File diff suppressed because it is too large
Load Diff
243
winixd/core/app.h
Normal file
243
winixd/core/app.h
Normal file
@@ -0,0 +1,243 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_app
|
||||
#define headerfile_winix_core_app
|
||||
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#include <errno.h>
|
||||
#include <fcgiapp.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "system.h"
|
||||
#include "mounts.h"
|
||||
#include "request.h"
|
||||
#include "synchro.h"
|
||||
#include "sessionmanager.h"
|
||||
#include "db/db.h"
|
||||
#include "functions/functions.h"
|
||||
#include "templates/templates.h"
|
||||
#include "compress.h"
|
||||
#include "postparser.h"
|
||||
#include "cookieparser.h"
|
||||
#include "postmultiparser.h"
|
||||
#include "acceptencodingparser.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
class App
|
||||
{
|
||||
public:
|
||||
|
||||
App();
|
||||
|
||||
bool InitFCGI();
|
||||
bool DropPrivileges();
|
||||
bool Init();
|
||||
void Start();
|
||||
void Close();
|
||||
void LogUserGroups();
|
||||
bool Demonize();
|
||||
void SetStopSignal();
|
||||
bool WasStopSignal();
|
||||
bool Lock();
|
||||
void Unlock();
|
||||
void StartThreads();
|
||||
void WaitForThreads();
|
||||
|
||||
// configuration read from a config file
|
||||
Config config;
|
||||
|
||||
// pointers to the current request and a session
|
||||
Cur cur;
|
||||
|
||||
// temporary one request object
|
||||
// current request
|
||||
Request req;
|
||||
|
||||
// users sessions
|
||||
SessionManager session_manager;
|
||||
|
||||
// database
|
||||
Db db;
|
||||
DbConn db_conn;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
model
|
||||
*/
|
||||
|
||||
// ...
|
||||
System system;
|
||||
|
||||
// functions (ls, cat, emacs, ...)
|
||||
Functions functions;
|
||||
|
||||
// false at the beginning
|
||||
// !! IMPROVE ME moze to do loggera dac?
|
||||
bool stdout_is_closed;
|
||||
|
||||
|
||||
/*
|
||||
view
|
||||
*/
|
||||
|
||||
Templates templates;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
enum Header
|
||||
{
|
||||
h_200,
|
||||
h_404,
|
||||
h_403
|
||||
};
|
||||
|
||||
PostParser post_parser;
|
||||
PostMultiParser post_multi_parser;
|
||||
CookieParser cookie_parser;
|
||||
AcceptEncodingParser accept_encoding_parser;
|
||||
Compress compress;
|
||||
FCGX_Request fcgi_request;
|
||||
int fcgi_socket;
|
||||
Synchro synchro;
|
||||
pthread_t signal_thread;
|
||||
std::string socket_to_send_on_exit;
|
||||
std::string send_data_buf;
|
||||
PT::SpaceToJSON json_generic_serializer;
|
||||
TextStream<std::wstring> json_out_stream;
|
||||
std::string aheader_name, aheader_value;
|
||||
std::wstring html_filtered;
|
||||
std::string output_8bit;
|
||||
BinaryPage compressed_output;
|
||||
std::wstring cookie_id_string;
|
||||
|
||||
bool InitFCGI(char * sock, char * sock_user, char * sock_group);
|
||||
bool InitFCGIChmodChownSocket(char * sock, char * sock_user, char * sock_group);
|
||||
bool DropPrivileges(char * user, char * group);
|
||||
bool DropPrivileges(const char * user, uid_t uid, gid_t gid, bool additional_groups);
|
||||
bool CheckAccessFromPlugins();
|
||||
void ProcessRequestThrow();
|
||||
void ProcessRequest();
|
||||
void BaseUrlRedirect(int code, bool add_subdomain);
|
||||
bool BaseUrlRedirect();
|
||||
void CheckIfNeedSSLredirect();
|
||||
void SetLocale();
|
||||
void CheckPostRedirect();
|
||||
void MakePage();
|
||||
void Make();
|
||||
void SaveSessionsIfNeeded(); // !! IMPROVE ME wywalic do menagera sesji??
|
||||
void LogAccess();
|
||||
void SendData(const BinaryPage & page, FCGX_Stream * out);
|
||||
void CreateJSONAnswer();
|
||||
|
||||
void ReadRequest();
|
||||
void SendTextAnswer();
|
||||
void SendBinaryAnswer();
|
||||
void SendAnswer();
|
||||
|
||||
void LogEnvironmentVariables();
|
||||
|
||||
void SetEnv(const char * name, std::wstring & env);
|
||||
void ReadEnvVariables();
|
||||
void ReadEnvRemoteIP();
|
||||
void ReadPostVars();
|
||||
|
||||
void CheckIE();
|
||||
void CheckKonqueror();
|
||||
void CheckRequestMethod();
|
||||
void CheckSSL();
|
||||
void SetSubdomain();
|
||||
|
||||
|
||||
Header GetHTTPStatusCode();
|
||||
void PrepareSessionCookie();
|
||||
void FilterContent();
|
||||
void SendHeaders();
|
||||
void SendCookies();
|
||||
bool AddHeader(const wchar_t * name, const wchar_t * value);
|
||||
bool AddHeader(const std::wstring & name, const std::wstring & value);
|
||||
bool AddHeader(const wchar_t * name, const PT::WTextStream & value);
|
||||
bool AddHeader(const std::wstring & name, const PT::WTextStream & value);
|
||||
bool PrepareHeadersStaticCreateResource(PT::WTextStream & out_path);
|
||||
void PrepareHeadersStatic();
|
||||
void PrepareHeaderContentType();
|
||||
void PrepareHeadersForbidden();
|
||||
void PrepareHeadersRedirect();
|
||||
void PrepareHeadersSendFile();
|
||||
void PrepareHeadersCompression(int compress_encoding);
|
||||
void PrepareHeadersNormal(Header header, size_t output_size);
|
||||
void PrepareHeaders(bool compressing, int compress_encoding, Header header, size_t output_size);
|
||||
int SelectDeflateVersion();
|
||||
void SelectCompression(size_t source_len, bool & compression_allowed, int & compression_encoding);
|
||||
bool CanSendContent();
|
||||
void ClearAfterRequest();
|
||||
|
||||
void LogUser(const char * msg, uid_t id);
|
||||
void LogGroup(const char * msg, gid_t id, bool put_logend = true);
|
||||
void LogUsers();
|
||||
void LogEffectiveGroups(std::vector<gid_t> & tab);
|
||||
void LogGroups();
|
||||
|
||||
static void * SpecialThreadForSignals(void*);
|
||||
void SendEmptyFastCGIPacket();
|
||||
|
||||
void CreateStaticTree();
|
||||
|
||||
// !! IMPROVE ME
|
||||
// !! move to the session manager?
|
||||
time_t last_sessions_save;
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
271
winixd/core/basethread.cpp
Normal file
271
winixd/core/basethread.cpp
Normal file
@@ -0,0 +1,271 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include "basethread.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
BaseThread::BaseThread() : thread_signal(PTHREAD_COND_INITIALIZER)
|
||||
{
|
||||
synchro = 0;
|
||||
thread_id = 0;
|
||||
work_mode = 0;
|
||||
wake_up_was_called = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BaseThread::SetSynchro(Synchro * psynchro)
|
||||
{
|
||||
synchro = psynchro;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BaseThread::Mode(int mode)
|
||||
{
|
||||
work_mode = mode;
|
||||
}
|
||||
|
||||
|
||||
pthread_t BaseThread::ThreadId()
|
||||
{
|
||||
return thread_id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool BaseThread::Lock()
|
||||
{
|
||||
return synchro->Lock();
|
||||
}
|
||||
|
||||
|
||||
void BaseThread::Unlock()
|
||||
{
|
||||
synchro->Unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool BaseThread::IsExitSignal()
|
||||
{
|
||||
bool res = true;
|
||||
|
||||
if( Lock() )
|
||||
{
|
||||
res = synchro->was_stop_signal;
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
bool BaseThread::BaseInit()
|
||||
{
|
||||
bool init_status = false;
|
||||
|
||||
if( Lock() )
|
||||
{
|
||||
init_status = Init(); // your virtual method
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return init_status;
|
||||
}
|
||||
|
||||
|
||||
void BaseThread::BaseUninit()
|
||||
{
|
||||
if( Lock() )
|
||||
{
|
||||
Uninit(); // your virtual method
|
||||
Unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool BaseThread::BaseSignalReceived()
|
||||
{
|
||||
bool make_do = false;
|
||||
|
||||
wake_up_was_called = false;
|
||||
|
||||
try
|
||||
{
|
||||
make_do = SignalReceived(); // your short-time virtual method (objects are locked)
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
}
|
||||
|
||||
return make_do;
|
||||
}
|
||||
|
||||
|
||||
// this is called only if your SignalReceived() returned true
|
||||
void BaseThread::BaseDo()
|
||||
{
|
||||
try
|
||||
{
|
||||
Do(); // your long-time virtual method (objects are *not* locked)
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// use it with Lock and Unlock
|
||||
bool BaseThread::WaitForSignal()
|
||||
{
|
||||
if( synchro->was_stop_signal || wake_up_was_called )
|
||||
return true;
|
||||
|
||||
return pthread_cond_wait(&thread_signal, &synchro->mutex) == 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// you should use this method with: synchro->Lock() and Unlock()
|
||||
void BaseThread::WakeUpThread()
|
||||
{
|
||||
wake_up_was_called = true;
|
||||
pthread_cond_signal(&thread_signal);
|
||||
}
|
||||
|
||||
|
||||
// use it with Lock and Unlock
|
||||
// it breaks only if there was a stop signal or the time has expired
|
||||
bool BaseThread::WaitForSignalSleep(time_t second)
|
||||
{
|
||||
timespec t;
|
||||
int res;
|
||||
|
||||
if( synchro->was_stop_signal )
|
||||
return true;
|
||||
|
||||
t.tv_sec = time(0) + second;
|
||||
t.tv_nsec = 0;
|
||||
|
||||
do
|
||||
{
|
||||
res = pthread_cond_timedwait(&thread_signal, &synchro->mutex, &t);
|
||||
}
|
||||
while( res == 0 && !synchro->was_stop_signal );
|
||||
// above condition means there was a signal
|
||||
// but it was not a stop signal so we should still wait
|
||||
|
||||
return res == 0 || res == ETIMEDOUT;
|
||||
}
|
||||
|
||||
|
||||
void BaseThread::WaitForThread()
|
||||
{
|
||||
pthread_join(thread_id, 0);
|
||||
}
|
||||
|
||||
|
||||
void BaseThread::SignalLoop()
|
||||
{
|
||||
bool make_do;
|
||||
|
||||
do
|
||||
{
|
||||
if( Lock() )
|
||||
{
|
||||
make_do = false;
|
||||
|
||||
if( WaitForSignal() ) // automatically unlock, wait and lock again when signal comes
|
||||
if( !synchro->was_stop_signal )
|
||||
make_do = BaseSignalReceived(); // your short-time virtual method will be called (objects locked)
|
||||
|
||||
Unlock(); // unlocking from WaitForSignal()
|
||||
|
||||
if( make_do )
|
||||
BaseDo(); // your long-time virtual method will be called (objects *not* locked)
|
||||
}
|
||||
}
|
||||
while( !IsExitSignal() );
|
||||
}
|
||||
|
||||
|
||||
void * BaseThread::StartRoutine(void * this_object)
|
||||
{
|
||||
BaseThread * base = reinterpret_cast<BaseThread*>(this_object);
|
||||
|
||||
if( base->synchro )
|
||||
{
|
||||
if( base->BaseInit() )
|
||||
{
|
||||
if( base->work_mode == 0 )
|
||||
base->SignalLoop();
|
||||
else
|
||||
base->Work();
|
||||
|
||||
base->BaseUninit();
|
||||
}
|
||||
}
|
||||
|
||||
pthread_exit(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool BaseThread::StartThread()
|
||||
{
|
||||
pthread_attr_t attr;
|
||||
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||
|
||||
int res = pthread_create(&thread_id, &attr, StartRoutine, this);
|
||||
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
return res == 0;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
158
winixd/core/basethread.h
Normal file
158
winixd/core/basethread.h
Normal file
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_basethread
|
||||
#define headerfile_winix_core_basethread
|
||||
|
||||
#include <pthread.h>
|
||||
#include "synchro.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
class BaseThread
|
||||
{
|
||||
public:
|
||||
|
||||
BaseThread();
|
||||
|
||||
// synchro object (must be set)
|
||||
void SetSynchro(Synchro * psynchro);
|
||||
|
||||
// work mode
|
||||
// we have two modes:
|
||||
// 0 - there is a loop with SignalReceived() and Do()
|
||||
// if SignalReceived() returns true then Do() is called
|
||||
// 1 - only Work() method is called
|
||||
// the thread exits after Work() has finished
|
||||
// default: 0
|
||||
void Mode(int mode);
|
||||
|
||||
// starting the second thread
|
||||
bool StartThread();
|
||||
|
||||
|
||||
// virtual methods which should/can be inherited by your class
|
||||
// the methods will be called from the other thread
|
||||
|
||||
// wake up the second thread
|
||||
// (if it waits for the signal)
|
||||
// you should use it with: synchro->Lock() and Unlock()
|
||||
// if the thread doesn't wait on a signal then nothing is done
|
||||
virtual void WakeUpThread();
|
||||
|
||||
// waiting until the thread exits
|
||||
// you should call WakeUpThread() before
|
||||
virtual void WaitForThread();
|
||||
|
||||
// initialize the thread
|
||||
// (global objects are locked)
|
||||
// if it returns false then the thread immediately exits
|
||||
// default: true
|
||||
virtual bool Init() { return true; }
|
||||
|
||||
// uninitialize the thread
|
||||
// this is called before the thread is prepare to detach
|
||||
// (global objects are locked)
|
||||
// it's called only if Init() returned true
|
||||
virtual void Uninit() {}
|
||||
|
||||
// returns the thread id
|
||||
// this identifier is set by StartThread() metdhod
|
||||
pthread_t ThreadId();
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
// signal came (work mode = 0 - default)
|
||||
// signal comes when an other thread calls WakeUpThread() method
|
||||
// check specific job and return true to call Do() next
|
||||
// (global objects are locked -- copy some global objects to local variables)
|
||||
virtual bool SignalReceived() { return false; };
|
||||
|
||||
// if SignalReceived() returned true then this method is called
|
||||
// global objects are *not* locked -- use only your local variables
|
||||
// if you have to do something on global objects use synchro->Lock() and synchro->Unlock()
|
||||
virtual void Do() {}
|
||||
|
||||
// this method is called after Init() when Mode(1) is used
|
||||
// this is for long-time job
|
||||
// this method is called only once
|
||||
// global objects are *not* locked
|
||||
virtual void Work() {}
|
||||
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
Synchro * synchro;
|
||||
pthread_t thread_id; // thread id - set by StartThread()
|
||||
pthread_cond_t thread_signal;
|
||||
int work_mode;
|
||||
bool wake_up_was_called;
|
||||
|
||||
void SignalLoop();
|
||||
static void * StartRoutine(void *);
|
||||
bool BaseInit();
|
||||
void BaseUninit();
|
||||
bool BaseSignalReceived();
|
||||
void BaseDo();
|
||||
bool WaitForSignal();
|
||||
bool WaitForSignalSleep(time_t second);
|
||||
|
||||
bool Lock();
|
||||
void Unlock();
|
||||
|
||||
// if the work done by Do() is long time consuming you should periodically check
|
||||
// wheter there was a signal for exiting, and if it was just simply return from Do()
|
||||
// (it's checking with locking and unlocking)
|
||||
bool IsExitSignal();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
626
winixd/core/bbcodeparser.cpp
Normal file
626
winixd/core/bbcodeparser.cpp
Normal file
@@ -0,0 +1,626 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "bbcodeparser.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
bool BBCODEParser::Equal(const wchar_t * str1, const wchar_t * str2)
|
||||
{
|
||||
while( *str1 == *str2 && *str1 != 0 )
|
||||
{
|
||||
str1 += 1;
|
||||
str2 += 1;
|
||||
}
|
||||
|
||||
return *str1 == *str2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool BBCODEParser::IsValidCharForName(int c)
|
||||
{
|
||||
if( (c>='a' && c<='z') ||
|
||||
(c>='A' && c<='Z') ||
|
||||
c=='*' || c=='_')
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool BBCODEParser::IsOpeningTagMark()
|
||||
{
|
||||
return (*pchar == '[');
|
||||
}
|
||||
|
||||
|
||||
// there are no commentaries in bbcode
|
||||
bool BBCODEParser::IsOpeningCommentaryTagMark()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool BBCODEParser::SkipCommentaryTagIfExists()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool BBCODEParser::IsClosingTagMark()
|
||||
{
|
||||
return (*pchar == ']');
|
||||
}
|
||||
|
||||
|
||||
bool BBCODEParser::IsClosingXmlSimpleTagMark()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// one enter will generate one <br>
|
||||
// two enters or more will generate only two br (<br><br>)
|
||||
void BBCODEParser::PutNormalText(const wchar_t * str, const wchar_t * end)
|
||||
{
|
||||
int br_len;
|
||||
|
||||
if( *pchar == 0 )
|
||||
{
|
||||
// trimming last white characters at end of the user text
|
||||
while( str<end && (IsWhite(*(end-1)) || *(end-1)==10) )
|
||||
--end;
|
||||
}
|
||||
|
||||
|
||||
while( str < end )
|
||||
{
|
||||
if( *str == 10 )
|
||||
{
|
||||
++str;
|
||||
br_len = 1;
|
||||
|
||||
// skipping white characters without a new line character
|
||||
while( str < end && IsWhite(*str) )
|
||||
++str;
|
||||
|
||||
if( str < end && *str == 10 )
|
||||
{
|
||||
br_len = 2;
|
||||
|
||||
// skipping white characters with new line characters
|
||||
while( str < end && (IsWhite(*str) || *str==10) )
|
||||
++str;
|
||||
}
|
||||
|
||||
if( !has_open_ol_tag && !has_open_ul_tag && !has_open_li_tag )
|
||||
{
|
||||
for(int i=0 ; i < br_len ; ++i)
|
||||
(*out_string) += L"<br>\n";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PrintEscape(*str);
|
||||
++str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::ReadNormalTextSkipWhite(const wchar_t * & start, const wchar_t * & last_non_white)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::CheckExceptions()
|
||||
{
|
||||
if( stack_len >= 2 )
|
||||
{
|
||||
if( pstack[stack_len-1].type == Item::opening &&
|
||||
pstack[stack_len-2].type == Item::opening &&
|
||||
IsNameEqual(L"*", pstack[stack_len-1].name) &&
|
||||
IsNameEqual(L"*", pstack[stack_len-2].name) )
|
||||
{
|
||||
// removing the last [*] from the stack
|
||||
// </li> was put automatically
|
||||
PopStack();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
bbcode format:
|
||||
[bbcodetag=value]some text[/bbcodetag]
|
||||
the value can be quoted, e.g.
|
||||
[bbcodetag="value"]some text[/bbcodetag], or
|
||||
[bbcodetag='value']some text[/bbcodetag]
|
||||
|
||||
the third string below (in tags table) is 'html_argument' from Tags,
|
||||
it can contain a special character % followed by a string which means:
|
||||
%1 - "value" escaped as for html
|
||||
%2 - "some text" escaped as for html
|
||||
%u1 - "value" trimmed and escaped as for url-es
|
||||
%u2 - "some text" trimmed and escaped as for url-es
|
||||
%% - one %
|
||||
|
||||
if you are using %2 or %u2 then "some text" is not treated as bbcode, e.g.
|
||||
[bbcodetag=value]some [b]text[/b][/bbcodetag] will produce:
|
||||
<htmltag arg="value">some [b]text[/b]</htmltag> (the inner tags [b][/b] were not parsed)
|
||||
|
||||
also when using %2 or %u2 the closing bbcode tag is skipped
|
||||
(if you want this tag then you can put it in 'html_argument')
|
||||
|
||||
and when using u (%u1 or %u2) the argument is trimmed from whitespaces and new lines
|
||||
at the beginning and at the end
|
||||
(because otherwise a space would be changed to %20 and this were probably not what you really wanted)
|
||||
*/
|
||||
const BBCODEParser::Tags * BBCODEParser::FindTag(const wchar_t * tag)
|
||||
{
|
||||
static Tags tags[] = {
|
||||
{L"*", L"li", L">", false},
|
||||
{L"b", L"em", L">", true},
|
||||
{L"i", L"span", L" class=\"bbitalic\">", true},
|
||||
{L"u", L"span", L" class=\"bbunderline\">", true},
|
||||
{L"s", L"span", L" class=\"bbstrike\">", true},
|
||||
{L"code", L"code", L" class=\"bbcode\">", false},
|
||||
{L"list", L"ul", L" class=\"bblist\">", false},
|
||||
{L"color", L"span", L" class=\"bbcol%1\">", true},
|
||||
{L"url", L"a", L" href=\"%u1\">", true},
|
||||
{L"img", L"img", L" alt=\"%1\" src=\"%u2\">", true},
|
||||
{L"quote", L"div", L" class=\"bbquote\">\n<span class=\"bbquotewho\">%1</span><br>\n", false},
|
||||
};
|
||||
|
||||
size_t i;
|
||||
size_t len = sizeof(tags) / sizeof(Tags);
|
||||
|
||||
for(i=0 ; i<len ; ++i)
|
||||
{
|
||||
if( Equal(tag, tags[i].bbcode) )
|
||||
return &tags[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const BBCODEParser::Tags * BBCODEParser::FindTag(const std::wstring & tag)
|
||||
{
|
||||
return FindTag(tag.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PrintArgumentCheckQuotes(const wchar_t * & start, const wchar_t * & end)
|
||||
{
|
||||
// skipping white characters from the argument
|
||||
while( start<end && IsWhite(*start) )
|
||||
++start;
|
||||
|
||||
// skipping first '=' character if exists
|
||||
if( start<end && *start == '=' )
|
||||
++start;
|
||||
|
||||
// skipping white characters from the argument
|
||||
// at the beginning
|
||||
while( start<end && IsWhite(*start) )
|
||||
++start;
|
||||
|
||||
// and at the end
|
||||
while( start<end && IsWhite(*(end-1)) )
|
||||
--end;
|
||||
|
||||
|
||||
if( start<end && (*start=='\'' || *start=='\"') )
|
||||
{
|
||||
++start;
|
||||
|
||||
if( start<end && *(start-1) == *(end-1) )
|
||||
--end;
|
||||
|
||||
// skipping white characters after a first quote char [url = " ww...."]
|
||||
while( start<end && IsWhite(*start) )
|
||||
++start;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PrintEncode(int c)
|
||||
{
|
||||
if( c == '&' )
|
||||
{
|
||||
(*out_string) += L"&";
|
||||
}
|
||||
else
|
||||
if( (c>='a' && c<='z') ||
|
||||
(c>='A' && c<='Z') ||
|
||||
(c>='0' && c<='9') ||
|
||||
(c=='_' || c=='?' || c=='.' || c==',' || c=='/' || c=='-' ||
|
||||
c=='+' || c=='*' || c=='(' || c==')' || c=='=' || c==':')
|
||||
)
|
||||
{
|
||||
(*out_string) += c;
|
||||
}
|
||||
else
|
||||
{
|
||||
wchar_t buffer[20];
|
||||
swprintf(buffer, 20, L"%02X", c);
|
||||
|
||||
(*out_string) += '%';
|
||||
(*out_string) += buffer;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PrintEscape(int c, bool change_quote)
|
||||
{
|
||||
if( c == '<' )
|
||||
{
|
||||
(*out_string) += L"<";
|
||||
}
|
||||
else
|
||||
if( c == '>' )
|
||||
{
|
||||
(*out_string) += L">";
|
||||
}
|
||||
else
|
||||
if( c == '&' )
|
||||
{
|
||||
(*out_string) += L"&";
|
||||
}
|
||||
else
|
||||
if( c == '\"' && change_quote )
|
||||
{
|
||||
(*out_string) += L""";
|
||||
}
|
||||
else
|
||||
{
|
||||
(*out_string) += c;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PrintArgumentEncode(const wchar_t * start, const wchar_t * end)
|
||||
{
|
||||
PrintArgumentCheckQuotes(start, end);
|
||||
TrimWhiteWithNewLines(start, end);
|
||||
|
||||
for( ; start<end ; ++start )
|
||||
PrintEncode(*start);
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PrintArgumentEscape(const wchar_t * start, const wchar_t * end)
|
||||
{
|
||||
PrintArgumentCheckQuotes(start, end);
|
||||
|
||||
for( ; start<end ; ++start )
|
||||
PrintEscape(*start, true); // quotes are escaped as well here
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::CheckOpeningTag(const Tags * tag, const wchar_t * tag_name, bool & condition)
|
||||
{
|
||||
if( Equal(tag->html_tag, tag_name) )
|
||||
{
|
||||
if( condition )
|
||||
{
|
||||
PutClosingTag(tag);
|
||||
(*out_string) += '\n';
|
||||
}
|
||||
|
||||
condition = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::CheckOpeningTag(const Tags * tag)
|
||||
{
|
||||
bool has_list_tag = has_open_ul_tag || has_open_ol_tag;
|
||||
|
||||
CheckOpeningTag(tag, L"li", has_open_li_tag);
|
||||
CheckOpeningTag(tag, L"ul", has_open_ul_tag);
|
||||
CheckOpeningTag(tag, L"ol", has_open_ol_tag);
|
||||
|
||||
if( has_open_li_tag && !has_list_tag )
|
||||
{
|
||||
(*out_string) += L"<ul>\n";
|
||||
has_open_ul_tag = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PrintEscape(const wchar_t * start, const wchar_t * end, bool change_quote)
|
||||
{
|
||||
for( ; start < end ; ++start)
|
||||
PrintEscape(*start, change_quote);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PrintEncode(const wchar_t * start, const wchar_t * end)
|
||||
{
|
||||
for( ; start < end ; ++start)
|
||||
PrintEncode(*start);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PutOpeningTagFromEzc()
|
||||
{
|
||||
// this can be a tag from Ezc templates system
|
||||
(*out_string) += '[';
|
||||
(*out_string) += LastItem().name;
|
||||
|
||||
const wchar_t * start = pchar;
|
||||
|
||||
while( *pchar && *pchar!=']' )
|
||||
++pchar;
|
||||
|
||||
if( *pchar == ']' )
|
||||
++pchar;
|
||||
|
||||
Put(start, pchar);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PutHtmlArgument1(const wchar_t * arg_start, const wchar_t * arg_end, bool has_u)
|
||||
{
|
||||
if( has_u )
|
||||
PrintArgumentEncode(arg_start, arg_end);
|
||||
else
|
||||
PrintArgumentEscape(arg_start, arg_end);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::TrimWhiteWithNewLines(const wchar_t * & start, const wchar_t * & end)
|
||||
{
|
||||
while( start < end && (IsWhite(*start) || *start==10) )
|
||||
++start;
|
||||
|
||||
while( start < end && (IsWhite(*(end-1)) || *(end-1)==10) )
|
||||
--end;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PutHtmlArgument2(const Tags * tag, bool has_u)
|
||||
{
|
||||
const wchar_t * start = pchar;
|
||||
const wchar_t * end = pchar;
|
||||
bool first_tag_removed = false;
|
||||
|
||||
while( *pchar != 0 )
|
||||
{
|
||||
if( IsOpeningTagMark() )
|
||||
{
|
||||
if( IsClosingTagForLastItem() )
|
||||
{
|
||||
// the last tag is skipped when using patterns with %2 or %u2
|
||||
|
||||
PopStack(); // removing opening tag from the stack
|
||||
first_tag_removed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pchar += 1;
|
||||
end = pchar;
|
||||
}
|
||||
}
|
||||
|
||||
if( !first_tag_removed )
|
||||
PopStack(); // user has forgotten to close the tag
|
||||
|
||||
if( has_u )
|
||||
{
|
||||
TrimWhiteWithNewLines(start, end);
|
||||
PrintEncode(start, end);
|
||||
}
|
||||
else
|
||||
{
|
||||
PrintEscape(start, end);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PutHtmlArgument(const Tags * tag, const wchar_t * arg_start, const wchar_t * arg_end)
|
||||
{
|
||||
const wchar_t * pattern = tag->html_argument;
|
||||
bool has_u;
|
||||
|
||||
while( *pattern )
|
||||
{
|
||||
if( *pattern == '%' )
|
||||
{
|
||||
++pattern;
|
||||
has_u = false;
|
||||
|
||||
if( *pattern == 'u' )
|
||||
{
|
||||
++pattern;
|
||||
has_u = true;
|
||||
}
|
||||
|
||||
if( *pattern == '1' )
|
||||
{
|
||||
++pattern;
|
||||
PutHtmlArgument1(arg_start, arg_end, has_u);
|
||||
}
|
||||
else
|
||||
if( *pattern == '2' )
|
||||
{
|
||||
++pattern;
|
||||
PutHtmlArgument2(tag, has_u);
|
||||
}
|
||||
else
|
||||
if( *pattern == '%' )
|
||||
{
|
||||
(*out_string) += '%';
|
||||
++pattern;
|
||||
}
|
||||
// else unrecognized, will be printed next time as a normal character
|
||||
}
|
||||
else
|
||||
{
|
||||
(*out_string) += *pattern;
|
||||
++pattern;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PutOpeningTagFromBBCode(const Tags * tag)
|
||||
{
|
||||
CheckOpeningTag(tag);
|
||||
PutOpeningTagMark();
|
||||
Put(tag->html_tag);
|
||||
|
||||
const wchar_t * start = pchar;
|
||||
|
||||
while( *pchar && *pchar != ']' )
|
||||
++pchar;
|
||||
|
||||
PutHtmlArgument(tag, start, pchar);
|
||||
|
||||
if( *pchar == ']' )
|
||||
++pchar;
|
||||
|
||||
if( !tag->inline_tag )
|
||||
{
|
||||
Put(10);
|
||||
SkipWhiteLines();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool BBCODEParser::PutOpeningTag()
|
||||
{
|
||||
const Tags * tag = FindTag(LastItem().name);
|
||||
|
||||
if( !tag )
|
||||
PutOpeningTagFromEzc();
|
||||
else
|
||||
PutOpeningTagFromBBCode(tag);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PutClosingTag(const Tags * tag)
|
||||
{
|
||||
if( !tag )
|
||||
return; // skipping the tag
|
||||
|
||||
PutOpeningTagMark();
|
||||
(*out_string) += '/';
|
||||
(*out_string) += tag->html_tag;
|
||||
PutClosingTagMark();
|
||||
|
||||
if( !tag->inline_tag )
|
||||
{
|
||||
(*out_string) += L"\n";
|
||||
SkipWhiteLines();
|
||||
}
|
||||
|
||||
if( Equal(tag->html_tag, L"li") )
|
||||
has_open_li_tag = false;
|
||||
|
||||
if( Equal(tag->html_tag, L"ol") )
|
||||
has_open_ol_tag = false;
|
||||
|
||||
if( Equal(tag->html_tag, L"ul") )
|
||||
has_open_ul_tag = false;
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PutClosingTag(const wchar_t * tag_name)
|
||||
{
|
||||
const Tags * tag = FindTag(tag_name);
|
||||
PutClosingTag(tag);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::Init()
|
||||
{
|
||||
has_open_li_tag = false;
|
||||
has_open_ol_tag = false;
|
||||
has_open_ul_tag = false;
|
||||
|
||||
SkipWhiteLines();
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::Uninit()
|
||||
{
|
||||
if( has_open_li_tag )
|
||||
(*out_string) += L"</li>\n";
|
||||
|
||||
if( has_open_ol_tag )
|
||||
(*out_string) += L"</ol>\n";
|
||||
|
||||
if( has_open_ul_tag )
|
||||
(*out_string) += L"</ul>\n";
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
125
winixd/core/bbcodeparser.h
Normal file
125
winixd/core/bbcodeparser.h
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_bbcodeparser
|
||||
#define headerfile_winix_core_bbcodeparser
|
||||
|
||||
#include "htmlfilter.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class BBCODEParser : public HTMLFilter
|
||||
{
|
||||
|
||||
struct Tags
|
||||
{
|
||||
const wchar_t * bbcode;
|
||||
const wchar_t * html_tag;
|
||||
const wchar_t * html_argument; // with closing '>'
|
||||
bool inline_tag;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
virtual methods
|
||||
(from HTMLFilter class)
|
||||
*/
|
||||
virtual void Init();
|
||||
virtual void Uninit();
|
||||
|
||||
virtual bool IsOpeningTagMark();
|
||||
virtual bool IsOpeningCommentaryTagMark();
|
||||
virtual bool IsClosingTagMark();
|
||||
virtual bool IsClosingXmlSimpleTagMark();
|
||||
|
||||
virtual bool IsValidCharForName(int c);
|
||||
virtual void CheckExceptions();
|
||||
virtual bool SkipCommentaryTagIfExists();
|
||||
|
||||
virtual bool PutOpeningTag();
|
||||
virtual void PutClosingTag(const wchar_t * tag);
|
||||
|
||||
virtual void PutNormalText(const wchar_t * str, const wchar_t * end);
|
||||
virtual void ReadNormalTextSkipWhite(const wchar_t * & start, const wchar_t * & last_non_white);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
others
|
||||
*/
|
||||
bool Equal(const wchar_t * str1, const wchar_t * str2);
|
||||
|
||||
void PutHtmlArgument1(const wchar_t * arg_start, const wchar_t * arg_end, bool has_u);
|
||||
void PutHtmlArgument2(const Tags * tag, bool has_u);
|
||||
void PutHtmlArgument(const Tags * tag, const wchar_t * arg_start, const wchar_t * arg_end);
|
||||
|
||||
void PutOpeningTagFromEzc();
|
||||
void PutOpeningTagFromBBCode(const Tags * tag);
|
||||
|
||||
const Tags * FindTag(const wchar_t * tag);
|
||||
const Tags * FindTag(const std::wstring & tag);
|
||||
void PrintArgumentCheckQuotes(const wchar_t * & start, const wchar_t * & end);
|
||||
|
||||
void PrintEscape(int c, bool change_quote = false);
|
||||
void PrintEncode(int c);
|
||||
|
||||
void PrintEscape(const wchar_t * start, const wchar_t * end, bool change_quote = false);
|
||||
void PrintEncode(const wchar_t * start, const wchar_t * end);
|
||||
|
||||
void PrintArgumentEncode(const wchar_t * start, const wchar_t * end);
|
||||
void PrintArgumentEscape(const wchar_t * start, const wchar_t * end);
|
||||
|
||||
void PutClosingTag(const Tags * tag);
|
||||
|
||||
void CheckOpeningTag(const Tags * tag, const wchar_t * tag_name, bool & condition);
|
||||
void CheckOpeningTag(const Tags * tag);
|
||||
|
||||
void TrimWhiteWithNewLines(const wchar_t * & start, const wchar_t * & end);
|
||||
|
||||
|
||||
|
||||
bool has_open_ol_tag; // has open html <ol> tag
|
||||
bool has_open_ul_tag; // has open html <ul> tag
|
||||
bool has_open_li_tag; // has open html <li> tag
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
433
winixd/core/compress.cpp
Normal file
433
winixd/core/compress.cpp
Normal file
@@ -0,0 +1,433 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "compress.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
Compress::Compress()
|
||||
{
|
||||
buffer_in = 0;
|
||||
buffer_out = 0;
|
||||
buffer_max_len = 65536; // 64KB
|
||||
ready_for_compress = false;
|
||||
compress_level = 6;
|
||||
raw_deflate_inited = false;
|
||||
deflate_inited = false;
|
||||
gzip_inited = false;
|
||||
}
|
||||
|
||||
|
||||
Compress::~Compress()
|
||||
{
|
||||
delete [] buffer_in;
|
||||
delete [] buffer_out;
|
||||
|
||||
if( raw_deflate_inited )
|
||||
deflateEnd(&strm_raw_deflate);
|
||||
|
||||
if( deflate_inited )
|
||||
deflateEnd(&strm_deflate);
|
||||
|
||||
if( gzip_inited )
|
||||
deflateEnd(&strm_gzip);
|
||||
}
|
||||
|
||||
|
||||
bool Compress::AllocateMemory()
|
||||
{
|
||||
if( buffer_in )
|
||||
delete [] buffer_in;
|
||||
|
||||
if( buffer_out )
|
||||
delete [] buffer_out;
|
||||
|
||||
buffer_in = 0;
|
||||
buffer_out = 0;
|
||||
|
||||
try
|
||||
{
|
||||
buffer_in = new char[buffer_max_len];
|
||||
buffer_out = new char[buffer_max_len];
|
||||
}
|
||||
catch(const std::bad_alloc &)
|
||||
{
|
||||
log << log1 << "Compress: can't allocate memory" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
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 error
|
||||
*/
|
||||
int Compress::Init(int compress_level_)
|
||||
{
|
||||
compress_level = compress_level_;
|
||||
|
||||
if( !AllocateMemory() )
|
||||
return 1;
|
||||
|
||||
if( InitRawDeflate() && InitDeflate() && InitGzip() )
|
||||
ready_for_compress = true;
|
||||
else
|
||||
return 100;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int Compress::MakeCompress(z_stream & strm, const char * source, size_t source_len, BinaryPage & out_stream, int encoding)
|
||||
{
|
||||
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_out;
|
||||
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;
|
||||
out_stream.write(buffer_out, have);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Compress::CopyToInputBuffer(BinaryPage::const_iterator & i, size_t len)
|
||||
{
|
||||
for(size_t a=0 ; a<len ; ++a, ++i)
|
||||
buffer_in[a] = *i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// new way
|
||||
int Compress::MakeCompress(z_stream & strm, const BinaryPage & page, BinaryPage & out, int encoding)
|
||||
{
|
||||
int ret, flush;
|
||||
size_t have;
|
||||
BinaryPage::const_iterator i = page.begin();
|
||||
|
||||
size_t source_len = page.size();
|
||||
|
||||
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*)buffer_in;
|
||||
CopyToInputBuffer(i, strm.avail_in);
|
||||
|
||||
do
|
||||
{
|
||||
strm.avail_out = buffer_max_len;
|
||||
strm.next_out = (Bytef*)buffer_out;
|
||||
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;
|
||||
out.write(buffer_out, have);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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:
|
||||
0 - ok;
|
||||
1 - can't allocate memory
|
||||
2 - error during compressing
|
||||
3 - not inited (use Init() first)
|
||||
100 - unknown
|
||||
*/
|
||||
int Compress::Compressing(const char * source, size_t source_len, BinaryPage & out_stream, int encoding)
|
||||
{
|
||||
int ret;
|
||||
z_stream * pstrm;
|
||||
|
||||
last_out_size = 0;
|
||||
out_stream.clear();
|
||||
|
||||
if( !ready_for_compress )
|
||||
{
|
||||
log << log1 << "Compress: not ready yet" << logend;
|
||||
return 3;
|
||||
}
|
||||
|
||||
// !! CHECK ME
|
||||
// it is correct to immediately return? what about headers in the compressed page?
|
||||
if( source_len == 0 )
|
||||
return 0;
|
||||
|
||||
pstrm = SelectStream(encoding);
|
||||
ret = MakeCompress(*pstrm, source, source_len, out_stream, encoding);
|
||||
ResetStream(pstrm, encoding);
|
||||
PutLog(source_len, encoding);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
return:
|
||||
0 - ok;
|
||||
1 - can't allocate memory
|
||||
2 - error during compressing
|
||||
3 - not inited (use Init() first)
|
||||
100 - unknown
|
||||
*/
|
||||
int Compress::Compressing(const BinaryPage & in, BinaryPage & out, int encoding)
|
||||
{
|
||||
int ret;
|
||||
z_stream * pstrm;
|
||||
|
||||
last_out_size = 0;
|
||||
out.clear();
|
||||
|
||||
if( !ready_for_compress )
|
||||
{
|
||||
log << log1 << "Compress: not ready yet" << logend;
|
||||
return 3;
|
||||
}
|
||||
|
||||
// !! CHECK ME
|
||||
// it is correct to immediately return? what about headers in the compressed page?
|
||||
if( in.empty() )
|
||||
return 0;
|
||||
|
||||
pstrm = SelectStream(encoding);
|
||||
ret = MakeCompress(*pstrm, in, out, encoding);
|
||||
ResetStream(pstrm, encoding);
|
||||
PutLog(in.size(), encoding);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
101
winixd/core/compress.h
Normal file
101
winixd/core/compress.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_compress
|
||||
#define headerfile_winix_core_compress
|
||||
|
||||
#include <cstring>
|
||||
#include <zlib.h>
|
||||
#include "requesttypes.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class Compress
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
Compress();
|
||||
~Compress();
|
||||
|
||||
int Init(int compress_level_ = 6);
|
||||
|
||||
/*
|
||||
encoding:
|
||||
0 - raw deflate data with no zlib header or trailer, and will not compute an adler32 check value
|
||||
(for Internet Explorer)
|
||||
1 - deflate
|
||||
2 - gzip
|
||||
*/
|
||||
int Compressing(const char * source, size_t source_len, BinaryPage & out_stream, int encoding = 2);
|
||||
int Compressing(const BinaryPage & in, BinaryPage & out, int encoding = 2);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool AllocateMemory();
|
||||
bool InitRawDeflate();
|
||||
bool InitDeflate();
|
||||
bool InitGzip();
|
||||
|
||||
int MakeCompress(z_stream & strm, const char * source, size_t source_len, BinaryPage & out_stream, int encoding);
|
||||
int MakeCompress(z_stream & strm, const BinaryPage & page, BinaryPage & out, int encoding);
|
||||
z_stream * SelectStream(int encoding);
|
||||
void ResetStream(z_stream * pstrm, int encoding);
|
||||
void PutLog(size_t source_len, int encoding);
|
||||
void CopyToInputBuffer(BinaryPage::const_iterator & i, size_t len);
|
||||
|
||||
int compress_level;
|
||||
size_t buffer_max_len;
|
||||
|
||||
// size of the last compressed page
|
||||
size_t last_out_size;
|
||||
|
||||
char * buffer_in;
|
||||
char * buffer_out;
|
||||
z_stream strm_raw_deflate, strm_deflate, strm_gzip;
|
||||
bool raw_deflate_inited, deflate_inited, gzip_inited;
|
||||
bool ready_for_compress;
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
511
winixd/core/config.cpp
Normal file
511
winixd/core/config.cpp
Normal file
@@ -0,0 +1,511 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2015, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "log.h"
|
||||
#include "plugin.h"
|
||||
#include "misc.h"
|
||||
#include "crypt.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
Config::Config()
|
||||
{
|
||||
errors_to_stdout = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//!! czy tu w ogole mozemy uzywac log << ?
|
||||
//!! przeciez jeszcze nie zostal przetworzony plik konfiguracyjny
|
||||
|
||||
|
||||
void Config::ShowError()
|
||||
{
|
||||
|
||||
switch( parser.status )
|
||||
{
|
||||
case PT::SpaceParser::no_space:
|
||||
log << log2 << "Config: space not set" << logend;
|
||||
break;
|
||||
|
||||
case PT::SpaceParser::ok:
|
||||
log << log2 << "Config: syntax ok" << logend;
|
||||
break;
|
||||
|
||||
case PT::SpaceParser::cant_open_file:
|
||||
if( errors_to_stdout )
|
||||
std::wcout << L"Config: I cannot open a config file: " << config_file << std::endl;
|
||||
|
||||
log << log1 << "Config: cant open a config file: " << config_file << logend;
|
||||
break;
|
||||
|
||||
case PT::SpaceParser::syntax_error:
|
||||
if( errors_to_stdout )
|
||||
std::wcout << "Config: syntax error, line: " << parser.line << std::endl;
|
||||
|
||||
log << log1 << "Config: syntax error, line: " << parser.line << logend;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool Config::ReadConfig(bool errors_to_stdout_, bool stdout_is_closed)
|
||||
{
|
||||
errors_to_stdout = errors_to_stdout_;
|
||||
|
||||
if( config_file.empty() )
|
||||
{
|
||||
log << log2 << "Config: name of the config file is empty" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
log << log2 << "Config: reading a config file" << logend;
|
||||
|
||||
parser.SplitSingle(true);
|
||||
parser.SetSpace(space);
|
||||
|
||||
PT::SpaceParser::Status status = parser.Parse(config_file);
|
||||
|
||||
if( status == PT::SpaceParser::ok )
|
||||
{
|
||||
AssignValues(stdout_is_closed);
|
||||
SetAdditionalVariables();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowError();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Config::AssignValues(bool stdout_is_closed)
|
||||
{
|
||||
server_mode = Text(L"server_mode");
|
||||
demonize = Bool(L"demonize", true);
|
||||
|
||||
user = Text(L"user");
|
||||
group = Text(L"group");
|
||||
additional_groups = Bool(L"additional_groups", true);
|
||||
|
||||
log_file = Text(L"log_file");
|
||||
log_notify_file = Text(L"log_notify_file");
|
||||
log_delimiter = Text(L"log_delimiter", L"---------------------------------------------------------------------------------");
|
||||
fcgi_socket = Text(L"fcgi_socket");
|
||||
fcgi_socket_chmod = Int(L"fcgi_socket_chmod", 0770);
|
||||
fcgi_socket_user = Text(L"fcgi_socket_user");
|
||||
fcgi_socket_group = Text(L"fcgi_socket_group");
|
||||
fcgi_socket_listen = Int(L"fcgi_socket_listen", 100);
|
||||
log_level = Int(L"log_level", 1);
|
||||
log_request = Int(L"log_request", 1);
|
||||
log_save_each_line = Bool(L"log_save_each_line", false);
|
||||
log_time_zone_id = Size(L"log_time_zone_id", 34);
|
||||
log_server_answer = Bool(L"log_server_answer", false);
|
||||
log_stdout = Bool(L"log_stdout", false);
|
||||
log_db_query = Bool(L"log_db_query", false);
|
||||
log_plugin_call = Bool(L"log_plugin_call", false);
|
||||
log_post_value_size = Size(L"log_post_value_size", 80);
|
||||
log_env_variables = Bool(L"log_env_variables", false);
|
||||
log_http_answer_headers = Bool(L"log_http_answer_headers", false);
|
||||
|
||||
post_file_max = Size(L"post_file_max", 8388608); // 8 MB
|
||||
|
||||
upload_dir = Text(L"upload_dir");
|
||||
common_dir = Text(L"common_dir");
|
||||
NoLastSlash(upload_dir);
|
||||
NoLastSlash(common_dir);
|
||||
|
||||
upload_group = Text(L"upload_group");
|
||||
upload_dirs_chmod = Int(L"upload_dirs_chmod", 0750);
|
||||
upload_files_chmod = Int(L"upload_files_chmod", 0640);
|
||||
ListText(L"static_dirs", static_dirs);
|
||||
dont_use_static_dirs = Bool(L"dont_use_static_dirs", false);
|
||||
create_thumb = Bool(L"create_thumb", true);
|
||||
thumb_mode = Int(L"thumb_mode", 2);
|
||||
thumb_cx = Size(L"thumb_cx", 150);
|
||||
thumb_cy = Size(L"thumb_cy", 150);
|
||||
thumb_quality = Int(L"thumb_quality", 92);
|
||||
image_resize = Bool(L"image_resize", true);
|
||||
image_mode = Int(L"image_mode", 6);
|
||||
image_cx = Size(L"image_cx", 1000);
|
||||
image_cy = Size(L"image_cy", 800);
|
||||
image_quality = Int(L"image_quality", 92);
|
||||
convert_cmd = Text(L"convert_cmd", L"/usr/local/bin/convert");
|
||||
|
||||
templates_dir = Text(L"templates_dir");
|
||||
templates_dir_default = Text(L"templates_dir_default");
|
||||
txt_templates_dir = Text(L"txt_templates_dir");
|
||||
txt_templates_dir_default = Text(L"txt_templates_dir_default");
|
||||
templates_fun_prefix = Text(L"templates_fun_prefix", L"fun_");
|
||||
templates_fun_postfix = Text(L"templates_fun_postfix", L".html");
|
||||
templates_index = Text(L"templates_index", L"index.html");
|
||||
template_only_root_use_template_fun = Bool(L"template_only_root_use_template_fun", false);
|
||||
|
||||
http_session_id_name = Text(L"http_session_id_name", L"session_id");
|
||||
db_database = Text(L"db_database");
|
||||
db_user = Text(L"db_user");
|
||||
db_pass = Text(L"db_pass");
|
||||
item_url_empty = Text(L"item_url_empty");
|
||||
|
||||
url_proto = Text(L"url_proto", L"http://");
|
||||
url_ssl_proto = Text(L"url_ssl_proto", L"https://");
|
||||
|
||||
use_ssl = Bool(L"use_ssl", false);
|
||||
use_ssl_static = Bool(L"use_ssl_static", false);
|
||||
use_ssl_common = Bool(L"use_ssl_common", false);
|
||||
use_ssl_only_for_logged_users = Bool(L"use_ssl_only_for_logged_users", true);
|
||||
use_ssl_redirect_code = Int(L"use_ssl_redirect_code", 303);
|
||||
|
||||
base_url = Text(L"base_url");
|
||||
base_url_static = Text(L"base_url_static");
|
||||
base_url_common = Text(L"base_url_common");
|
||||
|
||||
base_url_redirect = Bool(L"base_url_redirect", false);
|
||||
base_url_redirect_code = Int(L"base_url_redirect_code", 301);
|
||||
|
||||
NoLastSlash(base_url);
|
||||
NoLastSlash(base_url_static);
|
||||
NoLastSlash(base_url_common);
|
||||
|
||||
priv_no_user = Text(L"priv_no_user", L"-- no user --");
|
||||
priv_no_group = Text(L"priv_no_group", L"-- no group --");
|
||||
|
||||
session_max_idle = Int(L"session_max_idle", 10800); // 3h
|
||||
session_remember_max_idle = Int(L"session_remember_max_idle", 16070400); // 3 months
|
||||
session_file = Text(L"session_file");
|
||||
session_max = Size(L"session_max", 1000000);
|
||||
session_cookie_encode = Bool(L"session_cookie_encode", false);
|
||||
session_keys_file = Text(L"session_keys_file");
|
||||
session_allow_index_difference = Size(L"session_allow_index_difference", 8);
|
||||
session_index_time_increment = Long(L"session_index_time_increment", 30);
|
||||
session_key_renew_time = Size(L"session_key_renew_time", 172800); // 2 days
|
||||
broken_encoded_cookie_treshold = Size(L"broken_encoded_cookie_treshold", 2);
|
||||
session_hijacking_treshold = Size(L"session_hijacking_treshold", 128);
|
||||
no_session_cookie_treshold = Size(L"no_session_cookie_treshold", 128);
|
||||
no_session_cookie_ban_mode = Int(L"no_session_cookie_ban_mode", 0);
|
||||
|
||||
compression = Bool(L"compression", true);
|
||||
compression_page_min_size = Size(L"compression_page_min_size", 512);
|
||||
compression_encoding = Int(L"compression_encoding", 20);
|
||||
|
||||
html_filter = Bool(L"html_filter", true);
|
||||
html_filter_trim_white = Bool(L"html_filter_trim_white", true);
|
||||
html_filter_break_word = Int(L"html_filter_break_word", 60);
|
||||
html_filter_wrap_line = Int(L"html_filter_wrap_line", 110);
|
||||
html_filter_tabs = Size(L"html_filter_tabs", 2);
|
||||
html_filter_orphans = Bool(L"html_filter_orphans", true);
|
||||
html_filter_orphans_mode_str = Text(L"html_filter_orphans_mode_str", L"nbsp");
|
||||
html_filter_nofilter_tag = Text(L"html_filter_nofilter_tag", L"nofilter");
|
||||
|
||||
locale_dir = Text(L"locale_dir");
|
||||
locale_dir_default = Text(L"locale_dir_default");
|
||||
locale_max_id = Size(L"locale_max_id", 100);
|
||||
locale_default_id = Size(L"locale_default_id", 0);
|
||||
ListText(L"locale_files", locale_files);
|
||||
|
||||
title_separator = Text(L"title_separator", L" / ");
|
||||
|
||||
http_header_send_file = Text(L"http_header_send_file", L"X-LIGHTTPD-send-file");
|
||||
|
||||
editors_html_safe_mode = Bool(L"editors_html_safe_mode", true);
|
||||
editors_html_safe_mode_skip_root = Bool(L"editors_html_safe_mode_skip_root", true);
|
||||
|
||||
plugins_dir = Text(L"plugins_dir", L"/usr/local/winix/plugins");
|
||||
NoLastSlash(plugins_dir);
|
||||
ListText(L"plugins", plugin_file);
|
||||
|
||||
symlinks_follow_max = Size(L"symlinks_follow_max", 20);
|
||||
|
||||
ticket_form_prefix = Text(L"ticket_form_prefix", L"ticketparam");
|
||||
|
||||
pass_min_size = Size(L"pass_min_size", 5);
|
||||
pass_type = Int(L"pass_type", 12);
|
||||
pass_hash_use_salt = Bool(L"pass_hash_use_salt", false);
|
||||
pass_hash_salt = Text(L"pass_hash_salt");
|
||||
pass_use_rsa = Bool(L"pass_use_rsa", false);
|
||||
pass_rsa_private_key = Text(L"pass_rsa_private_key");
|
||||
|
||||
opensll_path = Text(L"opensll_path", L"/usr/bin/openssl");
|
||||
|
||||
pattern_cacher_when_delete = Size(L"pattern_cacher_when_delete", 130);
|
||||
pattern_cacher_how_many_delete = Size(L"pattern_cacher_how_many_delete", 30);
|
||||
|
||||
content_type_header = Int(L"content_type_header", 0);
|
||||
umask = Int(L"umask", 0222);
|
||||
|
||||
ezc_max_elements = Size(L"ezc_max_elements", 50000);
|
||||
ezc_max_loop_elements = Size(L"ezc_max_loop_elements", 5000);
|
||||
ezc_out_streams_size = Size(L"ezc_out_streams_size", 128);
|
||||
ezc_error_prefix = Text(L"ezc_error_prefix", L"<!-- ");
|
||||
ezc_error_postfix = Text(L"ezc_error_postfix", L" -->");
|
||||
|
||||
account_need_email_verification = Bool(L"account_need_email_verification", true);
|
||||
reset_password_code_expiration_time = Long(L"reset_password_code_expiration_time", 86400);
|
||||
|
||||
time_zone_default_id = Size(L"time_zone_default_id", 34);
|
||||
time_zone_max_id = Size(L"time_zone_max_id", 130);
|
||||
|
||||
etc_dir = Text(L"etc_dir", L"");
|
||||
time_zones_file = Text(L"time_zones_file", L"time_zones.conf");
|
||||
|
||||
use_ban_list = Bool(L"use_ban_list", true);
|
||||
ban_list_soft_max_size = Size(L"ban_list_soft_max_size", 50000);
|
||||
ban_list_max_size = Size(L"ban_list_max_size", 51000);
|
||||
ban_level_1_delay = Size(L"ban_level_1_delay", 1800);
|
||||
ban_level_2_delay = Size(L"ban_level_2_delay", 86400);
|
||||
ban_level_3_delay = Size(L"ban_level_3_delay", 604800);
|
||||
|
||||
incorrect_login_min_time_between_get_post = Size(L"incorrect_login_min_time_between_get_post", 2);
|
||||
incorrect_login_captcha_treshold = Size(L"incorrect_login_captcha_treshold", 3);
|
||||
incorrect_login_cannot_login_mode = Int(L"incorrect_login_cannot_login_mode", 0);
|
||||
incorrect_login_cannot_login_treshold = Size(L"incorrect_login_cannot_login_treshold", 20);
|
||||
incorrect_login_cannot_login_delay = Size(L"incorrect_login_cannot_login_delay", 1800);
|
||||
|
||||
pid_file = Text(L"pid_file", L"");
|
||||
allow_ezc_out_in_executable_items = Bool(L"allow_ezc_out_in_executable_items", false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Config::SetAdditionalVariables()
|
||||
{
|
||||
if( html_filter_orphans_mode_str == L"160" )
|
||||
html_filter_orphans_mode = HTMLFilter::orphan_160space;
|
||||
else
|
||||
html_filter_orphans_mode = HTMLFilter::orphan_nbsp;
|
||||
|
||||
for(size_t i=0 ; i<static_dirs.size() ; ++i)
|
||||
NoLastSlash(static_dirs[i]);
|
||||
|
||||
CheckPasswd();
|
||||
|
||||
if( content_type_header < 0 || content_type_header > 2 )
|
||||
content_type_header = 0;
|
||||
|
||||
if( locale_default_id > locale_max_id )
|
||||
locale_default_id = locale_max_id;
|
||||
|
||||
if( locale_files.empty() )
|
||||
locale_files.push_back(L"en");
|
||||
|
||||
if( !upload_group.empty() )
|
||||
upload_group_int = GetGroupId(upload_group);
|
||||
else
|
||||
upload_group_int = -1;
|
||||
|
||||
if( session_cookie_encode && session_keys_file.empty() )
|
||||
session_cookie_encode = false;
|
||||
|
||||
if( session_index_time_increment < 0 )
|
||||
session_index_time_increment = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Config::CheckPasswd()
|
||||
{
|
||||
switch(pass_type)
|
||||
{
|
||||
case WINIX_CRYPT_HASH_NONE:
|
||||
case WINIX_CRYPT_HASH_MD4:
|
||||
case WINIX_CRYPT_HASH_MD5:
|
||||
case WINIX_CRYPT_HASH_SHA1:
|
||||
case WINIX_CRYPT_HASH_SHA224:
|
||||
case WINIX_CRYPT_HASH_SHA256:
|
||||
case WINIX_CRYPT_HASH_SHA384:
|
||||
case WINIX_CRYPT_HASH_SHA512:
|
||||
break;
|
||||
|
||||
default:
|
||||
pass_type = WINIX_CRYPT_HASH_SHA256;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
std::wstring & Config::Text(const wchar_t * name)
|
||||
{
|
||||
return space.Text(name);
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Config::Text(const wchar_t * name, const wchar_t * def)
|
||||
{
|
||||
return space.Text(name, def);
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Config::Text(const std::wstring & name, const wchar_t * def)
|
||||
{
|
||||
return space.Text(name, def);
|
||||
}
|
||||
|
||||
|
||||
int Config::Int(const wchar_t * name)
|
||||
{
|
||||
return space.Int(name);
|
||||
}
|
||||
|
||||
|
||||
int Config::Int(const wchar_t * name, int def)
|
||||
{
|
||||
return space.Int(name, def);
|
||||
}
|
||||
|
||||
|
||||
int Config::Int(const std::wstring & name, int def)
|
||||
{
|
||||
return space.Int(name, def);
|
||||
}
|
||||
|
||||
|
||||
long Config::Long(const wchar_t * name)
|
||||
{
|
||||
return space.Long(name);
|
||||
}
|
||||
|
||||
long Config::Long(const wchar_t * name, long def)
|
||||
{
|
||||
return space.Long(name, def);
|
||||
}
|
||||
|
||||
long Config::Long(const std::wstring & name, long def)
|
||||
{
|
||||
return space.Long(name, def);
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t Config::Size(const wchar_t * name)
|
||||
{
|
||||
return space.Size(name);
|
||||
}
|
||||
|
||||
|
||||
size_t Config::Size(const wchar_t * name, size_t def)
|
||||
{
|
||||
return space.Size(name, def);
|
||||
}
|
||||
|
||||
|
||||
size_t Config::Size(const std::wstring & name, size_t def)
|
||||
{
|
||||
return space.Size(name, def);
|
||||
}
|
||||
|
||||
|
||||
bool Config::Bool(const wchar_t * name)
|
||||
{
|
||||
return space.Bool(name);
|
||||
}
|
||||
|
||||
|
||||
bool Config::Bool(const wchar_t * name, bool def)
|
||||
{
|
||||
return space.Bool(name, def);
|
||||
}
|
||||
|
||||
|
||||
bool Config::Bool(const std::wstring & name, bool def)
|
||||
{
|
||||
return space.Bool(name, def);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Config::ListText(const wchar_t * name, std::vector<std::wstring> & list)
|
||||
{
|
||||
return space.ListText(name, list);
|
||||
}
|
||||
|
||||
|
||||
bool Config::ListText(const std::wstring & name, std::vector<std::wstring> & list)
|
||||
{
|
||||
return space.ListText(name, list);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Config::HasValue(const wchar_t * name, const wchar_t * value)
|
||||
{
|
||||
return space.HasValue(name, value);
|
||||
}
|
||||
|
||||
bool Config::HasValue(const wchar_t * name, const std::wstring & value)
|
||||
{
|
||||
return space.HasValue(name, value);
|
||||
}
|
||||
|
||||
bool Config::HasValue(const std::wstring & name, const wchar_t * value)
|
||||
{
|
||||
return space.HasValue(name, value);
|
||||
}
|
||||
|
||||
bool Config::HasValue(const std::wstring & name, const std::wstring & value)
|
||||
{
|
||||
return space.HasValue(name, value);
|
||||
}
|
||||
|
||||
|
||||
void Config::Print(std::wostream & out)
|
||||
{
|
||||
space.Serialize(out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
|
||||
793
winixd/core/config.h
Normal file
793
winixd/core/config.h
Normal file
@@ -0,0 +1,793 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2015, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_config
|
||||
#define headerfile_winix_core_config
|
||||
|
||||
#include <string>
|
||||
#include "space/spaceparser.h"
|
||||
#include "htmlfilter.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Config
|
||||
{
|
||||
public:
|
||||
|
||||
// name of the config file
|
||||
// this is the parameter passed to winix programm
|
||||
std::wstring config_file;
|
||||
|
||||
// server mode
|
||||
// you can assign any string to it such as "production" "dev"
|
||||
// this value is not used by winix itself
|
||||
// you can refer to it from [server_mode] and [server_mode_is] ezc functions
|
||||
std::wstring server_mode;
|
||||
|
||||
|
||||
// start as a demon (in the background)
|
||||
// default: true
|
||||
bool demonize;
|
||||
|
||||
// system user's name to whom winix should drop privileges
|
||||
// used only if winix is started as the root
|
||||
std::wstring user;
|
||||
|
||||
// system group's name to which drop privileges
|
||||
// used only if winix is started as the root
|
||||
std::wstring group;
|
||||
|
||||
// setting additional effective groups from /etc/group
|
||||
// by using initgroups()
|
||||
// used only if winix is started as the root
|
||||
// default: true
|
||||
bool additional_groups;
|
||||
|
||||
// log file name, log file name for notifications (sending emails, etc)
|
||||
std::wstring log_file, log_notify_file;
|
||||
|
||||
// the log level (how much info should be inserted to logs)
|
||||
// 1 - minimum
|
||||
// 2 - (default)
|
||||
// 3 - maximum - all logs
|
||||
int log_level;
|
||||
|
||||
// logging to stdout too
|
||||
// this option is valid only if 'demonize' option is set to 'false'
|
||||
// default: false
|
||||
bool log_stdout;
|
||||
|
||||
// how many requests should be saved in the same time
|
||||
// if you have a very busy server you can incrase this value
|
||||
// default: 1
|
||||
int log_request;
|
||||
|
||||
// whether to save each line of the config (used for debugging purposes)
|
||||
// default: false
|
||||
bool log_save_each_line;
|
||||
|
||||
// time zone identifier used in log messages
|
||||
// this affects only the first line of logs (where there is IP address, request method etc)
|
||||
// see time_zone_default_id below for more info
|
||||
// default: 34 (Coordinated Universal Time UTC+00:00)
|
||||
size_t log_time_zone_id;
|
||||
|
||||
// put to log what server is sending back to the client (html/json etc)
|
||||
// default: false
|
||||
bool log_server_answer;
|
||||
|
||||
// logging db queries
|
||||
// warning: use it only on a developer's server as it logs the hashes of passwords too
|
||||
// default: false
|
||||
bool log_db_query;
|
||||
|
||||
// logging when a plugin function is called
|
||||
// default: false
|
||||
bool log_plugin_call;
|
||||
|
||||
// how many characters in values should be logged from POST parameters
|
||||
// default: 80
|
||||
// set to 0 to turn it off
|
||||
size_t log_post_value_size;
|
||||
|
||||
// request delimiter in the log file, default "---------"
|
||||
std::wstring log_delimiter;
|
||||
|
||||
// log environment variables (fastcgi environment)
|
||||
bool log_env_variables;
|
||||
|
||||
// log headers (+cookies) which are returned to the client
|
||||
// this is what winix has generated -- the web server can change or add other headers
|
||||
// default: false
|
||||
bool log_http_answer_headers;
|
||||
|
||||
// fast cgi: socket (unix domain)
|
||||
std::wstring fcgi_socket;
|
||||
|
||||
// fast cgi: socket permissions
|
||||
// chmod and chown of the socket are set before winix drops privileges
|
||||
int fcgi_socket_chmod;
|
||||
|
||||
// fast cgi: owner of the socket
|
||||
// chmod and chown of the socket are set before winix drops privileges
|
||||
std::wstring fcgi_socket_user;
|
||||
|
||||
// fast cgi: group of the socket
|
||||
// chmod and chown of the socket are set before winix drops privileges
|
||||
std::wstring fcgi_socket_group;
|
||||
|
||||
// fcgi_socket_listen is the listen queue depth used in the listen() call
|
||||
// when creating a FastCGI socket for the web server
|
||||
// default: 100
|
||||
int fcgi_socket_listen;
|
||||
|
||||
std::wstring templates_dir;
|
||||
std::wstring templates_dir_default; // html templates from winix
|
||||
|
||||
std::wstring txt_templates_dir;
|
||||
std::wstring txt_templates_dir_default; // txt (notifications) templates from winix
|
||||
|
||||
// prefix and postfix for functions templates
|
||||
// default:
|
||||
// prefix: "fun_"
|
||||
// postfix: ".html"
|
||||
std::wstring templates_fun_prefix;
|
||||
std::wstring templates_fun_postfix;
|
||||
|
||||
// main html template
|
||||
// default: index.html
|
||||
std::wstring templates_index;
|
||||
|
||||
// if true then only root can use 'template' winix function
|
||||
// default: false
|
||||
bool template_only_root_use_template_fun;
|
||||
|
||||
// the database name, user name and a password for the PostgreSQL database
|
||||
std::wstring db_database;
|
||||
std::wstring db_user;
|
||||
std::wstring db_pass;
|
||||
|
||||
// the name of the cookie which has the session identifier
|
||||
std::wstring http_session_id_name;
|
||||
|
||||
// string used in a place where is a user (or group) selected
|
||||
// !! IMPROVE ME should be moved to locales
|
||||
std::wstring priv_no_user;
|
||||
std::wstring priv_no_group;
|
||||
|
||||
// time in seconds when a user will be automatically logged out (iddle time)
|
||||
// default: 10800 = 3 hours
|
||||
int session_max_idle;
|
||||
|
||||
// time in seconds when a user will be automatically logged out
|
||||
// when he has selected the 'remember me' option when logging in
|
||||
// this time is usually greater than session_max_idle
|
||||
// default: 16070400 = 3 months
|
||||
int session_remember_max_idle;
|
||||
|
||||
// a file to which winix stores sessions info
|
||||
// it is used when winix starts (boots) and quits
|
||||
std::wstring session_file;
|
||||
|
||||
// how many sessions can be (zero turn off this checking)
|
||||
// default: 1000000 (one milion)
|
||||
size_t session_max;
|
||||
|
||||
// whether or not we should encode the session cookie
|
||||
// (we have a special algorithm)
|
||||
// default: false
|
||||
bool session_cookie_encode;
|
||||
|
||||
// if session_cookie_encode is true then you should provide
|
||||
// a file where AES keys will be stored
|
||||
std::wstring session_keys_file;
|
||||
|
||||
// each session has an index -- an unsigned int value
|
||||
// this value is sent in the cookie string (is encoded)
|
||||
// and is incremented when session_index_time_increment time is passed since the last incrementing
|
||||
// if a client sent the cookie back the difference between
|
||||
// current index and the index in the cookie should be less than or equal to session_allow_index_difference
|
||||
// default: 8
|
||||
size_t session_allow_index_difference;
|
||||
|
||||
// the time which should pass after the session index is incremented
|
||||
// default: 30
|
||||
// (session_allow_index_difference + 1) * session_index_time_increment should be less than a time
|
||||
// load of a page and all elements on it such as images (of course it depends on client's download too)
|
||||
time_t session_index_time_increment;
|
||||
|
||||
// time in seconds after a new AES key pair should be generated
|
||||
// we have 256 pairs of keys so this time multiplied by 256 should not be less than
|
||||
// the max time of a session (session_remember_max_idle),
|
||||
// by default: 256 * 2 days = 512 days = 1.4 year > 3 months (session_remember_max_idle)
|
||||
// default: 172800 = 2 days (max: 2678400 = 1 month, min: 10)
|
||||
size_t session_key_renew_time;
|
||||
|
||||
// after how many broken encoded cookie we should ban the current IP
|
||||
// default: 2 (value in the range <0 - 65535>)
|
||||
size_t broken_encoded_cookie_treshold;
|
||||
|
||||
// after how many incorrect session identifiers (or session indices) we should ban the current IP
|
||||
// do not set this value too low, as people connecting from the same IP address (from behind a NAT)
|
||||
// would be banned if they have an old session cookie remembered in the browser
|
||||
// default: 128 (value in the range <0 - 65535>)
|
||||
size_t session_hijacking_treshold;
|
||||
|
||||
// after how many times a client will be banned if it did not send a session cookie
|
||||
// this can be a bot such as a Google Bot or just people connecting from a NAT and all have the same IP
|
||||
// default: 128 (value in the range <0 - 65535>)
|
||||
size_t no_session_cookie_treshold;
|
||||
|
||||
// the way we behave when no_session_cookie_treshold limit is exceeded
|
||||
// 0 - if a client doesn't send a session cookie again then use a temporary session
|
||||
// (other sessions from this IP address are not affected)
|
||||
// 1 - add this IP address to ban list and create a temporary session
|
||||
// (this will block other sessions from this IP address too)
|
||||
// default: 0
|
||||
int no_session_cookie_ban_mode;
|
||||
|
||||
// allow the winix output to be compressed
|
||||
// default: true
|
||||
bool compression;
|
||||
|
||||
// compress only if the output is greater or equal to this value
|
||||
// default: 512 bytes
|
||||
size_t compression_page_min_size;
|
||||
|
||||
// 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
|
||||
int compression_encoding;
|
||||
|
||||
// plugins directory
|
||||
// default: /usr/local/winix/plugins
|
||||
std::wstring plugins_dir;
|
||||
|
||||
// plugins
|
||||
// you can provide either a relative path (plugins_dir will be used)
|
||||
// or a full path to a plugin
|
||||
std::vector<std::wstring> plugin_file;
|
||||
|
||||
// should the html code be cleaned by the html filter
|
||||
bool html_filter;
|
||||
|
||||
// should white characters be trimmed
|
||||
bool html_filter_trim_white;
|
||||
|
||||
// when long words should be broken (a space will be inserted)
|
||||
// default: after 60 non white characters there will be put a space
|
||||
// set zero to turn off
|
||||
size_t html_filter_break_word;
|
||||
|
||||
// when long lines should be broken (a new line character will be inserted)
|
||||
// default: 110
|
||||
// set zero to turn off
|
||||
size_t html_filter_wrap_line;
|
||||
|
||||
// how many spaces will be put at one tree level
|
||||
// default: 2
|
||||
size_t html_filter_tabs;
|
||||
|
||||
// use checking for 'orphans' for a specicic language
|
||||
// default: true
|
||||
bool html_filter_orphans;
|
||||
|
||||
// orphans mode
|
||||
// either: "nbsp" or "160"
|
||||
// default: "nbsp"
|
||||
std::wstring html_filter_orphans_mode_str;
|
||||
HTMLFilter::OrphanMode html_filter_orphans_mode;
|
||||
|
||||
// the html nofilter tag
|
||||
// content betweeng these tags (opening and closing) will not be filtered
|
||||
// and this tag will not be included in the html output
|
||||
// default: nofilter
|
||||
std::wstring html_filter_nofilter_tag;
|
||||
|
||||
// the url of a new empty item (if there is not the subject too)
|
||||
// !! IMPROVE ME should be moved to locale
|
||||
std::wstring item_url_empty;
|
||||
|
||||
// maximum length of a file send by post multipart form
|
||||
// default: 8388608 - 8MB
|
||||
// 0 - not used
|
||||
size_t post_file_max;
|
||||
|
||||
// directory for static files
|
||||
std::wstring upload_dir;
|
||||
|
||||
// additional static directories *not* managed by winix
|
||||
// you can refer to this directories by using 'static' mount point
|
||||
// e.g.
|
||||
// let we say in the config you have:
|
||||
// static_dirs = ("/home/a", "/home/b", "/home/c")
|
||||
// and in /etc/fstab (in winix) you have such a line:
|
||||
// "static /my/dir simplefs static(2)"
|
||||
// above line in /etc/fstab means that when a user enters http://domain.com/my/dir/file.txt
|
||||
// then "file.txt" will be taken from "/home/c/file.txt"
|
||||
// this is useful for some javascript files which are protected from running from other domains
|
||||
std::vector<std::wstring> static_dirs;
|
||||
|
||||
// sometimes you can have trouble when you have set an incorrect static path in /etc/fstab
|
||||
// in such a case set dont_use_static_dirs to true
|
||||
// and winix will behave as there are not any static directories
|
||||
// so you can correct your mistake and then set this value to false
|
||||
bool dont_use_static_dirs;
|
||||
|
||||
// static common directory
|
||||
// this is a place where there are some common javascripts, images, css files
|
||||
// winix is allowed to read some files from there
|
||||
// for example ckeditor config, vim config and so on
|
||||
// winix read it as a ezc template and put it into database
|
||||
// default: empty
|
||||
std::wstring common_dir;
|
||||
|
||||
// system group's name for new uploaded files (created directories in the file system)
|
||||
// it can be empty (it is not used then)
|
||||
std::wstring upload_group;
|
||||
|
||||
// this value will be set based on upload_group
|
||||
// will be -1 if upload_group is empty or if it is invalid
|
||||
int upload_group_int;
|
||||
|
||||
// chmod of newly created directories (under upload_dir)
|
||||
// default: 0750
|
||||
int upload_dirs_chmod;
|
||||
|
||||
// chmod of newly created files (under upload_dir)
|
||||
// default: 0640
|
||||
int upload_files_chmod;
|
||||
|
||||
// create a thumbnail from an image
|
||||
// default: true
|
||||
bool create_thumb;
|
||||
|
||||
// the mode of creating a thumbnail
|
||||
// width means thumb_cx, height means thumb_cy
|
||||
// 1: Width given, height automagically selected to preserve aspect ratio.
|
||||
// 2: Height given, width automagically selected to preserve aspect ratio.
|
||||
// 3: Maximum values of height and width given, aspect ratio preserved.
|
||||
// 4: Minimum values of width and height given, aspect ratio preserved.
|
||||
// 5: Width and height emphatically given, original aspect ratio ignored.
|
||||
// 6: Change as per widthxheight (3) but only if an image dimension exceeds a specified dimension.
|
||||
// 7: Change dimensions only if both image dimensions are less than specified dimensions.
|
||||
// default: 2
|
||||
int thumb_mode;
|
||||
|
||||
// width of thumbnails
|
||||
// default: 150
|
||||
size_t thumb_cx;
|
||||
|
||||
// height of thumbnails
|
||||
// default: 150
|
||||
size_t thumb_cy;
|
||||
|
||||
// quality of thumbnails
|
||||
// from 0 (the worst) to 100 (the best)
|
||||
// more info: http://www.imagemagick.org/script/command-line-options.php?ImageMagick=p4jtel7557hovd34ui3tgb54h6#quality
|
||||
// default: 92
|
||||
int thumb_quality;
|
||||
|
||||
// resizing images
|
||||
// this not affects thumbnails
|
||||
// default: true
|
||||
bool image_resize;
|
||||
|
||||
// the mode of resizing an image
|
||||
// the same as 'thumb_mode' above
|
||||
// default: 6
|
||||
int image_mode;
|
||||
|
||||
// width of images
|
||||
// default: 1000
|
||||
size_t image_cx;
|
||||
|
||||
// height of images
|
||||
// default: 800
|
||||
size_t image_cy;
|
||||
|
||||
// quality of an image (the same as in thumbnails)
|
||||
// from 0 (the worst) to 100 (the best)
|
||||
// default: 92
|
||||
int image_quality;
|
||||
|
||||
// the convert program (ImageMagic) (for images and thumbnails)
|
||||
// default: /usr/local/bin/convert
|
||||
std::wstring convert_cmd;
|
||||
|
||||
// directory with locale files
|
||||
std::wstring locale_dir;
|
||||
|
||||
// directory with default locale files (those from winix)
|
||||
std::wstring locale_dir_default;
|
||||
|
||||
// locale files (e.g. "en", "pl")
|
||||
// default: only one item: en
|
||||
std::vector<std::wstring> locale_files;
|
||||
|
||||
// a maximum value of a locale identifier
|
||||
// default: 100 (maximum: 1000)
|
||||
// each locale files should have its own identifier (in "winix_locale_id" field)
|
||||
// from zero to this value
|
||||
size_t locale_max_id;
|
||||
|
||||
// locale for not logged users
|
||||
// or for newly created accounts
|
||||
// default: 0
|
||||
size_t locale_default_id;
|
||||
|
||||
// url protocol
|
||||
// default: http://
|
||||
std::wstring url_proto;
|
||||
|
||||
// url protocol when using SSL
|
||||
// default: https://
|
||||
std::wstring url_ssl_proto;
|
||||
|
||||
// enables SSL
|
||||
// it means this site should be accessed through SSL encrypted connection
|
||||
// default: false
|
||||
bool use_ssl;
|
||||
|
||||
// enables SSL for static content
|
||||
// used mainly in templates, look at doc_base_url_static ezc function
|
||||
// default: false
|
||||
bool use_ssl_static;
|
||||
|
||||
// enables SSL for common content
|
||||
// used mainly in templates, look at doc_base_url_common ezc function
|
||||
// default: false
|
||||
bool use_ssl_common;
|
||||
|
||||
// if SSL is enabled then if this is true the SSL will be used
|
||||
// only for logged users
|
||||
// default: true
|
||||
bool use_ssl_only_for_logged_users;
|
||||
|
||||
// if current connection is without SSL and should be made through SSL
|
||||
// or if is via SSL and should be done in plain text
|
||||
// then we make a redirect
|
||||
// default: 303
|
||||
int use_ssl_redirect_code;
|
||||
|
||||
// when the HOST_HTTP environment variable is not equal to 'base_url'
|
||||
// (the part 'http://' and the last slash is removed)
|
||||
// the server will redirect into base_url + 'REQUEST_URI'
|
||||
// it's useful when you want to redirect from 'mydomain.tld' into 'www.mydomain.tld' etc.
|
||||
// set this option to false if you have multiple subdomains
|
||||
// default: false
|
||||
bool base_url_redirect;
|
||||
|
||||
// the HTTP code used during the base redirect
|
||||
// default: 301
|
||||
int base_url_redirect_code;
|
||||
|
||||
// the main address of the site (e.g. www.someserver.com)
|
||||
// (without http:// prefix)
|
||||
std::wstring base_url;
|
||||
|
||||
// static content not authorized by winix
|
||||
// (e.g. static.someserver.com)
|
||||
std::wstring base_url_static;
|
||||
|
||||
// additional static server for common content (not authorized)
|
||||
// (e.g. common.someserver.com)
|
||||
std::wstring base_url_common;
|
||||
|
||||
// separator used in <title> html tag
|
||||
std::wstring title_separator;
|
||||
|
||||
// http header recognized by www server as a file to send back
|
||||
// default: X-LIGHTTPD-send-file
|
||||
std::wstring http_header_send_file;
|
||||
|
||||
// in editors (emacs, ckeditor,...) the html will be filtered and unsafe tags
|
||||
// will be dropped (script, frame, etc.)
|
||||
// default: true;
|
||||
bool editors_html_safe_mode;
|
||||
|
||||
// don't filter the html code for root
|
||||
// default: true
|
||||
// (if true the html code for root is not filtered)
|
||||
bool editors_html_safe_mode_skip_root;
|
||||
|
||||
// how many maximum symlinks can be followed
|
||||
// (symlinks on directories as well)
|
||||
// default: 20
|
||||
size_t symlinks_follow_max;
|
||||
|
||||
// the prefix of a name of html form controls used in the ticket plugin
|
||||
// default: ticketparam
|
||||
std::wstring ticket_form_prefix;
|
||||
|
||||
// the minimal size of a user's password
|
||||
// default: 5
|
||||
size_t pass_min_size;
|
||||
|
||||
// how passwords should be stored
|
||||
// 0 - plain text
|
||||
// 1 - md4 hash
|
||||
// 2 - md5 hash
|
||||
// 10 - sha1 hash
|
||||
// 11 - sha224 hash
|
||||
// 12 - sha256 hash (default)
|
||||
// 13 - sha384 hash
|
||||
// 14 - sha512 hash
|
||||
int pass_type;
|
||||
|
||||
// whether the passwords' hashes should be salted or not
|
||||
// this affects newly created accounts
|
||||
// default: false
|
||||
bool pass_hash_use_salt;
|
||||
|
||||
// salt to a password's hash
|
||||
// default empty
|
||||
// !! once you set this salt don't change it any more (people wouldn't be allowed to login)
|
||||
std::wstring pass_hash_salt;
|
||||
|
||||
// whether the passwords' hashes should be encrypted
|
||||
// this affects newly created accounts
|
||||
// default: false
|
||||
bool pass_use_rsa;
|
||||
|
||||
// path to a RSA private key
|
||||
// this is actually private + public key in one file
|
||||
// generated by "openssl genrsa"
|
||||
// default empty which means encryption will not be used
|
||||
// !! once you set these keys don't change it any more (people wouldn't be allowed to login)
|
||||
std::wstring pass_rsa_private_key;
|
||||
|
||||
// path to 'openssl'
|
||||
// default: /usr/bin/openssl
|
||||
std::wstring opensll_path;
|
||||
|
||||
// setting when we should delete patterns (EZC patterns)
|
||||
// we are deleting when we have more (or equal) patterns than 'when_delete'
|
||||
// and then we are deleting 'how_many_del' patterns
|
||||
// those patterns comes from items (pages) with executable bit set
|
||||
size_t pattern_cacher_when_delete;
|
||||
size_t pattern_cacher_how_many_delete;
|
||||
|
||||
// header "Content-Type" send to the client
|
||||
// 0 - text/html - for HTML
|
||||
// 1 - application/xhtml+xml - for XHTML 1.0
|
||||
// 2 - application/xml - for XHTML 1.0 or for XHTML 1.1
|
||||
// default: 0
|
||||
// if utf8 is true then "; charset=UTF-8" will also be appended
|
||||
int content_type_header;
|
||||
|
||||
// global umask
|
||||
// it is used when an user doesn't have your own umask or for guests (not logged users)
|
||||
// default: 0222
|
||||
int umask;
|
||||
|
||||
// maximum number for elements through the whole template
|
||||
// default: 50000
|
||||
size_t ezc_max_elements;
|
||||
|
||||
// maximum number of each [for] loop
|
||||
// default: 5000 (from ezc generator)
|
||||
size_t ezc_max_loop_elements;
|
||||
|
||||
// how many output streams do we have in Request class
|
||||
// those streams used in ajax responses
|
||||
// you can use no more [ezc out] statements than this limit
|
||||
// default: 128
|
||||
size_t ezc_out_streams_size;
|
||||
|
||||
// prefix and postfix used when there is an error in Ezc patterns
|
||||
// default:
|
||||
// prefix: "<!-- "
|
||||
// postfix: " -->"
|
||||
std::wstring ezc_error_prefix;
|
||||
std::wstring ezc_error_postfix;
|
||||
|
||||
// when true then when a user want to create a new account
|
||||
// he has to provide his email and a message will be sent back to him
|
||||
// with a link to activate the account
|
||||
// default: true
|
||||
bool account_need_email_verification;
|
||||
|
||||
// when a user forgot his password we are able to send an email to him
|
||||
// with a link to the page where there is a html form for setting a new password
|
||||
// this option tells how long (in seconds) the link is valid
|
||||
// default: 86400 (24 hours)
|
||||
long reset_password_code_expiration_time;
|
||||
|
||||
// time zone identifier for not logged users
|
||||
// or for newly created accounts
|
||||
// those identifiers you can see in etc/time_zones.conf file
|
||||
// or by using timezone winix function with 'a' parameter (timezone/a) (!!IMPROVE ME NOT IMPLEMENTED YET)
|
||||
// default: 34 (Coordinated Universal Time UTC+00:00)
|
||||
size_t time_zone_default_id;
|
||||
|
||||
// a maximum value of a time zone identifier
|
||||
// time zones with an id greater than this will be skipped
|
||||
// default: 130 (maximum: 1000)
|
||||
size_t time_zone_max_id;
|
||||
|
||||
// a directory in which there are some config files
|
||||
// used mainly when winix starts
|
||||
// default: empty (means not for using)
|
||||
std::wstring etc_dir;
|
||||
|
||||
// a file in etc_dir with time zones info
|
||||
// default: time_zones.conf
|
||||
// this is a Space structure with all time zones
|
||||
// with following format:
|
||||
// "tz_-12:00" ( # the name of a space is also a key to the locale files
|
||||
// tz_offset_str = "-12:00" # a string representing the offset from UTC in a format: [+|-]HH:MM
|
||||
// tz_has_dst = "false" # whether the time zone has daylight saving time (bool)
|
||||
// tz_dst_offset = "" # offset of the daylight saving time
|
||||
// tz_dst_start = "" # when the daylight saving time starts, format: MM:DD HH:MM:SS
|
||||
// tz_dst_end = "") # when the daylight saving time ends, format: MM:DD HH:MM:SS
|
||||
// each time zone is in a seperate space
|
||||
std::wstring time_zones_file;
|
||||
|
||||
// turn on the IP ban mechanizm
|
||||
// we have got three levels of bans (level 1, level 2, and the highest level 3)
|
||||
// default: true
|
||||
bool use_ban_list;
|
||||
|
||||
// the so called 'soft' max size
|
||||
// read below description for explanation
|
||||
// this is introduced to avoid deleting only one record from the ban list
|
||||
// default: 50000
|
||||
size_t ban_list_soft_max_size;
|
||||
|
||||
// this is the 'hard' max size of an IP's ban list
|
||||
// if there are more records than this value
|
||||
// then some of them will be removed (until the size will be ban_list_soft_max_size equal)
|
||||
// this value should be a little larger from ban_list_soft_max_size
|
||||
// default: 51000
|
||||
size_t ban_list_max_size;
|
||||
|
||||
// delay in seconds of the first level ban
|
||||
// default: 1800 (30 minutes)
|
||||
// it means withing the next 30 minutes you see only 'you are banned...' message on your webbrowser
|
||||
size_t ban_level_1_delay;
|
||||
|
||||
// delay in seconds of the second level ban
|
||||
// default: 86400 (24 hours)
|
||||
size_t ban_level_2_delay;
|
||||
|
||||
// delay in seconds of the third level ban
|
||||
// default: 604800 (7 days)
|
||||
size_t ban_level_3_delay;
|
||||
|
||||
// the minimum time in seconds which has to pass between the first GET request
|
||||
// (showing your the login form) and the second POST request (which sends the
|
||||
// login and password to the server)
|
||||
// if the time is shorter then the login attempt is treated as incorrect
|
||||
// (the same as if you provide incorrect user/password)
|
||||
// default: 2
|
||||
size_t incorrect_login_min_time_between_get_post;
|
||||
|
||||
// how many incorrect logins there must have been passed to display a captcha
|
||||
// next to the login form
|
||||
// default: 3 (value in the range <0 - 65535>)
|
||||
size_t incorrect_login_captcha_treshold;
|
||||
|
||||
// the way how we prevent to login if there are too many incorrect login attempts
|
||||
// 0 - 'block logging' - do not show the login form in 'login' winix function
|
||||
// (instead a warning message will be printed)
|
||||
// 1 - add to ban list (warning: people from this IP will not be able to see your site and do anything)
|
||||
// default: 0
|
||||
int incorrect_login_cannot_login_mode;
|
||||
|
||||
// after how many incorrect login attempts we do the incorrect_login_cannot_login_mode action
|
||||
// default: 20 (value in the range <0 - 65535>)
|
||||
size_t incorrect_login_cannot_login_treshold;
|
||||
|
||||
// used when incorrect_login_cannot_login_mode is zero
|
||||
// it is the time which should be passed to allow logging
|
||||
// default: 1800 (30 minutes)
|
||||
// if incorrect_login_cannot_login_mode is one then ban_level_X_delay times
|
||||
// will be taken accordingly
|
||||
size_t incorrect_login_cannot_login_delay;
|
||||
|
||||
// pid file (a full path to a pid file)
|
||||
// default: empty which means there is not a pid file used
|
||||
// pid file is saved after winix has dropped privileges
|
||||
std::wstring pid_file;
|
||||
|
||||
// allow to use [ezc out] statement in executable items (used in 'run' winix function)
|
||||
// default false
|
||||
// warning: if you enable this options a user can override a different output stream when using ajax
|
||||
bool allow_ezc_out_in_executable_items;
|
||||
|
||||
|
||||
Config();
|
||||
bool ReadConfig(bool errors_to_stdout_, bool stdout_is_closed = true);
|
||||
|
||||
std::wstring & Text(const wchar_t * name);
|
||||
std::wstring & Text(const wchar_t * name, const wchar_t * def);
|
||||
std::wstring & Text(const std::wstring & name, const wchar_t * def);
|
||||
|
||||
int Int(const wchar_t *);
|
||||
int Int(const wchar_t * name, int def);
|
||||
int Int(const std::wstring & name, int def);
|
||||
long Long(const wchar_t *);
|
||||
long Long(const wchar_t * name, long def);
|
||||
long Long(const std::wstring & name, long def);
|
||||
size_t Size(const wchar_t *);
|
||||
size_t Size(const wchar_t * name, size_t def);
|
||||
size_t Size(const std::wstring & name, size_t def);
|
||||
bool Bool(const wchar_t *);
|
||||
bool Bool(const wchar_t * name, bool def);
|
||||
bool Bool(const std::wstring & name, bool def);
|
||||
bool ListText(const wchar_t * name, std::vector<std::wstring> & list);
|
||||
bool ListText(const std::wstring & name, std::vector<std::wstring> & list);
|
||||
|
||||
bool HasValue(const wchar_t * name, const wchar_t * value);
|
||||
bool HasValue(const wchar_t * name, const std::wstring & value);
|
||||
bool HasValue(const std::wstring & name, const wchar_t * value);
|
||||
bool HasValue(const std::wstring & name, const std::wstring & value);
|
||||
|
||||
// for debug
|
||||
void Print(std::wostream & out);
|
||||
|
||||
// raw access to the config
|
||||
PT::Space space;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
PT::SpaceParser parser;
|
||||
bool errors_to_stdout;
|
||||
|
||||
void ShowError();
|
||||
void AssignValues(bool stdout_is_closed);
|
||||
void SetAdditionalVariables();
|
||||
void CheckPasswd();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
121
winixd/core/cookieparser.h
Normal file
121
winixd/core/cookieparser.h
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_cookieparser
|
||||
#define headerfile_winix_core_cookieparser
|
||||
|
||||
#include "httpsimpleparser.h"
|
||||
#include "requesttypes.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class CookieParser : public HttpSimpleParser
|
||||
{
|
||||
|
||||
const wchar_t * cookie_string;
|
||||
CookieTab * cookie_tab;
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
virtual int GetChar()
|
||||
{
|
||||
if( !cookie_string || *cookie_string == 0 )
|
||||
return -1;
|
||||
|
||||
return (int)*(cookie_string++);
|
||||
}
|
||||
|
||||
|
||||
|
||||
virtual void Parameter(std::wstring & name, std::wstring & value)
|
||||
{
|
||||
// Cookie names are case insensitive according to section 3.1 of RFC 2965
|
||||
// (we don't use locale here)
|
||||
ToLower(name);
|
||||
|
||||
std::pair<CookieTab::iterator, bool> res = cookie_tab->insert( std::make_pair(name, value) );
|
||||
log << log2 << "Cookie, name: \"" << name << "\", value: \"" << value << "\"";
|
||||
|
||||
if( res.second == false )
|
||||
{
|
||||
res.first->second = value;
|
||||
log << " (overwritten)";
|
||||
}
|
||||
|
||||
log << logend;
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
CookieParser()
|
||||
{
|
||||
HttpSimpleParser::separator = ';';
|
||||
HttpSimpleParser::value_can_be_quoted = true;
|
||||
HttpSimpleParser::skip_white_chars = true;
|
||||
HttpSimpleParser::recognize_special_chars = false;
|
||||
HttpSimpleParser::getchar_returns_utf8_chars = false;
|
||||
}
|
||||
|
||||
|
||||
// cookie_string can be null
|
||||
void Parse(const wchar_t * cookie_string_, CookieTab & cookie_tab_)
|
||||
{
|
||||
cookie_string = cookie_string_;
|
||||
cookie_tab = &cookie_tab_;
|
||||
|
||||
HttpSimpleParser::Parse();
|
||||
}
|
||||
|
||||
void Parse(const std::wstring & cookie_string_, CookieTab & cookie_tab_)
|
||||
{
|
||||
Parse(cookie_string_.c_str(), cookie_tab_);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
348
winixd/core/crypt.cpp
Normal file
348
winixd/core/crypt.cpp
Normal file
@@ -0,0 +1,348 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cstring>
|
||||
#include "crypt.h"
|
||||
#include "utf8/utf8.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
void Crypt::SetConfig(Config * pconfig)
|
||||
{
|
||||
config = pconfig;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char Crypt::ConvertToHexForm(int val)
|
||||
{
|
||||
if( val < 10 )
|
||||
return val + '0';
|
||||
|
||||
return val - 10 + 'a';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool Crypt::HashBin(int hash, const char * in, size_t inlen, std::string & out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
if( !config )
|
||||
return false;
|
||||
|
||||
run.Clear();
|
||||
PT::WideToUTF8(config->opensll_path, command);
|
||||
run.Cmd(command);
|
||||
run.Par("dgst");
|
||||
run.Par("-binary");
|
||||
|
||||
switch(hash)
|
||||
{
|
||||
case WINIX_CRYPT_HASH_MD4: run.Par("-md4"); break;
|
||||
case WINIX_CRYPT_HASH_MD5: run.Par("-md5"); break;
|
||||
case WINIX_CRYPT_HASH_SHA1: run.Par("-sha1"); break;
|
||||
case WINIX_CRYPT_HASH_SHA224: run.Par("-sha224"); break;
|
||||
case WINIX_CRYPT_HASH_SHA256: run.Par("-sha256"); break;
|
||||
case WINIX_CRYPT_HASH_SHA384: run.Par("-sha384"); break;
|
||||
case WINIX_CRYPT_HASH_SHA512: run.Par("-sha512"); break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return run.Go(in, inlen, out) == 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypt::HashBin(int hash, const char * in, std::string & out)
|
||||
{
|
||||
size_t len = strlen(in);
|
||||
return HashBin(hash, in, len, out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypt::HashBin(int hash, const std::string & in, std::string & out)
|
||||
{
|
||||
return HashBin(hash, in.c_str(), in.size(), out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypt::HashBin(int hash, const wchar_t * in, size_t inlen, std::string & out)
|
||||
{
|
||||
PT::WideToUTF8(in, inlen, bufina);
|
||||
int res = HashBin(hash, bufina.c_str(), bufina.size(), out);
|
||||
bufina.clear();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
bool Crypt::HashBin(int hash, const wchar_t * in, std::string & out)
|
||||
{
|
||||
size_t len = wcslen(in);
|
||||
return HashBin(hash, in, len, out);
|
||||
}
|
||||
|
||||
|
||||
bool Crypt::HashBin(int hash, const std::wstring & in, std::string & out)
|
||||
{
|
||||
return HashBin(hash, in.c_str(), in.size(), out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypt::HashHex(int hash, const char * in, size_t inlen, std::string & out)
|
||||
{
|
||||
int res = HashBin(hash, in, inlen, out_temp);
|
||||
|
||||
ConvertToHexForm(out_temp, out);
|
||||
out_temp.clear();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
bool Crypt::HashHex(int hash, const char * in, std::string & out)
|
||||
{
|
||||
size_t len = strlen(in);
|
||||
return HashHex(hash, in, len, out);
|
||||
}
|
||||
|
||||
|
||||
bool Crypt::HashHex(int hash, const std::string & in, std::string & out)
|
||||
{
|
||||
return HashHex(hash, in.c_str(), in.size(), out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypt::HashHex(int hash, const wchar_t * in, size_t inlen, std::wstring & out)
|
||||
{
|
||||
int res = HashBin(hash, in, inlen, out_temp);
|
||||
|
||||
ConvertToHexForm(out_temp, out);
|
||||
out_temp.clear();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
bool Crypt::HashHex(int hash, const wchar_t * in, std::wstring & out)
|
||||
{
|
||||
size_t len = wcslen(in);
|
||||
return HashHex(hash, in, len, out);
|
||||
}
|
||||
|
||||
|
||||
bool Crypt::HashHex(int hash, const std::wstring & in, std::wstring & out)
|
||||
{
|
||||
return HashHex(hash, in.c_str(), in.size(), out);
|
||||
}
|
||||
|
||||
|
||||
bool IsAllWhite(const char * str)
|
||||
{
|
||||
for( ; *str ; ++str)
|
||||
{
|
||||
if( !(IsWhite(*str) || *str==10) )
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypt::RSA(bool encrypt, const char * keypath, const char * in, size_t inlen, std::string & out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
if( !config || IsAllWhite(keypath) )
|
||||
return false;
|
||||
|
||||
run.Clear();
|
||||
PT::WideToUTF8(config->opensll_path, command);
|
||||
run.Cmd(command);
|
||||
|
||||
run.Par("rsautl");
|
||||
run.Par("-inkey");
|
||||
run.Par(keypath);
|
||||
|
||||
if(encrypt)
|
||||
run.Par("-encrypt");
|
||||
else
|
||||
run.Par("-decrypt");
|
||||
|
||||
return run.Go(in, inlen, out) == 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypt::RSA(bool encrypt, const char * keypath, const std::string & in, std::string & out)
|
||||
{
|
||||
return RSA(encrypt, keypath, in.c_str(), in.size(), out);
|
||||
}
|
||||
|
||||
|
||||
bool Crypt::RSA(bool encrypt, const std::string & keypath, const std::string & in, std::string & out)
|
||||
{
|
||||
return RSA(encrypt, keypath.c_str(), in.c_str(), in.size(), out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypt::RSA(bool encrypt, const wchar_t * keypath, const char * in, size_t inlen, std::string & out)
|
||||
{
|
||||
PT::WideToUTF8(keypath, keypatha);
|
||||
return RSA(encrypt, keypatha.c_str(), in, inlen, out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypt::RSA(bool encrypt, const wchar_t * keypath, const std::string & in, std::string & out)
|
||||
{
|
||||
return RSA(encrypt, keypath, in.c_str(), in.size(), out);
|
||||
}
|
||||
|
||||
|
||||
bool Crypt::RSA(bool encrypt, const std::wstring & keypath, const std::string & in, std::string & out)
|
||||
{
|
||||
return RSA(encrypt, keypath.c_str(), in.c_str(), in.size(), out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool Crypt::PassHash(const std::wstring & salt, UserPass & up)
|
||||
{
|
||||
bool result = true;
|
||||
up.pass_hash_salted = false;
|
||||
|
||||
if( up.pass_type != WINIX_CRYPT_HASH_NONE )
|
||||
{
|
||||
pass_org = up.pass;
|
||||
pass_salted = up.pass;
|
||||
pass_salted += salt;
|
||||
|
||||
if( HashHex(up.pass_type, pass_salted, up.pass) )
|
||||
{
|
||||
if( !salt.empty() )
|
||||
up.pass_hash_salted = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "Crypt: problem with generating a hash, the password will not be hashed" << logend;
|
||||
|
||||
up.pass = pass_org;
|
||||
up.pass_type = WINIX_CRYPT_HASH_NONE;
|
||||
result = false;
|
||||
}
|
||||
|
||||
ClearString(pass_salted);
|
||||
ClearString(pass_org);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypt::PassCrypt(const std::wstring & path_to_rsa_private_key, UserPass & up)
|
||||
{
|
||||
bool result = false;
|
||||
ClearString(up.pass_encrypted);
|
||||
|
||||
if( !path_to_rsa_private_key.empty() )
|
||||
{
|
||||
PT::WideToUTF8(up.pass, passa);
|
||||
|
||||
if( RSA(true, path_to_rsa_private_key, passa, up.pass_encrypted) )
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ClearString(up.pass_encrypted);
|
||||
log << log1 << "AddUser: problem with encrypting, the password will not be encrypted!" << logend;
|
||||
}
|
||||
|
||||
ClearString(passa);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void Crypt::PassHashCrypt(const std::wstring & salt, const std::wstring & path_to_rsa_private_key, UserPass & up)
|
||||
{
|
||||
PassHash(salt, up);
|
||||
PassCrypt(path_to_rsa_private_key, up);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Crypt::PassHashCrypt(UserPass & up)
|
||||
{
|
||||
up.pass_type = config->pass_type;
|
||||
empty.clear();
|
||||
|
||||
if( config->pass_hash_use_salt && !config->pass_hash_salt.empty() )
|
||||
PassHash(config->pass_hash_salt, up);
|
||||
else
|
||||
PassHash(empty, up);
|
||||
|
||||
|
||||
if( config->pass_use_rsa && !config->pass_rsa_private_key.empty() )
|
||||
PassCrypt(config->pass_rsa_private_key, up);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
294
winixd/core/crypt.h
Normal file
294
winixd/core/crypt.h
Normal file
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_crypt
|
||||
#define headerfile_winix_core_crypt
|
||||
|
||||
#include <string>
|
||||
#include "run.h"
|
||||
#include "config.h"
|
||||
#include "user.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
/*
|
||||
the kind of hashes we are able to obtain in winix
|
||||
*/
|
||||
#define WINIX_CRYPT_HASH_NONE 0
|
||||
#define WINIX_CRYPT_HASH_MD4 1
|
||||
#define WINIX_CRYPT_HASH_MD5 2
|
||||
#define WINIX_CRYPT_HASH_SHA1 10
|
||||
#define WINIX_CRYPT_HASH_SHA224 11
|
||||
#define WINIX_CRYPT_HASH_SHA256 12
|
||||
#define WINIX_CRYPT_HASH_SHA384 13
|
||||
#define WINIX_CRYPT_HASH_SHA512 14
|
||||
|
||||
|
||||
|
||||
/*
|
||||
calculating hashes, encrypting and decrypting with RSA
|
||||
*/
|
||||
class Crypt
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
void SetConfig(Config * pconfig);
|
||||
|
||||
|
||||
/*
|
||||
calculating a hash from a given input
|
||||
|
||||
input:
|
||||
hash - the kind of the hash - WINIX_CRYPT_HASH_*
|
||||
in - input buffer
|
||||
inlen - the length of the buffer
|
||||
output:
|
||||
out - the hash in binary form
|
||||
*/
|
||||
bool HashBin(int hash, const char * in, size_t inlen, std::string & out);
|
||||
bool HashBin(int hash, const char * in, std::string & out);
|
||||
bool HashBin(int hash, const std::string & in, std::string & out);
|
||||
|
||||
|
||||
/*
|
||||
calculating a hash from a given input
|
||||
the input string is first changed to UTF8 and then hash is calculated
|
||||
|
||||
input:
|
||||
hash - the kind of the hash - WINIX_CRYPT_HASH_*
|
||||
in - input buffer
|
||||
inlen - the length of the buffer
|
||||
output:
|
||||
out - the hash in binary form
|
||||
*/
|
||||
bool HashBin(int hash, const wchar_t * in, size_t inlen, std::string & out);
|
||||
bool HashBin(int hash, const wchar_t * in, std::string & out);
|
||||
bool HashBin(int hash, const std::wstring & in, std::string & out);
|
||||
|
||||
|
||||
/*
|
||||
calculating a hash from a given input
|
||||
|
||||
input:
|
||||
hash - the kind of the hash - WINIX_CRYPT_HASH_*
|
||||
in - input buffer
|
||||
inlen - the length of the buffer
|
||||
output:
|
||||
out - the hash in the hex form (one byte is saved as two hex digits)
|
||||
*/
|
||||
bool HashHex(int hash, const char * in, size_t inlen, std::string & out);
|
||||
bool HashHex(int hash, const char * in, std::string & out);
|
||||
bool HashHex(int hash, const std::string & in, std::string & out);
|
||||
|
||||
|
||||
/*
|
||||
calculating a hash from a given input
|
||||
the input string is first changed to UTF8 and then hash is calculated
|
||||
|
||||
input:
|
||||
hash - the kind of the hash - WINIX_CRYPT_HASH_*
|
||||
in - input buffer
|
||||
inlen - the length of the buffer
|
||||
output:
|
||||
out - the hash in the hex form (one byte is saved as two hex digits)
|
||||
the 'out' here is std::wstring (not std::string like beforehand)
|
||||
*/
|
||||
bool HashHex(int hash, const wchar_t * in, size_t inlen, std::wstring & out);
|
||||
bool HashHex(int hash, const wchar_t * in, std::wstring & out);
|
||||
bool HashHex(int hash, const std::wstring & in, std::wstring & out);
|
||||
|
||||
|
||||
/*
|
||||
encrypt/decrypt by using RSA algorithm
|
||||
|
||||
input:
|
||||
encrypt - true means encrypting, false means decrypting
|
||||
keypath - path to a RSA private key (this is a private and public key in one file)
|
||||
in - input buffer
|
||||
inlen - the size of the buffer
|
||||
|
||||
output:
|
||||
out - encrypted or decrypted buffer (always binary)
|
||||
*/
|
||||
bool RSA(bool encrypt, const char * keypath, const char * in, size_t inlen, std::string & out);
|
||||
bool RSA(bool encrypt, const char * keypath, const std::string & in, std::string & out);
|
||||
bool RSA(bool encrypt, const std::string & keypath, const std::string & in, std::string & out);
|
||||
bool RSA(bool encrypt, const wchar_t * keypath, const char * in, size_t inlen, std::string & out);
|
||||
bool RSA(bool encrypt, const wchar_t * keypath, const std::string & in, std::string & out);
|
||||
bool RSA(bool encrypt, const std::wstring & keypath, const std::string & in, std::string & out);
|
||||
|
||||
|
||||
/*
|
||||
this method creates a hash from the given plain text password
|
||||
|
||||
input.
|
||||
salt - salt for the hash
|
||||
up.pass_type - what kind of hash do you want - look at WINIX_CRYPT_HASH_* macros (in crypt.h)
|
||||
up.pass - plain text password
|
||||
|
||||
if salt is empty then the hash will not be salted
|
||||
|
||||
output:
|
||||
up.pass_type - (can be changed to 0 when there is a problem with generating a hash)
|
||||
up.pass - hash from the password (or plain text if up.pass_type was zero)
|
||||
up.pass_hash_salted (true if the hash is salted - when salt was not empty)
|
||||
|
||||
if there is a problem with generating a hash the method stores a plain text password
|
||||
and changes up.pass_type to zero (plain text passwords are not salted)
|
||||
*/
|
||||
bool PassHash(const std::wstring & salt, UserPass & up);
|
||||
|
||||
|
||||
/*
|
||||
this method encrypts the given password
|
||||
|
||||
input:
|
||||
path_to_rsa_private_key - a path to rsa private key (this are a private and public keys both in one file)
|
||||
up.pass - given password (can be a plain text or a hash)
|
||||
|
||||
if path_to_rsa_private_key is empty then the password will not be encrypted
|
||||
|
||||
output:
|
||||
up.pass_encrypted
|
||||
|
||||
if there is a problem (or the path to the key is empty) then up.pass_encrypted will be empty
|
||||
and the method returns false
|
||||
*/
|
||||
bool PassCrypt(const std::wstring & path_to_rsa_private_key, UserPass & up);
|
||||
|
||||
|
||||
/*
|
||||
this method creates a hash from the given plain text password and then encrypts it
|
||||
|
||||
input:
|
||||
salt - salt for the hash
|
||||
path_to_rsa_private_key - a path to rsa private key (this are a private and public keys both in one file)
|
||||
up.pass_type - what kind of hash do you want - look at WINIX_CRYPT_HASH_* macros (in crypt.h)
|
||||
up.pass - plain text password
|
||||
|
||||
if salt is empty then the hash will not be salted
|
||||
if path_to_rsa_private_key is empty then the password will not be encrypted
|
||||
|
||||
output:
|
||||
up.pass_type - (can be changed to 0 when there is a problem with generating a hash)
|
||||
up.pass - hash from the password (or plain text if up.pass_type was zero)
|
||||
up.pass_hash_salted (true if the hash is salted - when salt was not empty)
|
||||
up.pass_encrypted - encrypted password (if not empty)
|
||||
|
||||
*/
|
||||
void PassHashCrypt(const std::wstring & salt, const std::wstring & path_to_rsa_private_key, UserPass & up);
|
||||
|
||||
|
||||
/*
|
||||
this method creates a hash from the given plain text password and then encrypts it
|
||||
|
||||
input:
|
||||
up.pass - plain text password
|
||||
|
||||
output:
|
||||
up.pass_type - what kind of hash there is in up.pass
|
||||
up.pass - hash from the password (or plain text if up.pass_type is zero)
|
||||
up.pass_hash_salted - true if the hash is salted (plain text are never salted)
|
||||
up.pass_encrypted - encrypted password (if not empty)
|
||||
*/
|
||||
void PassHashCrypt(UserPass & up);
|
||||
|
||||
|
||||
/*
|
||||
putting some characters into the string and then calling clear()
|
||||
*/
|
||||
template<class StringType>
|
||||
void ClearString(StringType & str);
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Config * config;
|
||||
Run run;
|
||||
std::string command, bufina, keypatha;
|
||||
//std::wstring pass_salted;//, pass_hashed;
|
||||
//std::string pass_hasheda, pass_encrypteda;
|
||||
|
||||
std::wstring pass_salted, pass_org;
|
||||
std::string passa, out_temp;
|
||||
std::wstring empty;
|
||||
|
||||
|
||||
template<typename StringType>
|
||||
void ConvertToHexForm(const std::string & in, StringType & out);
|
||||
|
||||
char ConvertToHexForm(int val);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<typename StringType>
|
||||
void Crypt::ClearString(StringType & str)
|
||||
{
|
||||
for(size_t i=0 ; i<str.size() ; ++i)
|
||||
str[i] = 0x0c;
|
||||
|
||||
str.clear();
|
||||
}
|
||||
|
||||
|
||||
template<typename StringType>
|
||||
void Crypt::ConvertToHexForm(const std::string & in, StringType & out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
if( in.size() * 2 > out.capacity() )
|
||||
out.reserve(in.size() * 2);
|
||||
|
||||
for(size_t i=0 ; i<in.size() ; ++i)
|
||||
{
|
||||
out += ConvertToHexForm(((unsigned char)in[i]) >> 4);
|
||||
out += ConvertToHexForm(((unsigned char)in[i]) & 0x0f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
68
winixd/core/cur.h
Normal file
68
winixd/core/cur.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_cur
|
||||
#define headerfile_winix_core_cur
|
||||
|
||||
#include "request.h"
|
||||
#include "session.h"
|
||||
#include "mount.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
/*
|
||||
current request and session
|
||||
*/
|
||||
struct Cur
|
||||
{
|
||||
Request * request;
|
||||
Session * session;
|
||||
Mount * mount;
|
||||
|
||||
// those pointers are never null, if there is no a session for the user
|
||||
// the 'session' pointer pointers at a special temporary session
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
365
winixd/core/dircontainer.cpp
Normal file
365
winixd/core/dircontainer.cpp
Normal file
@@ -0,0 +1,365 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "dircontainer.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
std::wstring DirContainer::dir_etc = L"etc";
|
||||
std::wstring DirContainer::dir_var = L"var";
|
||||
|
||||
|
||||
DirContainer::DirContainer()
|
||||
{
|
||||
is_root = false;
|
||||
is_etc = false;
|
||||
is_var = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
DirContainer::Iterator DirContainer::GetRoot()
|
||||
{
|
||||
if( !is_root )
|
||||
return table.end();
|
||||
|
||||
return root_iter;
|
||||
}
|
||||
|
||||
|
||||
DirContainer::Iterator DirContainer::GetEtc()
|
||||
{
|
||||
if( !is_etc )
|
||||
return table.end();
|
||||
|
||||
return etc_iter;
|
||||
}
|
||||
|
||||
|
||||
DirContainer::Iterator DirContainer::GetVar()
|
||||
{
|
||||
if( !is_var )
|
||||
return table.end();
|
||||
|
||||
return var_iter;
|
||||
}
|
||||
|
||||
|
||||
DirContainer::Iterator DirContainer::Begin()
|
||||
{
|
||||
return table.begin();
|
||||
}
|
||||
|
||||
|
||||
DirContainer::Iterator DirContainer::End()
|
||||
{
|
||||
return table.end();
|
||||
}
|
||||
|
||||
|
||||
DirContainer::SizeType DirContainer::Size()
|
||||
{
|
||||
return table.size(); // !! warning: it has O(n)
|
||||
}
|
||||
|
||||
bool DirContainer::Empty()
|
||||
{
|
||||
return table.empty();
|
||||
}
|
||||
|
||||
|
||||
bool DirContainer::IsNameOfSpecialFolder(const std::wstring & name)
|
||||
{
|
||||
return name == dir_etc || name == dir_var;
|
||||
}
|
||||
|
||||
|
||||
// looking for '/etc'
|
||||
// 'root' is found beforehand
|
||||
// CheckSpecialFolder() may not find everything (when the first is a special folder and then the root)
|
||||
void DirContainer::FindSpecialFolders()
|
||||
{
|
||||
is_etc = false;
|
||||
is_var = false;
|
||||
|
||||
if( !is_root )
|
||||
return;
|
||||
|
||||
DirContainer::ParentIterator i = FindFirstChild(root_iter->id);
|
||||
|
||||
for( ; i!=ParentEnd() ; i = NextChild(i) )
|
||||
{
|
||||
if( i->second->url == dir_etc )
|
||||
{
|
||||
is_etc = true;
|
||||
etc_iter = i->second;
|
||||
}
|
||||
else
|
||||
if( i->second->url == dir_var )
|
||||
{
|
||||
is_var = true;
|
||||
var_iter = i->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// this is used with PushBack() method
|
||||
void DirContainer::CheckSpecialFolder(const Item & item, Iterator iter)
|
||||
{
|
||||
if( item.parent_id == -1 )
|
||||
{
|
||||
is_root = true;
|
||||
root_iter = iter;
|
||||
}
|
||||
|
||||
if( !is_root )
|
||||
return;
|
||||
|
||||
if( item.parent_id==root_iter->id && item.url==dir_etc )
|
||||
{
|
||||
is_etc = true;
|
||||
etc_iter = iter;
|
||||
log << log2 << "DirCont: added special folder: /etc" << logend;
|
||||
}
|
||||
|
||||
if( item.parent_id==root_iter->id && item.url==dir_var )
|
||||
{
|
||||
is_var = true;
|
||||
var_iter = iter;
|
||||
log << log2 << "DirCont: added special folder: /var" << logend;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
DirContainer::Iterator DirContainer::PushBack(const Item & item)
|
||||
{
|
||||
if( item.parent_id == -1 && is_root )
|
||||
{
|
||||
log << log1 << "DirCont: more than one root dir - skipped, id: " << item.id << logend;
|
||||
return root_iter;
|
||||
}
|
||||
|
||||
Iterator last_iter = table.insert(table.end(), item);
|
||||
CheckSpecialFolder(item, last_iter);
|
||||
|
||||
log << log2 << "DirCont: added dir, url: " << item.url << ", id: " << item.id << ", parent_id: " << item.parent_id << logend;
|
||||
|
||||
table_id.insert( std::make_pair(last_iter->id, last_iter) );
|
||||
table_parent.insert( std::make_pair(last_iter->parent_id, last_iter) );
|
||||
|
||||
log << log3 << "DirCont: added indexes to dir, id: " << last_iter->id << ", parent_id: " << last_iter->parent_id << logend;
|
||||
|
||||
return last_iter;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool DirContainer::ChangeParent(long dir_id, long new_parent_id)
|
||||
{
|
||||
Iterator i = FindId(dir_id);
|
||||
|
||||
if( i == table.end() )
|
||||
return false;
|
||||
|
||||
if( i->parent_id == new_parent_id )
|
||||
return true; // nothing to do
|
||||
|
||||
ParentIterator p = FindFirstChild(i->parent_id);
|
||||
bool found = false;
|
||||
|
||||
for( ; p != table_parent.end() ; p = NextChild(p) )
|
||||
{
|
||||
if( p->second->id == dir_id )
|
||||
{
|
||||
table_parent.erase(p);
|
||||
log << log3 << "DirCont: removed parent index to dir: " << i->id << logend;
|
||||
|
||||
i->parent_id = new_parent_id;
|
||||
table_parent.insert( std::make_pair(new_parent_id, i) );
|
||||
log << log3 << "DirCont: added parent index to dir, id: " << i->id << ", parent_id: " << i->parent_id << logend;
|
||||
|
||||
found = true;
|
||||
|
||||
if( IsNameOfSpecialFolder(i->url) )
|
||||
FindSpecialFolders();
|
||||
|
||||
break; // that iterator (p) is only one
|
||||
}
|
||||
}
|
||||
|
||||
if( !found )
|
||||
log << log1 << "DirCont: cannot find parent_id: " << i->parent_id << " in parent indexes" << logend;
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DirContainer::Clear()
|
||||
{
|
||||
table.clear();
|
||||
table_id.clear();
|
||||
table_parent.clear();
|
||||
is_root = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
DirContainer::Iterator DirContainer::FindId(long id)
|
||||
{
|
||||
TableId::iterator i = table_id.find(id);
|
||||
|
||||
if( i == table_id.end() )
|
||||
return table.end();
|
||||
|
||||
return i->second;
|
||||
}
|
||||
|
||||
|
||||
|
||||
DirContainer::ParentIterator DirContainer::ParentBegin()
|
||||
{
|
||||
return table_parent.begin();
|
||||
}
|
||||
|
||||
|
||||
|
||||
DirContainer::ParentIterator DirContainer::ParentEnd()
|
||||
{
|
||||
return table_parent.end();
|
||||
}
|
||||
|
||||
|
||||
DirContainer::ParentSizeType DirContainer::ParentSize()
|
||||
{
|
||||
return table_parent.size();
|
||||
}
|
||||
|
||||
|
||||
bool DirContainer::ParentEmpty()
|
||||
{
|
||||
return table_parent.empty();
|
||||
}
|
||||
|
||||
|
||||
DirContainer::ParentIterator DirContainer::FindFirstChild(long parent)
|
||||
{
|
||||
ParentIterator i = table_parent.lower_bound(parent);
|
||||
|
||||
if( i == table_parent.end() || i->first != parent )
|
||||
return table_parent.end();
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
DirContainer::ParentIterator DirContainer::NextChild(ParentIterator i)
|
||||
{
|
||||
if( i == table_parent.end() )
|
||||
return table_parent.end();
|
||||
|
||||
long parent = i->first;
|
||||
++i;
|
||||
|
||||
if( i == table_parent.end() || i->first != parent )
|
||||
return table_parent.end();
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool DirContainer::DelById(long id)
|
||||
{
|
||||
TableId::iterator i = table_id.find(id);
|
||||
|
||||
if( i == table_id.end() )
|
||||
{
|
||||
log << log1 << "DirCont: delete: there is no directory with id: " << id << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
long parent_id = i->second->parent_id;
|
||||
TableParent::iterator z = table_parent.lower_bound(parent_id);
|
||||
bool found = false;
|
||||
|
||||
for( ; z != table_parent.end() && z->first == parent_id ; ++z )
|
||||
{
|
||||
if( z->second == i->second )
|
||||
{
|
||||
log << log2 << "DirCont: deleted directory id: " << id << ", url: " << i->second->url;
|
||||
|
||||
if( i->second->parent_id == -1 )
|
||||
{
|
||||
log << log2 << " (root directory)";
|
||||
is_root = false;
|
||||
}
|
||||
|
||||
log << log2 << logend;
|
||||
|
||||
table.erase(i->second);
|
||||
|
||||
log << log3 << "DirCont: deleted indexes into directory id: " << id << logend;
|
||||
table_id.erase(i);
|
||||
table_parent.erase(z);
|
||||
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( !found )
|
||||
{
|
||||
log << log1 << "DirCont: can't find an index_parent into directory id: " << id << ", url: " << i->second->url << " (deleting skipped)" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
128
winixd/core/dircontainer.h
Normal file
128
winixd/core/dircontainer.h
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_dircontainer
|
||||
#define headerfile_winix_core_dircontainer
|
||||
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include "item.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class DirContainer
|
||||
{
|
||||
|
||||
public:
|
||||
typedef std::list<Item> Table;
|
||||
typedef Table::iterator Iterator;
|
||||
typedef Table::size_type SizeType;
|
||||
|
||||
typedef std::map<long, Iterator> TableId;
|
||||
typedef std::multimap<long, Iterator> TableParent;
|
||||
typedef TableParent::iterator ParentIterator;
|
||||
typedef TableParent::size_type ParentSizeType;
|
||||
|
||||
|
||||
DirContainer();
|
||||
|
||||
Iterator GetRoot();
|
||||
Iterator GetEtc();
|
||||
Iterator GetVar();
|
||||
|
||||
Iterator Begin();
|
||||
Iterator End();
|
||||
SizeType Size();
|
||||
bool Empty();
|
||||
Iterator PushBack(const Item & item);
|
||||
bool ChangeParent(long dir_id, long new_parent_id);
|
||||
void Clear();
|
||||
|
||||
Iterator FindId(long id);
|
||||
|
||||
bool DelById(long id);
|
||||
|
||||
ParentIterator ParentBegin(); // IMPROVE ME: may it should be renamed to ChildBegin() similarly as FindFirstChild() ?
|
||||
ParentIterator ParentEnd();
|
||||
ParentSizeType ParentSize();
|
||||
bool ParentEmpty();
|
||||
ParentIterator FindFirstChild(long parent);
|
||||
ParentIterator NextChild(ParentIterator pi);
|
||||
|
||||
bool IsNameOfSpecialFolder(const std::wstring & name);
|
||||
void FindSpecialFolders();
|
||||
|
||||
private:
|
||||
|
||||
void CheckSpecialFolder(const Item & item, Iterator iter);
|
||||
|
||||
// main table with dirs
|
||||
Table table;
|
||||
|
||||
// true if there is a root dir in the table
|
||||
bool is_root;
|
||||
|
||||
// root
|
||||
Iterator root_iter;
|
||||
|
||||
// true if there is a etc dir in the table
|
||||
bool is_etc;
|
||||
|
||||
// etc
|
||||
Iterator etc_iter;
|
||||
|
||||
// true if there is a var dir in the table
|
||||
bool is_var;
|
||||
|
||||
// var
|
||||
Iterator var_iter;
|
||||
|
||||
// indexes
|
||||
TableId table_id;
|
||||
TableParent table_parent;
|
||||
|
||||
// names of folders
|
||||
static std::wstring dir_etc, dir_var;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
789
winixd/core/dirs.cpp
Normal file
789
winixd/core/dirs.cpp
Normal file
@@ -0,0 +1,789 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "dirs.h"
|
||||
#include "error.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
void Dirs::SetDb(Db * pdb)
|
||||
{
|
||||
db = pdb;
|
||||
}
|
||||
|
||||
|
||||
void Dirs::SetCur(Cur * pcur)
|
||||
{
|
||||
cur = pcur;
|
||||
}
|
||||
|
||||
|
||||
void Dirs::SetNotify(Notify * pnotify)
|
||||
{
|
||||
notify = pnotify;
|
||||
}
|
||||
|
||||
|
||||
void Dirs::Clear()
|
||||
{
|
||||
dir_tab.Clear();
|
||||
}
|
||||
|
||||
|
||||
bool Dirs::HasReadExecAccessForRoot(const Item & item)
|
||||
{
|
||||
// there must be at least one 'x' (for the root)
|
||||
return (item.privileges & 01111) != 0;
|
||||
}
|
||||
|
||||
|
||||
void Dirs::CheckRootDir()
|
||||
{
|
||||
DirContainer::Iterator i = dir_tab.GetRoot();
|
||||
|
||||
if( i != dir_tab.End() )
|
||||
{
|
||||
if( !HasReadExecAccessForRoot(*i) )
|
||||
{
|
||||
i->privileges = 07555;
|
||||
log << log1 << "Dirs: there is no access for a root (admin) to the root dir, setting 07555 for the root directory" << logend;
|
||||
db->EditPrivById(*i, i->id);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
log << log1 << "Dirs: there is no a root directory in the database (creating one)" << logend;
|
||||
|
||||
Item root;
|
||||
|
||||
root.type = Item::dir;
|
||||
root.parent_id = -1;
|
||||
root.user_id = -1;
|
||||
root.group_id = -1;
|
||||
root.privileges = 07555;
|
||||
|
||||
// !! upewnic sie ze baza nie zmieni url (gdyby wczesniej juz byl w bazie pusty url)
|
||||
// !! zrobic jakis wyjatek do wprowadzania roota?
|
||||
if( db->AddItem(root) == WINIX_ERR_OK )
|
||||
{
|
||||
dir_tab.PushBack(root);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Dirs::ReadDirs()
|
||||
{
|
||||
Clear();
|
||||
|
||||
db->GetDirs(dir_tab);
|
||||
CheckRootDir();
|
||||
dir_tab.FindSpecialFolders();
|
||||
}
|
||||
|
||||
|
||||
bool Dirs::ExtractName(const wchar_t * & s, std::wstring & name)
|
||||
{
|
||||
name.clear();
|
||||
|
||||
// skipping first slashes (can be more than one)
|
||||
for( ; *s == '/' ; ++s);
|
||||
|
||||
for( ; *s != 0 && *s != '/' ; ++s)
|
||||
name += *s;
|
||||
|
||||
return !name.empty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool Dirs::IsDir(long id)
|
||||
{
|
||||
DirContainer::Iterator i = dir_tab.FindId(id);
|
||||
|
||||
if( i == dir_tab.End() )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// !! dac clearowanie childs_tab
|
||||
// !! ewentualnie mozna dodac trzeci domyslny parametr bool clear_tab = true
|
||||
bool Dirs::GetDirChilds(long parent, std::vector<Item*> & childs_tab)
|
||||
{
|
||||
if( parent != -1 && !IsDir(parent) )
|
||||
return false;
|
||||
|
||||
DirContainer::ParentIterator i = dir_tab.FindFirstChild(parent);
|
||||
|
||||
for( ; i != dir_tab.ParentEnd() ; i = dir_tab.NextChild(i) )
|
||||
childs_tab.push_back( &(*i->second) );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
DirContainer::ParentIterator Dirs::FindFirstChild(long parent_id)
|
||||
{
|
||||
DirContainer::ParentIterator i = dir_tab.FindFirstChild(parent_id);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
DirContainer::ParentIterator Dirs::NextChild(DirContainer::ParentIterator i)
|
||||
{
|
||||
return dir_tab.NextChild(i);
|
||||
}
|
||||
|
||||
|
||||
DirContainer::ParentIterator Dirs::ParentEnd()
|
||||
{
|
||||
return dir_tab.ParentEnd();
|
||||
}
|
||||
|
||||
|
||||
// !! dodatkowo moze metoda AppendPath dodajaca sciezke do biezacego stringa?
|
||||
// albo tutaj stringa nie czyscic?
|
||||
// O(m * log n) (m- how many parts are in 'id')
|
||||
// path with a slash at the end and at the beginning
|
||||
bool Dirs::MakePath(long id, std::wstring & path, bool clear_path)
|
||||
{
|
||||
DirContainer::Iterator i;
|
||||
|
||||
if( clear_path )
|
||||
path.clear();
|
||||
|
||||
temp_path = '/';
|
||||
|
||||
while( true )
|
||||
{
|
||||
i = dir_tab.FindId(id);
|
||||
|
||||
if( i == dir_tab.End() ||
|
||||
i->parent_id == id ) // means a loop (something wrong in the db)
|
||||
{
|
||||
// we don't change path if there is no such a directory
|
||||
return false;
|
||||
}
|
||||
|
||||
if( i->parent_id == -1 )
|
||||
{
|
||||
path += temp_path;
|
||||
return true;
|
||||
}
|
||||
|
||||
id = i->parent_id;
|
||||
|
||||
temp_path.insert(0, i->url);
|
||||
temp_path.insert(temp_path.begin(), '/');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Dirs::MakePath(const std::vector<Item*> dir_tab, std::wstring & path, bool clear_path)
|
||||
{
|
||||
if( clear_path )
|
||||
path.clear();
|
||||
|
||||
for(size_t i=0 ; i<dir_tab.size() ; ++i)
|
||||
{
|
||||
path += dir_tab[i]->url;
|
||||
path += '/';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t Dirs::DirLevel(long id)
|
||||
{
|
||||
DirContainer::Iterator i;
|
||||
size_t level = 0;
|
||||
|
||||
while( true )
|
||||
{
|
||||
i = dir_tab.FindId(id);
|
||||
|
||||
if( i == dir_tab.End() ||
|
||||
i->parent_id == id ) // means a loop (something wrong in the db)
|
||||
{
|
||||
return level;
|
||||
}
|
||||
|
||||
if( i->parent_id == -1 )
|
||||
return level;
|
||||
|
||||
id = i->parent_id;
|
||||
level += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Dirs::IsChild(long parent_id, long child_id)
|
||||
{
|
||||
if( child_id == parent_id )
|
||||
return false;
|
||||
|
||||
DirContainer::Iterator i;
|
||||
|
||||
while( child_id != -1 )
|
||||
{
|
||||
i = dir_tab.FindId(child_id);
|
||||
|
||||
if( i == dir_tab.End() )
|
||||
return false;
|
||||
|
||||
if( i->parent_id == parent_id )
|
||||
return true;
|
||||
|
||||
child_id = i->parent_id;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool Dirs::ChangeParent(long dir_id, long new_parent_id)
|
||||
{
|
||||
return dir_tab.ChangeParent(dir_id, new_parent_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
checking whether dir_id has a parent parent_id (somewhere in the path)
|
||||
*/
|
||||
bool Dirs::HasParent(long dir_id, long parent_id)
|
||||
{
|
||||
DirContainer::Iterator i;
|
||||
|
||||
while( true )
|
||||
{
|
||||
i = dir_tab.FindId(dir_id);
|
||||
|
||||
if( i==dir_tab.End() || i->parent_id==-1 )
|
||||
return false;
|
||||
|
||||
if( i->parent_id == parent_id )
|
||||
return true;
|
||||
|
||||
dir_id = i->parent_id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool Dirs::CreateDirTab(long dir_id, std::vector<Item*> & out_dir_tab)
|
||||
{
|
||||
DirContainer::Iterator i;
|
||||
|
||||
out_dir_tab.clear();
|
||||
|
||||
do
|
||||
{
|
||||
i = dir_tab.FindId(dir_id);
|
||||
|
||||
if( i == dir_tab.End() )
|
||||
return false;
|
||||
|
||||
if( out_dir_tab.empty() )
|
||||
out_dir_tab.insert(out_dir_tab.end(), &(*i)); // !! I am not sure whether begin() can be used on an empty container
|
||||
else
|
||||
out_dir_tab.insert(out_dir_tab.begin(), &(*i));
|
||||
|
||||
dir_id = i->parent_id;
|
||||
}
|
||||
while( dir_id != -1 );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Item * Dirs::GetRootDir()
|
||||
{
|
||||
DirContainer::Iterator root = dir_tab.GetRoot();
|
||||
|
||||
if( root == dir_tab.End() )
|
||||
return 0;
|
||||
|
||||
return &(*root);
|
||||
}
|
||||
|
||||
|
||||
Item * Dirs::GetEtcDir()
|
||||
{
|
||||
DirContainer::Iterator etc = dir_tab.GetEtc();
|
||||
|
||||
if( etc == dir_tab.End() )
|
||||
return 0;
|
||||
|
||||
return &(*etc);
|
||||
}
|
||||
|
||||
|
||||
Item * Dirs::GetVarDir()
|
||||
{
|
||||
DirContainer::Iterator etc = dir_tab.GetVar();
|
||||
|
||||
if( etc == dir_tab.End() )
|
||||
return 0;
|
||||
|
||||
return &(*etc);
|
||||
}
|
||||
|
||||
|
||||
Item * Dirs::GetDir(const wchar_t * name, long parent)
|
||||
{
|
||||
DirContainer::ParentIterator i = dir_tab.FindFirstChild(parent);
|
||||
|
||||
for( ; i!=dir_tab.ParentEnd() ; i = dir_tab.NextChild(i) )
|
||||
if( i->second->url == name )
|
||||
return &(*i->second);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Item * Dirs::GetDir(const std::wstring & name, long parent)
|
||||
{
|
||||
return GetDir(name.c_str(), parent);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Item * Dirs::GetDir(const wchar_t * path)
|
||||
{
|
||||
if( *path == 0 )
|
||||
return 0;
|
||||
|
||||
DirContainer::Iterator root = dir_tab.GetRoot();
|
||||
|
||||
if( root == dir_tab.End() )
|
||||
// ops, we do not have a root dir
|
||||
return 0;
|
||||
|
||||
Item * pitem = &(*root);
|
||||
const wchar_t * s = path;
|
||||
|
||||
while( ExtractName(s, get_dir_temp) )
|
||||
{
|
||||
pitem = GetDir(get_dir_temp, pitem->id);
|
||||
|
||||
if( !pitem )
|
||||
return 0;
|
||||
}
|
||||
|
||||
return pitem;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Item * Dirs::GetDir(const std::wstring & path)
|
||||
{
|
||||
return GetDir(path.c_str());
|
||||
}
|
||||
|
||||
|
||||
Item * Dirs::GetDir(long id)
|
||||
{
|
||||
DirContainer::Iterator i = dir_tab.FindId(id);
|
||||
|
||||
if( i == dir_tab.End() )
|
||||
return 0;
|
||||
|
||||
return &(*i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Item * Dirs::AddDir(const Item & item)
|
||||
{
|
||||
return &(*dir_tab.PushBack(item));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
size_t Dirs::AnalyzeDir(Item * pdir, const std::wstring & path, long & dir_id, std::wstring & dir)
|
||||
{
|
||||
size_t i = 0;
|
||||
size_t old_i;
|
||||
|
||||
while( true )
|
||||
{
|
||||
dir_id = pdir->id;
|
||||
|
||||
// skipping slashes
|
||||
for( ; i<path.size() && path[i] == '/' ; ++i );
|
||||
|
||||
if( i == path.size() )
|
||||
return i; // end of the path
|
||||
|
||||
// creating a name
|
||||
old_i = i;
|
||||
analyze_temp.clear();
|
||||
|
||||
for( ; i<path.size() && path[i] != '/' ; ++i)
|
||||
analyze_temp += path[i];
|
||||
|
||||
pdir = GetDir(analyze_temp, pdir->id);
|
||||
|
||||
if( !pdir )
|
||||
return old_i; // analyze_temp is not a directory
|
||||
|
||||
dir += analyze_temp;
|
||||
dir += '/';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
the path should begin with a slash
|
||||
|
||||
return values:
|
||||
0 - directory exists
|
||||
dir_id - id of the directory
|
||||
dir - the path to the directory (with a slash at the end)
|
||||
file - if not empty means a file name (we don't check if the file really exists)
|
||||
1 - there is not a root dir
|
||||
2 - the path is empty
|
||||
3 - there is not such a directory
|
||||
*/
|
||||
int Dirs::AnalyzePath(const std::wstring & path, long & dir_id, std::wstring & dir, std::wstring & file)
|
||||
{
|
||||
Item * pdir = GetRootDir();
|
||||
dir = '/';
|
||||
file.clear();
|
||||
|
||||
if( !pdir )
|
||||
return 1;
|
||||
|
||||
if( path.empty() )
|
||||
return 2;
|
||||
|
||||
if( path[0] != '/' )
|
||||
return 3;
|
||||
|
||||
size_t i = AnalyzeDir(pdir, path, dir_id, dir);
|
||||
|
||||
if( i < path.size() )
|
||||
{
|
||||
// checking if at least one slash has left
|
||||
for(size_t a=i ; a < path.size() ; ++a)
|
||||
if( path[a] == '/' )
|
||||
return 3; // there is not such a directory
|
||||
|
||||
// the rest of the path is a file name
|
||||
file = path.c_str() + i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// current_dir_tab can be the same container as out_dir_tab
|
||||
void Dirs::CopyDirTab(const std::vector<Item*> & in, std::vector<Item*> & out)
|
||||
{
|
||||
if( &in != &out )
|
||||
{
|
||||
out.resize(in.size());
|
||||
|
||||
for(size_t i=0 ; i<in.size() ; ++i)
|
||||
out[i] = in[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
!! IMPROVE ME
|
||||
may dir_tab can be empty when link_to is not relative?
|
||||
and now the algorith doesn't check if link_to is not relative (it only uses dir_tab)
|
||||
*/
|
||||
bool Dirs::AnalyzeDir(std::vector<Item*> & dir_tab, const std::wstring & link_to, size_t & i)
|
||||
{
|
||||
size_t old_i;
|
||||
i = 0;
|
||||
|
||||
if( dir_tab.empty() )
|
||||
return false;
|
||||
|
||||
while( true )
|
||||
{
|
||||
// skipping slashes
|
||||
for( ; i<link_to.size() && link_to[i] == '/' ; ++i);
|
||||
|
||||
if( i == link_to.size() )
|
||||
return true; // end of the path
|
||||
|
||||
// creating a name
|
||||
old_i = i;
|
||||
analyze_temp.clear();
|
||||
|
||||
for( ; i<link_to.size() && link_to[i] != '/' ; ++i)
|
||||
analyze_temp += link_to[i];
|
||||
|
||||
if( analyze_temp == L".." )
|
||||
{
|
||||
if( dir_tab.size() <= 1 )
|
||||
return false;
|
||||
|
||||
dir_tab.pop_back();
|
||||
}
|
||||
else
|
||||
if( analyze_temp != L"." )
|
||||
{
|
||||
Item * pdir = GetDir(analyze_temp, dir_tab.back()->id);
|
||||
|
||||
if( !pdir )
|
||||
{
|
||||
i = old_i;
|
||||
return true; // analyze_temp is not a directory
|
||||
}
|
||||
|
||||
dir_tab.push_back(pdir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int Dirs::FollowLink(std::vector<Item*> & dir_tab, const std::wstring & link_to, std::wstring & out_item)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if( !AnalyzeDir(dir_tab, link_to, i) )
|
||||
return 2; // incorrect link_to
|
||||
|
||||
if( i < link_to.size() )
|
||||
{
|
||||
// checking if at least one slash has left
|
||||
for(size_t a=i ; a < link_to.size() ; ++a)
|
||||
if( link_to[a] == '/' )
|
||||
return 2; // there is not such a directory
|
||||
|
||||
// the rest of the path is a file name
|
||||
out_item = link_to.c_str() + i;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
return codes:
|
||||
ok:
|
||||
0 - the link_to is a path to a directory (out_item skipped, out_dir_tab will not be empty)
|
||||
1 - the link_to is a path to a file (out_item is used, out_dir_tab will not be empty)
|
||||
|
||||
error:
|
||||
2 - incorrect link_to
|
||||
3 - there is not a root dir
|
||||
4 - current_dir_tab was empty
|
||||
|
||||
current_dir_tab can be the same container as out_dir_tab
|
||||
link_to can be a relative path (without the first slash) and can contain ".." or "."
|
||||
*/
|
||||
int Dirs::FollowLink(const std::vector<Item*> & current_dir_tab, const std::wstring & link_to,
|
||||
std::vector<Item*> & out_dir_tab, std::wstring & out_item)
|
||||
{
|
||||
temp_link_to = link_to; // link_to can be from the out_item and would be cleared next
|
||||
out_item.clear();
|
||||
|
||||
if( current_dir_tab.empty() )
|
||||
return 4;
|
||||
|
||||
if( temp_link_to.empty() )
|
||||
{
|
||||
CopyDirTab(current_dir_tab, out_dir_tab);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( temp_link_to[0] == '/' )
|
||||
{
|
||||
// temp_link_to is an absolute path
|
||||
Item * pdir = GetRootDir();
|
||||
|
||||
if( !pdir )
|
||||
return 3;
|
||||
|
||||
out_dir_tab.clear();
|
||||
out_dir_tab.push_back(pdir);
|
||||
}
|
||||
else
|
||||
{
|
||||
// temp_link_to is a relative path
|
||||
CopyDirTab(current_dir_tab, out_dir_tab);
|
||||
}
|
||||
|
||||
return FollowLink(out_dir_tab, temp_link_to, out_item);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Dirs::SplitPath(const std::wstring & path, std::wstring & dir, std::wstring & file)
|
||||
{
|
||||
std::wstring::size_type i;
|
||||
|
||||
dir.clear();
|
||||
file.clear();
|
||||
|
||||
if( path.empty() )
|
||||
// !! moze dir ustawic na '/' ?
|
||||
return;
|
||||
|
||||
for( i=path.size()-1 ; i>0 && path[i]!='/' ; --i);
|
||||
|
||||
if( path[i] != '/' )
|
||||
{
|
||||
// we do not have any slashes '/'
|
||||
file = path;
|
||||
return;
|
||||
}
|
||||
|
||||
dir.assign(path, 0, i + 1); // +1 means with a slash at the end
|
||||
|
||||
if( i < path.size() - 1 )
|
||||
file.assign(path, i+1, path.size() - i - 1);
|
||||
}
|
||||
|
||||
|
||||
// !! dodac kasowanie z bazy
|
||||
bool Dirs::DelDir(long dir_id)
|
||||
{
|
||||
return dir_tab.DelById(dir_id);
|
||||
}
|
||||
|
||||
|
||||
Error Dirs::AddDirectory(Item & item, bool add_to_dir_tab, Item ** pdir, int notify_code)
|
||||
{
|
||||
if( pdir )
|
||||
*pdir = 0;
|
||||
|
||||
if( item.type != Item::dir )
|
||||
return WINIX_ERR_DIR_EXPECTED;
|
||||
|
||||
Error status = db->AddItem(item);
|
||||
|
||||
if( status == WINIX_ERR_OK )
|
||||
{
|
||||
Item * d = AddDir(item);
|
||||
|
||||
if( add_to_dir_tab && !cur->request->dir_tab.empty() && cur->request->dir_tab.back()->id == item.parent_id )
|
||||
cur->request->dir_tab.push_back(d);
|
||||
|
||||
if( pdir )
|
||||
*pdir = d;
|
||||
|
||||
if( notify_code )
|
||||
notify->ItemChanged(notify_code, item);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Item * Dirs::CreateVarDir()
|
||||
{
|
||||
Item * var = GetVarDir();
|
||||
|
||||
if( var )
|
||||
return var;
|
||||
|
||||
Item v;
|
||||
Item * root = GetRootDir();
|
||||
|
||||
if( root )
|
||||
{
|
||||
v.parent_id = root->id;
|
||||
v.user_id = root->user_id;
|
||||
v.group_id = root->group_id;
|
||||
v.privileges = root->privileges;
|
||||
v.subject = L"var";
|
||||
v.url = L"var";
|
||||
v.type = Item::dir;
|
||||
|
||||
AddDirectory(v, false, &var);
|
||||
}
|
||||
|
||||
return var;
|
||||
}
|
||||
|
||||
|
||||
// printing first and last slash
|
||||
void Dirs::LogDir(const std::vector<Item*> & dir_tab)
|
||||
{
|
||||
log << '/';
|
||||
|
||||
// skipping the first (root) directory
|
||||
for(size_t i=1 ; i<dir_tab.size() ; ++i)
|
||||
log << dir_tab[i]->url << '/';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
152
winixd/core/dirs.h
Normal file
152
winixd/core/dirs.h
Normal file
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_dirs
|
||||
#define headerfile_winix_core_dirs
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "item.h"
|
||||
#include "dircontainer.h"
|
||||
#include "db/db.h"
|
||||
#include "request.h"
|
||||
#include "notify/notify.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
// !! IMPROVE ME
|
||||
// we do not support '..' in a path (for simplicity and security reasons)
|
||||
// (we will support '..' in the future)
|
||||
|
||||
|
||||
class Dirs
|
||||
{
|
||||
public:
|
||||
|
||||
void Clear();
|
||||
void ReadDirs();
|
||||
|
||||
void SetCur(Cur * pcur);
|
||||
void SetDb(Db * pdb);
|
||||
void SetNotify(Notify * pnotify);
|
||||
|
||||
// these methods return false if there is no such a dir
|
||||
bool IsDir(long dir_id);
|
||||
bool GetDirChilds(long parent_id, std::vector<Item*> & childs_tab); // !! zamienic na GetChilds()
|
||||
bool MakePath(long dir_id, std::wstring & path, bool clear_path = true);
|
||||
void MakePath(const std::vector<Item*> dir_tab, std::wstring & path, bool clear_path = true);
|
||||
bool ChangeParent(long dir_id, long new_parent_id);
|
||||
|
||||
bool HasParent(long dir_id, long parent_id);
|
||||
|
||||
bool DelDir(long dir_id);
|
||||
|
||||
// if returns true then out_dir_tab is not empty
|
||||
bool CreateDirTab(long dir_id, std::vector<Item*> & out_dir_tab);
|
||||
|
||||
void LogDir(const std::vector<Item*> & dir_tab);
|
||||
|
||||
int AnalyzePath(const std::wstring & path, long & dir_id, std::wstring & dir, std::wstring & file);
|
||||
int FollowLink(const std::vector<Item*> & current_dir_tab, const std::wstring & link_to,
|
||||
std::vector<Item*> & out_dir_tab, std::wstring & out_item);
|
||||
static void SplitPath(const std::wstring & path, std::wstring & dir, std::wstring & file);
|
||||
|
||||
DirContainer::ParentIterator FindFirstChild(long parent_id);
|
||||
DirContainer::ParentIterator NextChild(DirContainer::ParentIterator i);
|
||||
DirContainer::ParentIterator ParentEnd(); // !! pozostalo do zamiany na child
|
||||
|
||||
|
||||
// these methods return null if there is no such a dir
|
||||
// !! zmienic nazwy wskazujace ze operujemy tylko na lokalnej tablicy
|
||||
Item * GetRootDir();
|
||||
Item * GetEtcDir();
|
||||
Item * GetVarDir();
|
||||
|
||||
Item * GetDir(const wchar_t * name, long parent);
|
||||
Item * GetDir(const std::wstring & name, long parent);
|
||||
Item * GetDir(const wchar_t * path);
|
||||
Item * GetDir(const std::wstring & path);
|
||||
Item * GetDir(long id);
|
||||
Item * AddDir(const Item & item);
|
||||
|
||||
void CheckRootDir();
|
||||
|
||||
Item * CreateVarDir();
|
||||
|
||||
// !! jak juz wczesniejsze nazwy beda zmienione to tutaj damy AddDir() /AddDir() juz istnieje przeciez?/
|
||||
Error AddDirectory(Item & item, bool add_to_dir_tab = false, Item ** pdir = 0, int notify_code = 0);
|
||||
|
||||
// returns how many levels of directories there are
|
||||
// "/" -> 0 (root dir)
|
||||
// "/abc" -> 1
|
||||
// "/abc/def" -> 2
|
||||
size_t DirLevel(long id);
|
||||
|
||||
// checking if child_id is really a child of parent_id
|
||||
bool IsChild(long parent_id, long child_id);
|
||||
|
||||
private:
|
||||
|
||||
Cur * cur;
|
||||
Db * db;
|
||||
Notify * notify;
|
||||
|
||||
DirContainer dir_tab;
|
||||
std::wstring temp_path;
|
||||
std::wstring temp_link_to;
|
||||
|
||||
size_t AnalyzeDir(Item * pdir, const std::wstring & path, long & dir_id, std::wstring & dir);
|
||||
bool AnalyzeDir(std::vector<Item*> & dir_tab, const std::wstring & link_to, size_t & i);
|
||||
std::wstring analyze_temp;
|
||||
std::wstring get_dir_temp;
|
||||
|
||||
void CopyDirTab(const std::vector<Item*> & in, std::vector<Item*> & out);
|
||||
int FollowLink(std::vector<Item*> & dir_tab, const std::wstring & link_to, std::wstring & out_item);
|
||||
|
||||
bool ExtractName(const wchar_t * & s, std::wstring & name);
|
||||
bool HasReadExecAccessForRoot(const Item & item);
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
111
winixd/core/error.h
Normal file
111
winixd/core/error.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_error
|
||||
#define headerfile_winix_core_error
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
#define WINIX_ERR_OK 0
|
||||
//#define WINIX_ERR_INCORRECT_PATH 1
|
||||
|
||||
//#define WINIX_ERR_NO_POSTVAR 2
|
||||
#define WINIX_ERR_INCORRECT_DIR 3
|
||||
#define WINIX_ERR_CANT_CHANGE_USER 4
|
||||
#define WINIX_ERR_CANT_CHANGE_GROUP 5
|
||||
#define WINIX_ERR_CANT_CHANGE_PRIVILEGES 6
|
||||
#define WINIX_ERR_PERMISSION_DENIED 7
|
||||
#define WINIX_ERR_NO_ROOT_DIR 8
|
||||
#define WINIX_ERR_NO_FUNCTION 9
|
||||
|
||||
|
||||
#define WINIX_ERR_NO_ITEM 10
|
||||
#define WINIX_ERR_UNKNOWN_PARAM 11
|
||||
|
||||
#define WINIX_ERR_MOUNT_UNKNOWN 12
|
||||
#define WINIX_ERR_UNKNOWN_FILESYSTEM 13
|
||||
#define WINIX_ERR_NO_MOUNTPOINT 14
|
||||
//#define WINIX_ERR_MOUNT_NO_PARAM 15
|
||||
|
||||
#define WINIX_ERR_NO_THREAD 16
|
||||
#define WINIX_ERR_EMPTY 17
|
||||
#define WINIX_ERR_SPAM 18
|
||||
#define WINIX_ERR_INCORRECT_REBUS 19
|
||||
|
||||
|
||||
|
||||
#define WINIX_ERR_NO_BOUNDARY 20
|
||||
#define WINIX_ERR_BROKEN_INPUT 21
|
||||
#define WINIX_ERR_INPUT_TOO_LARGE 22
|
||||
#define WINIX_ERR_CANT_CREATE_FILE 23
|
||||
|
||||
#define WINIX_ERR_NO_TICKET 24
|
||||
|
||||
//#define WINIX_ERR_PASSWORDS_DIFFERENT 25
|
||||
//#define WINIX_ERR_PASSWORD_TOO_SHORT 26
|
||||
//#define WINIX_ERR_USER_EXISTS 27
|
||||
//#define WINIX_ERR_LOGIN_EMPTY 28
|
||||
#define WINIX_DIFFERENT_MOUNT_POINTS 29
|
||||
|
||||
|
||||
#define WINIX_ERR_DB_FATAL_ERROR_DURING_CONNECTING 100
|
||||
#define WINIX_ERR_DB_INCORRECT_QUERY 101
|
||||
#define WINIX_ERR_DB_INCORRENT_RESULT_STATUS 102
|
||||
#define WINIX_ERR_DB_NO_COLUMN 103
|
||||
#define WINIX_ERR_DB_INCORRECT_LOGIN 104
|
||||
#define WINIX_ERR_DB_MORE_THAN_ONE_LOGIN 105
|
||||
#define WINIX_ERR_DB_ERR_CURRVAL 106
|
||||
|
||||
#define WINIX_ERR_FILE_EXPECTED 107
|
||||
#define WINIX_ERR_DIR_EXPECTED 108
|
||||
|
||||
//#define WINIX_ERR_UNKNOWN 1000
|
||||
#define WINIX_NOTHING_TO_DO 109
|
||||
#define WINIX_ERR_INCORRECT_URI 110
|
||||
|
||||
#define WINIX_ERR_INTERNAL_ERROR 2000
|
||||
|
||||
|
||||
typedef int Error;
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
74
winixd/core/group.h
Normal file
74
winixd/core/group.h
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_group
|
||||
#define headerfile_winix_core_group
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
struct Group
|
||||
{
|
||||
long id;
|
||||
std::wstring name; // group name
|
||||
std::vector<long> members; // users id
|
||||
|
||||
Group()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
void Clear()
|
||||
{
|
||||
id = -1;
|
||||
name.clear();
|
||||
members.clear();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
126
winixd/core/groups.cpp
Normal file
126
winixd/core/groups.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "groups.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
Groups::Groups()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
void Groups::Clear()
|
||||
{
|
||||
table.Clear();
|
||||
}
|
||||
|
||||
|
||||
void Groups::ReadGroups(Db * db)
|
||||
{
|
||||
Clear();
|
||||
|
||||
db->GetGroups(table);
|
||||
}
|
||||
|
||||
|
||||
Group * Groups::GetGroup(long group_id)
|
||||
{
|
||||
Table::Iterator i = table.FindId(group_id);
|
||||
|
||||
if( i == table.End() )
|
||||
return 0;
|
||||
|
||||
return &(*i);
|
||||
}
|
||||
|
||||
|
||||
Group * Groups::GetGroup(const std::wstring & name)
|
||||
{
|
||||
Table::Iterator i = table.FindName(name);
|
||||
|
||||
if( i == table.End() )
|
||||
return 0;
|
||||
|
||||
return &(*i);
|
||||
}
|
||||
|
||||
|
||||
long Groups::GetGroupId(const std::wstring & name)
|
||||
{
|
||||
Group * pgroup = GetGroup(name);
|
||||
|
||||
if( !pgroup )
|
||||
return -1;
|
||||
|
||||
return pgroup->id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Groups::Iterator Groups::Begin()
|
||||
{
|
||||
return table.Begin();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Groups::Iterator Groups::End()
|
||||
{
|
||||
return table.End();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Groups::SizeType Groups::Size()
|
||||
{
|
||||
return table.Size();
|
||||
}
|
||||
|
||||
|
||||
bool Groups::Remove(long group_id)
|
||||
{
|
||||
return table.Remove(group_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
81
winixd/core/groups.h
Normal file
81
winixd/core/groups.h
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_groups
|
||||
#define headerfile_winix_core_groups
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "group.h"
|
||||
#include "ugcontainer.h"
|
||||
#include "db/db.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class Groups
|
||||
{
|
||||
typedef UGContainer<Group> Table;
|
||||
|
||||
Table table;
|
||||
|
||||
public:
|
||||
|
||||
typedef Table::Iterator Iterator;
|
||||
typedef Table::SizeType SizeType;
|
||||
|
||||
Groups();
|
||||
void Clear();
|
||||
void ReadGroups(Db * db);
|
||||
Group * GetGroup(long group_id);
|
||||
Group * GetGroup(const std::wstring & name);
|
||||
long GetGroupId(const std::wstring & name);
|
||||
Iterator Begin();
|
||||
Iterator End();
|
||||
SizeType Size();
|
||||
bool Remove(long group_id);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
1524
winixd/core/htmlfilter.cpp
Normal file
1524
winixd/core/htmlfilter.cpp
Normal file
File diff suppressed because it is too large
Load Diff
346
winixd/core/htmlfilter.h
Normal file
346
winixd/core/htmlfilter.h
Normal file
@@ -0,0 +1,346 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_htmlfilter
|
||||
#define headerfile_winix_core_htmlfilter
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
// max length of a name of a html tag (with terminating null)
|
||||
#define WINIX_HTMLFILTER_ITEM_NAME_MAXLEN 30
|
||||
|
||||
// max length of a html lang attribute (e.g. "en", "pl")
|
||||
#define WINIX_HTMLFILTER_ITEM_LANG_MAXLEN 10
|
||||
|
||||
|
||||
#define WINIX_HTMLFILTER_ATTR_NAME_MAXLEN 40
|
||||
|
||||
|
||||
#define WINIX_HTMLFILTER_ATTR_VALUE_MAXLEN 500
|
||||
|
||||
|
||||
// depth of the html tree
|
||||
#define WINIX_HTMLFILTER_STACK_MAXLEN 100
|
||||
|
||||
// length of a buffer used for printing
|
||||
// it should be at least: WINIX_HTMLFILTER_ITEM_NAME_MAXLEN+3
|
||||
#define WINIX_HTMLFILTER_BUFFER_MAXLEN 2048
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
very lightweight filter for html
|
||||
(without using any dynamic memory - some memory is allocated only at the beginning - in ctors)
|
||||
this filter has O(n) complexity over the whole html string
|
||||
|
||||
such tags as: <script> <pre> <textarea> are treated in a special way
|
||||
all characters between the opening and closing tag (<script>....</script>) are untouched
|
||||
|
||||
if the filter finds that there are not closed tags it will close them,
|
||||
if the filter finds a closing tag which doesn't have an opening tag - it will skip it
|
||||
|
||||
tags which don't need to be closed: meta, input, br, img, link
|
||||
look at CheckExceptions() method
|
||||
|
||||
the filter recognizes xml simple tags (with / at the end) such as: <br />
|
||||
*/
|
||||
class HTMLFilter
|
||||
{
|
||||
public:
|
||||
|
||||
enum OrphanMode
|
||||
{
|
||||
orphan_nbsp, // putting " " string
|
||||
orphan_160space // putting 160 ascii code
|
||||
};
|
||||
|
||||
HTMLFilter();
|
||||
HTMLFilter(const HTMLFilter & f);
|
||||
HTMLFilter & operator=(const HTMLFilter & f);
|
||||
~HTMLFilter();
|
||||
|
||||
|
||||
// main methods used for filtering
|
||||
void Filter(const wchar_t * in, std::wstring & out);
|
||||
void Filter(const std::wstring & in, std::wstring & out);
|
||||
|
||||
|
||||
// insert a white space into long words
|
||||
// (only between html tags)
|
||||
// skipped in such tags: script, pre, textarea
|
||||
// break_after - after how many characters insert a space (0 - off)
|
||||
void BreakWord(size_t break_after_);
|
||||
|
||||
// insert a new line character into long lines
|
||||
// (only between html tags)
|
||||
// skipped in such tags: script, pre, textarea
|
||||
// wrap_line - after how many characters wrap a line (0 - off)
|
||||
// lines are wrapped only in 'body' tag (useful for text in 'title' tag which is in 'head' section)
|
||||
void WrapLine(size_t wrap_line_);
|
||||
|
||||
// trimming white characters (with new lines)
|
||||
// at the beginning, at the end and in the middle of a string
|
||||
// only between html tags
|
||||
// at the beginning and at the end only one space is left
|
||||
// skipped in such tags: script, pre, textarea
|
||||
// false by default
|
||||
void TrimWhite(bool trim);
|
||||
|
||||
|
||||
// first tabs in a tree
|
||||
// default: 2 (spaces)
|
||||
// set 0 to turn off
|
||||
void InsertTabs(size_t tabsize);
|
||||
|
||||
|
||||
// set a name of a html tag which will be used as 'nofilter' tag
|
||||
// elements between such tags are not filtered (similarly as in <pre> and <textarea>)
|
||||
// these tags (opening and closing) will no be placed in the html output
|
||||
void SetNoFilterTag(const std::wstring & tag_name);
|
||||
|
||||
// orphans are checked only in 'body' tag
|
||||
void AssignOrphans(const wchar_t * lang_code, const std::vector<std::wstring> & otab);
|
||||
void AssignOrphans(const std::wstring & lang_code, const std::vector<std::wstring> & otab);
|
||||
void ClearOrphans();
|
||||
|
||||
|
||||
// check 'orphans' for the specicic language
|
||||
// if an orphan is detected then the non-break space (" " or ascii 160 code) will be put
|
||||
// default disable (lang_none)
|
||||
void OrphansMode(OrphanMode mode = orphan_nbsp);
|
||||
|
||||
|
||||
// skipping some unsafe tags
|
||||
// (script, iframe, frame, frameset, applet, head, meta, html, link, body, ...)
|
||||
void SafeMode(bool safe_mode_);
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
// orphans for one language
|
||||
struct Orphans
|
||||
{
|
||||
std::vector<std::wstring> tab;
|
||||
size_t max_len;
|
||||
};
|
||||
|
||||
|
||||
// orphans for all languages
|
||||
// map<language_code, Orphans>
|
||||
typedef std::map<std::wstring, Orphans> OrphansTab;
|
||||
OrphansTab orphans_tab;
|
||||
|
||||
// html <nofilter> tag name
|
||||
std::wstring no_filter_tag;
|
||||
|
||||
|
||||
struct Item
|
||||
{
|
||||
std::wstring name; // max size: WINIX_HTMLFILTER_ITEM_NAME_MAXLEN
|
||||
|
||||
enum Type
|
||||
{
|
||||
opening, /* sample: <h1> */
|
||||
closing, /* sample: </h1> */
|
||||
simple, /* sample: <br/> */
|
||||
special, /* sample: <!doctype> */
|
||||
none
|
||||
} type;
|
||||
|
||||
// is there a new line after this tag
|
||||
bool new_line;
|
||||
|
||||
// current orphans table
|
||||
// (will be propagated)
|
||||
Orphans * porphans;
|
||||
|
||||
// this item or one from its parents is a 'body' html tag
|
||||
// (will be propagated)
|
||||
bool has_body_tag;
|
||||
|
||||
void Clear();
|
||||
Item();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
virtual methods
|
||||
*/
|
||||
virtual void Init();
|
||||
virtual void Uninit();
|
||||
|
||||
virtual bool IsOpeningTagMark();
|
||||
virtual bool IsOpeningCommentaryTagMark();
|
||||
virtual bool IsClosingTagMark();
|
||||
virtual bool IsClosingXmlSimpleTagMark();
|
||||
|
||||
virtual bool IsValidCharForName(int c);
|
||||
virtual bool IsValidCharForAttrName(int c);
|
||||
virtual void CheckExceptions();
|
||||
virtual bool SkipCommentaryTagIfExists();
|
||||
|
||||
virtual void Put(wchar_t c);
|
||||
virtual void Put(const wchar_t * str);
|
||||
virtual void Put(const wchar_t * str, const wchar_t * end);
|
||||
virtual void Put(const std::wstring & str);
|
||||
|
||||
virtual void PutOpeningTagMark();
|
||||
virtual void PutClosingTagMark();
|
||||
virtual bool PutOpeningTag();
|
||||
virtual void PutClosingTag(const wchar_t * tag);
|
||||
|
||||
virtual void PutNormalText(const wchar_t * str, const wchar_t * end);
|
||||
virtual void ReadNormalTextSkipWhite(const wchar_t * & start, const wchar_t * & last_non_white);
|
||||
|
||||
|
||||
/*
|
||||
others
|
||||
*/
|
||||
Item & GetItem(size_t i);
|
||||
Item & LastItem();
|
||||
|
||||
wchar_t ToLower(wchar_t c);
|
||||
void ToLower(std::wstring & str);
|
||||
|
||||
bool IsNameEqual(const wchar_t * name1, const wchar_t * name2);
|
||||
bool IsNameEqual(const wchar_t * name1, const std::wstring & name2);
|
||||
bool IsNameEqual(const std::wstring & name1, const wchar_t * name2);
|
||||
bool IsNameEqual(const std::wstring & name1, const std::wstring & name2);
|
||||
|
||||
bool IsNameEqual(const wchar_t * name1, const wchar_t * name2, size_t len);
|
||||
bool IsNameEqual(const wchar_t * name1, const std::wstring & name2, size_t len);
|
||||
bool IsNameEqual(const std::wstring & name1, const wchar_t * name2, size_t len);
|
||||
bool IsNameEqual(const std::wstring & name1, const std::wstring & name2, size_t len);
|
||||
|
||||
bool IsLastTag(const wchar_t * name);
|
||||
bool IsLastTag(const std::wstring & name);
|
||||
bool IsTagSafe(const wchar_t * tag);
|
||||
bool IsTagSafe(const std::wstring & tag);
|
||||
|
||||
int CheckOrphan(const wchar_t * str, const wchar_t * end, const std::wstring & orphan_str);
|
||||
bool CheckOrphan(const wchar_t * str, const wchar_t * end, const std::vector<std::wstring> & orphans);
|
||||
bool CheckOrphan(const wchar_t * str, const wchar_t * end);
|
||||
|
||||
bool IsWhite(int c);
|
||||
void SkipWhite();
|
||||
void SkipWhiteLines();
|
||||
void SkipWhiteWithFirstNewLine();
|
||||
void SkipWhiteLines(const wchar_t * & str, const wchar_t * end);
|
||||
bool IsClosingTagForLastItem();
|
||||
size_t OpeningCommentaryTagMarkSize();
|
||||
void SkipAndCheckClosingTag();
|
||||
|
||||
void PopStack();
|
||||
bool PushStack();
|
||||
void CheckNewLine();
|
||||
void CheckStackPrintRest();
|
||||
void AddForgottenTags();
|
||||
void CheckClosingTags();
|
||||
void ReadNormalText();
|
||||
bool PrintRest();
|
||||
bool PrintOpeningItem();
|
||||
void ReadItemName();
|
||||
void ReadItemAttrName();
|
||||
void ReadItemAttrValue(bool has_quote);
|
||||
|
||||
bool ReadItemAttr();
|
||||
bool CheckItemAttr();
|
||||
void PrintItemAttr();
|
||||
|
||||
void ReadItemClosing();
|
||||
void ReadItemSpecial();
|
||||
void ReadItemOpening();
|
||||
bool ReadItem();
|
||||
void ReadLoop();
|
||||
void Read();
|
||||
|
||||
void CheckChar(wchar_t c);
|
||||
|
||||
void CheckLineWrap();
|
||||
bool HasSemiloconAround(const wchar_t * str, const wchar_t * end);
|
||||
void PutNormalNonWhite(const wchar_t * & str, const wchar_t * end);
|
||||
void PutNormalWhite(const wchar_t * & str, const wchar_t * end);
|
||||
void PutLastTagWithClosingTag();
|
||||
void PutTextBetweenLastTagWithClosingTag();
|
||||
void PutTabs(size_t len);
|
||||
void PutNonBreakingSpace();
|
||||
void PutNewLine();
|
||||
void CalcOrphansMaxLen(Orphans & orphans);
|
||||
|
||||
const wchar_t * pchar;
|
||||
Item empty;
|
||||
Item * pstack; // stack pointer
|
||||
size_t stack_len; // length of the stack
|
||||
wchar_t * buffer; // buffer used when printing
|
||||
std::wstring * out_string;
|
||||
bool last_new_line;
|
||||
size_t break_after; // insert a space into long words after 'break_after' characters
|
||||
size_t wrap_line; // insert a new line character into long lines
|
||||
bool trim_white; // trimming white characters
|
||||
size_t tab_size;
|
||||
OrphanMode orphan_mode;
|
||||
std::wstring attr_name;
|
||||
std::vector<std::wstring> attr_value;
|
||||
std::wstring attr_value_temp;
|
||||
std::wstring attr_value_lower;
|
||||
bool attr_has_value;
|
||||
std::wstring lang_code_lower;
|
||||
size_t line_len; //length of the current line (without first spaces which create the html tree)
|
||||
bool safe_mode; // skipping some unsafe tags
|
||||
Orphans orphans_temp;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
273
winixd/core/httpsimpleparser.cpp
Normal file
273
winixd/core/httpsimpleparser.cpp
Normal file
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "httpsimpleparser.h"
|
||||
#include "misc.h"
|
||||
#include "utf8/utf8.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
void HttpSimpleParser::ToLower(std::wstring & s)
|
||||
{
|
||||
for(wchar_t & c : s)
|
||||
{
|
||||
if( c>='A' && c<='Z' )
|
||||
c = c - 'A' + 'a';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool HttpSimpleParser::IsWhite(int c)
|
||||
{
|
||||
if( c==' ' || c=='\t' )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void HttpSimpleParser::SkipWhiteChars()
|
||||
{
|
||||
while( IsWhite(last_c) )
|
||||
last_c = GetChar();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int HttpSimpleParser::ParseHalfHex(int c)
|
||||
{
|
||||
if( c>='a' && c<='z' )
|
||||
c += 'A' - 'a'; // to upper case
|
||||
|
||||
|
||||
if( c >= 'A' )
|
||||
c = c - 'A' + 10;
|
||||
else
|
||||
c = c - '0';
|
||||
|
||||
c &= 0xf;
|
||||
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void HttpSimpleParser::CheckSpecialChar()
|
||||
{
|
||||
if( last_c == '%' )
|
||||
{
|
||||
int c1 = GetChar();
|
||||
int c2 = GetChar();
|
||||
|
||||
if( c1==-1 || c2==-1 )
|
||||
{
|
||||
last_c = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
c1 = ParseHalfHex(c1);
|
||||
c2 = ParseHalfHex(c2);
|
||||
|
||||
last_c = (c1 << 4) + c2;
|
||||
}
|
||||
}
|
||||
else
|
||||
if( last_c == '+' )
|
||||
{
|
||||
last_c = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void HttpSimpleParser::ReadName()
|
||||
{
|
||||
// we're checking 'separator' and '=' because the string is allowed not having '=' (the value is optional)
|
||||
|
||||
utf8_token.clear();
|
||||
last_name.clear();
|
||||
|
||||
for( ; last_c!=-1 && last_c!=separator && last_c!='=' ; last_c = GetChar() )
|
||||
{
|
||||
if( recognize_special_chars )
|
||||
CheckSpecialChar();
|
||||
|
||||
if( last_c != -1 )
|
||||
{
|
||||
if( getchar_returns_utf8_chars )
|
||||
utf8_token += last_c;
|
||||
else
|
||||
last_name += last_c;
|
||||
}
|
||||
}
|
||||
|
||||
if( getchar_returns_utf8_chars )
|
||||
PT::UTF8ToWide(utf8_token, last_name);
|
||||
|
||||
if( last_c == '=' )
|
||||
last_c = GetChar();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void HttpSimpleParser::ReadQuotedValue()
|
||||
{
|
||||
// skipping '"'
|
||||
last_c = GetChar();
|
||||
utf8_token.clear();
|
||||
last_value.clear();
|
||||
|
||||
for( ; last_c!=-1 && last_c!='"' ; last_c = GetChar() )
|
||||
{
|
||||
if( recognize_special_chars )
|
||||
CheckSpecialChar();
|
||||
|
||||
if( last_c != -1 )
|
||||
{
|
||||
if( getchar_returns_utf8_chars )
|
||||
utf8_token += last_c;
|
||||
else
|
||||
last_value += last_c;
|
||||
}
|
||||
}
|
||||
|
||||
if( getchar_returns_utf8_chars )
|
||||
PT::UTF8ToWide(utf8_token, last_value);
|
||||
|
||||
if( last_c == '"' )
|
||||
last_c = GetChar();
|
||||
|
||||
// looking for a separator (skipping)
|
||||
for( ; last_c!=-1 && last_c!=separator ; last_c = GetChar() );
|
||||
|
||||
}
|
||||
|
||||
|
||||
void HttpSimpleParser::ReadNormalValue()
|
||||
{
|
||||
utf8_token.clear();
|
||||
last_value.clear();
|
||||
|
||||
for( ; last_c!=-1 && last_c!=separator ; last_c = GetChar() )
|
||||
{
|
||||
if( recognize_special_chars )
|
||||
CheckSpecialChar();
|
||||
|
||||
if( last_c != -1 )
|
||||
{
|
||||
if( getchar_returns_utf8_chars )
|
||||
utf8_token += last_c;
|
||||
else
|
||||
last_value += last_c;
|
||||
}
|
||||
}
|
||||
|
||||
if( getchar_returns_utf8_chars )
|
||||
PT::UTF8ToWide(utf8_token, last_value);
|
||||
}
|
||||
|
||||
|
||||
void HttpSimpleParser::ReadValue()
|
||||
{
|
||||
if( skip_white_chars )
|
||||
SkipWhiteChars();
|
||||
|
||||
if( value_can_be_quoted && last_c == '"' )
|
||||
ReadQuotedValue();
|
||||
else
|
||||
ReadNormalValue();
|
||||
|
||||
|
||||
if( last_c == separator )
|
||||
last_c = GetChar();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* there can be some important values like passwords so its better
|
||||
* to clear them now
|
||||
*/
|
||||
void HttpSimpleParser::Clear()
|
||||
{
|
||||
Overwrite(last_name);
|
||||
Overwrite(last_value);
|
||||
Overwrite(utf8_token);
|
||||
last_name.clear();
|
||||
last_value.clear();
|
||||
utf8_token.clear();
|
||||
}
|
||||
|
||||
|
||||
void HttpSimpleParser::Parse()
|
||||
{
|
||||
for( last_c = GetChar() ; last_c != -1 ; )
|
||||
{
|
||||
last_name.clear();
|
||||
last_value.clear();
|
||||
|
||||
if( read_name )
|
||||
ReadName();
|
||||
|
||||
ReadValue();
|
||||
|
||||
if( skip_white_chars )
|
||||
{
|
||||
TrimWhite(last_name);
|
||||
TrimWhite(last_value);
|
||||
}
|
||||
|
||||
Parameter(last_name, last_value); // user definied function
|
||||
}
|
||||
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
107
winixd/core/httpsimpleparser.h
Normal file
107
winixd/core/httpsimpleparser.h
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_httpsimpleparser
|
||||
#define headerfile_winix_core_httpsimpleparser
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class HttpSimpleParser
|
||||
{
|
||||
protected:
|
||||
|
||||
bool read_name;
|
||||
bool value_can_be_quoted;
|
||||
bool skip_white_chars;
|
||||
bool recognize_special_chars;
|
||||
|
||||
// if false then GetChar() returns wide characters (converted to int)
|
||||
// if true then GetChar() returns utf8 characters (we have to convert them from utf8 to wide chars)
|
||||
bool getchar_returns_utf8_chars;
|
||||
|
||||
int ParseHalfHex(int c);
|
||||
void ReadName();
|
||||
void ReadQuotedValue();
|
||||
void ReadNormalValue();
|
||||
void ReadValue();
|
||||
void Clear();
|
||||
|
||||
std::wstring last_name;
|
||||
std::wstring last_value;
|
||||
std::string utf8_token;
|
||||
|
||||
int last_c;
|
||||
int separator;
|
||||
|
||||
// '-1' means end (eof)
|
||||
// when there is an eof this method can be called more than once (it should always return -1 in such a case)
|
||||
virtual int GetChar() = 0;
|
||||
virtual void Parameter(std::wstring & last_name, std::wstring & last_value) = 0;
|
||||
|
||||
void ToLower(std::wstring & s);
|
||||
bool IsWhite(int c);
|
||||
|
||||
void SkipWhiteChars();
|
||||
void CheckSpecialChar();
|
||||
|
||||
void Parse();
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
HttpSimpleParser()
|
||||
{
|
||||
separator = '&';
|
||||
read_name = true;
|
||||
value_can_be_quoted = false;
|
||||
skip_white_chars = false;
|
||||
recognize_special_chars = true;
|
||||
getchar_returns_utf8_chars = false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
631
winixd/core/image.cpp
Normal file
631
winixd/core/image.cpp
Normal file
@@ -0,0 +1,631 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <ctime>
|
||||
#include "image.h"
|
||||
#include "utf8/utf8.h"
|
||||
#include "log.h"
|
||||
#include "system.h"
|
||||
#include "plugin.h"
|
||||
#include "lock.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
void Image::SetDb(Db * pdb)
|
||||
{
|
||||
db = pdb;
|
||||
}
|
||||
|
||||
|
||||
void Image::SetConfig(Config * pconfig)
|
||||
{
|
||||
config = pconfig;
|
||||
}
|
||||
|
||||
|
||||
void Image::SetSystem(System * psystem)
|
||||
{
|
||||
system = psystem;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// first thread (objects locked)
|
||||
Image::Scale Image::GetImageScale(long dir_id)
|
||||
{
|
||||
Scale scale;
|
||||
|
||||
Mount * m = system->mounts.CalcMount(dir_id);
|
||||
scale.cx = config->image_cx;
|
||||
scale.cy = config->image_cy;
|
||||
scale.aspect_mode = config->image_mode;
|
||||
scale.quality = config->image_quality;
|
||||
|
||||
// reading width and height from the mount point (if exists)
|
||||
int index = system->mounts.MountParImageSize();
|
||||
|
||||
if( m && m->param[index].defined && m->param[index].arg.size() == 2 )
|
||||
{
|
||||
scale.cx = Tol(m->param[index].arg[0]);
|
||||
scale.cy = Tol(m->param[index].arg[1]);
|
||||
}
|
||||
|
||||
// reading image mode from the mount point (if exists)
|
||||
index = system->mounts.MountParImageMode();
|
||||
|
||||
if( m && m->param[index].defined && m->param[index].arg.size() == 1 )
|
||||
scale.aspect_mode = Toi(m->param[index].arg[0]);
|
||||
|
||||
// reading image quality from the mount point (if exists)
|
||||
index = system->mounts.MountParImageQuality();
|
||||
|
||||
if( m && m->param[index].defined && m->param[index].arg.size() == 1 )
|
||||
scale.quality = Toi(m->param[index].arg[0]);
|
||||
|
||||
return scale;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// first thread (objects locked)
|
||||
Image::Scale Image::GetThumbScale(long dir_id)
|
||||
{
|
||||
Scale scale;
|
||||
|
||||
Mount * m = system->mounts.CalcMount(dir_id);
|
||||
scale.cx = config->thumb_cx;
|
||||
scale.cy = config->thumb_cy;
|
||||
scale.aspect_mode = config->thumb_mode;
|
||||
scale.quality = config->thumb_quality;
|
||||
|
||||
// reading width and height from the mount point (if exists)
|
||||
int index = system->mounts.MountParThumbSize();
|
||||
|
||||
if( m && m->param[index].defined && m->param[index].arg.size() == 2 )
|
||||
{
|
||||
scale.cx = Tol(m->param[index].arg[0]);
|
||||
scale.cy = Tol(m->param[index].arg[1]);
|
||||
}
|
||||
|
||||
// reading thumb mode from the mount point (if exists)
|
||||
index = system->mounts.MountParThumbMode();
|
||||
|
||||
if( m && m->param[index].defined && m->param[index].arg.size() == 1 )
|
||||
scale.aspect_mode = Toi(m->param[index].arg[0]);
|
||||
|
||||
// reading image quality from the mount point (if exists)
|
||||
index = system->mounts.MountParThumbQuality();
|
||||
|
||||
if( m && m->param[index].defined && m->param[index].arg.size() == 1 )
|
||||
scale.quality = Toi(m->param[index].arg[0]);
|
||||
|
||||
return scale;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// first thread (objects locked)
|
||||
void Image::Resize(long file_id, size_t cx, size_t cy, int aspect_mode, int quality)
|
||||
{
|
||||
item_temp.type = WINIX_IMAGE_TYPE_RESIZE;
|
||||
item_temp.file_id = file_id;
|
||||
item_temp.cx = cx;
|
||||
item_temp.cy = cy;
|
||||
item_temp.aspect_mode = aspect_mode;
|
||||
item_temp.quality = quality;
|
||||
|
||||
CheckParam(item_temp);
|
||||
image_tab.insert(image_tab.end(), item_temp);
|
||||
WakeUpThread();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// first thread (objects locked)
|
||||
void Image::CreateThumb(long file_id, size_t thumb_cx, size_t thumb_cy, int aspect_mode, int quality)
|
||||
{
|
||||
item_temp.type = WINIX_IMAGE_TYPE_CREATE_THUMB;
|
||||
item_temp.file_id = file_id;
|
||||
item_temp.thumb_cx = thumb_cx;
|
||||
item_temp.thumb_cy = thumb_cy;
|
||||
item_temp.aspect_mode = aspect_mode;
|
||||
item_temp.quality = quality;
|
||||
|
||||
CheckParam(item_temp);
|
||||
image_tab.insert(image_tab.end(), item_temp);
|
||||
WakeUpThread();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// first thread (objects locked)
|
||||
void Image::Crop(long file_id, size_t xoffset, size_t yoffset, size_t cx, size_t cy, int quality)
|
||||
{
|
||||
item_temp.type = WINIX_IMAGE_TYPE_CROP;
|
||||
item_temp.file_id = file_id;
|
||||
item_temp.xoffset = xoffset;
|
||||
item_temp.yoffset = yoffset;
|
||||
item_temp.cx = cx;
|
||||
item_temp.cy = cy;
|
||||
item_temp.quality = quality;
|
||||
|
||||
CheckParam(item_temp);
|
||||
image_tab.insert(image_tab.end(), item_temp);
|
||||
WakeUpThread();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// first thread (objects locked)
|
||||
void Image::CropThumb(long file_id, size_t xoffset, size_t yoffset, size_t cx, size_t cy, int quality)
|
||||
{
|
||||
item_temp.type = WINIX_IMAGE_TYPE_CROP_THUMB;
|
||||
item_temp.file_id = file_id;
|
||||
item_temp.xoffset = xoffset;
|
||||
item_temp.yoffset = yoffset;
|
||||
item_temp.cx = cx;
|
||||
item_temp.cy = cy;
|
||||
item_temp.quality = quality;
|
||||
|
||||
CheckParam(item_temp);
|
||||
image_tab.insert(image_tab.end(), item_temp);
|
||||
WakeUpThread();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// first thread (objects locked)
|
||||
void Image::CropNewThumb(long file_id, size_t xoffset, size_t yoffset, size_t cx, size_t cy,
|
||||
size_t thumb_cx, size_t thumb_cy, int aspect_mode, int quality)
|
||||
{
|
||||
item_temp.type = WINIX_IMAGE_TYPE_CREATE_CROP_NEW_THUMB;
|
||||
item_temp.file_id = file_id;
|
||||
item_temp.xoffset = xoffset;
|
||||
item_temp.yoffset = yoffset;
|
||||
item_temp.cx = cx;
|
||||
item_temp.cy = cy;
|
||||
item_temp.thumb_cx = thumb_cx;
|
||||
item_temp.thumb_cy = thumb_cy;
|
||||
item_temp.aspect_mode = aspect_mode;
|
||||
item_temp.quality = quality;
|
||||
|
||||
CheckParam(item_temp);
|
||||
image_tab.insert(image_tab.end(), item_temp);
|
||||
WakeUpThread();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Image::CheckParam(ImageItem & item)
|
||||
{
|
||||
SetMinMax(item.aspect_mode, 1, 7);
|
||||
SetMinMax(item.quality, 0, 100);
|
||||
SetMinMax(item.cx, 1, 30000);
|
||||
SetMinMax(item.cy, 1, 30000);
|
||||
SetMinMax(item.thumb_cx, 1, 30000);
|
||||
SetMinMax(item.thumb_cy, 1, 30000);
|
||||
SetMinMax(item.xoffset, 0, 30000);
|
||||
SetMinMax(item.yoffset, 0, 30000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread (objects locked)
|
||||
bool Image::SignalReceived()
|
||||
{
|
||||
return !image_tab.empty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread (objects not locked)
|
||||
void Image::Do()
|
||||
{
|
||||
ImageTab::iterator i;
|
||||
bool end;
|
||||
|
||||
Lock();
|
||||
i = image_tab.begin();
|
||||
Unlock();
|
||||
|
||||
do
|
||||
{
|
||||
class Lock lock_object(synchro);
|
||||
|
||||
if( i != image_tab.end() )
|
||||
{
|
||||
item_work = *i;
|
||||
image_tab.erase(i++);
|
||||
end = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
end = true;
|
||||
}
|
||||
|
||||
lock_object.Unlock();
|
||||
|
||||
if( !end )
|
||||
CreateImage();
|
||||
|
||||
}
|
||||
while( !end && !IsExitSignal() );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Image::Add(const std::wstring & in, TextStream<std::string> & out)
|
||||
{
|
||||
PT::WideToUTF8(in, add_tempa);
|
||||
out << add_tempa;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Image::EscapePath(const std::string & path, TextStream<std::string> & out, bool clear_stream)
|
||||
{
|
||||
if( clear_stream )
|
||||
out.Clear();
|
||||
|
||||
out << '"';
|
||||
|
||||
for(size_t i=0 ; i<path.size() ; ++i)
|
||||
{
|
||||
if( path[i] == '"' )
|
||||
out << '\\';
|
||||
|
||||
if( path[i] != 0 )
|
||||
out << path[i];
|
||||
}
|
||||
|
||||
out << '\"';
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
from: http://www.imagemagick.org/script/command-line-processing.php#geometry
|
||||
|
||||
scale% Height and width both scaled by specified percentage.
|
||||
scale-x%xscale-y% Height and width individually scaled by specified percentages. (Only one % symbol needed.)
|
||||
width Width given, height automagically selected to preserve aspect ratio.
|
||||
xheight Height given, width automagically selected to preserve aspect ratio.
|
||||
widthxheight Maximum values of height and width given, aspect ratio preserved.
|
||||
widthxheight^ Minimum values of width and height given, aspect ratio preserved.
|
||||
widthxheight! Width and height emphatically given, original aspect ratio ignored.
|
||||
widthxheight> Change as per widthxheight but only if an image dimension exceeds a specified dimension.
|
||||
widthxheight< Change dimensions only if both image dimensions exceed specified dimensions.
|
||||
*/
|
||||
void Image::SelectAspect(size_t cx, size_t cy)
|
||||
{
|
||||
switch( item_work.aspect_mode )
|
||||
{
|
||||
case WINIX_IMAGE_MODE_1:
|
||||
command << cx;
|
||||
break;
|
||||
|
||||
case WINIX_IMAGE_MODE_3:
|
||||
command << cx << "x" << cy;
|
||||
break;
|
||||
|
||||
case WINIX_IMAGE_MODE_4:
|
||||
command << '"' << cx << "x" << cy << "^\"";
|
||||
break;
|
||||
|
||||
case WINIX_IMAGE_MODE_5:
|
||||
command << '"' << cx << "x" << cy << "!\"";
|
||||
break;
|
||||
|
||||
case WINIX_IMAGE_MODE_6:
|
||||
command << '"' << cx << "x" << cy << ">\"";
|
||||
break;
|
||||
|
||||
case WINIX_IMAGE_MODE_7:
|
||||
command << '"' << cx << "x" << cy << "<\"";
|
||||
break;
|
||||
|
||||
case WINIX_IMAGE_MODE_2:
|
||||
default:
|
||||
command << "x" << cy;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread (objects locked)
|
||||
bool Image::CreateInputFileName()
|
||||
{
|
||||
bool thumb = (item_work.type == WINIX_IMAGE_TYPE_CROP_THUMB);
|
||||
|
||||
if( thumb && !file_work.has_thumb )
|
||||
{
|
||||
log << log1 << "Image: file id: " << file_work.id << ", url: " << file_work.url
|
||||
<< " doesn't have a thumbnail yet (skipping)" << logend;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if( system->MakeFilePath(file_work, src_path, thumb) )
|
||||
{
|
||||
PT::WideToUTF8(src_path, input_file_name);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "Image: cannot create a source path" << logend;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread (objects locked)
|
||||
void Image::CreateTmpFileName()
|
||||
{
|
||||
stream_tmp_path.Clear();
|
||||
stream_tmp_path << config->upload_dir << L"/tmp/image_" << std::time(0);
|
||||
PT::WideToUTF8(stream_tmp_path.Str(), tmp_file_name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread (objects are not locked)
|
||||
bool Image::CreateCommand()
|
||||
{
|
||||
class Lock lock_object(synchro);
|
||||
|
||||
iq.SetAll(true, false);
|
||||
iq.WhereId(item_work.file_id);
|
||||
|
||||
// the file could have been changed especially when there is a long queue of files
|
||||
if( db->GetItem(file_work, iq) != WINIX_ERR_OK )
|
||||
return false;
|
||||
|
||||
if( !CreateInputFileName() )
|
||||
return false;
|
||||
|
||||
command.Clear();
|
||||
Add(config->convert_cmd, command);
|
||||
|
||||
command << " ";
|
||||
EscapePath(input_file_name, command, false);
|
||||
command << " -quiet -quality " << item_work.quality;
|
||||
|
||||
if( item_work.type == WINIX_IMAGE_TYPE_RESIZE )
|
||||
{
|
||||
command << " -resize ";
|
||||
SelectAspect(item_work.cx, item_work.cy);
|
||||
}
|
||||
else
|
||||
if( item_work.type == WINIX_IMAGE_TYPE_CREATE_THUMB )
|
||||
{
|
||||
command << " -strip -thumbnail ";
|
||||
SelectAspect(item_work.thumb_cx, item_work.thumb_cy);
|
||||
}
|
||||
else
|
||||
if( item_work.type == WINIX_IMAGE_TYPE_CROP )
|
||||
{
|
||||
command << " -crop " << item_work.cx << "x" << item_work.cy
|
||||
<< "+" << item_work.xoffset << "+" << item_work.yoffset << " +repage ";
|
||||
}
|
||||
else
|
||||
if( item_work.type == WINIX_IMAGE_TYPE_CROP_THUMB )
|
||||
{
|
||||
command << " -strip -crop " << item_work.cx << "x" << item_work.cy
|
||||
<< "+" << item_work.xoffset << "+" << item_work.yoffset
|
||||
<< " +repage ";
|
||||
}
|
||||
else
|
||||
if( item_work.type == WINIX_IMAGE_TYPE_CREATE_CROP_NEW_THUMB )
|
||||
{
|
||||
command << " -strip -crop " << item_work.cx << "x" << item_work.cy
|
||||
<< "+" << item_work.xoffset << "+" << item_work.yoffset
|
||||
<< " +repage -thumbnail ";
|
||||
SelectAspect(item_work.thumb_cx, item_work.thumb_cy);
|
||||
}
|
||||
|
||||
CreateTmpFileName();
|
||||
|
||||
command << " ";
|
||||
EscapePath(tmp_file_name, command, false);
|
||||
|
||||
log << log4 << "Image: running: " << command.Str() << logend;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// second thread (objects are locked)
|
||||
void Image::ImageSavedCorrectly()
|
||||
{
|
||||
if( item_work.type == WINIX_IMAGE_TYPE_CREATE_THUMB )
|
||||
{
|
||||
if( !file_work.has_thumb )
|
||||
{
|
||||
file_work.has_thumb = true;
|
||||
db->EditHasThumbById(true, file_work.id);
|
||||
}
|
||||
|
||||
log << log3 << "Image: generated a thumbnail: " << dst_path << logend;
|
||||
plugin.Call((Session*)0, WINIX_CREATED_THUMB, &file_work);
|
||||
}
|
||||
else
|
||||
if( item_work.type == WINIX_IMAGE_TYPE_RESIZE )
|
||||
{
|
||||
log << log3 << "Image: image resized: " << dst_path << logend;
|
||||
plugin.Call((Session*)0, WINIX_IMAGE_RESIZED, &file_work);
|
||||
}
|
||||
else
|
||||
if( item_work.type == WINIX_IMAGE_TYPE_CROP )
|
||||
{
|
||||
log << log3 << "Image: image cropped: " << dst_path << logend;
|
||||
// !! IMPROVE ME add a correct message
|
||||
//plugin.Call((Session*)0, WINIX_IMAGE_RESIZED, &file_work);
|
||||
}
|
||||
else
|
||||
if( item_work.type == WINIX_IMAGE_TYPE_CROP_THUMB )
|
||||
{
|
||||
log << log3 << "Image: image thumbnail cropped: " << dst_path << logend;
|
||||
// !! IMPROVE ME add a correct message
|
||||
//plugin.Call((Session*)0, WINIX_IMAGE_RESIZED, &file_work);
|
||||
}
|
||||
else
|
||||
if( item_work.type == WINIX_IMAGE_TYPE_CREATE_CROP_NEW_THUMB )
|
||||
{
|
||||
log << log3 << "Image: a new thumbnail from an original image was cropped: " << dst_path << logend;
|
||||
// !! IMPROVE ME add a correct message
|
||||
//plugin.Call((Session*)0, WINIX_IMAGE_RESIZED, &file_work);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// second thread (objects are not locked)
|
||||
void Image::SaveImage()
|
||||
{
|
||||
class Lock lock_object(synchro);
|
||||
|
||||
// the file could have been changed especially when creating the image lasted too long
|
||||
iq.SetAll(true, false);
|
||||
iq.WhereId(item_work.file_id);
|
||||
|
||||
if( db->GetItem(file_work, iq) == WINIX_ERR_OK )
|
||||
{
|
||||
bool thumb = (item_work.type == WINIX_IMAGE_TYPE_CREATE_THUMB ||
|
||||
item_work.type == WINIX_IMAGE_TYPE_CROP_THUMB ||
|
||||
item_work.type == WINIX_IMAGE_TYPE_CREATE_CROP_NEW_THUMB );
|
||||
|
||||
if( system->MakeFilePath(file_work, dst_path, thumb, true, config->upload_dirs_chmod, config->upload_group_int) )
|
||||
{
|
||||
if( RenameFile(stream_tmp_path.Str(), dst_path) )
|
||||
{
|
||||
// it doesn't matter for us if there is an error when chmod/chown on a file
|
||||
// the admin (root) will correct it
|
||||
SetPriv(dst_path, config->upload_files_chmod, config->upload_group_int);
|
||||
ImageSavedCorrectly();
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "Image: cannot move a temporary file: " << stream_tmp_path.Str()
|
||||
<< ", to: " << dst_path << logend;
|
||||
|
||||
Winix::RemoveFile(stream_tmp_path.Str());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "Image: cannot create a destination path" << logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread (objects are not locked)
|
||||
void Image::CreateImage()
|
||||
{
|
||||
if( CreateCommand() )
|
||||
{
|
||||
int res = std::system(command.CStr());
|
||||
|
||||
if( res == 0 )
|
||||
{
|
||||
SaveImage();
|
||||
}
|
||||
else
|
||||
{
|
||||
class Lock lock_object(synchro);
|
||||
log << log3 << "Image: some problems with creating an image"
|
||||
<< ", 'convert' process returned: " << res << logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread (objects are not locked)
|
||||
// !! there is a problem with GIF files
|
||||
// Bus error (core dumped)
|
||||
/*
|
||||
#include "wand/MagickWand.h"
|
||||
|
||||
// compiler options:
|
||||
// include: -I/usr/local/include/ImageMagick
|
||||
// link with: `MagickWand-config --ldflags --libs`
|
||||
|
||||
void Image::CreateThumbnail()
|
||||
{
|
||||
PT::WideToUTF8(item_work.source, sourcea);
|
||||
PT::WideToUTF8(item_work.dst, dsta);
|
||||
|
||||
MagickWandGenesis();
|
||||
|
||||
MagickWand * wand = NewMagickWand();
|
||||
|
||||
if( MagickReadImage(wand, sourcea.c_str()) )
|
||||
{
|
||||
MagickThumbnailImage(wand, item_work.cx, item_work.cy);
|
||||
|
||||
if( MagickWriteImage(wand, dsta.c_str()) )
|
||||
{
|
||||
Lock();
|
||||
log << log3 << "Image: created a thumbnail: " << dsta << logend;
|
||||
Unlock();
|
||||
}
|
||||
}
|
||||
|
||||
DestroyMagickWand(wand);
|
||||
|
||||
MagickWandTerminus();
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
215
winixd/core/image.h
Normal file
215
winixd/core/image.h
Normal file
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_image
|
||||
#define headerfile_winix_core_image
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include "basethread.h"
|
||||
#include "textstream.h"
|
||||
#include "db/db.h"
|
||||
#include "core/item.h"
|
||||
#include "core/config.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class System;
|
||||
|
||||
|
||||
// aspect modes:
|
||||
// Width given, height automagically selected to preserve aspect ratio.
|
||||
#define WINIX_IMAGE_MODE_1 1
|
||||
|
||||
// Height given, width automagically selected to preserve aspect ratio.
|
||||
#define WINIX_IMAGE_MODE_2 2
|
||||
|
||||
// Maximum values of height and width given, aspect ratio preserved.
|
||||
#define WINIX_IMAGE_MODE_3 3
|
||||
|
||||
// Minimum values of width and height given, aspect ratio preserved.
|
||||
#define WINIX_IMAGE_MODE_4 4
|
||||
|
||||
// Width and height emphatically given, original aspect ratio ignored.
|
||||
#define WINIX_IMAGE_MODE_5 5
|
||||
|
||||
// Change as per widthxheight but only if an image dimension exceeds a specified dimension.
|
||||
#define WINIX_IMAGE_MODE_6 6
|
||||
|
||||
// Change dimensions only if both image dimensions are less than specified dimensions.
|
||||
#define WINIX_IMAGE_MODE_7 7
|
||||
|
||||
|
||||
|
||||
|
||||
// resizing
|
||||
#define WINIX_IMAGE_TYPE_RESIZE 1
|
||||
|
||||
// generating a thumbnail
|
||||
#define WINIX_IMAGE_TYPE_CREATE_THUMB 2
|
||||
|
||||
// cropping an image
|
||||
#define WINIX_IMAGE_TYPE_CROP 3
|
||||
|
||||
// cropping an thumbnail
|
||||
#define WINIX_IMAGE_TYPE_CROP_THUMB 4
|
||||
|
||||
// creating by cropping a new thumbnail (from an original image)
|
||||
#define WINIX_IMAGE_TYPE_CREATE_CROP_NEW_THUMB 5
|
||||
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
class Image : public BaseThread
|
||||
{
|
||||
public:
|
||||
|
||||
struct Scale
|
||||
{
|
||||
size_t cx;
|
||||
size_t cy;
|
||||
int aspect_mode;
|
||||
int quality;
|
||||
|
||||
Scale()
|
||||
{
|
||||
cx = cy = 1;
|
||||
aspect_mode = 2;
|
||||
quality = 100;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
void SetDb(Db * pdb);
|
||||
void SetConfig(Config * pconfig);
|
||||
void SetSystem(System * psystem);
|
||||
|
||||
|
||||
// returning scale info for a directory
|
||||
Scale GetImageScale(long dir_id);
|
||||
|
||||
// returning scale info (for thumbnails) for a directory
|
||||
Scale GetThumbScale(long dir_id);
|
||||
|
||||
|
||||
|
||||
void Resize(long file_id, size_t cx, size_t cy, int aspect_mode, int quality);
|
||||
|
||||
// creating a new thumbnail from an original image
|
||||
void CreateThumb(long file_id, size_t thumb_cx, size_t thumb_cy, int aspect_mode, int quality);
|
||||
|
||||
// cropping an image (the thumbnail is not changed)
|
||||
void Crop(long file_id, size_t xoffset, size_t yoffset, size_t cx, size_t cy, int quality);
|
||||
|
||||
// cropping an existing thumbnail
|
||||
void CropThumb(long file_id, size_t xoffset, size_t yoffset, size_t cx, size_t cy, int quality);
|
||||
|
||||
// creating and cropping a new thumbnail (from an original image)
|
||||
void CropNewThumb(long file_id, size_t xoffset, size_t yoffset, size_t cx, size_t cy,
|
||||
size_t thumb_cx, size_t thumb_cy, int aspect_mode, int quality);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Db * db;
|
||||
Config * config;
|
||||
System * system;
|
||||
|
||||
struct ImageItem
|
||||
{
|
||||
int type; // WINIX_IMAGE_TYPE_*
|
||||
long file_id;
|
||||
size_t cx;
|
||||
size_t cy;
|
||||
size_t xoffset; // xoffset and yoffset are used when cropping
|
||||
size_t yoffset;
|
||||
size_t thumb_cx;
|
||||
size_t thumb_cy;
|
||||
int aspect_mode;
|
||||
int quality;
|
||||
};
|
||||
|
||||
|
||||
template<typename int_type>
|
||||
void SetMinMax(int_type & var, int var_min, int var_max)
|
||||
{
|
||||
if( static_cast<int>(var) < var_min )
|
||||
var = var_min;
|
||||
|
||||
if( static_cast<int>(var) > var_max )
|
||||
var = var_max;
|
||||
}
|
||||
|
||||
|
||||
// queue of thumbnails to create
|
||||
typedef std::list<ImageItem> ImageTab;
|
||||
ImageTab image_tab;
|
||||
ImageItem item_temp;
|
||||
|
||||
// only for second thread
|
||||
ImageItem item_work;
|
||||
std::wstring src_path, dst_path;
|
||||
TextStream<std::string> command;
|
||||
TextStream<std::wstring> stream_tmp_path;
|
||||
DbItemQuery iq;
|
||||
std::string add_tempa;
|
||||
std::string input_file_name;
|
||||
std::string tmp_file_name;
|
||||
Item file_work;
|
||||
|
||||
virtual bool SignalReceived();
|
||||
virtual void Do();
|
||||
bool CreateCommand();
|
||||
bool CreateInputFileName();
|
||||
void CreateTmpFileName();
|
||||
void SaveImage();
|
||||
void CreateImage();
|
||||
void SelectAspect(size_t cx, size_t cy);
|
||||
void EscapePath(const std::string & path, TextStream<std::string> & out, bool clear_stream = true);
|
||||
void CheckParam(ImageItem & item);
|
||||
void Add(const std::wstring & in, TextStream<std::string> & out);
|
||||
void ImageSavedCorrectly();
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
201
winixd/core/ipban.h
Normal file
201
winixd/core/ipban.h
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_ipban
|
||||
#define headerfile_winix_core_ipban
|
||||
|
||||
#include <ctime>
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
// telling if the IPBan record is active
|
||||
// we have two records: active and non active
|
||||
// non active records is something like a history
|
||||
// it is used to remember the last ban level
|
||||
// so based on this in the future a next greater ban can be calculated
|
||||
#define WINIX_IPBAN_FLAG_ACTIVE 1
|
||||
|
||||
// current ban level
|
||||
// (if one of these flag is set and the record is active then it means the IP is banned at the moment)
|
||||
// level 1: banned for short time
|
||||
// level 2: can be set after level 1 has expired and the attacker still have not given up
|
||||
// banned for some longer time in level 1
|
||||
// level 3: can be set after level 2
|
||||
// banned for much more time
|
||||
#define WINIX_IPBAN_FLAG_BAN_LEVEL1 2
|
||||
#define WINIX_IPBAN_FLAG_BAN_LEVEL2 4
|
||||
#define WINIX_IPBAN_FLAG_BAN_LEVEL3 8
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
struct defining some restrictions to an IP address
|
||||
*/
|
||||
struct IPBan
|
||||
{
|
||||
// at the moment only IPv4 are supported
|
||||
int ip;
|
||||
|
||||
// one or more flags from WINIX_IPBAN_FLAG_*
|
||||
int flags;
|
||||
|
||||
// when this record was last used
|
||||
time_t last_used;
|
||||
|
||||
// when the restrictions (ban) should be removed
|
||||
// valid only if some of WINIX_IPBAN_FLAG_BAN_LEVELX flags are set
|
||||
// actually we do not remove the record but unsets WINIX_IPBAN_FLAG_ACTIVE flag
|
||||
// so in the future we can check whether we need to change
|
||||
// the ban level to a greater value
|
||||
time_t expires;
|
||||
|
||||
|
||||
// how many incorrect login attempts there are
|
||||
unsigned short int incorrect_login_events;
|
||||
|
||||
// how many incorrect encoded cookie were sent
|
||||
// only used if config.session_cookie_encode is true and session_keys_file is defined
|
||||
unsigned short int broken_encoded_cookie_events;
|
||||
|
||||
// how many incorrect session identifiers were sent
|
||||
unsigned short int session_hijacking_events;
|
||||
|
||||
// client didn't send a session cookie
|
||||
// it can be a bot or just someone wants to DOS the server
|
||||
// (a new session will be create)
|
||||
unsigned short int no_session_cookie_events;
|
||||
|
||||
|
||||
bool HasFlag(int flag) const
|
||||
{
|
||||
return (flags & flag) != 0;
|
||||
}
|
||||
|
||||
|
||||
void SetFlag(int flag)
|
||||
{
|
||||
flags = flags | flag;
|
||||
}
|
||||
|
||||
|
||||
void ClearFlag(int flag)
|
||||
{
|
||||
flags = flags & (~flag);
|
||||
}
|
||||
|
||||
|
||||
bool IsIPBanned() const
|
||||
{
|
||||
if( !HasFlag(WINIX_IPBAN_FLAG_ACTIVE) )
|
||||
return false;
|
||||
|
||||
return HasFlag(WINIX_IPBAN_FLAG_BAN_LEVEL1) ||
|
||||
HasFlag(WINIX_IPBAN_FLAG_BAN_LEVEL2) ||
|
||||
HasFlag(WINIX_IPBAN_FLAG_BAN_LEVEL3);
|
||||
}
|
||||
|
||||
|
||||
void IncrementBanLevel(time_t level1_expires, time_t level2_expires, time_t level3_expires)
|
||||
{
|
||||
if( HasFlag(WINIX_IPBAN_FLAG_BAN_LEVEL3) )
|
||||
{
|
||||
expires = level3_expires;
|
||||
return;
|
||||
}
|
||||
else
|
||||
if( HasFlag(WINIX_IPBAN_FLAG_BAN_LEVEL2) )
|
||||
{
|
||||
SetFlag(WINIX_IPBAN_FLAG_BAN_LEVEL3);
|
||||
expires = level3_expires;
|
||||
return;
|
||||
}
|
||||
else
|
||||
if( HasFlag(WINIX_IPBAN_FLAG_BAN_LEVEL1) )
|
||||
{
|
||||
SetFlag(WINIX_IPBAN_FLAG_BAN_LEVEL2);
|
||||
expires = level2_expires;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
SetFlag(WINIX_IPBAN_FLAG_BAN_LEVEL1);
|
||||
expires = level1_expires;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
IPBan()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
void Clear()
|
||||
{
|
||||
ip = 0;
|
||||
flags = 0;
|
||||
last_used = 0;
|
||||
expires = 0;
|
||||
incorrect_login_events = 0;
|
||||
broken_encoded_cookie_events = 0;
|
||||
session_hijacking_events = 0;
|
||||
no_session_cookie_events = 0;
|
||||
}
|
||||
|
||||
|
||||
void ResetEventsCounters()
|
||||
{
|
||||
ClearFlag(WINIX_IPBAN_FLAG_ACTIVE);
|
||||
incorrect_login_events = 0;
|
||||
broken_encoded_cookie_events = 0;
|
||||
session_hijacking_events = 0;
|
||||
no_session_cookie_events = 0;
|
||||
expires = 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
253
winixd/core/ipbancontainer.cpp
Normal file
253
winixd/core/ipbancontainer.cpp
Normal file
@@ -0,0 +1,253 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include "ipbancontainer.h"
|
||||
#include "log.h"
|
||||
#include "date/date.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
IPBanContainer::IPBanContainer()
|
||||
{
|
||||
is_ipban_tab_sorted = true; // an empty list is sorted
|
||||
soft_max_size = 100;
|
||||
max_size = 110;
|
||||
}
|
||||
|
||||
|
||||
void IPBanContainer::SetMaxSize(size_t soft_size, size_t size)
|
||||
{
|
||||
soft_max_size = soft_size;
|
||||
max_size = size;
|
||||
|
||||
if( max_size < soft_max_size )
|
||||
max_size = soft_max_size + 1;
|
||||
|
||||
ipban_tab.reserve(max_size);
|
||||
sort_helper_tab.reserve(max_size);
|
||||
}
|
||||
|
||||
|
||||
// returning a reference to the added (or existed) record
|
||||
IPBan & IPBanContainer::AddIP(int ip)
|
||||
{
|
||||
IPBan * old_ip_ban = FindIP(ip);
|
||||
|
||||
if( !old_ip_ban )
|
||||
{
|
||||
IPBan ip_ban;
|
||||
ip_ban.ip = ip;
|
||||
|
||||
if( ipban_tab.size() >= max_size )
|
||||
RemoveOldRecords();
|
||||
|
||||
ipban_tab.push_back(ip_ban);
|
||||
is_ipban_tab_sorted = false;
|
||||
return ipban_tab.back();
|
||||
}
|
||||
else
|
||||
{
|
||||
return *old_ip_ban;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void IPBanContainer::RemoveIP(int ip)
|
||||
{
|
||||
IPBan * ipban = FindIP(ip);
|
||||
|
||||
if( ipban )
|
||||
{
|
||||
size_t index = ipban - &ipban_tab[0];
|
||||
ipban_tab.erase(ipban_tab.begin() + index);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool IPBanContainer::IsSorted()
|
||||
{
|
||||
return is_ipban_tab_sorted;
|
||||
}
|
||||
|
||||
|
||||
void IPBanContainer::Clear()
|
||||
{
|
||||
ipban_tab.clear();
|
||||
is_ipban_tab_sorted = true;
|
||||
}
|
||||
|
||||
|
||||
// we need to remove some old records for the size of the container
|
||||
// to be less or equal to soft_max_size
|
||||
void IPBanContainer::RemoveOldRecords()
|
||||
{
|
||||
size_t to_remove = 0;
|
||||
|
||||
if( ipban_tab.size() >= soft_max_size )
|
||||
to_remove = ipban_tab.size() - soft_max_size;
|
||||
|
||||
if( to_remove > 0 )
|
||||
{
|
||||
sort_helper_tab.resize(ipban_tab.size());
|
||||
|
||||
for(size_t i=0 ; i<ipban_tab.size() ; ++i)
|
||||
sort_helper_tab[i] = i;
|
||||
|
||||
std::sort(sort_helper_tab.begin(), sort_helper_tab.end(), SortByLastUsedHelper(this));
|
||||
sort_helper_tab.resize(to_remove);
|
||||
std::sort(sort_helper_tab.begin(), sort_helper_tab.end());
|
||||
|
||||
while( to_remove-- > 0 )
|
||||
ipban_tab.erase(ipban_tab.begin() + sort_helper_tab[to_remove]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// for debug purposes
|
||||
void IPBanContainer::PrintTab()
|
||||
{
|
||||
log << log4 << "ipban_tab (size: " << ipban_tab.size() << ")" << logend;
|
||||
|
||||
for(size_t i=0 ; i<ipban_tab.size() ; ++i)
|
||||
{
|
||||
log << log4 << i << ": ip: " << ipban_tab[i].ip << ", flags: " << ipban_tab[i].flags << ", last_used: ";
|
||||
|
||||
PT::Date date(ipban_tab[i].last_used);
|
||||
log << date << ", expires: ";
|
||||
|
||||
date = ipban_tab[i].expires;
|
||||
log << date << logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// for debug purposes
|
||||
void IPBanContainer::PrintTab2()
|
||||
{
|
||||
log << log4 << "sort_helper_tab (size: " << sort_helper_tab.size() << ")" << logend;
|
||||
|
||||
for(size_t i=0 ; i<sort_helper_tab.size() ; ++i)
|
||||
{
|
||||
IPBan & ipban = ipban_tab[sort_helper_tab[i]];
|
||||
|
||||
log << log4 << i << ": ip: " << ipban.ip << ", flags: " << ipban.flags << ", last_used: ";
|
||||
|
||||
PT::Date date(ipban.last_used);
|
||||
log << date << ", expires: ";
|
||||
|
||||
date = ipban.expires;
|
||||
log << date << logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool IPBanContainer::SortByLastUsedHelper::operator()(size_t index1, size_t index2)
|
||||
{
|
||||
if( index1 < container->ipban_tab.size() &&
|
||||
index2 < container->ipban_tab.size() )
|
||||
{
|
||||
IPBan & ip1 = container->ipban_tab[index1];
|
||||
IPBan & ip2 = container->ipban_tab[index2];
|
||||
|
||||
// prefer to select records which do not have WINIX_IPBAN_FLAG_ACTIVE
|
||||
if( ip1.HasFlag(WINIX_IPBAN_FLAG_ACTIVE) !=
|
||||
ip2.HasFlag(WINIX_IPBAN_FLAG_ACTIVE) )
|
||||
{
|
||||
return ip2.HasFlag(WINIX_IPBAN_FLAG_ACTIVE);
|
||||
}
|
||||
|
||||
return ip1.last_used < ip2.last_used;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
IPBan * IPBanContainer::FindIP(int ip)
|
||||
{
|
||||
// !! IMPROVE ME add binary search if is_ipban_tab_sorted is true
|
||||
|
||||
for(size_t i=0 ; i<ipban_tab.size() ; ++i)
|
||||
{
|
||||
if( ipban_tab[i].ip == ip )
|
||||
{
|
||||
return &ipban_tab[i];
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
IPBan & IPBanContainer::GetIPBan(size_t index)
|
||||
{
|
||||
return ipban_tab[index];
|
||||
}
|
||||
|
||||
|
||||
void IPBanContainer::Sort()
|
||||
{
|
||||
std::sort(ipban_tab.begin(), ipban_tab.end(), SortIPBansFunction);
|
||||
is_ipban_tab_sorted = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t IPBanContainer::Size()
|
||||
{
|
||||
return ipban_tab.size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool IPBanContainer::SortIPBansFunction(const IPBan & ip1, const IPBan & ip2)
|
||||
{
|
||||
return ip1.ip < ip2.ip;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
90
winixd/core/ipbancontainer.h
Normal file
90
winixd/core/ipbancontainer.h
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_ipbancontainer
|
||||
#define headerfile_winix_core_ipbancontainer
|
||||
|
||||
#include <vector>
|
||||
#include "ipban.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class IPBanContainer
|
||||
{
|
||||
public:
|
||||
|
||||
IPBanContainer();
|
||||
|
||||
IPBan & AddIP(int ip);
|
||||
IPBan * FindIP(int ip);
|
||||
void Sort();
|
||||
size_t Size();
|
||||
IPBan & GetIPBan(size_t index);
|
||||
void SetMaxSize(size_t soft_size, size_t size);
|
||||
void RemoveIP(int ip);
|
||||
void Clear();
|
||||
bool IsSorted();
|
||||
|
||||
private:
|
||||
|
||||
std::vector<IPBan> ipban_tab;
|
||||
bool is_ipban_tab_sorted;
|
||||
size_t soft_max_size, max_size;
|
||||
std::vector<size_t> sort_helper_tab;
|
||||
|
||||
static bool SortIPBansFunction(const IPBan & ip1, const IPBan & ip2);
|
||||
void RemoveOldRecords();
|
||||
void PrintTab();
|
||||
void PrintTab2();
|
||||
|
||||
struct SortByLastUsedHelper
|
||||
{
|
||||
IPBanContainer * container;
|
||||
|
||||
SortByLastUsedHelper(IPBanContainer * c) : container(c) {}
|
||||
bool operator()(size_t index1, size_t index2);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
115
winixd/core/item.cpp
Normal file
115
winixd/core/item.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "item.h"
|
||||
#include "misc.h"
|
||||
#include "crypt.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
Item::Item()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
// !! IMPROVE ME
|
||||
// now we have Request::start_time and Request::start_date
|
||||
// we can somehow get the current time from the request
|
||||
// may setting the date should be completetly removed from here?
|
||||
void Item::SetDateToNow()
|
||||
{
|
||||
date_creation = std::time(0);
|
||||
date_modification = date_creation;
|
||||
}
|
||||
|
||||
|
||||
void Item::SetDateModifyToNow()
|
||||
{
|
||||
date_modification = std::time(0);
|
||||
}
|
||||
|
||||
|
||||
void Item::Clear()
|
||||
{
|
||||
id = -1;
|
||||
|
||||
user_id = -1;
|
||||
group_id = -1;
|
||||
privileges = 0;
|
||||
modification_user_id = -1;
|
||||
|
||||
guest_name.clear();
|
||||
|
||||
subject.clear();
|
||||
content.clear();
|
||||
modify_index = 0;
|
||||
url.clear();
|
||||
|
||||
content_type = ct_formatted_text;
|
||||
|
||||
type = none;
|
||||
parent_id = -1;
|
||||
|
||||
link_to.clear();
|
||||
link_redirect = false;
|
||||
|
||||
content_id = -1;
|
||||
ref = 1;
|
||||
|
||||
file_path.clear();
|
||||
file_fs = -1;
|
||||
file_type = WINIX_ITEM_FILETYPE_NONE;
|
||||
hash.clear();
|
||||
hash_type = WINIX_CRYPT_HASH_NONE;
|
||||
file_size = 0;
|
||||
has_thumb = false;
|
||||
|
||||
html_template.clear();
|
||||
sort_index = 0;
|
||||
meta.Clear();
|
||||
ameta.Clear();
|
||||
|
||||
SetDateToNow();
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
153
winixd/core/item.h
Normal file
153
winixd/core/item.h
Normal file
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_item
|
||||
#define headerfile_winix_core_item
|
||||
|
||||
#include <string>
|
||||
#include "space/space.h"
|
||||
#include "date/date.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
#define WINIX_ITEM_FILETYPE_NONE 0
|
||||
#define WINIX_ITEM_FILETYPE_IMAGE 1
|
||||
#define WINIX_ITEM_FILETYPE_DOCUMENT 2
|
||||
#define WINIX_ITEM_FILETYPE_VIDEO 3
|
||||
#define WINIX_ITEM_FILETYPE_UNKNOWN 10
|
||||
|
||||
|
||||
|
||||
|
||||
struct Item
|
||||
{
|
||||
long id;
|
||||
long parent_id;
|
||||
|
||||
|
||||
long user_id;
|
||||
long group_id;
|
||||
std::wstring guest_name; // used as a user name when user_id is equal -1
|
||||
|
||||
long modification_user_id; // who has modified the item last (not taken into account when checking permissions)
|
||||
|
||||
int privileges;
|
||||
|
||||
PT::Date date_creation;
|
||||
PT::Date date_modification;
|
||||
|
||||
std::wstring subject;
|
||||
std::wstring content;
|
||||
|
||||
std::wstring url;
|
||||
int modify_index;
|
||||
|
||||
enum ContentType
|
||||
{
|
||||
ct_text = 0,
|
||||
ct_formatted_text,
|
||||
ct_html,
|
||||
ct_bbcode,
|
||||
ct_raw
|
||||
};
|
||||
|
||||
ContentType content_type;
|
||||
|
||||
|
||||
|
||||
enum Type
|
||||
{
|
||||
dir = 0,
|
||||
file = 1,
|
||||
symlink = 2,
|
||||
|
||||
none = 1000
|
||||
};
|
||||
|
||||
|
||||
Type type;
|
||||
|
||||
|
||||
// used when type is symlink or to a directory too (function 'default')
|
||||
std::wstring link_to;
|
||||
int link_redirect; // !! IMPROVE ME should it be 'bool'?
|
||||
|
||||
|
||||
// static file (if exists)
|
||||
std::wstring file_path; // relative file path
|
||||
int file_fs; // file system type where the file was saved
|
||||
int file_type; // file type (none, image, doc, etc)
|
||||
bool has_thumb; // whether or not we have a thumbnail
|
||||
std::wstring hash; // file hash (md4, md5, ...)
|
||||
int hash_type; // hash type WINIX_CRYPT_HASH_* (see crypt.h)
|
||||
size_t file_size; // size of the file
|
||||
|
||||
|
||||
std::wstring html_template;
|
||||
|
||||
|
||||
// sort index used when displaying a group of items
|
||||
int sort_index;
|
||||
|
||||
|
||||
// meta information
|
||||
PT::Space meta;
|
||||
PT::Space ameta;
|
||||
|
||||
|
||||
// methods
|
||||
Item();
|
||||
void SetDateToNow();
|
||||
void SetDateModifyToNow();
|
||||
void Clear();
|
||||
|
||||
|
||||
private:
|
||||
// used by the database
|
||||
long content_id; // content id in 'content' table
|
||||
int ref; // content references
|
||||
|
||||
friend class Db;
|
||||
friend struct DbItemColumns;
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
198
winixd/core/job.cpp
Normal file
198
winixd/core/job.cpp
Normal file
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "job.h"
|
||||
#include "plugin.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
Job::Job()
|
||||
{
|
||||
jobs_queue_tab.resize(WINIX_JOBS_HOW_MANY_PRIORITIES);
|
||||
}
|
||||
|
||||
|
||||
void Job::CheckPriority(int & priority) const
|
||||
{
|
||||
if( priority < 0 )
|
||||
priority = 0;
|
||||
|
||||
if( priority >= WINIX_JOBS_HOW_MANY_PRIORITIES )
|
||||
priority = WINIX_JOBS_HOW_MANY_PRIORITIES - 1;
|
||||
}
|
||||
|
||||
|
||||
// first thread (objects locked)
|
||||
void Job::Add(PT::Space & job, int priority)
|
||||
{
|
||||
CheckPriority(priority);
|
||||
jobs_queue_tab[priority].push(job);
|
||||
WakeUpThread();
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t Job::Size(int priority) const
|
||||
{
|
||||
CheckPriority(priority);
|
||||
return jobs_queue_tab[priority].size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t Job::Size() const
|
||||
{
|
||||
size_t sum = 0;
|
||||
|
||||
for(size_t i=0 ; i<WINIX_JOBS_HOW_MANY_PRIORITIES ; ++i)
|
||||
sum += Size(i);
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
bool Job::Empty(int priority) const
|
||||
{
|
||||
CheckPriority(priority);
|
||||
return jobs_queue_tab[priority].empty();
|
||||
}
|
||||
|
||||
|
||||
bool Job::Empty() const
|
||||
{
|
||||
for(size_t i=0 ; i<WINIX_JOBS_HOW_MANY_PRIORITIES ; ++i)
|
||||
if( !Empty(i) )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
second thread
|
||||
*/
|
||||
|
||||
// second thread (objects locked)
|
||||
bool Job::SignalReceived()
|
||||
{
|
||||
return !Empty();
|
||||
}
|
||||
|
||||
|
||||
// second thread (objects not locked)
|
||||
void Job::Do()
|
||||
{
|
||||
size_t i = WINIX_JOBS_HOW_MANY_PRIORITIES;
|
||||
bool is_empty;
|
||||
|
||||
while( i-- > 0 && !IsExitSignal() )
|
||||
{
|
||||
do
|
||||
{
|
||||
Lock();
|
||||
is_empty = Empty(i);
|
||||
Unlock();
|
||||
|
||||
if( !is_empty )
|
||||
DoQueue(jobs_queue_tab[i]);
|
||||
}
|
||||
while( !is_empty && !IsExitSignal() );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// second thread (objects not locked, jobs_queue is not empty)
|
||||
void Job::DoQueue(JobsQueue & jobs_queue)
|
||||
{
|
||||
bool is_empty;
|
||||
|
||||
do
|
||||
{
|
||||
Lock();
|
||||
// references will not be invalidated after insertion to jobs_queue
|
||||
// (jobs_queue is std::queue and it uses std::deque by default)
|
||||
PT::Space & job = jobs_queue.front();
|
||||
Unlock();
|
||||
|
||||
DoJob(job);
|
||||
|
||||
Lock();
|
||||
jobs_queue.pop();
|
||||
is_empty = jobs_queue.empty();
|
||||
Unlock();
|
||||
}
|
||||
while( !is_empty && !IsExitSignal() );
|
||||
}
|
||||
|
||||
|
||||
// second thread (objects not locked)
|
||||
void Job::DoJob(PT::Space & job)
|
||||
{
|
||||
try
|
||||
{
|
||||
PluginRes res = plugin.Call((Session*)0, WINIX_JOB, &job);
|
||||
|
||||
if( res.res_true == 0 )
|
||||
DoWinixJob(job);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// second thread (objects not locked)
|
||||
void Job::DoWinixJob(PT::Space & job)
|
||||
{
|
||||
Lock();
|
||||
//log << log1 << "standard winix job: " << job.Text(L"type") << logend;
|
||||
Unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
115
winixd/core/job.h
Normal file
115
winixd/core/job.h
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_jobs
|
||||
#define headerfile_winix_core_jobs
|
||||
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include "basethread.h"
|
||||
#include "space/space.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
#define WINIX_JOBS_HOW_MANY_PRIORITIES 32
|
||||
|
||||
|
||||
|
||||
|
||||
class Job : public BaseThread
|
||||
{
|
||||
public:
|
||||
|
||||
Job();
|
||||
|
||||
/*
|
||||
add a new job to the queue
|
||||
priority: 0-31 (0 - the lowest priority, 31 - the highest priority)
|
||||
*/
|
||||
void Add(PT::Space & job, int priority = 0);
|
||||
|
||||
|
||||
/*
|
||||
queue size, and size of all jobs in any priority
|
||||
*/
|
||||
size_t Size(int priority) const;
|
||||
size_t Size() const;
|
||||
|
||||
|
||||
/*
|
||||
true if specified queue is empty
|
||||
or if all queues are empty
|
||||
*/
|
||||
bool Empty(int priority) const;
|
||||
bool Empty() const;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
typedef std::queue<PT::Space> JobsQueue;
|
||||
typedef std::vector<JobsQueue> JobsQueueTab;
|
||||
JobsQueueTab jobs_queue_tab;
|
||||
|
||||
void CheckPriority(int & priority) const;
|
||||
void SaveToFile();
|
||||
void ReadFromFile();
|
||||
|
||||
|
||||
/*
|
||||
second thread
|
||||
*/
|
||||
|
||||
// standard winix jobs
|
||||
// Image image;
|
||||
// sending emails
|
||||
// etc.
|
||||
|
||||
bool SignalReceived();
|
||||
void Do();
|
||||
void DoQueue(JobsQueue & jobs_queue);
|
||||
void DoJob(PT::Space & job);
|
||||
void DoWinixJob(PT::Space & job);
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
144
winixd/core/lastcontainer.cpp
Normal file
144
winixd/core/lastcontainer.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "lastcontainer.h"
|
||||
#include "log.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
LastItem::LastItem()
|
||||
{
|
||||
user_id = 0;
|
||||
ip = 0;
|
||||
session_id = 0;
|
||||
}
|
||||
|
||||
|
||||
bool LastItem::IsLoggedOut()
|
||||
{
|
||||
return end.year > 1970;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
LastContainer::Iterator LastContainer::Begin()
|
||||
{
|
||||
return last_tab.begin();
|
||||
}
|
||||
|
||||
|
||||
LastContainer::Iterator LastContainer::End()
|
||||
{
|
||||
return last_tab.end();
|
||||
}
|
||||
|
||||
|
||||
LastContainer::Iterator LastContainer::FindNotLoggedOut(long user_id, long session_id)
|
||||
{
|
||||
LastTab::iterator i;
|
||||
|
||||
for(i=last_tab.begin() ; i!=last_tab.end() ; ++i)
|
||||
{
|
||||
if( i->user_id == user_id && i->session_id == session_id && !i->IsLoggedOut() )
|
||||
return i;
|
||||
}
|
||||
|
||||
return last_tab.end();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void LastContainer::UserLogin(long user_id, const std::wstring & name, unsigned int ip, long session_id)
|
||||
{
|
||||
LastTab::iterator i = FindNotLoggedOut(user_id, session_id);
|
||||
|
||||
if( i != last_tab.end() )
|
||||
{
|
||||
log << log1 << "LC: such a user and session_id exist, not added as a new one" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
if( last_tab.size() >= WINIX_LASTCONTAINER_TABLE_SIZE ) // last_tab has O(n) complexity
|
||||
last_tab.erase(last_tab.begin());
|
||||
|
||||
LastItem li;
|
||||
|
||||
li.user_id = user_id;
|
||||
li.name = name;
|
||||
li.ip = ip;
|
||||
li.session_id = session_id;
|
||||
li.start = std::time(0);
|
||||
|
||||
last_tab.insert(last_tab.end(), li);
|
||||
|
||||
log << log2 << "LC: added user: " << name << " into the last table" << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void LastContainer::UserLogout(long user_id, long session_id)
|
||||
{
|
||||
LastTab::iterator i = FindNotLoggedOut(user_id, session_id);
|
||||
|
||||
if( i != last_tab.end() )
|
||||
{
|
||||
i->end = std::time(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
!! IMPROVE ME
|
||||
users read from the session file (at boot time)
|
||||
are not added to LastContainer
|
||||
*/
|
||||
|
||||
log << log4 << "LC: there is no such a user to log out: user_id: "
|
||||
<< user_id << " ses_id: " << session_id << logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
112
winixd/core/lastcontainer.h
Normal file
112
winixd/core/lastcontainer.h
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_lastcontainer
|
||||
#define headerfile_winix_core_lastcontainer
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include "date/date.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
// how many items we store in the 'last' function
|
||||
#define WINIX_LASTCONTAINER_TABLE_SIZE 100
|
||||
|
||||
|
||||
|
||||
struct LastItem
|
||||
{
|
||||
long user_id;
|
||||
|
||||
// additional we store the whole string-name
|
||||
// (you can delete a user from the database but we can still print the name)
|
||||
std::wstring name;
|
||||
|
||||
// ip address
|
||||
unsigned int ip;
|
||||
|
||||
// session id (used when logging out)
|
||||
long session_id;
|
||||
|
||||
// start logging and end logging
|
||||
PT::Date start;
|
||||
PT::Date end;
|
||||
|
||||
|
||||
LastItem();
|
||||
bool IsLoggedOut();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class LastContainer
|
||||
{
|
||||
public:
|
||||
|
||||
typedef std::list<LastItem> LastTab;
|
||||
typedef LastTab::iterator Iterator;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
Iterator Begin();
|
||||
Iterator End();
|
||||
void UserLogin(long user_id, const std::wstring & name, unsigned int ip, long session_id);
|
||||
void UserLogout(long user_id, long session_id);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
LastTab last_tab;
|
||||
Iterator FindNotLoggedOut(long user_id, long session_id);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
413
winixd/core/loadavg.cpp
Normal file
413
winixd/core/loadavg.cpp
Normal file
@@ -0,0 +1,413 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "loadavg.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
LoadAvg::LoadAvg()
|
||||
{
|
||||
current1.Clear();
|
||||
current5.Clear();
|
||||
current15.Clear();
|
||||
|
||||
cache_load1 = 0.0;
|
||||
cache_load5 = 0.0;
|
||||
cache_load15 = 0.0;
|
||||
|
||||
cache_req_per_sec1 = 0.0;
|
||||
cache_req_per_sec5 = 0.0;
|
||||
cache_req_per_sec15 = 0.0;
|
||||
|
||||
was_stop_request = false;
|
||||
|
||||
CreateTable();
|
||||
}
|
||||
|
||||
|
||||
LoadAvg & LoadAvg::operator=(const LoadAvg & l)
|
||||
{
|
||||
current1 = l.current1;
|
||||
current5 = l.current5;
|
||||
current15 = l.current15;
|
||||
|
||||
cache_load1 = l.cache_load1;
|
||||
cache_load5 = l.cache_load5;
|
||||
cache_load15 = l.cache_load15;
|
||||
|
||||
cache_req_per_sec1 = l.cache_req_per_sec1;
|
||||
cache_req_per_sec5 = l.cache_req_per_sec5;
|
||||
cache_req_per_sec15 = l.cache_req_per_sec15;
|
||||
|
||||
was_stop_request = l.was_stop_request;
|
||||
|
||||
CreateTable();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
LoadAvg::LoadAvg(const LoadAvg & l)
|
||||
{
|
||||
operator=(l);
|
||||
}
|
||||
|
||||
|
||||
LoadAvg::~LoadAvg()
|
||||
{
|
||||
delete [] tab1;
|
||||
delete [] tab5;
|
||||
delete [] tab15;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void LoadAvg::CreateTable(size_t seconds, size_t granularity, Times* & tab, size_t & len)
|
||||
{
|
||||
len = (seconds / granularity) + 1; // rounding up (len mininum is 1)
|
||||
tab = new Times[len];
|
||||
|
||||
for(size_t i=0 ; i<len ; ++i)
|
||||
{
|
||||
tab[i].Clear();
|
||||
tab[i].dp = granularity; // at the beginning we assume the pause for all items
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LoadAvg::CreateTable()
|
||||
{
|
||||
CreateTable(60, WINIX_LOADAVG_GRANULARITY1, tab1, len1);
|
||||
CreateTable(60 * 5, WINIX_LOADAVG_GRANULARITY5, tab5, len5);
|
||||
CreateTable(60 * 15, WINIX_LOADAVG_GRANULARITY15, tab15, len15);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void LoadAvg::MoveTab(Times * tab, size_t len)
|
||||
{
|
||||
if( len > 1 )
|
||||
{
|
||||
for(size_t i=0 ; i<len-1 ; ++i)
|
||||
tab[i] = tab[i+1];
|
||||
}
|
||||
|
||||
tab[len-1].Clear();
|
||||
}
|
||||
|
||||
|
||||
void LoadAvg::UpdateTimer1()
|
||||
{
|
||||
MoveTab(tab1, len1);
|
||||
|
||||
tab1[len1-1] = current1;
|
||||
current1.Clear();
|
||||
cache_load1 = 0.0;
|
||||
cache_req_per_sec1 = 0.0;
|
||||
}
|
||||
|
||||
|
||||
void LoadAvg::UpdateTimer5()
|
||||
{
|
||||
MoveTab(tab5, len5);
|
||||
|
||||
tab5[len5-1] = current5;
|
||||
current5.Clear();
|
||||
cache_load5 = 0.0;
|
||||
cache_req_per_sec5 = 0.0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void LoadAvg::UpdateTimer15()
|
||||
{
|
||||
MoveTab(tab15, len15);
|
||||
|
||||
tab15[len15-1] = current15;
|
||||
current15.Clear();
|
||||
cache_load15 = 0.0;
|
||||
cache_req_per_sec15 = 0.0;
|
||||
}
|
||||
|
||||
|
||||
void LoadAvg::CheckTimers()
|
||||
{
|
||||
if( current1.dr + current1.dp > (double)WINIX_LOADAVG_GRANULARITY1 )
|
||||
UpdateTimer1();
|
||||
|
||||
if( current5.dr + current5.dp > (double)WINIX_LOADAVG_GRANULARITY5 )
|
||||
UpdateTimer5();
|
||||
|
||||
if( current15.dr + current15.dp > (double)WINIX_LOADAVG_GRANULARITY15 )
|
||||
UpdateTimer15();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void LoadAvg::StartRequest()
|
||||
{
|
||||
clock_gettime(CLOCK_REALTIME, &start_req);
|
||||
|
||||
if( was_stop_request )
|
||||
{
|
||||
double dp = (start_req.tv_sec - stop_req.tv_sec);
|
||||
dp += double(start_req.tv_nsec - stop_req.tv_nsec) / 1000000000.0; // make sure that tv_nsec has signed type
|
||||
|
||||
current1.dp += dp;
|
||||
current5.dp += dp;
|
||||
current15.dp += dp;
|
||||
|
||||
CheckTimers();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void LoadAvg::StopRequest()
|
||||
{
|
||||
char buf[50];
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &stop_req);
|
||||
|
||||
double dr = (stop_req.tv_sec - start_req.tv_sec);
|
||||
dr += double(stop_req.tv_nsec - start_req.tv_nsec) / 1000000000.0; // make sure that tv_nsec has signed type
|
||||
|
||||
current1.dr += dr;
|
||||
current5.dr += dr;
|
||||
current15.dr += dr;
|
||||
|
||||
current1.req += 1;
|
||||
current5.req += 1;
|
||||
current15.req += 1;
|
||||
|
||||
sprintf(buf, "%f", dr);
|
||||
SetNonZeroDigitsAfterComma(buf, 2);
|
||||
|
||||
log << log2 << "LA: request took: " << buf << "s" << logend;
|
||||
was_stop_request = true;
|
||||
}
|
||||
|
||||
|
||||
void LoadAvg::SumTab(Times * tab, size_t len, double expected, Times & t)
|
||||
{
|
||||
size_t i = len;
|
||||
|
||||
while( i-- > 0 && t.dr+t.dp < expected )
|
||||
{
|
||||
t.dr += tab[i].dr;
|
||||
t.dp += tab[i].dp;
|
||||
t.req += tab[i].req;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LoadAvg::Calculate1()
|
||||
{
|
||||
Times t = current1;
|
||||
|
||||
SumTab(tab1, len1, 60.0, t);
|
||||
|
||||
if( t.dr+t.dp == 0.0 )
|
||||
{
|
||||
cache_load1 = 0.0;
|
||||
cache_req_per_sec1 = 0.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cache_load1 = t.dr / (t.dr+t.dp);
|
||||
cache_req_per_sec1 = t.req / (t.dr+t.dp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LoadAvg::Calculate5()
|
||||
{
|
||||
Times t = current5;
|
||||
|
||||
SumTab(tab5, len5, 60.0 * 5, t);
|
||||
|
||||
if( t.dr+t.dp == 0.0 )
|
||||
{
|
||||
cache_load5 = 0.0;
|
||||
cache_req_per_sec5 = 0.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cache_load5 = t.dr / (t.dr+t.dp);
|
||||
cache_req_per_sec5 = t.req / (t.dr+t.dp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LoadAvg::Calculate15()
|
||||
{
|
||||
Times t = current15;
|
||||
|
||||
SumTab(tab15, len15, 60.0 * 15, t);
|
||||
|
||||
if( t.dr+t.dp == 0.0 )
|
||||
{
|
||||
cache_load15 = 0.0;
|
||||
cache_req_per_sec15 = 0.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cache_load15 = t.dr / (t.dr+t.dp);
|
||||
cache_req_per_sec15 = t.req / (t.dr+t.dp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
double LoadAvg::LoadAvgNow()
|
||||
{
|
||||
double load = 0.0;
|
||||
|
||||
double dr = current1.dr;
|
||||
double dp = current1.dp;
|
||||
|
||||
if( tab1[len1-1].dr + tab1[len1-1].dp < WINIX_LOADAVG_GRANULARITY1 * 2 )
|
||||
{
|
||||
dr += tab1[len1-1].dr;
|
||||
dp += tab1[len1-1].dp;
|
||||
}
|
||||
|
||||
if( dr + dp != 0.0 )
|
||||
load = dr / (dr + dp);
|
||||
|
||||
return load;
|
||||
}
|
||||
|
||||
|
||||
|
||||
double LoadAvg::LoadAvg1()
|
||||
{
|
||||
if( cache_load1 != 0.0 )
|
||||
return cache_load1;
|
||||
|
||||
Calculate1();
|
||||
|
||||
return cache_load1;
|
||||
}
|
||||
|
||||
|
||||
double LoadAvg::LoadAvg5()
|
||||
{
|
||||
if( cache_load5 != 0.0 )
|
||||
return cache_load5;
|
||||
|
||||
Calculate5();
|
||||
|
||||
return cache_load5;
|
||||
}
|
||||
|
||||
|
||||
double LoadAvg::LoadAvg15()
|
||||
{
|
||||
if( cache_load15 != 0.0 )
|
||||
return cache_load15;
|
||||
|
||||
Calculate15();
|
||||
|
||||
return cache_load15;
|
||||
}
|
||||
|
||||
|
||||
|
||||
double LoadAvg::ReqPerSecNow()
|
||||
{
|
||||
double req_per_sec = 0.0;
|
||||
|
||||
double dr = current1.dr;
|
||||
double dp = current1.dp;
|
||||
double req = current1.req;
|
||||
|
||||
if( tab1[len1-1].dr + tab1[len1-1].dp < WINIX_LOADAVG_GRANULARITY1 * 2 )
|
||||
{
|
||||
dr += tab1[len1-1].dr;
|
||||
dp += tab1[len1-1].dp;
|
||||
req += tab1[len1-1].req;
|
||||
}
|
||||
|
||||
if( dr + dp != 0.0 )
|
||||
req_per_sec = req / (dr + dp);
|
||||
|
||||
return req_per_sec;
|
||||
}
|
||||
|
||||
|
||||
double LoadAvg::ReqPerSec1()
|
||||
{
|
||||
if( cache_req_per_sec1 != 0.0 )
|
||||
return cache_req_per_sec1;
|
||||
|
||||
Calculate1();
|
||||
|
||||
return cache_req_per_sec1;
|
||||
}
|
||||
|
||||
|
||||
double LoadAvg::ReqPerSec5()
|
||||
{
|
||||
if( cache_req_per_sec5 != 0.0 )
|
||||
return cache_req_per_sec5;
|
||||
|
||||
Calculate5();
|
||||
|
||||
return cache_req_per_sec5;
|
||||
}
|
||||
|
||||
|
||||
double LoadAvg::ReqPerSec15()
|
||||
{
|
||||
if( cache_req_per_sec15 != 0.0 )
|
||||
return cache_req_per_sec15;
|
||||
|
||||
Calculate15();
|
||||
|
||||
return cache_req_per_sec15;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
146
winixd/core/loadavg.h
Normal file
146
winixd/core/loadavg.h
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_loadavg
|
||||
#define headerfile_winix_core_loadavg
|
||||
|
||||
#include <ctime>
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
// in seconds
|
||||
#define WINIX_LOADAVG_GRANULARITY1 2
|
||||
#define WINIX_LOADAVG_GRANULARITY5 15
|
||||
#define WINIX_LOADAVG_GRANULARITY15 45
|
||||
|
||||
|
||||
|
||||
class LoadAvg
|
||||
{
|
||||
public:
|
||||
LoadAvg();
|
||||
~LoadAvg();
|
||||
LoadAvg & operator=(const LoadAvg & l);
|
||||
LoadAvg(const LoadAvg & l);
|
||||
|
||||
void StartRequest();
|
||||
void StopRequest();
|
||||
|
||||
double LoadAvgNow(); // load average withing last WINIX_LOADAVG_GRANULARITY1 seconds
|
||||
double LoadAvg1();
|
||||
double LoadAvg5();
|
||||
double LoadAvg15();
|
||||
|
||||
double ReqPerSecNow();
|
||||
double ReqPerSec1();
|
||||
double ReqPerSec5();
|
||||
double ReqPerSec15();
|
||||
|
||||
private:
|
||||
|
||||
struct Times
|
||||
{
|
||||
double dr; // time for the request (in seconds)
|
||||
double dp; // time for the pause between requestes (in seconds)
|
||||
long req; // how many requests
|
||||
|
||||
void Clear()
|
||||
{
|
||||
dr = 0.0;
|
||||
dp = 0.0;
|
||||
req = 0;
|
||||
}
|
||||
|
||||
Times & operator=(const Times & t)
|
||||
{
|
||||
dr = t.dr;
|
||||
dp = t.dp;
|
||||
req = t.req;
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
void CheckTimers();
|
||||
void UpdateTimer1();
|
||||
void UpdateTimer5();
|
||||
void UpdateTimer15();
|
||||
|
||||
Times current1;
|
||||
Times current5;
|
||||
Times current15;
|
||||
|
||||
void CreateTable(size_t seconds, size_t granulatiry, Times* & tab, size_t & len);
|
||||
void CreateTable();
|
||||
|
||||
void MoveTab(Times * tab, size_t len);
|
||||
void SumTab(Times * tab, size_t len, double expected, Times & t);
|
||||
|
||||
void Calculate1();
|
||||
void Calculate5();
|
||||
void Calculate15();
|
||||
|
||||
bool was_stop_request;
|
||||
timespec start_req, stop_req;
|
||||
|
||||
Times * tab1;
|
||||
size_t len1;
|
||||
|
||||
Times * tab5;
|
||||
size_t len5;
|
||||
|
||||
Times * tab15;
|
||||
size_t len15;
|
||||
|
||||
double cache_load1;
|
||||
double cache_load5;
|
||||
double cache_load15;
|
||||
|
||||
double cache_req_per_sec1;
|
||||
double cache_req_per_sec5;
|
||||
double cache_req_per_sec15;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
82
winixd/core/lock.cpp
Normal file
82
winixd/core/lock.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "lock.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
Lock::Lock()
|
||||
{
|
||||
synchro = 0;
|
||||
}
|
||||
|
||||
|
||||
Lock::Lock(Synchro * synchro_)
|
||||
{
|
||||
synchro = synchro_;
|
||||
synchro->Lock();
|
||||
}
|
||||
|
||||
|
||||
Lock::Lock(Synchro & synchro_)
|
||||
{
|
||||
synchro = &synchro_;
|
||||
synchro->Lock();
|
||||
}
|
||||
|
||||
|
||||
Lock::~Lock()
|
||||
{
|
||||
Unlock();
|
||||
}
|
||||
|
||||
|
||||
void Lock::Unlock()
|
||||
{
|
||||
if( synchro )
|
||||
{
|
||||
synchro->Unlock();
|
||||
synchro = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
71
winixd/core/lock.h
Normal file
71
winixd/core/lock.h
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_lock
|
||||
#define headerfile_winix_core_lock
|
||||
|
||||
#include "synchro.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class Lock
|
||||
{
|
||||
public:
|
||||
|
||||
Lock(Synchro * synchro_);
|
||||
Lock(Synchro & synchro_);
|
||||
|
||||
~Lock();
|
||||
|
||||
void Unlock();
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Synchro * synchro;
|
||||
|
||||
Lock();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
487
winixd/core/log.cpp
Normal file
487
winixd/core/log.cpp
Normal file
@@ -0,0 +1,487 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "log.h"
|
||||
#include <ctime>
|
||||
#include <string.h>
|
||||
#include "utf8/utf8.h"
|
||||
#include "timezones.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
Log::Log()
|
||||
{
|
||||
log_level = 1;
|
||||
current_level = 100; // nothing to log (call Init() first)
|
||||
request = 0;
|
||||
max_requests = 1;
|
||||
lines = 0;
|
||||
max_lines = 5000;
|
||||
log_file_open = false;
|
||||
time_zones = 0;
|
||||
}
|
||||
|
||||
|
||||
Log::~Log()
|
||||
{
|
||||
SaveLogAndClear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Log::SetTimeZones(TimeZones * ptime_zones)
|
||||
{
|
||||
time_zones = ptime_zones;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int Log::LogLevel()
|
||||
{
|
||||
return log_level;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Log::Init(int log_level_, bool save_each_line_, const std::wstring & log_file_, bool log_std, int log_max_requests)
|
||||
{
|
||||
log_level = log_level_;
|
||||
log_stdout = log_std;
|
||||
max_requests = log_max_requests;
|
||||
save_each_line = save_each_line_;
|
||||
|
||||
PT::WideToUTF8(log_file_, log_file);
|
||||
// don't open the file here
|
||||
// because it would be created with the root as an owner
|
||||
}
|
||||
|
||||
|
||||
void Log::OpenFile()
|
||||
{
|
||||
if( !log_file.empty() )
|
||||
{
|
||||
file.open( log_file.c_str(), std::ios_base::out | std::ios_base::app );
|
||||
log_file_open = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Log::PrintDate(const PT::Date & date, size_t time_zone_id)
|
||||
{
|
||||
if( time_zones )
|
||||
{
|
||||
TimeZone * tz = time_zones->GetZone(time_zone_id);
|
||||
|
||||
if( tz )
|
||||
{
|
||||
PT::Date local_date = tz->ToLocal(date);
|
||||
log << local_date;
|
||||
}
|
||||
else
|
||||
{
|
||||
(*this) << date << " UTC"; // unknown time zone identifier
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
(*this) << date << " UTC"; // time_zones object was not set
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const void * s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
buffer << s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const char * s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
if( !s )
|
||||
return *this;
|
||||
|
||||
buffer << s;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::string & s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
buffer << s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::string * s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
buffer << *s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const wchar_t * s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
if( s )
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::wstring & s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::wstring * s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << *s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(int s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(long s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(char s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Log & Log::operator<<(wchar_t s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Log & Log::operator<<(size_t s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(double s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const PT::Space & s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const PT::Date & date)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << date;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(LogManipulators m)
|
||||
{
|
||||
switch(m)
|
||||
{
|
||||
case logend:
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << '\n';
|
||||
lines += 1;
|
||||
|
||||
if( save_each_line )
|
||||
SaveLogAndClear();
|
||||
}
|
||||
break;
|
||||
|
||||
case logsave:
|
||||
SaveLogAndClear();
|
||||
break;
|
||||
|
||||
case logendrequest:
|
||||
if( ++request >= max_requests || lines > max_lines )
|
||||
SaveLogAndClear();
|
||||
break;
|
||||
|
||||
case log1:
|
||||
current_level = 1;
|
||||
break;
|
||||
|
||||
case log2:
|
||||
current_level = 2;
|
||||
break;
|
||||
|
||||
case log3:
|
||||
current_level = 3;
|
||||
break;
|
||||
|
||||
case log4:
|
||||
current_level = 4;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
char Log::GetHEXdigit(unsigned char c)
|
||||
{
|
||||
if( c < 10 )
|
||||
return c + '0';
|
||||
|
||||
return c - 10 + 'A';
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Log::ToHEX(char * buf, unsigned char c)
|
||||
{
|
||||
buf[0] = GetHEXdigit(c >> 4);
|
||||
buf[1] = GetHEXdigit(c & 0xf);
|
||||
buf[2] = 0;
|
||||
}
|
||||
|
||||
|
||||
void Log::LogBinary(const char * blob, size_t blob_len)
|
||||
{
|
||||
size_t i=0;
|
||||
char buf[3];
|
||||
|
||||
|
||||
while( i < blob_len )
|
||||
{
|
||||
size_t oldi = i;
|
||||
|
||||
for(size_t a=0 ; a<16 ; ++a)
|
||||
{
|
||||
if( i < blob_len )
|
||||
{
|
||||
ToHEX(buf, blob[i]);
|
||||
buffer << buf << ' ';
|
||||
++i;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer << " ";
|
||||
}
|
||||
|
||||
if( a == 7 )
|
||||
{
|
||||
if( i < blob_len )
|
||||
buffer << "- ";
|
||||
else
|
||||
buffer << " ";
|
||||
}
|
||||
}
|
||||
|
||||
i = oldi;
|
||||
buffer << ' ';
|
||||
|
||||
for(size_t a=0 ; a<16 && i<blob_len ; ++a, ++i)
|
||||
{
|
||||
if( blob[i] > 31 && blob[i] < 127 )
|
||||
buffer << blob[i];
|
||||
else
|
||||
buffer << '.';
|
||||
}
|
||||
|
||||
(*this) << logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Log::LogBinary(const std::string & blob)
|
||||
{
|
||||
LogBinary(blob.c_str(), blob.size());
|
||||
}
|
||||
|
||||
|
||||
void Log::SystemErr(int err)
|
||||
{
|
||||
(*this) << "errno: " << err;
|
||||
|
||||
const char * err_msg = strerror(err);
|
||||
|
||||
if( err_msg )
|
||||
(*this) << " (" << err_msg << ")";
|
||||
}
|
||||
|
||||
|
||||
void Log::SaveLogAndClear()
|
||||
{
|
||||
SaveLog();
|
||||
|
||||
buffer.Clear();
|
||||
request = 0;
|
||||
lines = 0;
|
||||
}
|
||||
|
||||
|
||||
void Log::SaveLog()
|
||||
{
|
||||
if( buffer.Str().empty() )
|
||||
return;
|
||||
|
||||
if( log_stdout )
|
||||
PT::WideToUTF8(buffer.Str(), std::cout);
|
||||
|
||||
if( log_file.empty() )
|
||||
return;
|
||||
|
||||
if( !log_file_open || !file )
|
||||
{
|
||||
file.close();
|
||||
file.clear();
|
||||
|
||||
OpenFile();
|
||||
|
||||
if( !file )
|
||||
return;
|
||||
}
|
||||
|
||||
PT::WideToUTF8(buffer.Str(), file);
|
||||
file.flush();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
199
winixd/core/log.h
Normal file
199
winixd/core/log.h
Normal file
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_log
|
||||
#define headerfile_winix_core_log
|
||||
|
||||
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "textstream.h"
|
||||
#include "logmanipulators.h"
|
||||
#include "textstream/textstream.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class TimeZones;
|
||||
|
||||
|
||||
|
||||
class Log
|
||||
{
|
||||
public:
|
||||
|
||||
Log();
|
||||
~Log();
|
||||
|
||||
void SetTimeZones(TimeZones * ptime_zones);
|
||||
void Init(int log_level_, bool save_each_line_, const std::wstring & log_file_, bool log_std, int log_max_requests);
|
||||
|
||||
Log & operator<<(const void * s);
|
||||
Log & operator<<(const char * s);
|
||||
Log & operator<<(const std::string * s);
|
||||
Log & operator<<(const std::string & s);
|
||||
Log & operator<<(const wchar_t * s);
|
||||
Log & operator<<(const std::wstring * s);
|
||||
Log & operator<<(const std::wstring & s);
|
||||
Log & operator<<(int s);
|
||||
Log & operator<<(long s);
|
||||
Log & operator<<(char s);
|
||||
Log & operator<<(wchar_t s);
|
||||
Log & operator<<(size_t s);
|
||||
Log & operator<<(double s);
|
||||
Log & operator<<(const PT::Space & space);
|
||||
Log & operator<<(LogManipulators m);
|
||||
Log & operator<<(const PT::Date & date);
|
||||
|
||||
void PrintDate(const PT::Date & date, size_t time_zone_id);
|
||||
|
||||
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||
Log & operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf);
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void LogString(const StringType & value, size_t max_size);
|
||||
|
||||
void LogBinary(const char * blob, size_t blob_len);
|
||||
void LogBinary(const std::string & blob);
|
||||
|
||||
|
||||
void SystemErr(int err);
|
||||
void SaveLog();
|
||||
void SaveLogAndClear();
|
||||
|
||||
int LogLevel();
|
||||
|
||||
|
||||
private:
|
||||
|
||||
// time zones for printing the time in the log file
|
||||
TimeZones * time_zones;
|
||||
|
||||
// buffer for the log
|
||||
TextStream<std::wstring> buffer;
|
||||
|
||||
// log lovel from the config file
|
||||
int log_level;
|
||||
|
||||
// current level set by a modifier (e.g. log << log3)
|
||||
int current_level;
|
||||
|
||||
// current request for logging
|
||||
// starts from zero and incremented after logendrequest modifier
|
||||
int request;
|
||||
|
||||
// how many request to save at once
|
||||
int max_requests;
|
||||
|
||||
// file log
|
||||
std::string log_file;
|
||||
std::ofstream file;
|
||||
|
||||
// logging to stdout
|
||||
bool log_stdout;
|
||||
|
||||
// how many lines there are in the buffer
|
||||
int lines;
|
||||
|
||||
// is the config file already open
|
||||
bool log_file_open;
|
||||
|
||||
// how many lines can be in the config buffer
|
||||
// default: 5000
|
||||
int max_lines;
|
||||
|
||||
// whether to save each line (for debug)
|
||||
bool save_each_line;
|
||||
|
||||
void OpenFile();
|
||||
char GetHEXdigit(unsigned char c);
|
||||
void ToHEX(char * buf, unsigned char c);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void Log::LogString(const StringType & value, size_t max_size)
|
||||
{
|
||||
size_t min_size = value.size() < max_size ? value.size() : max_size;
|
||||
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
for(size_t i=0 ; i<min_size ; ++i)
|
||||
{
|
||||
if( value[i] < 32 )
|
||||
buffer << '.';
|
||||
else
|
||||
buffer << value[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||
Log & Log::operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
buffer << buf;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
extern Log log;
|
||||
extern Log nlog;
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
|
||||
// for convenience, we have to use only #include "log.h" in the winix
|
||||
#include "slog.h"
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
83
winixd/core/logmanipulators.h
Normal file
83
winixd/core/logmanipulators.h
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_logmanipulators
|
||||
#define headerfile_winix_core_logmanipulators
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
log1 - the first level
|
||||
log2
|
||||
log3
|
||||
log4 - the last level (debug level)
|
||||
logend - the end of a line
|
||||
logendrequest - end of a current request
|
||||
logsave - current log buffer is saved and cleared
|
||||
|
||||
manipulators used by the session logger (SLog)
|
||||
loginfo - normal info to a user
|
||||
logerror - we are reporting an error
|
||||
logwarning - we are reporting a warning
|
||||
|
||||
make sure that loginfo, logerror and logwarning have values less than 32 (space)
|
||||
their are used as control codes in a string
|
||||
*/
|
||||
enum LogManipulators
|
||||
{
|
||||
log1,
|
||||
log2,
|
||||
log3,
|
||||
log4,
|
||||
logend,
|
||||
logendrequest,
|
||||
logsave,
|
||||
|
||||
loginfo,
|
||||
logerror,
|
||||
logwarning
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
1435
winixd/core/misc.cpp
Normal file
1435
winixd/core/misc.cpp
Normal file
File diff suppressed because it is too large
Load Diff
1073
winixd/core/misc.h
Normal file
1073
winixd/core/misc.h
Normal file
File diff suppressed because it is too large
Load Diff
145
winixd/core/mount.cpp
Normal file
145
winixd/core/mount.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "mount.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
Mount::Mount()
|
||||
{
|
||||
dir_id = -1;
|
||||
type = -1;
|
||||
fs = -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Mount::ClearParams()
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for(i=0 ; i<param.size() ; ++i)
|
||||
param[i].Clear();
|
||||
}
|
||||
|
||||
|
||||
bool Mount::IsPar(int code)
|
||||
{
|
||||
if( code < 0 || code >= (int)param.size() )
|
||||
return false;
|
||||
|
||||
if( !param[code].defined )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Mount::IsArg(int code, const wchar_t * arg)
|
||||
{
|
||||
ParamRow::ParamArg::iterator i;
|
||||
|
||||
if( code < 0 || code >= (int)param.size() )
|
||||
return false;
|
||||
|
||||
if( !param[code].defined )
|
||||
return false;
|
||||
|
||||
for(i=param[code].arg.begin() ; i!=param[code].arg.end() ; ++i)
|
||||
{
|
||||
if( *i == arg )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool Mount::IsArg(int code, const std::wstring & arg)
|
||||
{
|
||||
return IsArg(code, arg.c_str());
|
||||
}
|
||||
|
||||
|
||||
bool Mount::IsArg(int code, int arg)
|
||||
{
|
||||
ParamRow::ParamArg::iterator i;
|
||||
|
||||
if( code < 0 || code >= (int)param.size() )
|
||||
return false;
|
||||
|
||||
if( !param[code].defined )
|
||||
return false;
|
||||
|
||||
for(i=param[code].arg.begin() ; i!=param[code].arg.end() ; ++i)
|
||||
{
|
||||
if( Toi(*i) == arg )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
const std::wstring & Mount::Arg(int code, int arg) const
|
||||
{
|
||||
if( code < 0 || code >= (int)param.size() )
|
||||
return empty_str;
|
||||
|
||||
if( !param[code].defined )
|
||||
return empty_str;
|
||||
|
||||
if( arg >= (int)param[code].arg.size() )
|
||||
return empty_str;
|
||||
|
||||
return param[code].arg[arg];
|
||||
}
|
||||
|
||||
|
||||
const std::wstring & Mount::FirstArg(int code) const
|
||||
{
|
||||
return Arg(code, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
103
winixd/core/mount.h
Normal file
103
winixd/core/mount.h
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef headerfile_winix_core_mount
|
||||
#define headerfile_winix_core_mount
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class Mount
|
||||
{
|
||||
public:
|
||||
|
||||
struct ParamRow
|
||||
{
|
||||
typedef std::vector<std::wstring> ParamArg;
|
||||
|
||||
bool defined;
|
||||
ParamArg arg;
|
||||
|
||||
ParamRow() { defined = false; }
|
||||
void Clear() { defined = false; arg.clear(); }
|
||||
};
|
||||
|
||||
typedef std::vector<ParamRow> Param;
|
||||
|
||||
|
||||
long dir_id;
|
||||
int type;
|
||||
int fs;
|
||||
Param param;
|
||||
|
||||
|
||||
|
||||
|
||||
Mount();
|
||||
void ClearParams();
|
||||
|
||||
bool IsPar(int code);
|
||||
|
||||
bool IsArg(int code, const wchar_t * arg);
|
||||
bool IsArg(int code, const std::wstring & arg);
|
||||
bool IsArg(int code, int arg);
|
||||
|
||||
// returning the arg argument if defined (or an empty string)
|
||||
const std::wstring & Arg(int code, int arg) const;
|
||||
|
||||
// returning the first argument (arg=0) if defined (or an empty string)
|
||||
const std::wstring & FirstArg(int code) const;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
// for Arg() methods when the argument is not defined
|
||||
const std::wstring empty_str;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
528
winixd/core/mountparser.cpp
Normal file
528
winixd/core/mountparser.cpp
Normal file
@@ -0,0 +1,528 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "mountparser.h"
|
||||
#include "log.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
MountParser::MountParser()
|
||||
{
|
||||
dirs = 0;
|
||||
skip_static = false;
|
||||
mount_type_tab = 0;
|
||||
mount_fs_tab = 0;
|
||||
mount_par_tab = 0;
|
||||
static_mount_id = -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void MountParser::SkipStaticDirs(bool skip)
|
||||
{
|
||||
skip_static = skip;
|
||||
}
|
||||
|
||||
|
||||
void MountParser::SetStaticMountId(int id)
|
||||
{
|
||||
static_mount_id = id;
|
||||
}
|
||||
|
||||
|
||||
void MountParser::SetDirs(Dirs * pdirs)
|
||||
{
|
||||
dirs = pdirs;
|
||||
}
|
||||
|
||||
|
||||
void MountParser::SetMountTypeTab(const std::vector<std::wstring> & tab)
|
||||
{
|
||||
mount_type_tab = &tab;
|
||||
}
|
||||
|
||||
|
||||
void MountParser::SetMountFsTab(const std::vector<std::wstring> & tab)
|
||||
{
|
||||
mount_fs_tab = &tab;
|
||||
}
|
||||
|
||||
|
||||
void MountParser::SetMountParTab(const std::vector<std::wstring> & tab)
|
||||
{
|
||||
mount_par_tab = &tab;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool MountParser::IsWhite(int c)
|
||||
{
|
||||
if( c==' ' || c=='\t' || c==13 || c==160 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void MountParser::SkipWhite()
|
||||
{
|
||||
while( IsWhite(*pinput) )
|
||||
++pinput;
|
||||
}
|
||||
|
||||
|
||||
void MountParser::SkipLine()
|
||||
{
|
||||
while( *pinput && *pinput != 10 )
|
||||
++pinput;
|
||||
|
||||
if( *pinput == 10 )
|
||||
++pinput;
|
||||
}
|
||||
|
||||
|
||||
void MountParser::ReadWordQuote(std::wstring & res)
|
||||
{
|
||||
++pinput;
|
||||
|
||||
while( *pinput && *pinput!=10 && *pinput!='\"' )
|
||||
{
|
||||
if( pinput[0]=='\\' && pinput[1]=='\"' )
|
||||
{
|
||||
res += '\"';
|
||||
pinput += 2;
|
||||
}
|
||||
else
|
||||
if( pinput[0]=='\\' && pinput[1]=='\\' )
|
||||
{
|
||||
res += '\\';
|
||||
pinput += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
res += *pinput;
|
||||
pinput += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if( *pinput == '"' )
|
||||
++pinput;
|
||||
}
|
||||
|
||||
|
||||
// a white character is the separator
|
||||
void MountParser::ReadWordWhite(std::wstring & res)
|
||||
{
|
||||
while( *pinput && *pinput!=10 && !IsWhite(*pinput) )
|
||||
{
|
||||
res += *pinput;
|
||||
++pinput;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// the comma or the second bracket ')' are the separators
|
||||
void MountParser::ReadWordComma(std::wstring & res)
|
||||
{
|
||||
while( *pinput && *pinput!=10 && *pinput!=',' && *pinput!=')' )
|
||||
{
|
||||
res += *pinput;
|
||||
++pinput;
|
||||
}
|
||||
|
||||
// trimming last white characters
|
||||
// (white characters can be in the middle of the string)
|
||||
TrimWhite(res);
|
||||
}
|
||||
|
||||
|
||||
void MountParser::ReadWord(std::wstring & res, bool comma_bracket_separator)
|
||||
{
|
||||
res.clear();
|
||||
SkipWhite();
|
||||
|
||||
if( *pinput == '"' )
|
||||
{
|
||||
ReadWordQuote(res);
|
||||
}
|
||||
else
|
||||
if( comma_bracket_separator )
|
||||
{
|
||||
ReadWordComma(res);
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadWordWhite(res);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void MountParser::ReadParamArgsLoop(Mount::ParamRow::ParamArg & args)
|
||||
{
|
||||
SkipWhite();
|
||||
|
||||
while( *pinput && *pinput!=10 && *pinput!=')' )
|
||||
{
|
||||
ReadWord(temp_arg, true);
|
||||
|
||||
if( !temp_arg.empty() )
|
||||
args.push_back(temp_arg);
|
||||
|
||||
if( *pinput == ',' )
|
||||
++pinput;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MountParser::ReadParamArgs(Mount::ParamRow::ParamArg & args)
|
||||
{
|
||||
SkipWhite();
|
||||
args.clear();
|
||||
|
||||
|
||||
if( *pinput == '(' )
|
||||
{
|
||||
++pinput;
|
||||
ReadParamArgsLoop(args);
|
||||
|
||||
if( *pinput != ')' )
|
||||
{
|
||||
// there should be ')' at the end
|
||||
// temporarily we do nothing
|
||||
}
|
||||
else
|
||||
{
|
||||
++pinput;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void MountParser::ReadParamName(std::wstring & res)
|
||||
{
|
||||
SkipWhite();
|
||||
res.clear();
|
||||
|
||||
while( *pinput && *pinput!=10 && *pinput!=',' && *pinput!='(' && !IsWhite(*pinput) )
|
||||
{
|
||||
res += *pinput;
|
||||
++pinput;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MountParser::ReadParam(std::wstring & res, Mount::ParamRow::ParamArg & args)
|
||||
{
|
||||
ReadParamName(res);
|
||||
|
||||
if( res.empty() )
|
||||
return;
|
||||
|
||||
ReadParamArgs(args);
|
||||
|
||||
SkipWhite();
|
||||
|
||||
if( *pinput == ',' )
|
||||
++pinput;
|
||||
}
|
||||
|
||||
|
||||
int MountParser::FindIndex(const std::vector<std::wstring> * tab, const std::wstring & value)
|
||||
{
|
||||
for(size_t i=0 ; i < tab->size() ; ++i)
|
||||
{
|
||||
if( (*tab)[i] == value )
|
||||
return static_cast<int>(i);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
bool MountParser::ReadMountType()
|
||||
{
|
||||
ReadWord(temp);
|
||||
|
||||
if( temp.empty() )
|
||||
{
|
||||
// an empty line (some white characters only)
|
||||
return false;
|
||||
}
|
||||
|
||||
mount.type = FindIndex(mount_type_tab, temp);
|
||||
|
||||
if( mount.type != -1 )
|
||||
{
|
||||
log << log3 << "MP: mount type: " << (*mount_type_tab)[mount.type] << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "MP: unknown mount type: " << temp << logend;
|
||||
slog << logerror << T("unknown_mount_type") << ": " << temp << logend;
|
||||
}
|
||||
|
||||
return mount.type != -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool MountParser::ReadMountPoint()
|
||||
{
|
||||
ReadWord(last_dir);
|
||||
|
||||
pdir = dirs->GetDir(last_dir);
|
||||
|
||||
if( pdir )
|
||||
{
|
||||
mount.dir_id = pdir->id;
|
||||
log << log3 << "MP: mount point (directory): " << last_dir << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "MP: there is no such a mount point (directory): " << last_dir << logend;
|
||||
slog << logerror << T("no_such_dir") << ": " << last_dir << logend;
|
||||
}
|
||||
|
||||
return pdir != 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool MountParser::ReadFs()
|
||||
{
|
||||
ReadWord(temp);
|
||||
mount.fs = FindIndex(mount_fs_tab, temp);
|
||||
|
||||
if( mount.fs != -1 )
|
||||
{
|
||||
log << log2 << "MP: file system: " << (*mount_fs_tab)[mount.fs] << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "MP: unknown filesystem: " << temp << logend;
|
||||
slog << logerror << T("unknown_filesystem") << ": " << temp << " (" << last_dir << ")" << logend;
|
||||
}
|
||||
|
||||
return mount.fs != -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void MountParser::LogMountParams()
|
||||
{
|
||||
size_t i;
|
||||
|
||||
log << log3 << "MP: mount param: " << temp << "(";
|
||||
|
||||
for(i=0 ; i<param_args.size() ; ++i)
|
||||
{
|
||||
log << param_args[i];
|
||||
|
||||
if( i != param_args.size()-1 )
|
||||
log << ",";
|
||||
}
|
||||
|
||||
log << ")" << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void MountParser::ReadMountParams()
|
||||
{
|
||||
mount.ClearParams();
|
||||
|
||||
for( ReadParam(temp, param_args) ; !temp.empty() ; ReadParam(temp, param_args) )
|
||||
{
|
||||
int code = FindIndex(mount_par_tab, temp);
|
||||
|
||||
if( code != -1 )
|
||||
{
|
||||
mount.param[code].defined = true;
|
||||
mount.param[code].arg = param_args;
|
||||
LogMountParams();
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "MP: unknown mount param: " << temp << logend;
|
||||
slog << logwarning << T("unknown_mount_param") << ": " << temp << " (" << T("skipped") << ")" << logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void MountParser::AddParams(Mount::Param & src, Mount::Param & dst)
|
||||
{
|
||||
if( src.size() != dst.size() )
|
||||
{
|
||||
log << log1 << "MP: addparams: incorrect sizes" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
for(size_t p=0 ; p < src.size() ; ++p)
|
||||
{
|
||||
if( src[p].defined && !dst[p].defined )
|
||||
dst[p] = src[p];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool MountParser::AddParamsBefore(long dir_id)
|
||||
{
|
||||
std::map<long, Mount>::iterator i = poutput->find(dir_id);
|
||||
|
||||
if( i == poutput->end() )
|
||||
return false;
|
||||
|
||||
AddParams(i->second.param, mount_inserted.first->second.param);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
adding all non-existing parameters to this mount point from parents
|
||||
*/
|
||||
void MountParser::AddParamsBefore()
|
||||
{
|
||||
if( !pdir )
|
||||
return;
|
||||
|
||||
Item * dir;
|
||||
long dir_id = pdir->parent_id;
|
||||
|
||||
while( dir_id != -1 )
|
||||
{
|
||||
if( AddParamsBefore(dir_id) )
|
||||
{
|
||||
// we don't have to check others parents
|
||||
// the parameters are already copied
|
||||
break;
|
||||
}
|
||||
|
||||
dir = dirs->GetDir(dir_id);
|
||||
|
||||
if( !dir )
|
||||
break;
|
||||
|
||||
dir_id = dir->parent_id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
adding all non-existing parameters to childs (childs to this mount point)
|
||||
*/
|
||||
void MountParser::AddParamsAfter()
|
||||
{
|
||||
std::map<long, Mount>::iterator i = poutput->begin();
|
||||
|
||||
for( ; i != poutput->end() ; ++i)
|
||||
{
|
||||
if( dirs->IsChild(mount_inserted.first->second.dir_id, i->first) )
|
||||
AddParams(mount_inserted.first->second.param, i->second.param);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MountParser::ReadRow()
|
||||
{
|
||||
if( ReadMountType() && ReadMountPoint() && ReadFs() )
|
||||
{
|
||||
ReadMountParams();
|
||||
|
||||
if( skip_static && mount.type==static_mount_id )
|
||||
{
|
||||
log << log1 << "MP: static mount points are skipped (dont_use_static_dirs in config is true)" << logend;
|
||||
slog << logwarning << T("skipped_static_mount") << ": " << last_dir << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
mount_inserted = poutput->insert( std::make_pair(mount.dir_id, mount) );
|
||||
|
||||
if( mount_inserted.second )
|
||||
{
|
||||
AddParamsBefore();
|
||||
AddParamsAfter();
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "MP: this mount point exists (skipped)" << logend;
|
||||
slog << logwarning << T("mount_exists") << ": " << last_dir << " (" << T("skipped") << ")" << logend;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
SkipLine();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void MountParser::Parse(const std::wstring & input, std::map<long, Mount> & output)
|
||||
{
|
||||
if( !dirs || !mount_type_tab || !mount_fs_tab || !mount_par_tab )
|
||||
{
|
||||
log << log1 << "MP: input tables not set" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
pinput = input.c_str();
|
||||
poutput = &output;
|
||||
mount.param.resize(mount_par_tab->size());
|
||||
mount.ClearParams();
|
||||
poutput->clear();
|
||||
|
||||
while( *pinput )
|
||||
ReadRow();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
119
winixd/core/mountparser.h
Normal file
119
winixd/core/mountparser.h
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_mountparser
|
||||
#define headerfile_winix_core_mountparser
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "mount.h"
|
||||
#include "item.h"
|
||||
#include "dirs.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class MountParser
|
||||
{
|
||||
public:
|
||||
|
||||
MountParser();
|
||||
|
||||
void SkipStaticDirs(bool skip);
|
||||
void SetStaticMountId(int id);
|
||||
|
||||
void Parse(const std::wstring & input, std::map<long, Mount> & output);
|
||||
void SetDirs(Dirs * pdirs);
|
||||
void SetMountTypeTab(const std::vector<std::wstring> & tab);
|
||||
void SetMountFsTab(const std::vector<std::wstring> & tab);
|
||||
void SetMountParTab(const std::vector<std::wstring> & tab);
|
||||
|
||||
private:
|
||||
|
||||
Dirs * dirs;
|
||||
bool skip_static;
|
||||
int static_mount_id;
|
||||
|
||||
const std::vector<std::wstring> * mount_type_tab;
|
||||
const std::vector<std::wstring> * mount_fs_tab;
|
||||
const std::vector<std::wstring> * mount_par_tab;
|
||||
|
||||
bool IsWhite(int c);
|
||||
void SkipWhite();
|
||||
void SkipLine();
|
||||
void ReadWordQuote(std::wstring & res);
|
||||
void ReadWordWhite(std::wstring & res);
|
||||
void ReadWordComma(std::wstring & res);
|
||||
void ReadWord(std::wstring & res, bool comma_bracket_separator = false);
|
||||
void ReadParamArgsLoop(Mount::ParamRow::ParamArg & args);
|
||||
void ReadParamArgs(Mount::ParamRow::ParamArg & args);
|
||||
void ReadParamName(std::wstring & res);
|
||||
void ReadParam(std::wstring & res, Mount::ParamRow::ParamArg & args);
|
||||
int FindIndex(const std::vector<std::wstring> * tab, const std::wstring & value);
|
||||
bool ReadMountType();
|
||||
bool ReadMountPoint();
|
||||
bool ReadFs();
|
||||
void LogMountParams();
|
||||
void ReadMountParams();
|
||||
void ReadRow();
|
||||
void AddParams(Mount::Param & src, Mount::Param & dst);
|
||||
bool AddParamsBefore(long dir_id);
|
||||
void AddParamsBefore();
|
||||
void AddParamsAfter();
|
||||
|
||||
const wchar_t * pinput;
|
||||
std::wstring temp;
|
||||
std::wstring last_dir;
|
||||
std::wstring temp_arg;
|
||||
Mount::ParamRow::ParamArg param_args;
|
||||
Mount mount;
|
||||
Item * pdir;
|
||||
std::map<long, Mount> * poutput;
|
||||
std::pair<std::map<long, Mount>::iterator, bool> mount_inserted;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
357
winixd/core/mounts.cpp
Normal file
357
winixd/core/mounts.cpp
Normal file
@@ -0,0 +1,357 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "mounts.h"
|
||||
#include "request.h"
|
||||
#include "log.h"
|
||||
#include "db/db.h"
|
||||
#include "plugin.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
Mounts::Mounts()
|
||||
{
|
||||
pmount = &empty_mount;
|
||||
skip_static = false;
|
||||
}
|
||||
|
||||
|
||||
void Mounts::SkipStaticDirs(bool skip)
|
||||
{
|
||||
skip_static = skip;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Mounts::CreateMountType()
|
||||
{
|
||||
mount_type_cms = AddMountType(L"cms");
|
||||
mount_type_static = AddMountType(L"static");
|
||||
}
|
||||
|
||||
|
||||
void Mounts::CreateMountFs()
|
||||
{
|
||||
mount_fs_simplefs = AddMountFs(L"simplefs");
|
||||
mount_fs_hashfs = AddMountFs(L"hashfs");
|
||||
}
|
||||
|
||||
|
||||
void Mounts::CreateMountPar()
|
||||
{
|
||||
mount_par_page = AddMountPar(L"page");
|
||||
mount_par_thumb_size = AddMountPar(L"thumb_size");
|
||||
mount_par_thumb_mode = AddMountPar(L"thumb_mode");
|
||||
mount_par_thumb_quality = AddMountPar(L"thumb_quality");
|
||||
mount_par_image_size = AddMountPar(L"image_size");
|
||||
mount_par_image_mode = AddMountPar(L"image_mode");
|
||||
mount_par_image_quality = AddMountPar(L"image_quality");
|
||||
mount_par_emacs_on = AddMountPar(L"emacs_on");
|
||||
mount_par_mkdir_on = AddMountPar(L"mkdir_on");
|
||||
mount_par_app = AddMountPar(L"app");
|
||||
mount_par_html_template = AddMountPar(L"html_template");
|
||||
mount_par_change_template = AddMountPar(L"change_template");
|
||||
mount_par_static = AddMountPar(L"static");
|
||||
mount_par_css = AddMountPar(L"css");
|
||||
mount_par_lang = AddMountPar(L"lang");
|
||||
}
|
||||
|
||||
|
||||
void Mounts::CreateMounts()
|
||||
{
|
||||
CreateMountType();
|
||||
CreateMountFs();
|
||||
CreateMountPar();
|
||||
|
||||
plugin.Call((Session*)0, WINIX_ADD_MOUNTS);
|
||||
|
||||
empty_mount.param.resize(mount_par_tab.size());
|
||||
empty_mount.ClearParams();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Mounts::SetDirs(Dirs * pdirs)
|
||||
{
|
||||
dirs = pdirs;
|
||||
}
|
||||
|
||||
|
||||
void Mounts::SetDb(Db * pdb)
|
||||
{
|
||||
db = pdb;
|
||||
}
|
||||
|
||||
void Mounts::SetCur(Cur * pcur)
|
||||
{
|
||||
cur = pcur;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int Mounts::AddMountType(const wchar_t * type)
|
||||
{
|
||||
mount_type_tab.push_back(type);
|
||||
|
||||
return static_cast<int>(mount_type_tab.size()) - 1;
|
||||
}
|
||||
|
||||
|
||||
int Mounts::AddMountType(const std::wstring & type)
|
||||
{
|
||||
return AddMountType(type.c_str());
|
||||
}
|
||||
|
||||
|
||||
int Mounts::AddMountFs(const wchar_t * fs)
|
||||
{
|
||||
mount_fs_tab.push_back(fs);
|
||||
|
||||
return static_cast<int>(mount_fs_tab.size()) - 1;
|
||||
}
|
||||
|
||||
|
||||
const std::wstring & Mounts::GetMountType(int id)
|
||||
{
|
||||
if( id < 0 || id >= (int)mount_type_tab.size() )
|
||||
return empty_str;
|
||||
|
||||
return mount_type_tab[id];
|
||||
}
|
||||
|
||||
|
||||
int Mounts::FindMountType(const std::wstring & type)
|
||||
{
|
||||
for(size_t i=0 ; i<mount_type_tab.size() ; ++i)
|
||||
if( mount_type_tab[i] == type )
|
||||
return (int)i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int Mounts::AddMountFs(const std::wstring & fs)
|
||||
{
|
||||
return AddMountFs(fs.c_str());
|
||||
}
|
||||
|
||||
|
||||
const std::wstring & Mounts::GetMountFs(int id)
|
||||
{
|
||||
if( id < 0 || id >= (int)mount_fs_tab.size() )
|
||||
return empty_str;
|
||||
|
||||
return mount_fs_tab[id];
|
||||
}
|
||||
|
||||
|
||||
int Mounts::AddMountPar(const wchar_t * par)
|
||||
{
|
||||
mount_par_tab.push_back(par);
|
||||
|
||||
return static_cast<int>(mount_par_tab.size()) - 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int Mounts::AddMountPar(const std::wstring & par)
|
||||
{
|
||||
return AddMountPar(par.c_str());
|
||||
}
|
||||
|
||||
|
||||
const std::wstring & Mounts::GetMountPar(int id)
|
||||
{
|
||||
if( id < 0 || id >= (int)mount_par_tab.size() )
|
||||
return empty_str;
|
||||
|
||||
return mount_par_tab[id];
|
||||
}
|
||||
|
||||
|
||||
|
||||
// reading from 'mounts'
|
||||
void Mounts::ReadMounts(const std::wstring & mounts)
|
||||
{
|
||||
mount_parser.SkipStaticDirs(skip_static);
|
||||
mount_parser.SetStaticMountId(mount_type_static);
|
||||
mount_parser.SetDirs(dirs);
|
||||
mount_parser.SetMountTypeTab(mount_type_tab);
|
||||
mount_parser.SetMountFsTab(mount_fs_tab);
|
||||
mount_parser.SetMountParTab(mount_par_tab);
|
||||
|
||||
mount_parser.Parse(mounts, mount_tab);
|
||||
|
||||
CalcCurMount();
|
||||
plugin.Call((Session*)0, WINIX_FSTAB_CHANGED);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// reading from /etc/fstab
|
||||
Error Mounts::ReadMounts()
|
||||
{
|
||||
static std::wstring file = L"fstab";
|
||||
|
||||
Item * etc = dirs->GetEtcDir();
|
||||
|
||||
if( !etc )
|
||||
{
|
||||
log << log1 << "M: there is no /etc directory" << logend;
|
||||
return WINIX_ERR_NO_ITEM;
|
||||
}
|
||||
|
||||
Item fstab;
|
||||
Error err = db->GetItem(etc->id, file, fstab);
|
||||
|
||||
if( err == WINIX_ERR_NO_ITEM )
|
||||
{
|
||||
log << log1 << "M: there is no /etc/fstab file" << logend;
|
||||
return err;
|
||||
}
|
||||
|
||||
if( err != WINIX_ERR_OK )
|
||||
{
|
||||
log << log1 << "M: cannot read /etc/fstab" << logend;
|
||||
return err;
|
||||
}
|
||||
|
||||
ReadMounts(fstab.content);
|
||||
|
||||
return WINIX_ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
void Mounts::MountCmsForRoot()
|
||||
{
|
||||
Mount mount;
|
||||
mount.type = MountTypeCms();
|
||||
mount.fs = MountFsSimplefs();
|
||||
|
||||
Item * proot = dirs->GetRootDir();
|
||||
|
||||
if( proot )
|
||||
mount.dir_id = proot->id;
|
||||
else
|
||||
{
|
||||
mount.dir_id = -1;
|
||||
log << log1 << "M: there is no a root dir" << logend;
|
||||
}
|
||||
|
||||
mount.param.resize(mount_par_tab.size());
|
||||
mount.ClearParams();
|
||||
|
||||
std::pair<MountTab::iterator, bool> res = mount_tab.insert( std::make_pair(mount.dir_id, mount) );
|
||||
pmount = &(res.first->second);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Mount * Mounts::CalcCurMount()
|
||||
{
|
||||
std::vector<Item*>::reverse_iterator i;
|
||||
|
||||
pmount = &empty_mount;
|
||||
|
||||
// when the program starts (when the dir_tab is empty()
|
||||
// we don't want to call MountCmsForRoot()
|
||||
if( cur->request->dir_tab.empty() )
|
||||
return pmount;
|
||||
|
||||
for(i = cur->request->dir_tab.rbegin() ; i!=cur->request->dir_tab.rend() ; ++i)
|
||||
{
|
||||
std::map<long, Mount>::iterator m = mount_tab.find( (*i)->id );
|
||||
|
||||
if( m != mount_tab.end() )
|
||||
{
|
||||
pmount = &(m->second);
|
||||
log << log2 << "M: current mount point is: " << GetMountType(pmount->type)
|
||||
<< ", fs: " << GetMountFs(pmount->fs) << logend;
|
||||
return pmount;
|
||||
}
|
||||
}
|
||||
|
||||
// if nothing was found
|
||||
// we assume that 'cms' mount point is used
|
||||
MountCmsForRoot();
|
||||
log << log2 << "M: current mount point is: " << GetMountType(pmount->type) << " (default)"
|
||||
<< ", fs: " << GetMountFs(pmount->fs) << logend;
|
||||
|
||||
return pmount;
|
||||
}
|
||||
|
||||
|
||||
// can return null pointer
|
||||
// and we don't assume cms as a default mount point if nothing is found
|
||||
Mount * Mounts::CalcMount(long dir_id)
|
||||
{
|
||||
while( true )
|
||||
{
|
||||
Item * pdir = dirs->GetDir(dir_id);
|
||||
|
||||
if( !pdir )
|
||||
return 0;
|
||||
|
||||
std::map<long, Mount>::iterator m = mount_tab.find( pdir->id );
|
||||
|
||||
if( m != mount_tab.end() )
|
||||
return &(m->second);
|
||||
|
||||
dir_id = pdir->parent_id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const Mounts::MountTab * Mounts::GetMountTab()
|
||||
{
|
||||
return &mount_tab;
|
||||
}
|
||||
|
||||
|
||||
Mount * Mounts::GetEmptyMount()
|
||||
{
|
||||
return &empty_mount;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
202
winixd/core/mounts.h
Normal file
202
winixd/core/mounts.h
Normal file
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef headerfile_winix_core_mounts
|
||||
#define headerfile_winix_core_mounts
|
||||
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "mount.h"
|
||||
#include "error.h"
|
||||
#include "dirs.h"
|
||||
#include "db/db.h"
|
||||
#include "request.h"
|
||||
#include "mountparser.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
class Mounts
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
void SkipStaticDirs(bool skip);
|
||||
|
||||
/*
|
||||
mount point's types
|
||||
*/
|
||||
int AddMountType(const wchar_t * type);
|
||||
int AddMountType(const std::wstring & type);
|
||||
const std::wstring & GetMountType(int id);
|
||||
|
||||
// id of a specific mount type (the id is always valid)
|
||||
int MountTypeCms() { return mount_type_cms; }
|
||||
int MountTypeStatic() { return mount_type_static; }
|
||||
|
||||
// return -1 if there is no such a mount type
|
||||
// or index otherwhise
|
||||
int FindMountType(const std::wstring & type);
|
||||
|
||||
|
||||
/*
|
||||
file systems
|
||||
*/
|
||||
int AddMountFs(const wchar_t * fs);
|
||||
int AddMountFs(const std::wstring & fs);
|
||||
const std::wstring & GetMountFs(int id);
|
||||
|
||||
// id of a specific file system (the id is always valid)
|
||||
int MountFsSimplefs() { return mount_fs_simplefs; }
|
||||
int MountFsHashfs() { return mount_fs_hashfs; }
|
||||
|
||||
|
||||
/*
|
||||
mount point's parameters
|
||||
*/
|
||||
int AddMountPar(const wchar_t * par);
|
||||
int AddMountPar(const std::wstring & par);
|
||||
const std::wstring & GetMountPar(int id);
|
||||
|
||||
int MountParPage() { return mount_par_page; }
|
||||
int MountParThumbSize() { return mount_par_thumb_size; }
|
||||
int MountParThumbMode() { return mount_par_thumb_mode; }
|
||||
int MountParThumbQuality() { return mount_par_thumb_quality; }
|
||||
int MountParImageSize() { return mount_par_image_size; }
|
||||
int MountParImageMode() { return mount_par_image_mode; }
|
||||
int MountParImageQuality() { return mount_par_image_quality; }
|
||||
int MountParEmacsOn() { return mount_par_emacs_on; }
|
||||
int MountParMkdirOn() { return mount_par_mkdir_on; }
|
||||
int MountParApp() { return mount_par_app; }
|
||||
int MountParHtmlTemplate() { return mount_par_html_template; }
|
||||
int MountParChangeTemplate() { return mount_par_change_template; }
|
||||
int MountParStatic() { return mount_par_static; }
|
||||
int MountParCss() { return mount_par_css; }
|
||||
int MountParLang() { return mount_par_lang; }
|
||||
|
||||
void SetDirs(Dirs * pdirs);
|
||||
void SetDb(Db * pdb);
|
||||
void SetCur(Cur * pcur);
|
||||
|
||||
// dir_id, mount_point
|
||||
typedef std::map<long, Mount> MountTab;
|
||||
|
||||
Mounts();
|
||||
void CreateMounts();
|
||||
|
||||
void ReadMounts(const std::wstring & mounts);
|
||||
Error ReadMounts();
|
||||
|
||||
Mount * CalcCurMount();
|
||||
Mount * CalcMount(long dir_id);
|
||||
|
||||
// current mount point
|
||||
// will not be null after calling CalcCurMount() or ReadMounts([...])
|
||||
// !! nie korzystac obecnie z niego
|
||||
// korzystac z cur->mount
|
||||
// a tez zostanie wycofany
|
||||
Mount * pmount;
|
||||
|
||||
|
||||
const MountTab * GetMountTab();
|
||||
|
||||
// at the beginning used to initialize cur->mount
|
||||
Mount * GetEmptyMount();
|
||||
|
||||
private:
|
||||
|
||||
Db * db;
|
||||
Dirs * dirs;
|
||||
Cur * cur;
|
||||
|
||||
bool skip_static;
|
||||
|
||||
Mount empty_mount;
|
||||
const std::wstring empty_str;
|
||||
|
||||
MountParser mount_parser;
|
||||
|
||||
std::vector<std::wstring> mount_type_tab;
|
||||
|
||||
int mount_type_cms;
|
||||
int mount_type_static;
|
||||
|
||||
// simplefs
|
||||
// hashfs
|
||||
std::vector<std::wstring> mount_fs_tab;
|
||||
|
||||
int mount_fs_simplefs;
|
||||
int mount_fs_hashfs;
|
||||
|
||||
std::vector<std::wstring> mount_par_tab;
|
||||
|
||||
int mount_par_page;
|
||||
int mount_par_thumb_size;
|
||||
int mount_par_thumb_mode;
|
||||
int mount_par_thumb_quality;
|
||||
int mount_par_image_size;
|
||||
int mount_par_image_mode;
|
||||
int mount_par_image_quality;
|
||||
int mount_par_emacs_on;
|
||||
int mount_par_mkdir_on;
|
||||
int mount_par_app;
|
||||
int mount_par_html_template;
|
||||
int mount_par_change_template;
|
||||
int mount_par_static;
|
||||
int mount_par_css;
|
||||
int mount_par_lang;
|
||||
|
||||
|
||||
MountTab mount_tab;
|
||||
|
||||
void CreateMountType();
|
||||
void CreateMountFs();
|
||||
void CreateMountPar();
|
||||
|
||||
void MountCmsForRoot();
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
528
winixd/core/plugin.cpp
Normal file
528
winixd/core/plugin.cpp
Normal file
@@ -0,0 +1,528 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <string.h>
|
||||
#include "plugin.h"
|
||||
#include "pluginmsg.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
void Plugin::UnloadPlugins()
|
||||
{
|
||||
size_t i;
|
||||
|
||||
slots.clear();
|
||||
|
||||
for(i=0 ; i<plugins.size() ; ++i)
|
||||
dlclose(plugins[i].handle);
|
||||
|
||||
plugins.clear();
|
||||
}
|
||||
|
||||
|
||||
Plugin::Plugin()
|
||||
{
|
||||
current_plugin = -1;
|
||||
|
||||
db = 0;
|
||||
config = 0;
|
||||
cur = 0;
|
||||
system = 0;
|
||||
functions = 0;
|
||||
templates = 0;
|
||||
synchro = 0;
|
||||
session_manager = 0;
|
||||
}
|
||||
|
||||
|
||||
Plugin::~Plugin()
|
||||
{
|
||||
UnloadPlugins();
|
||||
}
|
||||
|
||||
|
||||
void Plugin::SetDb(Db * pdb)
|
||||
{
|
||||
db = pdb;
|
||||
}
|
||||
|
||||
void Plugin::SetConfig(Config * pconfig)
|
||||
{
|
||||
config = pconfig;
|
||||
}
|
||||
|
||||
|
||||
void Plugin::SetCur(Cur * pcur)
|
||||
{
|
||||
cur = pcur;
|
||||
}
|
||||
|
||||
|
||||
void Plugin::SetSystem(System * psystem)
|
||||
{
|
||||
system = psystem;
|
||||
}
|
||||
|
||||
|
||||
void Plugin::SetFunctions(Functions * pfunctions)
|
||||
{
|
||||
functions = pfunctions;
|
||||
}
|
||||
|
||||
|
||||
void Plugin::SetTemplates(Templates * ptemplates)
|
||||
{
|
||||
templates = ptemplates;
|
||||
}
|
||||
|
||||
|
||||
void Plugin::SetSynchro(Synchro * psynchro)
|
||||
{
|
||||
synchro = psynchro;
|
||||
}
|
||||
|
||||
|
||||
void Plugin::SetSessionManager(SessionManager * psession_manager)
|
||||
{
|
||||
session_manager = psession_manager;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Plugin::Lock()
|
||||
{
|
||||
if( synchro )
|
||||
synchro->Lock();
|
||||
}
|
||||
|
||||
|
||||
void Plugin::Unlock()
|
||||
{
|
||||
if( synchro )
|
||||
synchro->Unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool Plugin::SetPointers(PluginInfo & info)
|
||||
{
|
||||
// for safety we call a plugin function only when all our pointers are not null
|
||||
bool res = (db && config && cur && system && functions && templates && synchro && session_manager);
|
||||
|
||||
if( !res )
|
||||
{
|
||||
Lock();
|
||||
log << log1 << "Plugin: cannot call a function - some of the winix pointers are null" << logend;
|
||||
Unlock();
|
||||
}
|
||||
|
||||
info.db = db;
|
||||
info.config = config;
|
||||
info.cur = cur;
|
||||
info.system = system;
|
||||
info.functions = functions;
|
||||
info.templates = templates;
|
||||
info.synchro = synchro;
|
||||
info.session_manager = session_manager;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
void Plugin::LoadPlugins(const std::wstring & plugins_dir, const std::vector<std::wstring> & plugins)
|
||||
{
|
||||
for(size_t i=0 ; i<plugins.size() ; ++i)
|
||||
{
|
||||
if( !plugins[i].empty() && plugins[i][0] == '/' )
|
||||
{
|
||||
LoadPlugin(plugins[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
temp_path = plugins_dir;
|
||||
temp_path += '/';
|
||||
temp_path += plugins[i];
|
||||
LoadPlugin(temp_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// we don't have to use Lock() here because plugins are read
|
||||
// before threads are started
|
||||
void * Plugin::LoadInitFun(const wchar_t * filename, Fun1 & fun_init)
|
||||
{
|
||||
char file[WINIX_OS_PATH_SIZE];
|
||||
|
||||
if( !WideToUTF8(filename, file, WINIX_OS_PATH_SIZE) )
|
||||
return 0;
|
||||
|
||||
void * p = dlopen(file, RTLD_NOW | RTLD_LOCAL);
|
||||
|
||||
if( !p )
|
||||
{
|
||||
log << log1 << "Plugin: cannot load a plugin: \"" << filename << "\"" << logend;
|
||||
log << log1 << "Plugin: dlerror: " << dlerror() << logend;
|
||||
return 0;
|
||||
}
|
||||
|
||||
fun_init = (Fun1)dlsym(p, "Init");
|
||||
|
||||
if( !fun_init )
|
||||
{
|
||||
log << log1 << "Plugin: cannot load a plugin: " << filename
|
||||
<< " (there is no Init() function)" << logend;
|
||||
|
||||
dlclose(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
log << log2 << "Plugin: plugin loaded"
|
||||
<< ", file: " << filename
|
||||
<< ", index: " << plugins.size() << logend;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Plugin::LoadPlugin(const wchar_t * filename)
|
||||
{
|
||||
Fun1 fun_init;
|
||||
void * plugin_handle;
|
||||
int old_current_plugin;
|
||||
PluginInfo info;
|
||||
|
||||
if( !SetPointers(info) )
|
||||
return;
|
||||
|
||||
if( !(plugin_handle = LoadInitFun(filename, fun_init)) )
|
||||
return;
|
||||
|
||||
info.Clear();
|
||||
old_current_plugin = current_plugin;
|
||||
current_plugin = (int)plugins.size();
|
||||
info.plugin_id = current_plugin;
|
||||
|
||||
fun_init(info);
|
||||
|
||||
PluginsItem item;
|
||||
item.handle = plugin_handle;
|
||||
item.plugin_name = reinterpret_cast<const wchar_t *>(info.p1);
|
||||
|
||||
plugins.push_back(item);
|
||||
|
||||
current_plugin = old_current_plugin;
|
||||
}
|
||||
|
||||
|
||||
void Plugin::LoadPlugin(const std::wstring & filename)
|
||||
{
|
||||
LoadPlugin(filename.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Plugin::HasPlugin(const wchar_t * name)
|
||||
{
|
||||
if( *name == 0 )
|
||||
return false;
|
||||
|
||||
for(size_t i=0 ; i<plugins.size() ; ++i)
|
||||
{
|
||||
if( plugins[i].plugin_name && Equal(plugins[i].plugin_name, name) )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Plugin::HasPlugin(const std::wstring & name)
|
||||
{
|
||||
return HasPlugin(name.c_str());
|
||||
}
|
||||
|
||||
|
||||
bool Plugin::HasMessage(int message)
|
||||
{
|
||||
return (slots.find(message) != slots.end());
|
||||
}
|
||||
|
||||
|
||||
void Plugin::Call(Session * ses, int message, Slots::iterator & slot, PluginInfo & info)
|
||||
{
|
||||
if( !SetPointers(info) )
|
||||
return;
|
||||
|
||||
current_plugin = slot->second.index;
|
||||
info.plugin_id = current_plugin;
|
||||
info.session = ses;
|
||||
|
||||
if( current_plugin != -1 && ses )
|
||||
info.plugin_data_base = ses->plugin_data.Get(current_plugin);
|
||||
else
|
||||
info.plugin_data_base = 0;
|
||||
|
||||
if( !slot->second.is_running )
|
||||
{
|
||||
if( config->log_plugin_call )
|
||||
{
|
||||
Lock();
|
||||
log << log1 << "Plugin: calling plugin id: " << slot->second.index << ", message: " << message << logend;
|
||||
Unlock();
|
||||
}
|
||||
|
||||
slot->second.is_running = true;
|
||||
|
||||
if( slot->second.fun1 )
|
||||
slot->second.fun1(info);
|
||||
|
||||
if( slot->second.fun2 )
|
||||
slot->second.fun2();
|
||||
|
||||
slot->second.is_running = false;
|
||||
|
||||
if( config->log_plugin_call )
|
||||
{
|
||||
Lock();
|
||||
log << log1 << "Plugin: returning from plugin id: " << slot->second.index << ", message: " << message
|
||||
<< ", result: " << (info.res? "true" : "false") << logend;
|
||||
Unlock();
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
log << log1 << "Plugin: id: " << slot->second.index
|
||||
<< ", message: " << message
|
||||
<< ", recurrences are not allowed" << logend;
|
||||
Unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PluginRes Plugin::Call(Session * ses, int message, void * p1_, void * p2_, long l1_, long l2_)
|
||||
{
|
||||
PluginRes res;
|
||||
int old_current_plugin = current_plugin;
|
||||
PluginInfo info;
|
||||
|
||||
Slots::iterator i = slots.lower_bound(message);
|
||||
|
||||
for( ; i!=slots.end() && i->first==message ; ++i )
|
||||
{
|
||||
info.Clear();
|
||||
info.p1 = p1_;
|
||||
info.p2 = p2_;
|
||||
info.l1 = l1_;
|
||||
info.l2 = l2_;
|
||||
|
||||
Call(ses, message, i, info);
|
||||
|
||||
if( info.res )
|
||||
++res.res_true;
|
||||
else
|
||||
++res.res_false;
|
||||
}
|
||||
|
||||
current_plugin = old_current_plugin;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(int message)
|
||||
{
|
||||
return Call(cur->session, message, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(int message, void * p1_)
|
||||
{
|
||||
return Call(cur->session, message, p1_, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(int message, void * p1_, void * p2_)
|
||||
{
|
||||
return Call(cur->session, message, p1_, p2_, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(int message, long l1_)
|
||||
{
|
||||
return Call(cur->session, message, 0, 0, l1_, 0);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(int message, long l1_, long l2_)
|
||||
{
|
||||
return Call(cur->session, message, 0, 0, l1_, l2_);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(int message, void * p1_, long l1_)
|
||||
{
|
||||
return Call(cur->session, message, p1_, 0, l1_, 0);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(int message, void * p1_, long l1_, long l2_)
|
||||
{
|
||||
return Call(cur->session, message, p1_, 0, l1_, l2_);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(int message, void * p1_, void * p2_, long l1_)
|
||||
{
|
||||
return Call(cur->session, message, p1_, p2_, l1_, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
PluginRes Plugin::Call(Session * ses, int message)
|
||||
{
|
||||
return Call(ses, message, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(Session * ses, int message, void * p1_)
|
||||
{
|
||||
return Call(ses, message, p1_, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(Session * ses, int message, void * p1_, void * p2_)
|
||||
{
|
||||
return Call(ses, message, p1_, p2_, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(Session * ses, int message, long l1_)
|
||||
{
|
||||
return Call(ses, message, 0, 0, l1_, 0);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(Session * ses, int message, long l1_, long l2_)
|
||||
{
|
||||
return Call(ses, message, 0, 0, l1_, l2_);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(Session * ses, int message, void * p1_, long l1_)
|
||||
{
|
||||
return Call(ses, message, p1_, 0, l1_, 0);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(Session * ses, int message, void * p1_, long l1_, long l2_)
|
||||
{
|
||||
return Call(ses, message, p1_, 0, l1_, l2_);
|
||||
}
|
||||
|
||||
|
||||
PluginRes Plugin::Call(Session * ses, int message, void * p1_, void * p2_, long l1_)
|
||||
{
|
||||
return Call(ses, message, p1_, p2_, l1_, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
size_t Plugin::Size()
|
||||
{
|
||||
return plugins.size();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
!! IMPROVE ME
|
||||
Assign() can work only if other threads are not started
|
||||
we can add some barrier/flag so when other threads starts
|
||||
then we cannot use Assign() method
|
||||
*/
|
||||
void Plugin::Assign(int message, Fun1 fun1)
|
||||
{
|
||||
Slot s;
|
||||
|
||||
if( current_plugin == -1 )
|
||||
return;
|
||||
|
||||
s.fun1 = fun1;
|
||||
s.index = current_plugin;
|
||||
|
||||
slots.insert( std::make_pair(message, s) );
|
||||
log << log3 << "Plugin: added function for message: " << message << ", plugin index: " << s.index << logend;
|
||||
}
|
||||
|
||||
|
||||
void Plugin::Assign(int message, Fun2 fun2)
|
||||
{
|
||||
Slot s;
|
||||
|
||||
if( current_plugin == -1 )
|
||||
return;
|
||||
|
||||
s.fun2 = fun2;
|
||||
s.index = current_plugin;
|
||||
|
||||
slots.insert( std::make_pair(message, s) );
|
||||
log << log3 << "Plugin: added function for message: " << message << ", plugin index: " << s.index << logend;
|
||||
}
|
||||
|
||||
|
||||
const Plugin::Plugins * Plugin::GetPlugins()
|
||||
{
|
||||
return &plugins;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
281
winixd/core/plugin.h
Normal file
281
winixd/core/plugin.h
Normal file
@@ -0,0 +1,281 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_plugin
|
||||
#define headerfile_winix_core_plugin
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include "pluginmsg.h"
|
||||
#include "log.h"
|
||||
#include "plugindata.h"
|
||||
#include "config.h"
|
||||
#include "request.h"
|
||||
#include "system.h"
|
||||
#include "sessionmanager.h"
|
||||
#include "synchro.h"
|
||||
#include "functions/functions.h"
|
||||
#include "templates/templates.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
all your plugin functions can have signature either:
|
||||
void my_function(PluginInfo & info); or
|
||||
void my_function();
|
||||
|
||||
only the main Init should have:
|
||||
extern "C" void Init(PluginFunction & info);
|
||||
|
||||
in the Init you can add your own functions by using plugin.Assign() method
|
||||
and you can set the name of the plugin by setting info.p1 pointer
|
||||
to a string buffer (const wchar_t *)
|
||||
(this buffer will not be copied so it should not be destroyed after Init finishes)
|
||||
also in Init you can only use logger (log) info.config and info.db objects
|
||||
(the rest winix objects are not initialized yet)
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
struct PluginInfo
|
||||
{
|
||||
// these variables are used for some purposes
|
||||
// depending on a hook in which they are used
|
||||
void * p1;
|
||||
void * p2;
|
||||
long l1;
|
||||
long l2;
|
||||
|
||||
// unique plugin identifier
|
||||
int plugin_id;
|
||||
|
||||
// objects from winix which are accessible from a plugin
|
||||
Db * db;
|
||||
Config * config;
|
||||
Cur * cur;
|
||||
System * system;
|
||||
Functions * functions;
|
||||
Templates * templates;
|
||||
Synchro * synchro;
|
||||
SessionManager * session_manager;
|
||||
|
||||
// a session
|
||||
// some messages are sent in a session's context e.g. logging a user
|
||||
// this pointer in not always the same as cur->session, it is preferred
|
||||
// to use this pointer instead of cur->session
|
||||
// (cur->session can point at a temporary object)
|
||||
// this pointer can be null
|
||||
Session * session;
|
||||
|
||||
// pointer to the plugin session (can be null if not set by the plugin or if session is null)
|
||||
// this is taken from session->plugin_data.Get()
|
||||
// you should use WINIX_SESSION_CREATED and WINIX_PLUGIN_SESSION_DATA_REMOVE
|
||||
// to create your plugin's session data
|
||||
PluginDataBase * plugin_data_base; // !! zmienic nazwe na plugin_session_base ? a moze session_base; a moze plugin_session?
|
||||
|
||||
// function return status
|
||||
// default: false (if not set by the plugin)
|
||||
bool res;
|
||||
|
||||
|
||||
void Clear()
|
||||
{
|
||||
// pointers to winix objects are not cleared here
|
||||
|
||||
p1 = 0;
|
||||
p2 = 0;
|
||||
l1 = 0;
|
||||
l2 = 0;
|
||||
|
||||
plugin_id = -1;
|
||||
session = 0;
|
||||
plugin_data_base = 0;
|
||||
|
||||
res = false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
this structure tells how many plugins returned true and false
|
||||
*/
|
||||
struct PluginRes
|
||||
{
|
||||
int res_false;
|
||||
int res_true;
|
||||
|
||||
PluginRes()
|
||||
{
|
||||
res_false = 0;
|
||||
res_true = 0;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
class Plugin
|
||||
{
|
||||
public:
|
||||
|
||||
// index of a plugin which is called by Call() method
|
||||
// normally: -1
|
||||
int current_plugin;
|
||||
|
||||
// Fun is a type of a function you should provide in your plugin
|
||||
typedef void (*Fun1)(PluginInfo &);
|
||||
typedef void (*Fun2)(void);
|
||||
|
||||
|
||||
struct Slot
|
||||
{
|
||||
Fun1 fun1;
|
||||
Fun2 fun2;
|
||||
int index; // plugin index (which plugin has inserted the slot)
|
||||
bool is_running;
|
||||
|
||||
Slot()
|
||||
{
|
||||
fun1 = 0;
|
||||
fun2 = 0;
|
||||
index = -1;
|
||||
is_running = false;
|
||||
}
|
||||
};
|
||||
|
||||
struct PluginsItem
|
||||
{
|
||||
void * handle;
|
||||
const wchar_t * plugin_name; // plugin name (can be null if was not set by the plugin)
|
||||
};
|
||||
|
||||
typedef std::vector<PluginsItem> Plugins;
|
||||
|
||||
|
||||
Plugin();
|
||||
~Plugin();
|
||||
|
||||
void SetDb(Db * pdb);
|
||||
void SetConfig(Config * pconfig);
|
||||
void SetCur(Cur * pcur);
|
||||
void SetSystem(System * psystem);
|
||||
void SetFunctions(Functions * pfunctions);
|
||||
void SetTemplates(Templates * ptemplates);
|
||||
void SetSynchro(Synchro * psynchro);
|
||||
void SetSessionManager(SessionManager * psession_manager);
|
||||
|
||||
void LoadPlugin(const wchar_t * filename);
|
||||
void LoadPlugin(const std::wstring & filename);
|
||||
|
||||
void LoadPlugins(const std::wstring & plugins_dir, const std::vector<std::wstring> & plugins);
|
||||
void UnloadPlugins();
|
||||
|
||||
bool HasPlugin(const wchar_t * name);
|
||||
bool HasPlugin(const std::wstring & name);
|
||||
|
||||
bool HasMessage(int message);
|
||||
|
||||
PluginRes Call(int message);
|
||||
PluginRes Call(int message, void * p1_);
|
||||
PluginRes Call(int message, void * p1_, void * p2_);
|
||||
PluginRes Call(int message, long l1_);
|
||||
PluginRes Call(int message, long l1_, long l2_);
|
||||
PluginRes Call(int message, void * p1_, long l1_);
|
||||
PluginRes Call(int message, void * p1_, long l1_, long l2_);
|
||||
PluginRes Call(int message, void * p1_, void * p2_, long l1_);
|
||||
PluginRes Call(Session * ses, int message, void * p1_, void * p2_, long l1_, long l2_);
|
||||
|
||||
PluginRes Call(Session * ses, int message);
|
||||
PluginRes Call(Session * ses, int message, void * p1_);
|
||||
PluginRes Call(Session * ses, int message, void * p1_, void * p2_);
|
||||
PluginRes Call(Session * ses, int message, long l1_);
|
||||
PluginRes Call(Session * ses, int message, long l1_, long l2_);
|
||||
PluginRes Call(Session * ses, int message, void * p1_, long l1_);
|
||||
PluginRes Call(Session * ses, int message, void * p1_, long l1_, long l2_);
|
||||
PluginRes Call(Session * ses, int message, void * p1_, void * p2_, long l1_);
|
||||
|
||||
// how many plugins there are
|
||||
size_t Size();
|
||||
|
||||
// assign a function to a message
|
||||
// you can assign more than one function to a specific message
|
||||
void Assign(int message, Fun1);
|
||||
void Assign(int message, Fun2);
|
||||
|
||||
// return a const pointer to the plugin tab
|
||||
const Plugins * GetPlugins();
|
||||
|
||||
private:
|
||||
|
||||
Db * db;
|
||||
Config * config;
|
||||
Cur * cur;
|
||||
System * system;
|
||||
Functions * functions;
|
||||
Templates * templates;
|
||||
Synchro * synchro;
|
||||
SessionManager * session_manager;
|
||||
|
||||
std::wstring temp_path; // used when loading plugins
|
||||
|
||||
Plugins plugins;
|
||||
|
||||
typedef std::multimap<int, Slot> Slots;
|
||||
Slots slots;
|
||||
|
||||
void * LoadInitFun(const wchar_t * filename, Fun1 & fun_init);
|
||||
void Call(Session * ses, int message, Slots::iterator & slot, PluginInfo & info);
|
||||
|
||||
bool SetPointers(PluginInfo & info);
|
||||
void Lock();
|
||||
void Unlock();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
extern Plugin plugin;
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
189
winixd/core/plugindata.cpp
Normal file
189
winixd/core/plugindata.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "plugindata.h"
|
||||
#include "plugin.h"
|
||||
#include "log.h"
|
||||
#include "session.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
PluginData::PluginData()
|
||||
{
|
||||
session = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PluginData::PluginData(const PluginData & p)
|
||||
{
|
||||
operator=(p);
|
||||
}
|
||||
|
||||
|
||||
|
||||
PluginData & PluginData::operator=(const PluginData & p)
|
||||
{
|
||||
// we don't copy all pointers - only resize the table
|
||||
// pointers will be set to zero
|
||||
Resize(p.Size());
|
||||
session = 0;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PluginData::~PluginData()
|
||||
{
|
||||
DeleteAll();
|
||||
}
|
||||
|
||||
|
||||
void PluginData::SetSession(Session * ses)
|
||||
{
|
||||
session = ses;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PluginData::Assign(size_t index, PluginDataBase * data)
|
||||
{
|
||||
if( index >= table.size() )
|
||||
Resize(index+1);
|
||||
|
||||
table[index] = data;
|
||||
}
|
||||
|
||||
|
||||
void PluginData::Assign(PluginDataBase * data)
|
||||
{
|
||||
if( plugin.current_plugin == -1 )
|
||||
{
|
||||
log << log1 << "PD: Assign(PluginDataBase*) should be called only from plugins" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
Assign(plugin.current_plugin, data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
PluginDataBase * PluginData::Get(size_t index)
|
||||
{
|
||||
if( index >= table.size() )
|
||||
Resize(index+1);
|
||||
|
||||
return table[index];
|
||||
}
|
||||
|
||||
|
||||
PluginDataBase * PluginData::Get()
|
||||
{
|
||||
if( plugin.current_plugin == -1 )
|
||||
{
|
||||
log << log1 << "PD: Get() should be called only from plugins" << logend;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Get(plugin.current_plugin);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void PluginData::DeleteAll()
|
||||
{
|
||||
bool all_null = true;
|
||||
|
||||
/*
|
||||
when we copy a session's object (and this object then)
|
||||
we resize the table and there are only null pointers there
|
||||
consequently if all pointers are null there is no sens
|
||||
to send WINIX_PLUGIN_SESSION_DATA_REMOVE
|
||||
*/
|
||||
|
||||
for(size_t i=0 ; i<table.size() ; ++i)
|
||||
{
|
||||
if( table[i] != 0 )
|
||||
{
|
||||
all_null = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
in the future this message may be removed
|
||||
and we directly 'delete' the pointers
|
||||
*/
|
||||
|
||||
if( !all_null )
|
||||
plugin.Call(session, WINIX_PLUGIN_SESSION_DATA_REMOVE);
|
||||
|
||||
table.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
size_t PluginData::Size() const
|
||||
{
|
||||
return table.size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PluginData::Resize(size_t new_size)
|
||||
{
|
||||
size_t old_size = table.size();
|
||||
|
||||
if( old_size == new_size )
|
||||
return;
|
||||
|
||||
table.resize(new_size);
|
||||
|
||||
for(size_t i = old_size ; i<new_size ; ++i)
|
||||
table[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
102
winixd/core/plugindata.h
Normal file
102
winixd/core/plugindata.h
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_plugindata
|
||||
#define headerfile_winix_core_plugindata
|
||||
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
struct Session;
|
||||
|
||||
|
||||
struct PluginDataBase
|
||||
{
|
||||
virtual ~PluginDataBase() {}
|
||||
|
||||
/*
|
||||
!! CHECK ME
|
||||
it is still in use?
|
||||
|
||||
when deleting sessions we first call Clear() method
|
||||
consequently the destructor has nothing to do
|
||||
(and it does not throw an exception)
|
||||
*/
|
||||
virtual void Clear() {}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
class PluginData
|
||||
{
|
||||
public:
|
||||
|
||||
PluginData();
|
||||
PluginData(const PluginData & p);
|
||||
PluginData & operator=(const PluginData & p);
|
||||
~PluginData();
|
||||
|
||||
void SetSession(Session * ses);
|
||||
|
||||
void Assign(size_t index, PluginDataBase * data);
|
||||
void Assign(PluginDataBase * data);
|
||||
|
||||
PluginDataBase * Get(size_t index);
|
||||
PluginDataBase * Get();
|
||||
|
||||
void DeleteAll();
|
||||
|
||||
size_t Size() const;
|
||||
void Resize(size_t new_size);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Session * session;
|
||||
std::vector<PluginDataBase*> table;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
332
winixd/core/pluginmsg.h
Normal file
332
winixd/core/pluginmsg.h
Normal file
@@ -0,0 +1,332 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2015, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef headerfile_winix_core_pluginmsg
|
||||
#define headerfile_winix_core_pluginmsg
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
// here you can add your own EZC functions ([function])
|
||||
// PluginInfo.p1 is a pointer to Ezc::Functions object
|
||||
// PluginInfo.p2 is a pointer to Ezc::Objects object
|
||||
// session pointer is null
|
||||
#define WINIX_TEMPLATES_CREATEFUNCTIONS 999
|
||||
|
||||
// here you can add your own EZC functions to notify system
|
||||
// warning: this functions will be called from an other thread
|
||||
// so you should use synchro->Lock() and synchro->Unlock()
|
||||
// when attempting to winix objects
|
||||
// PluginInfo.p1 is a pointer to Ezc::Functions object
|
||||
// which is defined as:
|
||||
// Ezc::Functions<NotifyStream> ezc_functions;
|
||||
// and Notify Stream is:
|
||||
// typedef TextStream<std::wstring> NotifyStream;
|
||||
// session pointer is null
|
||||
#define WINIX_NOTIFY_TEMPLATES_CREATEFUNCTIONS 998
|
||||
|
||||
// winix function and parameters have been parsed
|
||||
// the request.status is OK
|
||||
// (the winix function was not called yet)
|
||||
#define WINIX_PREPARE_REQUEST 20000
|
||||
|
||||
// post and get functions have done their jobs
|
||||
// now you can act
|
||||
// this is called only if the request.status is OK
|
||||
#define WINIX_PROCESS_REQUEST 20010
|
||||
|
||||
// prepere your content for displaying
|
||||
// this is called after WINIX_PROCESS_REQUEST
|
||||
// and when there is not a redirect
|
||||
// request.status is not checked here
|
||||
#define WINIX_CONTENT_MAKE 20020
|
||||
|
||||
// here you can attach your own session data (based on PluginDataBase class)
|
||||
// call cur->session->plugin_data.Assign(pointer)
|
||||
#define WINIX_SESSION_CREATED 30000
|
||||
|
||||
// here you should remove your session data
|
||||
// this message can be sent even if you don't assing your plugin data
|
||||
#define WINIX_PLUGIN_SESSION_DATA_REMOVE 30010
|
||||
|
||||
// when a session is changed (you can save a pointer to your data here)
|
||||
// 'session changed' means that there is a new request
|
||||
// !! IMPROVE ME it has to be changed to a better name
|
||||
#define WINIX_SESSION_CHANGED 30020
|
||||
|
||||
// a session has changed its id
|
||||
// it is typically when you are logging in
|
||||
// 'login' winix function will change the session id (for security reasons)
|
||||
// in l1 you have the old id, in l2 you have the new id
|
||||
#define WINIX_SESSION_CHANGED_ID 30025
|
||||
|
||||
// a session is going to be removed
|
||||
// it is called from session manager's thread (with lock/unlock)
|
||||
#define WINIX_PREPARE_SESSION_TO_REMOVE 30027
|
||||
|
||||
// a session has been removed
|
||||
// it is called from session manager's thread (with lock/unlock)
|
||||
// in l1 you have the old session id
|
||||
#define WINIX_SESSION_REMOVED 30029
|
||||
|
||||
// the winix is closing
|
||||
// there is not any sessions available (cur->session is null)
|
||||
// session pointer is null
|
||||
#define WINIX_CLOSE 30040
|
||||
|
||||
// preparing to remove a file (rm function)
|
||||
// in p1 you have a pointer to the Item struct (file)
|
||||
// valid members are:
|
||||
// id, parent_id, type, url, file_path, file_fs, file_type, has_thumb, hash, hash_type, file_size
|
||||
// user_id, group_id, privileges
|
||||
// (sometimes rest members can be valid as well -- when you call directly fun_rm->RemoveFileOrSymlink() method)
|
||||
#define WINIX_FILE_PREPARE_TO_REMOVE 30045
|
||||
|
||||
// a file or symlink was removed (rm function)
|
||||
// in p1 you have a pointer to the Item struct (old file)
|
||||
// valid members are the same as in WINIX_FILE_PREPARE_TO_REMOVE
|
||||
#define WINIX_FILE_REMOVED 30050
|
||||
|
||||
// preparing to remove a directory (rm function)
|
||||
// in p1 you have a pointer to the Item struct (directory)
|
||||
// this message is sent after checking the directory permissions
|
||||
// so consequently if there is no any database error then the
|
||||
// directory will be removed
|
||||
// and after removed WINIX_DIR_REMOVED message is sent
|
||||
#define WINIX_DIR_PREPARE_TO_REMOVE 30070
|
||||
|
||||
// directory was removed (rm function)
|
||||
// PluginInfo::l1 is the directory id
|
||||
#define WINIX_DIR_REMOVED 30060
|
||||
|
||||
// winix is initialized,
|
||||
// now you can initialize your plugin
|
||||
// session pointer is null
|
||||
#define WINIX_PLUGIN_INIT 30080
|
||||
|
||||
// here you can add your own mount point, file systems, mount parameters
|
||||
// for adding a new mount type call: system->mounts.AddMountType("new_mount_name")
|
||||
// session pointer is null
|
||||
#define WINIX_ADD_MOUNTS 30090
|
||||
|
||||
// add plugin functions (winix functions) here
|
||||
// call info.functions->Add() to add a function
|
||||
// session pointer is null
|
||||
#define WINIX_CREATE_FUNCTIONS 30100
|
||||
|
||||
// choose a default function
|
||||
// if you do not select it then it will be choosen by winix
|
||||
#define WINIX_SELECT_DEFAULT_FUNCTION 30110
|
||||
|
||||
// /etc/fstab has been changed
|
||||
// now we have new mount points
|
||||
// session pointer is null
|
||||
#define WINIX_FSTAB_CHANGED 30120
|
||||
|
||||
// here you add your own template to notification system
|
||||
// call system->notify.AddTemplate() method
|
||||
// with a template file name
|
||||
// session pointer is null
|
||||
#define WINIX_NOTIFY_ADD_TEMPLATE 30130
|
||||
|
||||
// the request is being ended
|
||||
// you can clear some of your objects here
|
||||
#define WINIX_END_REQUEST 30140
|
||||
|
||||
// a new file (page) has been added
|
||||
// in p1 you have a pointer to the Item struct
|
||||
#define WINIX_FILE_ADDED 30150
|
||||
|
||||
// a file (page) has been changed (edited)
|
||||
// in p1 you have a pointer to the Item struct
|
||||
#define WINIX_FILE_CHANGED 30160
|
||||
|
||||
// a new directory has been added
|
||||
// in p1 you have a pointer to the Item struct
|
||||
#define WINIX_DIR_ADDED 30155
|
||||
|
||||
// a file (page) has been copied
|
||||
// in p1 you have a pointer to the Item struct
|
||||
// not every fields of Item struct are filled
|
||||
#define WINIX_FILE_COPIED 30170
|
||||
|
||||
// a file will be moved
|
||||
// in p1 you have a pointer to the Item struct
|
||||
// valid members are:
|
||||
// id, parent_id, type, url, file_path, file_fs, file_type, has_thumb, hash, hash_type, file_size
|
||||
// user_id, group_id, privileges, meta
|
||||
// (sometimes rest members can be valid as well -- when you call directly fun_rm->RemoveFileOrSymlink() method)
|
||||
#define WINIX_FILE_PREPARE_TO_MOVE 30180
|
||||
|
||||
// a file has been moved
|
||||
// in p1 you have a pointer to the Item struct (new file)
|
||||
// valid members are the same as in WINIX_FILE_PREPARE_TO_MOVE
|
||||
#define WINIX_FILE_MOVED 30190
|
||||
|
||||
// a thumbnail was created
|
||||
// this message is called from another thread
|
||||
// the thread called Lock() before sending this message
|
||||
// in p1 you have a pointer to the Item struct
|
||||
// session pointer is null
|
||||
#define WINIX_CREATED_THUMB 30500
|
||||
|
||||
// an image has been resized
|
||||
// this message is called from another thread
|
||||
// the thread called Lock() before sending this message
|
||||
// in p1 you have a pointer to the Item struct
|
||||
// session pointer is null
|
||||
#define WINIX_IMAGE_RESIZED 30520
|
||||
|
||||
// content of a directory was sorted
|
||||
// (winix 'sort' function was used)
|
||||
// in p1 you have a pointer to the Item struct (of the directory)
|
||||
// this is from system->dirs so you should not change the item
|
||||
#define WINIX_DIR_CONTENT_SORTED 30500
|
||||
|
||||
// a user will be logged in
|
||||
// set PluginInfo::res to false (it is by default) to prevent logging the user
|
||||
// directly after this message (if you do not return false)
|
||||
// a WINIX_USER_LOGGED is sent
|
||||
// in p1 you have a pointer to User struct
|
||||
// (if at least one plugin returns false then the user will not be logged)
|
||||
// this message is not sent when winix starts and sessions are read from a sessions file
|
||||
// in such a case only WINIX_USER_LOGGED will be sent
|
||||
#define WINIX_PREPARE_USER_TO_LOGIN 30550
|
||||
|
||||
// a user has been logged
|
||||
// send from 'login' winix function
|
||||
// this message is also called when winix starts and reads sessions
|
||||
// from the session file
|
||||
#define WINIX_USER_LOGGED 30600
|
||||
|
||||
// a user is going to logout
|
||||
// in p1 you have a pointer to User struct
|
||||
// the message is also sent from the session GC (special thread for deleting sessions)
|
||||
// in such a case is sent with Lock() and Unlock()
|
||||
// so *don't* use Lock() in your procedure
|
||||
#define WINIX_PREPARE_USER_TO_LOGOUT 30610
|
||||
|
||||
// here you add your own html templates
|
||||
// call TemplatesFunctions::patterns.Add(L"file_name.html")
|
||||
// the method returns an index which you have to remember
|
||||
// and you can get the template with patterns.Get(index, lang_index)
|
||||
// it returns a pointer to Ezc::Pattern (or null pointer if the index is wrong)
|
||||
// the message will be sent too whenever 'reload/templates' winix function is called
|
||||
// templates you should add only in this message
|
||||
// in other cases after 'reload' function the indexes would be wrong
|
||||
// session pointer is null
|
||||
#define WINIX_ADD_TEMPLATE 31000
|
||||
|
||||
// a user will be removed
|
||||
// in p1 you have a pointer to the User struct
|
||||
// directly after this message a WINIX_USER_REMOVED will be sent
|
||||
#define WINIX_PREPARE_TO_REMOVE_USER 31010
|
||||
|
||||
// a user has been removed
|
||||
// in l1 you have the old user_id
|
||||
#define WINIX_USER_REMOVED 31020
|
||||
|
||||
// here you can check if the domain part of the URL address is correct
|
||||
// if no then set cur-request->redirect_to
|
||||
// and winix will do a 'base url redirect' without making any winix functions
|
||||
// the session pointer in info is null
|
||||
#define WINIX_BASE_URL_REDIRECT 31030
|
||||
|
||||
// raw POST parameters
|
||||
// in p1 there is a pointer to std::wstring meaning a parameter's name
|
||||
// in p2 there is a pointer to std::wstring value
|
||||
// this is sent only from PostParser
|
||||
// PostMultiParser (multipart/form-data html forms) doesn't send this messsage
|
||||
// there is no a session set (session pointer is null)
|
||||
// this message is sent for each name value pairs
|
||||
//
|
||||
#define WINIX_POST_PARAMS 31040
|
||||
|
||||
// this is the raw string sent in POST method
|
||||
// in p1 there is a pointer to std::string object
|
||||
#define WINIX_RAW_POST_STRING 31050
|
||||
|
||||
// this message is sent before calling MakePost() or MakeGet()
|
||||
// if you return false (which is default) you can prevent the access
|
||||
// to the resource
|
||||
#define WINIX_CHECK_PLUGIN_ACCESS 31060
|
||||
|
||||
// http headers (without cookies) were created and are ready to send
|
||||
// here you can make some changes to them
|
||||
// in p1 you have a pointer to the PT::Space (Request::out_headers)
|
||||
#define WINIX_PREPARE_TO_SEND_HTTP_HEADERS 31070
|
||||
|
||||
// http cookies were created and are ready to send
|
||||
// here you can make some changes to them
|
||||
// in p1 you have a pointer to the PT::Space (Request::out_cookies)
|
||||
#define WINIX_PREPARE_TO_SEND_HTTP_COOKIES 31080
|
||||
|
||||
|
||||
/*
|
||||
messages sent from other threads
|
||||
!! IMPROVE ME check if some above messages should be moved here
|
||||
*/
|
||||
|
||||
|
||||
// a job has to be done
|
||||
// this is sent from the other thread (without locking)
|
||||
// in p1 you have a pointer to PT::Space
|
||||
// session is null
|
||||
// if you process the job then return 'true' from the processing method (from plugin call)
|
||||
// so this prevent to make a standard (system) job
|
||||
#define WINIX_JOB 31200
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// values from 4000 - 4099 reserved for 'thread' plugin
|
||||
// see plugins/thread/pluginmsg.h
|
||||
|
||||
// values from 4100 - 4199 reserved for 'ticket' plugin
|
||||
// see plugins/ticket/pluginmsg.h
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
621
winixd/core/postmultiparser.cpp
Normal file
621
winixd/core/postmultiparser.cpp
Normal file
@@ -0,0 +1,621 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include "postmultiparser.h"
|
||||
#include "log.h"
|
||||
#include "utf8/utf8.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
PostMultiParser::PostMultiParser()
|
||||
{
|
||||
in_buffer = new unsigned char[WINIX_POSTMULTI_INPUT_BUFFER];
|
||||
}
|
||||
|
||||
|
||||
PostMultiParser::PostMultiParser(const PostMultiParser & p)
|
||||
{
|
||||
in_buffer = new unsigned char[WINIX_POSTMULTI_INPUT_BUFFER];
|
||||
config = p.config;
|
||||
}
|
||||
|
||||
|
||||
PostMultiParser & PostMultiParser::operator=(const PostMultiParser & p)
|
||||
{
|
||||
in_buffer = new unsigned char[WINIX_POSTMULTI_INPUT_BUFFER];
|
||||
config = p.config;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
PostMultiParser::~PostMultiParser()
|
||||
{
|
||||
delete [] in_buffer;
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::SetConfig(Config * pconfig)
|
||||
{
|
||||
config = pconfig;
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::ReadBoundary()
|
||||
{
|
||||
boundary.clear();
|
||||
|
||||
while( last != -1 && last != 10 && last != 13 )
|
||||
{
|
||||
boundary += last;
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
if( last == 13 )
|
||||
{
|
||||
ReadChar();
|
||||
line_end_dos = true;
|
||||
}
|
||||
|
||||
if( last == 10 )
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
|
||||
bool PostMultiParser::IsWhite(int c)
|
||||
{
|
||||
if( c==' ' || c=='\t' || c==13 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::SkipWhite()
|
||||
{
|
||||
while( IsWhite(last) )
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
|
||||
bool PostMultiParser::IsHeader()
|
||||
{
|
||||
SkipWhite();
|
||||
|
||||
if( last == 10 )
|
||||
{
|
||||
ReadChar();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PostMultiParser::ReadHeaderName()
|
||||
{
|
||||
SkipWhite();
|
||||
|
||||
while( last!=-1 && last!=':' && last!='=' && !IsWhite(last) && last!=10 )
|
||||
{
|
||||
header_name += last;
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
SkipWhite();
|
||||
|
||||
if( last != ':' && last != '=' )
|
||||
{
|
||||
err = WINIX_ERR_BROKEN_INPUT;
|
||||
return;
|
||||
}
|
||||
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::ReadHeaderValue()
|
||||
{
|
||||
bool was_apost = false;
|
||||
|
||||
SkipWhite();
|
||||
|
||||
if( last == '"' )
|
||||
{
|
||||
was_apost = true;
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
while( last!=-1 && last!=10 &&
|
||||
((!was_apost && last!=';' && !IsWhite(last)) || (was_apost && last!='"')))
|
||||
{
|
||||
header_value += last;
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
if( was_apost )
|
||||
{
|
||||
if( last != '"' )
|
||||
{
|
||||
err = WINIX_ERR_BROKEN_INPUT;
|
||||
return;
|
||||
}
|
||||
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
SkipWhite();
|
||||
|
||||
if( last != ';' && last != 10 )
|
||||
{
|
||||
err = WINIX_ERR_BROKEN_INPUT;
|
||||
return;
|
||||
}
|
||||
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::ReadPartHeader()
|
||||
{
|
||||
header_name.clear();
|
||||
header_value.clear();
|
||||
|
||||
ReadHeaderName();
|
||||
|
||||
if( err != WINIX_ERR_OK )
|
||||
return;
|
||||
|
||||
ReadHeaderValue();
|
||||
|
||||
if( err != WINIX_ERR_OK )
|
||||
return;
|
||||
|
||||
log << "PMP: " << header_name << ": " << header_value << logend;
|
||||
|
||||
if( header_name == "name" )
|
||||
name = header_value;
|
||||
|
||||
if( header_name == "filename" )
|
||||
filename = header_value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool PostMultiParser::HasBoundary()
|
||||
{
|
||||
if( content.size() < boundary.size() )
|
||||
return false;
|
||||
|
||||
size_t c = content.size() - boundary.size();
|
||||
size_t b = 0;
|
||||
|
||||
for( ; c<content.size() ; ++c, ++b)
|
||||
{
|
||||
if( content[c] != boundary[b] )
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PostMultiParser::LogFirst(const std::string & to_log, size_t len)
|
||||
{
|
||||
if( len > to_log.size() )
|
||||
len = to_log.size();
|
||||
|
||||
log << log3 << "PMP: Content (";
|
||||
|
||||
if( len > 0 )
|
||||
log << "len: " << to_log.size() << ", first " << len << " bytes";
|
||||
else
|
||||
log << "empty";
|
||||
|
||||
log << "): \"";
|
||||
log.LogString(to_log, len);
|
||||
log << "\"" << logend;
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::ReadContentSkipBoundary(bool has_boundary)
|
||||
{
|
||||
if( has_boundary && content.size() >= boundary.size() )
|
||||
{
|
||||
content.erase(content.size()-boundary.size());
|
||||
content_len -= boundary.size();
|
||||
}
|
||||
|
||||
// the last new line character doesn't belong to the content
|
||||
// this is a new line character before the boundary
|
||||
if( !content.empty() && content[content.size()-1] == 10 )
|
||||
{
|
||||
content.erase(content.size()-1);
|
||||
content_len -= 1;
|
||||
|
||||
if( line_end_dos && !content.empty() && content[content.size()-1] == 13 )
|
||||
{
|
||||
content.erase(content.size()-1);
|
||||
content_len -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::ReadContentToFileLoop()
|
||||
{
|
||||
bool has_boundary = false;
|
||||
|
||||
|
||||
while( last!=-1 )
|
||||
{
|
||||
content += last;
|
||||
content_len += 1;
|
||||
ReadChar();
|
||||
|
||||
if( HasBoundary() )
|
||||
{
|
||||
has_boundary = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if( content.size() >= WINIX_POSTMULTI_OUTPUT_BUFFER )
|
||||
{
|
||||
tmp_file.write(content.c_str(), content.size());
|
||||
content.clear();
|
||||
}
|
||||
|
||||
if( config->post_file_max != 0 && content_len > config->post_file_max )
|
||||
{
|
||||
err = WINIX_ERR_INPUT_TOO_LARGE;
|
||||
log << log1 << "PMP: content greater than " << config->post_file_max << " (skipping)" << logend;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ReadContentSkipBoundary(has_boundary);
|
||||
|
||||
// saving the rest
|
||||
if( !content.empty() )
|
||||
{
|
||||
tmp_file.write(content.c_str(), content.size());
|
||||
content.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void PostMultiParser::ReadContentToFile()
|
||||
{
|
||||
time_t t1, t2;
|
||||
|
||||
content.clear();
|
||||
content.reserve(WINIX_POSTMULTI_OUTPUT_BUFFER);
|
||||
content_len = 0;
|
||||
t1 = time(0);
|
||||
|
||||
ReadContentToFileLoop();
|
||||
|
||||
tmp_file.close();
|
||||
log << log2 << "PMP: content size: " << content_len << " bytes" << logend;
|
||||
|
||||
t2 = time(0);
|
||||
|
||||
if( t2 - t1 > 1 )
|
||||
log << log2 << "PMP: content read in " << (t2-t1) << " sec" << logend;
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::ReadContentLoop()
|
||||
{
|
||||
bool has_boundary = false;
|
||||
|
||||
|
||||
while( last!=-1 && !(has_boundary=HasBoundary()) )
|
||||
{
|
||||
content += last;
|
||||
content_len += 1;
|
||||
ReadChar();
|
||||
|
||||
if( config->post_file_max != 0 && content_len > (size_t)config->post_file_max )
|
||||
{
|
||||
err = WINIX_ERR_INPUT_TOO_LARGE;
|
||||
log << log1 << "PMP: content greater than " << config->post_file_max << " (skipping)" << logend;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ReadContentSkipBoundary(has_boundary);
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::ReadContent()
|
||||
{
|
||||
content.clear();
|
||||
content_len = 0;
|
||||
|
||||
ReadContentLoop();
|
||||
|
||||
log << log2 << "PMP: content size: " << content_len << " bytes" << logend;
|
||||
|
||||
if( !IsSubStringNoCase("pass", name.c_str()) )
|
||||
LogFirst(content, config->log_post_value_size);
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::ConvStr(const std::string & src, std::wstring & dst)
|
||||
{
|
||||
PT::UTF8ToWide(src, dst);
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::AddNormalPostVar()
|
||||
{
|
||||
if( post_tab->size() >= WINIX_POSTTABLE_MAXSIZE )
|
||||
{
|
||||
err = WINIX_ERR_INPUT_TOO_LARGE;
|
||||
log << log1 << "PMP: more than " << WINIX_POSTTABLE_MAXSIZE << " post variables (skipping)" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
ConvStr(name, namew);
|
||||
ConvStr(content, contentw);
|
||||
|
||||
bool added = InsertPostVar(*post_tab, namew, contentw);
|
||||
|
||||
log << log2 << "PMP: POST var, name: \"" << namew << "\"";
|
||||
|
||||
if( !added )
|
||||
log << log2 << " (skipped)";
|
||||
|
||||
log << logend;
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::AddFilePostVar()
|
||||
{
|
||||
if( post_file_tab->size() >= WINIX_POSTTABLE_MAXSIZE )
|
||||
{
|
||||
err = WINIX_ERR_INPUT_TOO_LARGE;
|
||||
log << log1 << "PMP: more than " << WINIX_POSTTABLE_MAXSIZE << " post file variables (skipping)" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
ConvStr(name, namew);
|
||||
ConvStr(filename, post_file_temp.filename);
|
||||
post_file_temp.tmp_filename = tmp_filename;
|
||||
post_file_temp.file_size = content_len;
|
||||
|
||||
bool added = InsertPostVar(*post_file_tab, namew, post_file_temp);
|
||||
|
||||
log << log2 << "PMP: POST FILE var, name: \"" << namew << "\"";
|
||||
|
||||
if( !added )
|
||||
log << log2 << " (skipped)";
|
||||
|
||||
log << logend;
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::AddPostVar()
|
||||
{
|
||||
if( name.empty() )
|
||||
return;
|
||||
|
||||
if( filename.empty() )
|
||||
AddNormalPostVar();
|
||||
else
|
||||
AddFilePostVar();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PostMultiParser::CheckBoundaryEnd()
|
||||
{
|
||||
if( last == '-' )
|
||||
{
|
||||
ReadChar();
|
||||
|
||||
if( last != '-' )
|
||||
{
|
||||
err = WINIX_ERR_BROKEN_INPUT;
|
||||
return;
|
||||
}
|
||||
|
||||
// end of parsing
|
||||
// the rest input (if exists) is ignored
|
||||
last = -1;
|
||||
}
|
||||
|
||||
// skipping a new line after the boundary
|
||||
if( last == 13 )
|
||||
ReadChar();
|
||||
|
||||
if( last == 10 )
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::CreateTmpFile()
|
||||
{
|
||||
wchar_t buf[WINIX_OS_PATH_SIZE];
|
||||
size_t buf_len = sizeof(buf)/sizeof(wchar_t);
|
||||
|
||||
if( config->upload_dir.empty() )
|
||||
{
|
||||
log << log1 << "PMP: upload_dir is not set in the config" << logend;
|
||||
err = WINIX_ERR_CANT_CREATE_FILE;
|
||||
return;
|
||||
}
|
||||
|
||||
swprintf(buf, buf_len, L"%ls/tmp/pmp_%u_%d_%u", config->upload_dir.c_str(), (unsigned)getpid(), tmp_filename_postfix, rand());
|
||||
tmp_filename_postfix += 1;
|
||||
|
||||
tmp_filename = buf;
|
||||
PT::WideToUTF8(tmp_filename, atmp_filename);
|
||||
|
||||
tmp_file.open(atmp_filename.c_str(), std::ios_base::binary | std::ios_base::out);
|
||||
// !! IMPROVE ME dodac ustawienie chmod config.upload_files_chmod dla tymczasowego pliku
|
||||
|
||||
if( !tmp_file )
|
||||
{
|
||||
log << log1 << "PMP: can't create a temporary file: " << atmp_filename << logend;
|
||||
err = WINIX_ERR_CANT_CREATE_FILE;
|
||||
return;
|
||||
}
|
||||
|
||||
log << log3 << "PMP: using temporary file for the content: " << atmp_filename << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void PostMultiParser::ReadPart()
|
||||
{
|
||||
name.clear();
|
||||
filename.clear();
|
||||
|
||||
while( IsHeader() )
|
||||
ReadPartHeader();
|
||||
|
||||
if( err != WINIX_ERR_OK )
|
||||
return;
|
||||
|
||||
if( !filename.empty() )
|
||||
CreateTmpFile();
|
||||
|
||||
if( err != WINIX_ERR_OK )
|
||||
return;
|
||||
|
||||
if( !filename.empty() )
|
||||
ReadContentToFile();
|
||||
else
|
||||
ReadContent();
|
||||
|
||||
if( err == WINIX_ERR_OK )
|
||||
{
|
||||
AddPostVar();
|
||||
CheckBoundaryEnd();
|
||||
}
|
||||
|
||||
if( err != WINIX_ERR_OK && !filename.empty() )
|
||||
{
|
||||
log << log1 << "PMP: deleting the tmp file: " << tmp_filename << logend;
|
||||
RemoveFile(tmp_filename);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PostMultiParser::ReadChar()
|
||||
{
|
||||
if( last == -1 )
|
||||
return;
|
||||
|
||||
if( in_buffer_ind >= in_buffer_len )
|
||||
{
|
||||
if( in_buffer_len < WINIX_POSTMULTI_INPUT_BUFFER )
|
||||
{
|
||||
last = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
in_buffer_len = FCGX_GetStr((char*)in_buffer, WINIX_POSTMULTI_INPUT_BUFFER, in);
|
||||
in_buffer_ind = 0;
|
||||
}
|
||||
|
||||
if( in_buffer_len == 0 )
|
||||
{
|
||||
last = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
last = in_buffer[in_buffer_ind];
|
||||
in_buffer_ind += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Error PostMultiParser::Parse(FCGX_Stream * in_, PostTab & post_tab_, PostFileTab & post_file_tab_)
|
||||
{
|
||||
in = in_;
|
||||
last = 0;
|
||||
err = WINIX_ERR_OK;
|
||||
var_index = 1;
|
||||
line_end_dos = false;
|
||||
in_buffer_ind = WINIX_POSTMULTI_INPUT_BUFFER;
|
||||
in_buffer_len = WINIX_POSTMULTI_INPUT_BUFFER;
|
||||
post_tab = &post_tab_;
|
||||
post_file_tab = &post_file_tab_;
|
||||
tmp_filename_postfix = 1;
|
||||
|
||||
ReadChar();
|
||||
ReadBoundary();
|
||||
|
||||
if( boundary.empty() )
|
||||
return WINIX_ERR_NO_BOUNDARY;
|
||||
|
||||
while( last!=-1 && err == WINIX_ERR_OK )
|
||||
ReadPart();
|
||||
|
||||
if( err != WINIX_ERR_OK )
|
||||
{
|
||||
RemovePostFileTmp(*post_file_tab);
|
||||
post_tab->clear();
|
||||
post_file_tab->clear();
|
||||
|
||||
if( err != WINIX_ERR_INPUT_TOO_LARGE && err != WINIX_ERR_CANT_CREATE_FILE )
|
||||
log << log1 << "PMP: syntax error" << logend;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
165
winixd/core/postmultiparser.h
Normal file
165
winixd/core/postmultiparser.h
Normal file
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_postmultiparser
|
||||
#define headerfile_winix_core_postmultiparser
|
||||
|
||||
#include <string>
|
||||
#include <fcgiapp.h>
|
||||
#include <fstream>
|
||||
#include "error.h"
|
||||
#include "requesttypes.h"
|
||||
#include "config.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
// 2 MB
|
||||
#define WINIX_POSTMULTI_INPUT_BUFFER 2097152
|
||||
#define WINIX_POSTMULTI_OUTPUT_BUFFER 2097152
|
||||
|
||||
|
||||
class PostMultiParser
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
PostMultiParser();
|
||||
PostMultiParser(const PostMultiParser &);
|
||||
PostMultiParser & operator=(const PostMultiParser &);
|
||||
~PostMultiParser();
|
||||
|
||||
void SetConfig(Config * pconfig);
|
||||
Error Parse(FCGX_Stream * in_, PostTab & post_tab_, PostFileTab & post_file_tab_);
|
||||
|
||||
private:
|
||||
|
||||
Config * config;
|
||||
|
||||
FCGX_Stream * in;
|
||||
unsigned char * in_buffer;
|
||||
std::ofstream tmp_file;
|
||||
std::wstring tmp_filename;
|
||||
std::string atmp_filename;
|
||||
int tmp_filename_postfix;
|
||||
size_t in_buffer_ind;
|
||||
size_t in_buffer_len;
|
||||
|
||||
PostTab * post_tab;
|
||||
PostFileTab * post_file_tab;
|
||||
|
||||
int last; // last read character
|
||||
int var_index; // used as a postfix to the same name (is auto increment)
|
||||
|
||||
bool line_end_dos;
|
||||
std::string boundary;
|
||||
std::string content;
|
||||
size_t content_len;
|
||||
|
||||
std::string header_name, header_value;
|
||||
Error err;
|
||||
|
||||
std::string name, filename;
|
||||
std::wstring namew, contentw;
|
||||
PostFile post_file_temp;
|
||||
|
||||
void LogFirst(const std::string & to_log, size_t len);
|
||||
void ConvStr(const std::string & src, std::wstring & dst);
|
||||
|
||||
bool IsWhite(int c);
|
||||
void SkipWhite();
|
||||
|
||||
void AddNormalPostVar();
|
||||
void AddFilePostVar();
|
||||
void AddPostVar();
|
||||
|
||||
void ReadBoundary();
|
||||
|
||||
bool IsHeader();
|
||||
void ReadHeaderName();
|
||||
void ReadHeaderValue();
|
||||
void ReadPartHeader();
|
||||
|
||||
void CreateTmpFile();
|
||||
bool HasBoundary();
|
||||
void ReadContentSkipBoundary(bool has_boundary);
|
||||
void ReadContentToFileLoop();
|
||||
void ReadContentToFile();
|
||||
void ReadContentLoop();
|
||||
void ReadContent();
|
||||
void CheckBoundaryEnd();
|
||||
void ReadPart();
|
||||
|
||||
void ReadChar();
|
||||
|
||||
|
||||
template<class Container, class Value>
|
||||
bool InsertPostVar(Container & container, std::wstring & key, const Value & value);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class Container, class Value>
|
||||
bool PostMultiParser::InsertPostVar(Container & container, std::wstring & key, const Value & value)
|
||||
{
|
||||
bool added;
|
||||
std::pair<typename Container::iterator, bool> res;
|
||||
|
||||
res = container.insert( std::make_pair(key, value) );
|
||||
added = res.second;
|
||||
|
||||
if( !added )
|
||||
{
|
||||
key += L"_inc";
|
||||
key += Toa(var_index);
|
||||
res = container.insert( std::make_pair(key, value) );
|
||||
added = res.second;
|
||||
var_index += 1;
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
163
winixd/core/postparser.h
Normal file
163
winixd/core/postparser.h
Normal file
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_postparser
|
||||
#define headerfile_winix_core_postparser
|
||||
|
||||
#include <fcgiapp.h>
|
||||
#include <string>
|
||||
#include "httpsimpleparser.h"
|
||||
#include "requesttypes.h"
|
||||
#include "misc.h"
|
||||
#include "utf8/utf8.h"
|
||||
#include "log.h"
|
||||
#include "plugin.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class PostParser : public HttpSimpleParser
|
||||
{
|
||||
FCGX_Stream * in;
|
||||
PostTab * post_tab;
|
||||
size_t log_value_size;
|
||||
int var_index;
|
||||
bool has_winix_post_params_msg;
|
||||
bool has_winix_raw_post_msg;
|
||||
std::string raw_post;
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
virtual int GetChar()
|
||||
{
|
||||
int c = FCGX_GetChar(in);
|
||||
|
||||
if( c != -1 && has_winix_raw_post_msg )
|
||||
raw_post += c;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
void CreateLog(bool param_added, const std::wstring & name, const std::wstring & value)
|
||||
{
|
||||
log << log2 << "Method POST, name: \"" << name << "\"";
|
||||
|
||||
if( log_value_size > 0 && !IsSubStringNoCase(L"pass", name.c_str()) )
|
||||
{
|
||||
log << ", value: ";
|
||||
|
||||
if( value.size() > log_value_size )
|
||||
log << "(first " << log_value_size << " characters) ";
|
||||
|
||||
log << "\"";
|
||||
log.LogString(value, log_value_size);
|
||||
log << "\" (size: " << value.size() << ")";
|
||||
}
|
||||
|
||||
if( param_added == false )
|
||||
log << log2 << " (skipped)";
|
||||
|
||||
log << log2 << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
virtual void Parameter(std::wstring & name, std::wstring & value)
|
||||
{
|
||||
bool added;
|
||||
std::pair<PostTab::iterator, bool> res;
|
||||
|
||||
if( has_winix_post_params_msg )
|
||||
plugin.Call(0, WINIX_POST_PARAMS, &name, &value);
|
||||
|
||||
res = post_tab->insert( std::make_pair(name, value) );
|
||||
added = res.second;
|
||||
|
||||
if( !added )
|
||||
{
|
||||
name += L"_inc";
|
||||
name += Toa(var_index);
|
||||
res = post_tab->insert( std::make_pair(name, value) );
|
||||
added = res.second;
|
||||
var_index += 1;
|
||||
}
|
||||
|
||||
CreateLog(added, name, value);
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
|
||||
PostParser()
|
||||
{
|
||||
log_value_size = 0;
|
||||
HttpSimpleParser::getchar_returns_utf8_chars = true;
|
||||
}
|
||||
|
||||
void LogValueSize(size_t s)
|
||||
{
|
||||
log_value_size = s;
|
||||
}
|
||||
|
||||
void Parse(FCGX_Stream * in_, PostTab & post_tab_)
|
||||
{
|
||||
in = in_;
|
||||
post_tab = &post_tab_;
|
||||
var_index = 1;
|
||||
raw_post.clear();
|
||||
|
||||
has_winix_post_params_msg = plugin.HasMessage(WINIX_POST_PARAMS);
|
||||
has_winix_raw_post_msg = plugin.HasMessage(WINIX_RAW_POST_STRING);
|
||||
|
||||
HttpSimpleParser::Parse();
|
||||
|
||||
if( has_winix_raw_post_msg )
|
||||
plugin.Call(0, WINIX_RAW_POST_STRING, &raw_post);
|
||||
|
||||
raw_post.clear();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
196
winixd/core/rebus.cpp
Normal file
196
winixd/core/rebus.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <limits.h>
|
||||
#include "log.h"
|
||||
#include "rebus.h"
|
||||
#include "misc.h"
|
||||
#include "cur.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
void Rebus::SetCur(Cur * pcur)
|
||||
{
|
||||
cur = pcur;
|
||||
}
|
||||
|
||||
|
||||
bool Rebus::InitPair(int a, int b, Item & item)
|
||||
{
|
||||
wchar_t buffer[100];
|
||||
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
|
||||
bool add = false;
|
||||
|
||||
if( a+b <= 15 )
|
||||
{
|
||||
swprintf(buffer, buffer_len, L"%d+%d", a, b);
|
||||
item.question = buffer;
|
||||
item.answer = a+b;
|
||||
add = true;
|
||||
}
|
||||
|
||||
if( a-b >= 0 )
|
||||
{
|
||||
swprintf(buffer, buffer_len, L"%d-%d", a, b);
|
||||
item.question = buffer;
|
||||
item.answer = a-b;
|
||||
add = true;
|
||||
}
|
||||
|
||||
return add;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Rebus::Init()
|
||||
{
|
||||
int a, b;
|
||||
Item item;
|
||||
|
||||
item.key = 0;
|
||||
|
||||
for(a=1 ; a<10 ; ++a)
|
||||
{
|
||||
for(b=1 ; b<10 ; ++b)
|
||||
{
|
||||
if( InitPair(a, b, item) )
|
||||
{
|
||||
table.push_back(item);
|
||||
item.key += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Rebus::Item * Rebus::Rand()
|
||||
{
|
||||
size_t len = table.size();
|
||||
|
||||
if( len == 0 )
|
||||
// empty table, call Init() first
|
||||
return 0;
|
||||
|
||||
size_t i = size_t( ((double)rand()*(double)len) / (double(RAND_MAX)+1.0) );
|
||||
|
||||
if( i >= table.size() )
|
||||
{
|
||||
log << log1 << "Rebus: Rand() index too big" << logend;
|
||||
// oops, it should not be greater then table.size()
|
||||
return &table[0];
|
||||
}
|
||||
|
||||
return &table[0] + i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Rebus::IsAnswerOk(Rebus::Item * item, const std::wstring & answer)
|
||||
{
|
||||
if( item == 0 )
|
||||
return false;
|
||||
|
||||
wchar_t * end;
|
||||
const wchar_t * a = answer.c_str();
|
||||
a = SkipWhite(a);
|
||||
|
||||
int value = (int)wcstol(a, &end, 10);
|
||||
|
||||
if( a == end )
|
||||
// nothing has been read
|
||||
return false;
|
||||
|
||||
log << log2 << "Rebus: your answer is: " << value << logend;
|
||||
end = (wchar_t*)SkipWhite(end);
|
||||
|
||||
if( *end != 0 )
|
||||
{
|
||||
// something is at the end
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = (item->answer == value);
|
||||
|
||||
if( result )
|
||||
log << log3 << "Rebus: the answer is correct" << logend;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Rebus::CheckRebus()
|
||||
{
|
||||
if( !cur->session )
|
||||
return false;
|
||||
|
||||
if( cur->session->puser )
|
||||
// logged users don't have to use the rebus
|
||||
return true;
|
||||
|
||||
if( cur->session->rebus_checked )
|
||||
return true;
|
||||
|
||||
cur->session->rebus_checked = true;
|
||||
|
||||
if( !cur->session->rebus_item )
|
||||
{
|
||||
log << log1 << "Rebus: rebus not set" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( IsAnswerOk(cur->session->rebus_item, cur->request->PostVar(L"rebus")) )
|
||||
return true;
|
||||
|
||||
log << log1 << "Rebus: rebus has an incorrect answer, expected: "
|
||||
<< cur->session->rebus_item->answer << logend;
|
||||
|
||||
// don't add cur->session->spam_score when the rebus has incorrect answer
|
||||
// a user could have made a mistake
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
85
winixd/core/rebus.h
Normal file
85
winixd/core/rebus.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_rebus
|
||||
#define headerfile_winix_core_rebus
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
struct Cur;
|
||||
|
||||
|
||||
class Rebus
|
||||
{
|
||||
public:
|
||||
|
||||
struct Item
|
||||
{
|
||||
int key;
|
||||
std::wstring question;
|
||||
int answer;
|
||||
};
|
||||
|
||||
|
||||
|
||||
void SetCur(Cur * pcur);
|
||||
void Init();
|
||||
Item * Rand();
|
||||
bool IsAnswerOk(Item * item, const std::wstring & answer);
|
||||
bool CheckRebus();
|
||||
|
||||
private:
|
||||
|
||||
Cur * cur;
|
||||
|
||||
bool InitPair(int a, int b, Item & item);
|
||||
|
||||
std::vector<Item> table;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
340
winixd/core/request.cpp
Normal file
340
winixd/core/request.cpp
Normal file
@@ -0,0 +1,340 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2015, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "request.h"
|
||||
#include "log.h"
|
||||
#include "plugin.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Request::Request()
|
||||
{
|
||||
id = 0;
|
||||
config = 0;
|
||||
}
|
||||
|
||||
|
||||
void Request::SetConfig(Config * pconfig)
|
||||
{
|
||||
config = pconfig;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Request::ClearOutputStreams()
|
||||
{
|
||||
size_t len = 0;
|
||||
|
||||
out_main_stream.Clear();
|
||||
|
||||
if( config )
|
||||
len = config->ezc_out_streams_size;
|
||||
|
||||
/*
|
||||
* clearing buffers and setting 'escape' flag to true
|
||||
* for all streams which were used in the map
|
||||
*/
|
||||
out_streams.ClearMap();
|
||||
out_streams.ResizeTab(len);
|
||||
out_main_stream_use_html_filter = true;
|
||||
out_streams_use_html_filter = true;
|
||||
}
|
||||
|
||||
|
||||
void Request::Clear()
|
||||
{
|
||||
// id is never 0
|
||||
if( ++id == 0 )
|
||||
++id;
|
||||
|
||||
RemovePostFileTmp(post_file_tab);
|
||||
ClearOutputStreams();
|
||||
|
||||
post_tab.clear();
|
||||
post_file_tab.clear();
|
||||
cookie_tab.clear();
|
||||
|
||||
method = unknown_method;
|
||||
|
||||
out_headers.Clear();
|
||||
out_cookies.Clear();
|
||||
|
||||
page_generated = false;
|
||||
|
||||
env_request_method.clear();
|
||||
env_request_uri.clear();
|
||||
env_http_cookie.clear();
|
||||
env_remote_addr.clear();
|
||||
env_http_host.clear();
|
||||
env_http_user_agent.clear();
|
||||
env_http_accept_encoding.clear();
|
||||
env_fcgi_role.clear();
|
||||
env_content_type.clear();
|
||||
env_https.clear();
|
||||
|
||||
item_tab.clear();
|
||||
item.Clear();
|
||||
dir_tab.clear();
|
||||
last_item = &item;
|
||||
is_item = false;
|
||||
function = 0;
|
||||
param_tab.clear();
|
||||
anchor.clear();
|
||||
|
||||
status = WINIX_ERR_OK;
|
||||
|
||||
browser_msie = false;
|
||||
redirect_to.clear();
|
||||
redirect_type = 303;
|
||||
x_sendfile.clear();
|
||||
send_as_attachment = false;
|
||||
|
||||
using_ssl = false;
|
||||
|
||||
start_time = 0;
|
||||
start_date.Clear();
|
||||
|
||||
subdomain.clear();
|
||||
return_info_only = false;
|
||||
info.Clear();
|
||||
info_serializer = 0;
|
||||
return_json = false;
|
||||
|
||||
out_bin_stream.clear();
|
||||
send_bin_stream = false;
|
||||
|
||||
gen_trim_white = false;
|
||||
gen_skip_new_line = false;
|
||||
gen_use_special_chars = false;
|
||||
|
||||
ip = 0;
|
||||
use_200_status_for_not_found_and_permission_denied = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Request::RequestStarts()
|
||||
{
|
||||
// clearing it is better to use at the end of a request
|
||||
// so starting is much faster
|
||||
|
||||
start_time = std::time(0);
|
||||
start_date = start_time;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool Request::IsPostVar(const wchar_t * var)
|
||||
{
|
||||
PostTab::iterator p;
|
||||
|
||||
p = post_tab.find(var);
|
||||
|
||||
if( p == post_tab.end() )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Request::IsPostVar(const std::wstring & var)
|
||||
{
|
||||
PostTab::iterator p;
|
||||
|
||||
p = post_tab.find(var);
|
||||
|
||||
if( p == post_tab.end() )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
const std::wstring & Request::PostVar(const wchar_t * var)
|
||||
{
|
||||
PostTab::iterator p = post_tab.find(var);
|
||||
|
||||
if( p == post_tab.end() )
|
||||
return str_empty;
|
||||
|
||||
return p->second;
|
||||
}
|
||||
|
||||
|
||||
const std::wstring & Request::PostVar(const std::wstring & var)
|
||||
{
|
||||
PostTab::iterator p = post_tab.find(var);
|
||||
|
||||
if( p == post_tab.end() )
|
||||
return str_empty;
|
||||
|
||||
return p->second;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Request::PostVar(const wchar_t * var, std::wstring & result)
|
||||
{
|
||||
PostTab::iterator p = post_tab.find(var);
|
||||
|
||||
if( p == post_tab.end() )
|
||||
{
|
||||
result.clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
result = p->second;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Request::PostVar(const std::wstring & var, std::wstring & result)
|
||||
{
|
||||
PostTab::iterator p = post_tab.find(var);
|
||||
|
||||
if( p == post_tab.end() )
|
||||
{
|
||||
result.clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
result = p->second;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::wstring * Request::PostVarp(const wchar_t * var)
|
||||
{
|
||||
PostTab::iterator p = post_tab.find(var);
|
||||
|
||||
if( p == post_tab.end() )
|
||||
return 0;
|
||||
|
||||
return &p->second;
|
||||
}
|
||||
|
||||
|
||||
std::wstring * Request::PostVarp(const std::wstring & var)
|
||||
{
|
||||
PostTab::iterator p = post_tab.find(var);
|
||||
|
||||
if( p == post_tab.end() )
|
||||
return 0;
|
||||
|
||||
return &p->second;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool Request::AllPostVarEmpty()
|
||||
{
|
||||
PostTab::iterator i;
|
||||
|
||||
for(i=post_tab.begin() ; i!=post_tab.end() ; ++i)
|
||||
if( !i->second.empty() )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Request::IsParam(const wchar_t * param_name)
|
||||
{
|
||||
ParamTab::iterator i;
|
||||
|
||||
for(i=param_tab.begin() ; i!=param_tab.end() ; ++i)
|
||||
{
|
||||
if( i->name == param_name )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool Request::IsParam(const std::wstring & param_name)
|
||||
{
|
||||
ParamTab::iterator i;
|
||||
|
||||
for(i=param_tab.begin() ; i!=param_tab.end() ; ++i)
|
||||
{
|
||||
if( i->name == param_name )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
const std::wstring & Request::ParamValue(const wchar_t * param_name)
|
||||
{
|
||||
ParamTab::iterator i;
|
||||
|
||||
for(i=param_tab.begin() ; i!=param_tab.end() ; ++i)
|
||||
{
|
||||
if( i->name == param_name )
|
||||
return i->value;
|
||||
}
|
||||
|
||||
return str_empty;
|
||||
}
|
||||
|
||||
|
||||
const std::wstring & Request::ParamValue(const std::wstring & param_name)
|
||||
{
|
||||
return ParamValue(param_name.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
430
winixd/core/request.h
Normal file
430
winixd/core/request.h
Normal file
@@ -0,0 +1,430 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2015, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_request
|
||||
#define headerfile_winix_core_request
|
||||
|
||||
#include <fcgiapp.h>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include "requesttypes.h"
|
||||
#include "item.h"
|
||||
#include "error.h"
|
||||
#include "config.h"
|
||||
#include "textstream.h"
|
||||
#include "templates/htmltextstream.h"
|
||||
#include "date/date.h"
|
||||
#include "space/space.h"
|
||||
#include "space/spacetojson.h"
|
||||
#include "textstream/textstream.h"
|
||||
#include "outstreams.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
class FunctionBase;
|
||||
|
||||
|
||||
|
||||
|
||||
struct Request
|
||||
{
|
||||
/*
|
||||
request id
|
||||
is incremented for each request and is never 0
|
||||
(from -1 will be incremented to one)
|
||||
it's used for some optimizations e.g. in templates
|
||||
*/
|
||||
size_t id;
|
||||
|
||||
|
||||
/*
|
||||
request start time
|
||||
Time() methods are very slow so it is better to directly use those two values
|
||||
they are set when a request starts
|
||||
*/
|
||||
time_t start_time;
|
||||
PT::Date start_date;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
*
|
||||
* variables representing input from client's browser
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
the HTTP method
|
||||
!! IMPROVE ME add the rest methods here
|
||||
*/
|
||||
enum Method { get, post, head, unknown_method } method;
|
||||
|
||||
|
||||
/*
|
||||
subdomain
|
||||
subdomain = HTTP_HOST environment variable - config->base_url
|
||||
*/
|
||||
std::wstring subdomain;
|
||||
|
||||
|
||||
/*
|
||||
raw parameters
|
||||
!! CHECK ME may post_tab and cookie_tab should be changed to PT::Space now?
|
||||
or may change the name to cookie_in? or in_cookie?
|
||||
*/
|
||||
PostTab post_tab;
|
||||
PostFileTab post_file_tab;
|
||||
CookieTab cookie_tab;
|
||||
|
||||
|
||||
/*
|
||||
html anchor (those part of URI after '#' character)
|
||||
*/
|
||||
std::wstring anchor;
|
||||
|
||||
|
||||
// environment variables
|
||||
std::wstring env_request_method;
|
||||
std::wstring env_request_uri;
|
||||
std::wstring env_http_cookie;
|
||||
std::wstring env_remote_addr;
|
||||
std::wstring env_http_host;
|
||||
std::wstring env_http_user_agent;
|
||||
std::wstring env_http_accept_encoding;
|
||||
std::wstring env_fcgi_role;
|
||||
std::wstring env_content_type;
|
||||
std::wstring env_https;
|
||||
|
||||
// current IP address of the remote host (read from REMOTE_ADDR environment variable)
|
||||
// (at the moment only IPv4 are supported)
|
||||
int ip;
|
||||
|
||||
// true if the browser is Microsoft Internet Explorer
|
||||
bool browser_msie;
|
||||
|
||||
// true if the browser is Konqueror
|
||||
bool browser_konqueror;
|
||||
|
||||
// true if we are using an encrypted connection (SSL)
|
||||
bool using_ssl;
|
||||
|
||||
|
||||
/*
|
||||
request input variables representing the winix filesystem
|
||||
*/
|
||||
|
||||
// current directory
|
||||
std::vector<Item*> dir_tab;
|
||||
|
||||
// true if a file exists
|
||||
bool is_item;
|
||||
|
||||
// current file (valid if is_item is true)
|
||||
Item item;
|
||||
|
||||
// current winix function
|
||||
// null if there is no a function
|
||||
FunctionBase * function;
|
||||
|
||||
// parameters (name:value)
|
||||
ParamTab param_tab;
|
||||
|
||||
// this is a pointer either to the item (if exists) or to the last directory
|
||||
Item * last_item;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
*
|
||||
* variables for generating output to the client's browser
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
// request status
|
||||
// !! CHANGE ME it'll be better to use ordinary http result codes
|
||||
Error status;
|
||||
|
||||
// if not empty means an address for redirecting to
|
||||
// it should be url-encoded
|
||||
std::wstring redirect_to;
|
||||
|
||||
// a redirect type
|
||||
// following redirect types are supported:
|
||||
// 300 Multiple Choices
|
||||
// 301 Moved Permanently
|
||||
// 302 Found
|
||||
// 303 See Other (default)
|
||||
// 307 Temporary Redirect
|
||||
int redirect_type;
|
||||
|
||||
// send header X-LIGHTTPD-send-file with path to a file
|
||||
std::wstring x_sendfile;
|
||||
|
||||
// send as attachment (causes generating header: content-disposition: attachment)
|
||||
bool send_as_attachment;
|
||||
|
||||
// headers send to the client (without cookies)
|
||||
PT::Space out_headers;
|
||||
|
||||
// cookies send to the client
|
||||
// a value can be either a cookie value or the whole cookie string (with domain, date etc)
|
||||
PT::Space out_cookies;
|
||||
|
||||
// winix can return either a text answer or a binary answer
|
||||
// if send_bin_stream is true then the binary answer is sent (out_bin_stream)
|
||||
// or if send_bin_stream is false then the text answer is sent
|
||||
// default: false
|
||||
//
|
||||
//
|
||||
// winix answer send to the client's browser
|
||||
// |
|
||||
// |
|
||||
// depending on send_bin_stream
|
||||
// (if false) ------------------------------------------------- (if true)
|
||||
// | |
|
||||
// text answer binary answer
|
||||
// | |
|
||||
// depending on return_json sending out_bin_stream
|
||||
// (if false) ------------------------------------ (if true)
|
||||
// | |
|
||||
// normal request ajax request
|
||||
// | |
|
||||
// sending out_main_stream |
|
||||
// |
|
||||
// |
|
||||
// depending on return_info_only
|
||||
// (if false) ------------------------------------------------------------- (if true)
|
||||
// | |
|
||||
// generating JSON object from: generating JSON object only from info
|
||||
// out_streams and info, e.g.: e.g.:
|
||||
// { { info object serialized here }
|
||||
// "out": { out_streams serialized here e.g.:
|
||||
// "stream_name_1": "some html content",
|
||||
// "stream_name_2": "some other html content"
|
||||
// },
|
||||
// "info": { info object serialized here }
|
||||
// }
|
||||
//
|
||||
//
|
||||
bool send_bin_stream;
|
||||
|
||||
// binary page sent to the client if send_bin_stream is true
|
||||
BinaryPage out_bin_stream;
|
||||
|
||||
// when returning the text answer we can either return the whole html page (normal requests)
|
||||
// or a JSON object (for requests generated from AJAX)
|
||||
// if return_json is false then we return the whole html page (which is in out_main_stream)
|
||||
// if return_json is true we are creating an JSON object from out_streams
|
||||
// and from info space (see above picture)
|
||||
// (or just only from info if return_info_only is true)
|
||||
// default: false
|
||||
// return_json is set to true by App at the beginning of a request
|
||||
// if reqtype:json parameter is present (in the url)
|
||||
// note: return_json is only valid if send_bin_stream is false
|
||||
bool return_json;
|
||||
|
||||
// main text output stream where the html otput is generated from ezc templates
|
||||
// here the whole html page (with doctype, head, body) is generated
|
||||
HtmlTextStream out_main_stream;
|
||||
|
||||
// text output streams used in ajax requests (send in JSON format to the client)
|
||||
// in ezc templates you can use [ezc out "stream_name"] keyword
|
||||
// to switch between streams
|
||||
Ezc::OutStreams<HtmlTextStream> out_streams;
|
||||
|
||||
// if true the JSON object is generated only from info (out_streams are not used)
|
||||
// default: false
|
||||
bool return_info_only;
|
||||
|
||||
// additional info added when sending the JSON answer
|
||||
PT::Space info;
|
||||
|
||||
// info serializer
|
||||
// if not set then the json_generic_serializer from App will be used
|
||||
// default: null (json_generic_serializer used)
|
||||
PT::SpaceToJSON * info_serializer;
|
||||
|
||||
|
||||
|
||||
|
||||
// if set to true then the standard template system will not be used
|
||||
// default: false
|
||||
bool page_generated;
|
||||
|
||||
// whether or not the main html stream should be filtered by our html filter
|
||||
bool out_main_stream_use_html_filter;
|
||||
|
||||
// whether or not the ajax streams should be filtered by our html filter
|
||||
// this filter is only aplied to streams in "out" space, "info" space is not touched
|
||||
bool out_streams_use_html_filter;
|
||||
|
||||
|
||||
// if this variable is true then winix always return 200 OK header
|
||||
// when the status would be 404 (not found) or 403 (permission denied)
|
||||
// default: false
|
||||
bool use_200_status_for_not_found_and_permission_denied;
|
||||
|
||||
// options used by ezc generators
|
||||
bool gen_trim_white;
|
||||
bool gen_skip_new_line;
|
||||
bool gen_use_special_chars;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
additional variables used for common uses
|
||||
*/
|
||||
|
||||
// usually items in the current directory (depends on the function)
|
||||
std::vector<Item> item_tab;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Request();
|
||||
void SetConfig(Config * pconfig);
|
||||
|
||||
void RequestStarts();
|
||||
void Clear();
|
||||
|
||||
|
||||
|
||||
bool IsParam(const wchar_t * param_name);
|
||||
bool IsParam(const std::wstring & param_name);
|
||||
const std::wstring & ParamValue(const wchar_t * param_name); // returns an empty string if there is no such a parameter
|
||||
const std::wstring & ParamValue(const std::wstring & param_name); // returns an empty string if there is no such a parameter
|
||||
|
||||
bool IsPostVar(const wchar_t * var);
|
||||
bool IsPostVar(const std::wstring & var);
|
||||
const std::wstring & PostVar(const wchar_t * var); // returns an empty string if there is no such a parameter
|
||||
const std::wstring & PostVar(const std::wstring & var); // returns an empty string if there is no such a parameter
|
||||
bool PostVar(const wchar_t * var, std::wstring & result);
|
||||
bool PostVar(const std::wstring & var, std::wstring & result);
|
||||
std::wstring * PostVarp(const wchar_t * var);
|
||||
std::wstring * PostVarp(const std::wstring & var);
|
||||
|
||||
bool AllPostVarEmpty(); // returning true if all post vars are empty
|
||||
|
||||
|
||||
// setting a cookie
|
||||
// name - cookie name (either const wchar_t, or std::wstring or PT::WTextStream)
|
||||
// value - cookie value (can be everything which can be put to PT::WTextStream stream)
|
||||
// the return std::wstring reference is a reference to the cookie inserted value (in out_cookies structure)
|
||||
template<typename NameType, typename ValueType>
|
||||
std::wstring & AddCookie(const NameType & name, const ValueType & value, PT::Date * expires = 0);
|
||||
|
||||
template<typename NameType, typename ValueType>
|
||||
std::wstring & AddCookie(const NameType & name, const ValueType & value, PT::Date & expires);
|
||||
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Config * config;
|
||||
|
||||
// used in ParamValue() and PostVar() when there is no such a param
|
||||
const std::wstring str_empty;
|
||||
|
||||
void ClearOutputStreams();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename NameType, typename ValueType>
|
||||
std::wstring & Request::AddCookie(const NameType & name, const ValueType & value, PT::Date * expires)
|
||||
{
|
||||
PT::WTextStream cookie;
|
||||
|
||||
cookie << value;
|
||||
|
||||
if( cookie.empty() )
|
||||
cookie << L"\"\""; // cookie empty value
|
||||
|
||||
if( expires )
|
||||
cookie << L"; expires=" << DateToStrCookie(*expires) << L" GMT";
|
||||
|
||||
cookie << L"; path=/; domain=" << config->base_url;
|
||||
|
||||
/*
|
||||
!! IMPROVE ME add an option to the config
|
||||
|
||||
don't use '; secure' flag if you are using both sites (with SSL
|
||||
and without SSL) -- with secure flag the cookie is sent only through
|
||||
SSL and if you accidentally open a new window without SSL (http://)
|
||||
then winix will create a new session for you and the previous session (https://)
|
||||
will be lost (the session cookie will be overwritten in the client's browser)
|
||||
*/
|
||||
|
||||
return out_cookies.Add(name, cookie);
|
||||
}
|
||||
|
||||
|
||||
template<typename NameType, typename ValueType>
|
||||
std::wstring & Request::AddCookie(const NameType & name, const ValueType & value, PT::Date & expires)
|
||||
{
|
||||
return AddCookie(name, value, &expires);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
#endif
|
||||
|
||||
87
winixd/core/requesttypes.h
Normal file
87
winixd/core/requesttypes.h
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_requesttypes
|
||||
#define headerfile_winix_core_requesttypes
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "textstream/textstream.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
// !! IMPROVE ME
|
||||
// !! narazie uzywane tylko w post multi parserze
|
||||
// dodac do zwyklego parsera post
|
||||
#define WINIX_POSTTABLE_MAXSIZE 50
|
||||
|
||||
|
||||
|
||||
struct PostFile
|
||||
{
|
||||
std::wstring filename; // original file name
|
||||
std::wstring tmp_filename; // file with content (in /tmp)
|
||||
size_t file_size;
|
||||
};
|
||||
|
||||
|
||||
// parameters from get name:value
|
||||
struct Param
|
||||
{
|
||||
std::wstring name;
|
||||
std::wstring value;
|
||||
};
|
||||
|
||||
|
||||
// some global types used by Request class
|
||||
typedef std::map<std::wstring, std::wstring> PostTab;
|
||||
typedef std::map<std::wstring, PostFile> PostFileTab;
|
||||
typedef std::vector<Param> ParamTab;
|
||||
|
||||
typedef std::map<std::wstring, std::wstring> CookieTab;
|
||||
|
||||
|
||||
typedef PT::TextStreamBase<char, 1, 4096> BinaryPage;
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
420
winixd/core/run.cpp
Normal file
420
winixd/core/run.cpp
Normal file
@@ -0,0 +1,420 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <cstring>
|
||||
#include "run.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
Run::Run()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::Clear()
|
||||
{
|
||||
parlen = 0;
|
||||
envlen = 0;
|
||||
command = 0;
|
||||
last_status = 0;
|
||||
last_return = 255;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::SetName()
|
||||
{
|
||||
size_t i=0;
|
||||
|
||||
while( command[i] )
|
||||
i += 1;
|
||||
|
||||
while( i>0 && command[i-1]!='/' )
|
||||
i -= 1;
|
||||
|
||||
par[0] = const_cast<char*>(command + i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::Par(const char * p)
|
||||
{
|
||||
if( parlen < WINIX_RUN_MAX_PARAMETERS )
|
||||
{
|
||||
// they will be copied (fork)
|
||||
// the first (zero) parameter is a program name
|
||||
par[1 + parlen++] = const_cast<char*>(p);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::Env(const char * e)
|
||||
{
|
||||
if( envlen < WINIX_RUN_MAX_PARAMETERS )
|
||||
{
|
||||
// they will be copied (fork)
|
||||
env[envlen++] = const_cast<char*>(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::Cmd(const char * c)
|
||||
{
|
||||
command = c;
|
||||
SetName();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::Par(const std::string & p)
|
||||
{
|
||||
Par(p.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::Env(const std::string & e)
|
||||
{
|
||||
Env(e.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::Cmd(const std::string & c)
|
||||
{
|
||||
Cmd(c.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int Run::LastStatus()
|
||||
{
|
||||
return last_status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int Run::LastReturn()
|
||||
{
|
||||
return last_return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::Write(const char * in, size_t inlen)
|
||||
{
|
||||
ssize_t len;
|
||||
|
||||
while( inlen > 0 )
|
||||
{
|
||||
len = write(desout[1], in, inlen);
|
||||
|
||||
if( len < 0 || len > (ssize_t)inlen )
|
||||
{
|
||||
// something wrong
|
||||
last_status = 3;
|
||||
break;
|
||||
}
|
||||
|
||||
in += len;
|
||||
inlen -= len;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::Read(std::string & out)
|
||||
{
|
||||
char buf[1024];
|
||||
size_t buflen = sizeof(buf) / sizeof(char);
|
||||
ssize_t len;
|
||||
|
||||
do
|
||||
{
|
||||
len = read(desin[0], buf, buflen);
|
||||
|
||||
if( len < 0 || len > (ssize_t)buflen )
|
||||
{
|
||||
// something wrong
|
||||
last_status = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
if( len > 0 )
|
||||
out.append(buf, len);
|
||||
}
|
||||
while( len > 0 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::ChildThrow()
|
||||
{
|
||||
if( dup2(desout[0], 0) == -1 )
|
||||
return;
|
||||
|
||||
if( dup2(desin[1], 1) == -1 )
|
||||
return;
|
||||
|
||||
close(2);
|
||||
close(desin[0]);
|
||||
close(desout[1]);
|
||||
|
||||
par[parlen+1] = 0;
|
||||
env[envlen] = 0;
|
||||
|
||||
execve(command, par, env);
|
||||
// if we are here then there is something wrong (execve failed)
|
||||
// !! IMPROVE ME may add exit() ?
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::Child()
|
||||
{
|
||||
try
|
||||
{
|
||||
ChildThrow();
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
}
|
||||
|
||||
exit(255);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Run::CreatePipes()
|
||||
{
|
||||
int res = pipe(desin);
|
||||
|
||||
if( res != 0 )
|
||||
{
|
||||
log << log1 << "Run: pipe failed (desin)" << logend;
|
||||
last_status = 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
res = pipe(desout);
|
||||
|
||||
if( res != 0 )
|
||||
{
|
||||
log << log1 << "Run: pipe failed (desout)" << logend;
|
||||
last_status = 1;
|
||||
close(desin[0]);
|
||||
close(desin[1]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Run::Fork()
|
||||
{
|
||||
childpid = fork();
|
||||
|
||||
if( childpid == -1 )
|
||||
{
|
||||
log << log1 << "Run: fork failed" << logend;
|
||||
last_status = 2;
|
||||
close(desin[0]);
|
||||
close(desin[1]);
|
||||
close(desout[0]);
|
||||
close(desout[1]);
|
||||
return false;
|
||||
}
|
||||
|
||||
if( childpid == 0 )
|
||||
Child();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::WriteRead(const char * in, size_t inlen, std::string * out)
|
||||
{
|
||||
if( in && inlen>0 )
|
||||
Write(in, inlen);
|
||||
|
||||
close(desout[1]);
|
||||
|
||||
if( out )
|
||||
Read(*out);
|
||||
|
||||
close(desin[0]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Run::CheckStatus()
|
||||
{
|
||||
int status;
|
||||
|
||||
pid_t res = waitpid(childpid, &status, 0);
|
||||
|
||||
if( res == -1 )
|
||||
{
|
||||
log << log1 << "Run: waitpid failed" << logend;
|
||||
last_status = 6;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( WIFEXITED(status) )
|
||||
{
|
||||
last_return = WEXITSTATUS(status);
|
||||
}
|
||||
else
|
||||
{
|
||||
if( WIFCONTINUED(status) )
|
||||
log << log1 << "Run: child error: WIFCONTINUED" << logend;
|
||||
else
|
||||
if( WIFSIGNALED(status) )
|
||||
log << log1 << "Run: child error: WIFSIGNALED" << logend;
|
||||
else
|
||||
if( WIFSTOPPED(status) )
|
||||
log << log1 << "Run: child error: WIFSTOPPED" << logend;
|
||||
|
||||
last_status = 5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int Run::Go(const char * in, size_t inlen, std::string * out)
|
||||
{
|
||||
last_status = 0;
|
||||
last_return = 255;
|
||||
|
||||
if( out )
|
||||
out->clear();
|
||||
|
||||
if( !command )
|
||||
{
|
||||
last_status = 7;
|
||||
return last_return;
|
||||
}
|
||||
|
||||
if( !CreatePipes() )
|
||||
return last_return;
|
||||
|
||||
if( !Fork() )
|
||||
return last_return;
|
||||
|
||||
// here goes the parent
|
||||
close(desin[1]);
|
||||
close(desout[0]);
|
||||
WriteRead(in, inlen, out);
|
||||
CheckStatus();
|
||||
|
||||
if( last_status != 0 )
|
||||
log << log1 << "Run: a problem with the command, last_status: " << last_status << logend;
|
||||
|
||||
return last_return;
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const char * in, size_t inlen, std::string & out)
|
||||
{
|
||||
return Go(in, inlen, &out);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const char * in, std::string & out)
|
||||
{
|
||||
size_t len = strlen(in);
|
||||
return Go(in, len, &out);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const char * in, size_t inlen)
|
||||
{
|
||||
return Go(in, inlen, 0);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const char * in)
|
||||
{
|
||||
size_t len = strlen(in);
|
||||
return Go(in, len, 0);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go()
|
||||
{
|
||||
return Go(0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const std::string in, std::string & out)
|
||||
{
|
||||
return Go(in.c_str(), in.size(), &out);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const std::string in)
|
||||
{
|
||||
return Go(in.c_str(), in.size(), 0);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(std::string & out)
|
||||
{
|
||||
return Go(0, 0, &out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
198
winixd/core/run.h
Normal file
198
winixd/core/run.h
Normal file
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_run
|
||||
#define headerfile_winix_core_run
|
||||
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
/*
|
||||
how many parameters and environment variables can be passed to a program
|
||||
*/
|
||||
#define WINIX_RUN_MAX_PARAMETERS 30
|
||||
|
||||
|
||||
/*
|
||||
objects of this class allows you to run an external program
|
||||
|
||||
when you call Go() then:
|
||||
1. winix creates pipes for communicating with a child process
|
||||
2. then winix fork()
|
||||
3. the child process execve() the specified command
|
||||
4. winix (parent) sends 'in' to the standard input of the child process
|
||||
5. after sending it closes the descriptor so the child sees it as end-of-file
|
||||
6. now winix reads what the child sends to standard output (until EOF)
|
||||
7. winix waitpid() for the child
|
||||
8. Go() returns
|
||||
*/
|
||||
class Run
|
||||
{
|
||||
public:
|
||||
|
||||
Run();
|
||||
|
||||
|
||||
/*
|
||||
clearing parameters, environment variables and the command
|
||||
(and clearing LastStatus and LastResult)
|
||||
so you can call another different program now
|
||||
*/
|
||||
void Clear();
|
||||
|
||||
|
||||
/*
|
||||
setting parameters
|
||||
each parameter should be passed in different call to Par() method
|
||||
sample:
|
||||
if you want to call from your shell:
|
||||
$ myprog -a -b -f "test file"
|
||||
you should call Par() in this way:
|
||||
Par("-a");
|
||||
Par("-b");
|
||||
Par("test file"); // apostrophes are not needed here
|
||||
|
||||
arguments passed to Par() should not be changed afterwards, this method does not copy them anywhere
|
||||
it uses only the pointer
|
||||
*/
|
||||
void Par(const char * p);
|
||||
void Par(const std::string & p);
|
||||
|
||||
|
||||
/*
|
||||
setting environment variables
|
||||
one variable per one Env() call
|
||||
|
||||
arguments passed to Env() should not be changed afterwards, this method does not copy them anywhere
|
||||
it uses only the pointer
|
||||
*/
|
||||
void Env(const char * e);
|
||||
void Env(const std::string & e);
|
||||
|
||||
|
||||
/*
|
||||
full path to command you want to execute
|
||||
|
||||
arguments passed to Cmd() should not be changed afterwards, this method does not copy them anywhere
|
||||
it uses only the pointer
|
||||
*/
|
||||
void Cmd(const char * c);
|
||||
void Cmd(const std::string & c);
|
||||
|
||||
|
||||
/*
|
||||
executing the command
|
||||
you should call Par(), Env() and Cmd() beforehand
|
||||
*/
|
||||
int Go(const char * in, size_t inlen, std::string & out);
|
||||
int Go(const char * in, std::string & out);
|
||||
int Go(const char * in, size_t inlen);
|
||||
int Go(const char * in);
|
||||
int Go(const std::string in, std::string & out);
|
||||
int Go(const std::string in);
|
||||
int Go(std::string & out);
|
||||
int Go();
|
||||
|
||||
|
||||
/*
|
||||
last status:
|
||||
0 - ok (program was successfully called)
|
||||
1 - pipe failed
|
||||
2 - fork failed
|
||||
3 - write failed
|
||||
4 - read failed
|
||||
5 - child process has done something wrong (caught a signal etc.)
|
||||
6 - waitpid failed
|
||||
7 - the command is not set (call Cmd method first)
|
||||
*/
|
||||
int LastStatus();
|
||||
|
||||
|
||||
/*
|
||||
the code which the command returned (usually "0" means no errors found)
|
||||
if LastStatus is different from zero then LastReturn always returns 255
|
||||
(so you don't have to check LastStatus() first)
|
||||
*/
|
||||
int LastReturn();
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
int Go(const char * in, size_t inlen, std::string * out);
|
||||
void SetName();
|
||||
void CheckStatus();
|
||||
void WriteRead(const char * in, size_t inlen, std::string * out);
|
||||
bool CreatePipes();
|
||||
bool Fork();
|
||||
void ChildThrow();
|
||||
void Child();
|
||||
void Write(const char * in, size_t inlen);
|
||||
void Read(std::string & out);
|
||||
|
||||
|
||||
int last_status;
|
||||
|
||||
// the return code returned by a program (if last_status==0)
|
||||
// if last_status!=0 then last_return is 255
|
||||
int last_return;
|
||||
|
||||
int desin[2];
|
||||
int desout[2];
|
||||
|
||||
char * par[WINIX_RUN_MAX_PARAMETERS + 2];
|
||||
char * env[WINIX_RUN_MAX_PARAMETERS + 1];
|
||||
const char * command;
|
||||
|
||||
size_t parlen;
|
||||
size_t envlen;
|
||||
|
||||
pid_t childpid;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
128
winixd/core/session.cpp
Normal file
128
winixd/core/session.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "session.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
Session::Session()
|
||||
{
|
||||
Clear();
|
||||
plugin_data.SetSession(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Session::Session(const Session & ses)
|
||||
{
|
||||
operator=(ses);
|
||||
}
|
||||
|
||||
Session & Session::operator=(const Session & ses)
|
||||
{
|
||||
/*
|
||||
we can only copy ses.id because it is needen in SessionContainer
|
||||
it have indexes to id
|
||||
*/
|
||||
|
||||
Clear();
|
||||
id = ses.id;
|
||||
plugin_data.SetSession(this);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Session::SetTimesTo(time_t time)
|
||||
{
|
||||
start_time = time;
|
||||
start_date = time;
|
||||
|
||||
last_time = start_time;
|
||||
last_date = start_date;
|
||||
|
||||
// the first request can be a POST (it doesn't matter)
|
||||
last_time_get = start_time;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// clear_plugin_data is used when clearing the temporary session
|
||||
void Session::Clear(bool clear_plugin_data)
|
||||
{
|
||||
id = 0;
|
||||
id_index = 0;
|
||||
id_index_changed = 0;
|
||||
puser = 0;
|
||||
rebus_item = 0;
|
||||
rebus_checked = false;
|
||||
remember_me = false;
|
||||
new_session = true;
|
||||
spam_score = 0;
|
||||
remove_me = false;
|
||||
|
||||
start_time = 0;
|
||||
last_time = 0;
|
||||
last_time_get = 0;
|
||||
start_date.Clear();
|
||||
last_date.Clear();
|
||||
|
||||
log_buffer.Clear();
|
||||
last_css.clear();
|
||||
ip_ban = 0;
|
||||
|
||||
if( clear_plugin_data )
|
||||
plugin_data.Resize(0);
|
||||
}
|
||||
|
||||
|
||||
// clearing some variables when a request is ended (just for safety)
|
||||
void Session::ClearAfterRequest()
|
||||
{
|
||||
// ip_ban list can be sorted by SessionManager (in the special thread)
|
||||
ip_ban = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
139
winixd/core/session.h
Normal file
139
winixd/core/session.h
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_session
|
||||
#define headerfile_winix_core_session
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
#include "item.h"
|
||||
#include "error.h"
|
||||
#include "user.h"
|
||||
#include "plugindata.h"
|
||||
#include "rebus.h"
|
||||
#include "textstream.h"
|
||||
#include "date/date.h"
|
||||
#include "ipban.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
struct Session
|
||||
{
|
||||
Session();
|
||||
Session(const Session & ses);
|
||||
Session & operator=(const Session & ses);
|
||||
|
||||
void SetTimesTo(time_t time);
|
||||
void Clear(bool clear_plugin_data = true);
|
||||
void ClearAfterRequest();
|
||||
|
||||
|
||||
// 0 - means that there is a temporary session
|
||||
long id;
|
||||
|
||||
// a session index
|
||||
// incremented each time a request to this session is made
|
||||
unsigned int id_index;
|
||||
|
||||
// the last time when id_index was incremented
|
||||
time_t id_index_changed;
|
||||
|
||||
// true if the session was created now
|
||||
bool new_session;
|
||||
|
||||
// when this session was created
|
||||
// (the same values)
|
||||
time_t start_time;
|
||||
PT::Date start_date;
|
||||
|
||||
// when this session was last used
|
||||
// (the same values)
|
||||
time_t last_time;
|
||||
PT::Date last_date;
|
||||
|
||||
// when there was a last get request
|
||||
// (used to calculate spam or invalid login attempts etc.)
|
||||
time_t last_time_get;
|
||||
|
||||
|
||||
// 0 - means that nobody is logged
|
||||
User * puser;
|
||||
|
||||
// if false the session will end when the user browser is shutdown
|
||||
bool remember_me;
|
||||
|
||||
|
||||
// rebus - set by rebus_question(Info & i) from templates
|
||||
Rebus::Item * rebus_item;
|
||||
bool rebus_checked;
|
||||
|
||||
|
||||
int spam_score;
|
||||
|
||||
// if true then this session will be removed by SessionManager
|
||||
// without checking the time expiration
|
||||
bool remove_me;
|
||||
|
||||
PluginData plugin_data;
|
||||
|
||||
|
||||
// buffer for the session log
|
||||
TextStream<std::wstring> log_buffer;
|
||||
|
||||
// !! IMPROVE ME it is still needed?
|
||||
// css cannot be taken directly from the mountpoint?
|
||||
// table with css files
|
||||
// used by some javascript wysiwyg editors (ckeditor, tinymce)
|
||||
std::vector<std::wstring> last_css;
|
||||
|
||||
// pointer to IPBan struct if exists for this IP
|
||||
// many sessions can pointer to the same IPBan struct
|
||||
// (it can be null)
|
||||
IPBan * ip_ban;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
244
winixd/core/sessioncontainer.cpp
Normal file
244
winixd/core/sessioncontainer.cpp
Normal file
@@ -0,0 +1,244 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "sessioncontainer.h"
|
||||
#include "log.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
SessionContainer::SessionContainer()
|
||||
{
|
||||
table_size = 0;
|
||||
}
|
||||
|
||||
|
||||
void SessionContainer::SetCur(Cur * pcur)
|
||||
{
|
||||
cur = pcur;
|
||||
}
|
||||
|
||||
|
||||
void SessionContainer::SetConfig(Config * pconfig)
|
||||
{
|
||||
config = pconfig;
|
||||
}
|
||||
|
||||
|
||||
void SessionContainer::SetTmpSession(Session * psession)
|
||||
{
|
||||
tmp_session = psession;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionContainer::Clear()
|
||||
{
|
||||
Table::iterator i = table.begin();
|
||||
|
||||
log << log3 << "SC: deleting all sessions" << logend;
|
||||
|
||||
cur->session = tmp_session;
|
||||
|
||||
// don't use table.clear();
|
||||
// because plugins session data would not be erased
|
||||
while( i != table.end() )
|
||||
{
|
||||
i->plugin_data.DeleteAll(); // it's better to call it here instead in the destructor
|
||||
table.erase(i++);
|
||||
}
|
||||
|
||||
// erasing indexes
|
||||
index_id.clear();
|
||||
table_size = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionContainer::EraseById(long id)
|
||||
{
|
||||
IndexId::iterator i = index_id.find(id);
|
||||
|
||||
if( i != index_id.end() )
|
||||
{
|
||||
Session * old_session = tmp_session;
|
||||
|
||||
if( cur->session != &(*i->second) )
|
||||
old_session = cur->session;
|
||||
|
||||
cur->session = &(*i->second);
|
||||
|
||||
log << log4 << "SC: deleting session, id: " << i->second->id << logend;
|
||||
|
||||
// call first DeleteAll() because if not then it would be called from the destructor
|
||||
// and there'll be a problem if it throws an exception there
|
||||
i->second->plugin_data.DeleteAll();
|
||||
table.erase(i->second);
|
||||
index_id.erase(i);
|
||||
table_size -= 1;
|
||||
|
||||
cur->session = old_session;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "SC: I cannot delete a session with id: " << id
|
||||
<< " (there is no such a session)" << logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t SessionContainer::Size()
|
||||
{
|
||||
// don't use table.size() as it has O(n) complexity on FreeBSD
|
||||
return table_size;
|
||||
}
|
||||
|
||||
|
||||
SessionContainer::Iterator SessionContainer::Begin()
|
||||
{
|
||||
return table.begin();
|
||||
}
|
||||
|
||||
|
||||
SessionContainer::Iterator SessionContainer::End()
|
||||
{
|
||||
return table.end();
|
||||
}
|
||||
|
||||
|
||||
Session & SessionContainer::Back()
|
||||
{
|
||||
return table.back();
|
||||
}
|
||||
|
||||
|
||||
SessionContainer::IdIterator SessionContainer::IdBegin()
|
||||
{
|
||||
return index_id.begin();
|
||||
}
|
||||
|
||||
|
||||
SessionContainer::IdIterator SessionContainer::IdEnd()
|
||||
{
|
||||
return index_id.end();
|
||||
}
|
||||
|
||||
|
||||
|
||||
SessionContainer::Iterator SessionContainer::AddSession(long id)
|
||||
{
|
||||
std::pair<IndexId::iterator, bool> index_id_res = index_id.insert( std::make_pair(id, table.end()) );
|
||||
|
||||
if( !index_id_res.second )
|
||||
{
|
||||
// that element already exists (was not inserted now)
|
||||
return End();
|
||||
}
|
||||
|
||||
Iterator last = table.insert(table.end(), empty_session);
|
||||
last->id = id;
|
||||
index_id_res.first->second = last;
|
||||
table_size += 1;
|
||||
|
||||
log << log3 << "SC: added session, id: " << id << logend;
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
SessionContainer::Iterator SessionContainer::FindById(long id)
|
||||
{
|
||||
IndexId::iterator i;
|
||||
|
||||
i = index_id.find(id);
|
||||
|
||||
if( i == index_id.end() )
|
||||
return table.end();
|
||||
|
||||
return i->second;
|
||||
}
|
||||
|
||||
|
||||
bool SessionContainer::ChangeSessionId(SessionContainer::Iterator ses, long new_id)
|
||||
{
|
||||
std::pair<IndexId::iterator, bool> index_id_res = index_id.insert( std::make_pair(new_id, ses) );
|
||||
|
||||
if( !index_id_res.second )
|
||||
{
|
||||
log << log1 << "SC: session with id: " << new_id << " already exists" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
long old_id = ses->id;
|
||||
index_id.erase(old_id); // remove the old index
|
||||
ses->id = new_id;
|
||||
log << log3 << "SC: changed session id from: " << old_id << " to " << new_id << logend;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SessionContainer::ChangeSessionId(long old_id, long new_id)
|
||||
{
|
||||
IndexId::iterator i = index_id.find(old_id);
|
||||
|
||||
if( i != index_id.end() )
|
||||
{
|
||||
return ChangeSessionId(i->second, new_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log2 << "SC: there is no a session with id: " << old_id << logend;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
|
||||
108
winixd/core/sessioncontainer.h
Normal file
108
winixd/core/sessioncontainer.h
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_sessioncontainer
|
||||
#define headerfile_winix_core_sessioncontainer
|
||||
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <ctime>
|
||||
|
||||
#include "session.h"
|
||||
#include "cur.h"
|
||||
#include "config.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
class SessionContainer
|
||||
{
|
||||
public:
|
||||
|
||||
typedef std::list<Session> Table;
|
||||
typedef Table::iterator Iterator;
|
||||
typedef std::map<long, Iterator> IndexId;
|
||||
typedef IndexId::iterator IdIterator;
|
||||
|
||||
|
||||
SessionContainer();
|
||||
|
||||
void SetCur(Cur * pcur);
|
||||
void SetConfig(Config * pconfig);
|
||||
void SetTmpSession(Session * psession);
|
||||
|
||||
void Clear();
|
||||
|
||||
size_t Size();
|
||||
Iterator Begin();
|
||||
Iterator End();
|
||||
Session & Back();
|
||||
Iterator AddSession(long id);
|
||||
Iterator FindById(long);
|
||||
|
||||
IdIterator IdBegin();
|
||||
IdIterator IdEnd();
|
||||
|
||||
void EraseById(long id);
|
||||
|
||||
bool ChangeSessionId(Iterator ses, long new_id);
|
||||
bool ChangeSessionId(long old_id, long new_id);
|
||||
|
||||
private:
|
||||
|
||||
Table table;
|
||||
IndexId index_id;
|
||||
|
||||
Cur * cur;
|
||||
Config * config;
|
||||
Session * tmp_session;
|
||||
|
||||
Session empty_session;
|
||||
|
||||
// in FreeBSD implementation (GCC) list::size() has linear complexity
|
||||
// so we use our own table_size with O(1)
|
||||
size_t table_size;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
561
winixd/core/sessionidmanager.cpp
Normal file
561
winixd/core/sessionidmanager.cpp
Normal file
@@ -0,0 +1,561 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include "sessionidmanager.h"
|
||||
#include "space/spaceparser.h"
|
||||
#include "utf8/utf8.h"
|
||||
#include "date/date.h"
|
||||
#include "log.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
SessionIdManager::SessionIdManager()
|
||||
{
|
||||
algorithm_type = 'a';
|
||||
key_tab_size = 256;
|
||||
key_index = 0;
|
||||
last_key_generated = 0;
|
||||
key_renew_time = 60;
|
||||
was_inited = false;
|
||||
}
|
||||
|
||||
|
||||
void SessionIdManager::Init(const std::wstring & keys_file)
|
||||
{
|
||||
was_inited = true;
|
||||
|
||||
key_tab1.resize(key_tab_size);
|
||||
key_tab2.resize(key_tab_size);
|
||||
|
||||
aes1.resize(key_tab_size);
|
||||
aes2.resize(key_tab_size);
|
||||
|
||||
key_file_name = keys_file;
|
||||
ReadKeysFromFile(key_file_name);
|
||||
}
|
||||
|
||||
|
||||
void SessionIdManager::SetKeyRenewTime(time_t renew_time)
|
||||
{
|
||||
key_renew_time = renew_time;
|
||||
|
||||
if( key_renew_time < 10 )
|
||||
key_renew_time = 10;
|
||||
|
||||
time_t one_month = 60 * 60 * 24 * 31;
|
||||
|
||||
if( key_renew_time > one_month )
|
||||
key_renew_time = one_month;
|
||||
}
|
||||
|
||||
|
||||
void SessionIdManager::ReadKey(const wchar_t * name, PT::Space & space, std::vector<std::string> & dest_key)
|
||||
{
|
||||
std::vector<std::wstring> keys;
|
||||
std::string key_ascii, key_base64_decoded;
|
||||
|
||||
space.ListText(name, keys);
|
||||
|
||||
for(size_t i=0 ; i<key_tab_size ; ++i)
|
||||
dest_key[i].clear();
|
||||
|
||||
for(size_t i=0 ; i<keys.size() && i<key_tab_size ; ++i)
|
||||
{
|
||||
dest_key[i].clear();
|
||||
|
||||
if( PT::WideToUTF8(keys[i], key_ascii) )
|
||||
{
|
||||
if( base64.Decode(key_ascii, key_base64_decoded) )
|
||||
{
|
||||
size_t len = key_base64_decoded.size();
|
||||
|
||||
if( len == 16 || len == 24 || len == 32 )
|
||||
{
|
||||
dest_key[i] = key_base64_decoded;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SessionIdManager::InitializeAesKeys(size_t index)
|
||||
{
|
||||
if( !aes1[index].Key((unsigned char*)key_tab1[index].c_str(), key_tab1[index].size()) )
|
||||
log << log1 << "SIM: I cannot initialize a key1, size of the key: " << key_tab1[index].size() << logend;
|
||||
|
||||
if( !aes2[index].Key((unsigned char*)key_tab2[index].c_str(), key_tab2[index].size()) )
|
||||
log << log1 << "SIM: I cannot initialize a key2, size of the key: " << key_tab2[index].size() << logend;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void SessionIdManager::InitializeAesKeys()
|
||||
{
|
||||
for(size_t i=0 ; i < key_tab_size ; ++i)
|
||||
{
|
||||
if( AreKeysCorrect(i) )
|
||||
InitializeAesKeys(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool SessionIdManager::ReadKeysFromFile(const wchar_t * file)
|
||||
{
|
||||
PT::Space space;
|
||||
PT::SpaceParser parser;
|
||||
PT::Date date;
|
||||
|
||||
parser.SetSpace(space);
|
||||
PT::SpaceParser::Status status = parser.Parse(file);
|
||||
|
||||
if( status == PT::SpaceParser::ok )
|
||||
{
|
||||
key_index = space.Size(L"key_index");
|
||||
|
||||
if( key_index >= 256 )
|
||||
key_index = 0;
|
||||
|
||||
if( date.Parse(space.Text(L"last_key_generated", L"0")) )
|
||||
last_key_generated = date.ToTime();
|
||||
|
||||
ReadKey(L"key_tab1", space, key_tab1);
|
||||
ReadKey(L"key_tab2", space, key_tab2);
|
||||
InitializeAesKeys();
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "SIM: I cannot read the session keys from: " << file << logend;
|
||||
}
|
||||
|
||||
return status == PT::SpaceParser::ok;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool SessionIdManager::ReadKeysFromFile(const std::wstring & file)
|
||||
{
|
||||
return ReadKeysFromFile(file.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionIdManager::SaveKeysToFile(std::vector<std::string> & keys)
|
||||
{
|
||||
out_file << "(\n";
|
||||
|
||||
for(size_t i=0 ; i<keys.size() ; ++i)
|
||||
{
|
||||
base64.Encode(keys[i], tmp_key_base64_encoded);
|
||||
out_file << '\"' << tmp_key_base64_encoded << "\"\n";
|
||||
}
|
||||
|
||||
out_file << ")\n\n";
|
||||
tmp_key_base64_encoded.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool SessionIdManager::SaveKeysToFile(const wchar_t * file)
|
||||
{
|
||||
PT::Date date = last_key_generated;
|
||||
PT::WideToUTF8(file, file_name_ascii);
|
||||
|
||||
out_file.clear();
|
||||
out_file.open(file_name_ascii, std::ios_base::binary | std::ios_base::out);
|
||||
|
||||
if( out_file )
|
||||
{
|
||||
out_file << "key_index = " << key_index << "\n";
|
||||
|
||||
out_file << "last_key_generated = \"";
|
||||
date.Serialize(out_file);
|
||||
out_file << "\"\n\n";
|
||||
|
||||
out_file << "key_tab1 = ";
|
||||
SaveKeysToFile(key_tab1);
|
||||
|
||||
out_file << "key_tab2 = ";
|
||||
SaveKeysToFile(key_tab2);
|
||||
|
||||
out_file.flush();
|
||||
}
|
||||
|
||||
out_file.close();
|
||||
file_name_ascii.clear();
|
||||
SetPriv(file, 0600);
|
||||
|
||||
return !out_file.fail();
|
||||
}
|
||||
|
||||
|
||||
bool SessionIdManager::SaveKeysToFile(const std::wstring & file)
|
||||
{
|
||||
return SaveKeysToFile(file.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool SessionIdManager::AreKeysCorrect(size_t index)
|
||||
{
|
||||
if( index >= 256 )
|
||||
return false;
|
||||
|
||||
size_t len = key_tab1[index].size();
|
||||
|
||||
if( len != 16 && len != 24 && len != 32 )
|
||||
return false;
|
||||
|
||||
len = key_tab2[index].size();
|
||||
|
||||
if( len != 16 && len != 24 && len != 32 )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* IMPROVE ME
|
||||
* we need a better algorithm
|
||||
*/
|
||||
void SessionIdManager::GenerateKey(std::string & key, time_t cur_utc_time)
|
||||
{
|
||||
unsigned int pid = (unsigned int)getpid();
|
||||
unsigned int t = (unsigned int)cur_utc_time;
|
||||
|
||||
key.clear();
|
||||
|
||||
for(size_t i=0 ; i<16 ; ++i)
|
||||
{
|
||||
unsigned int r = rand();
|
||||
unsigned int v = r ^ pid ^ t;
|
||||
v = ((v >> 24) ^ (v >> 16) ^ (v >> 8) ^ v) & 0xff;
|
||||
|
||||
key.push_back((unsigned char)v);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionIdManager::GenerateKeys(size_t index, time_t cur_utc_time)
|
||||
{
|
||||
GenerateKey(key_tab1[index], cur_utc_time);
|
||||
GenerateKey(key_tab2[index], cur_utc_time);
|
||||
InitializeAesKeys(index);
|
||||
|
||||
last_key_generated = cur_utc_time;
|
||||
|
||||
if( !SaveKeysToFile(key_file_name) )
|
||||
log << log2 << "SIM: I cannot save the session keys to: " << key_file_name << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionIdManager::CheckKeys(time_t cur_utc_time)
|
||||
{
|
||||
if( !AreKeysCorrect(key_index) )
|
||||
{
|
||||
log << log1 << "SIM: keys with index: " << key_index << " are incorrect, generating new keys" << logend;
|
||||
|
||||
GenerateKeys(key_index, cur_utc_time);
|
||||
}
|
||||
else
|
||||
if( last_key_generated + key_renew_time < cur_utc_time )
|
||||
{
|
||||
key_index += 1;
|
||||
|
||||
if( key_index >= key_tab_size )
|
||||
key_index = 0;
|
||||
|
||||
log << log2 << "SIM: generating new AES keys with index: " << key_index << logend;
|
||||
|
||||
GenerateKeys(key_index, cur_utc_time);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionIdManager::RandPadding(size_t & pad_top_size, char & pad_top_value,
|
||||
size_t & pad_bottom_size, char & pad_bottom_value)
|
||||
{
|
||||
pad_top_size = (std::rand() * 5) / RAND_MAX; // multiply by 5 (not by 4)
|
||||
|
||||
if( pad_top_size > 4 )
|
||||
pad_top_size = 4;
|
||||
|
||||
pad_top_size += 5; // now pad_top_size is from <5;9>
|
||||
pad_top_value = (char)std::rand();
|
||||
|
||||
pad_bottom_size = 14 - pad_top_size; // pad_bottom_size is from <5;9> too
|
||||
pad_bottom_value = (char)std::rand();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionIdManager::AppendSum(std::string & str)
|
||||
{
|
||||
int s = 0;
|
||||
|
||||
for(size_t i=0 ; i<str.size() ; ++i)
|
||||
s += (int)(unsigned char)str[i];
|
||||
|
||||
str += (unsigned char)s;
|
||||
}
|
||||
|
||||
|
||||
void SessionIdManager::AppendXor(std::string & str)
|
||||
{
|
||||
int s = 0;
|
||||
|
||||
for(size_t i=0 ; i<str.size() ; ++i)
|
||||
s ^= (int)(unsigned char)str[i];
|
||||
|
||||
str += (unsigned char)s;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionIdManager::CopyString(const std::string & in, std::wstring & out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
if( out.capacity() < in.size() )
|
||||
out.reserve(in.size());
|
||||
|
||||
for(size_t i=0 ; i<in.size() ; ++i)
|
||||
out += in[i];
|
||||
}
|
||||
|
||||
|
||||
void SessionIdManager::CopyString(const std::wstring & in, std::string & out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
if( out.capacity() < in.size() )
|
||||
out.reserve(in.size());
|
||||
|
||||
for(size_t i=0 ; i<in.size() ; ++i)
|
||||
out += in[i];
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool SessionIdManager::Encode(std::string & str)
|
||||
{
|
||||
if( str.size() != 34 )
|
||||
return false;
|
||||
|
||||
if( !aes1[key_index].Encode((unsigned char*)string_token.c_str()+2, 16) )
|
||||
{
|
||||
log << log1 << "SIM: I cannot AES encode the first part of the token" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( !aes2[key_index].Encode((unsigned char*)string_token.c_str()+16+2, 16) )
|
||||
{
|
||||
log << log1 << "SIM: I cannot AES encode the second part of the token" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SessionIdManager::EncodeToken(size_t id, unsigned int index, time_t cur_utc_time, std::wstring & token)
|
||||
{
|
||||
size_t pad_top_size;
|
||||
size_t pad_bottom_size;
|
||||
char pad_top_value;
|
||||
char pad_bottom_value;
|
||||
|
||||
string_token.clear();
|
||||
string_token.reserve(50);
|
||||
|
||||
if( !was_inited )
|
||||
return false;
|
||||
|
||||
CheckKeys(cur_utc_time);
|
||||
RandPadding(pad_top_size, pad_top_value, pad_bottom_size, pad_bottom_value);
|
||||
|
||||
string_token += algorithm_type;
|
||||
string_token += (unsigned char)key_index;
|
||||
string_token += pad_top_value;
|
||||
string_token += pad_bottom_value;
|
||||
string_token += (unsigned char)pad_top_size;
|
||||
string_token += (unsigned char)pad_bottom_size;
|
||||
string_token.append(pad_top_size, pad_top_value);
|
||||
Append(string_token, id);
|
||||
Append(string_token, index);
|
||||
string_token.append(pad_bottom_size, pad_bottom_value);
|
||||
AppendSum(string_token);
|
||||
AppendXor(string_token);
|
||||
|
||||
if( !Encode(string_token) )
|
||||
return false;
|
||||
|
||||
base64.Encode(string_token, string_token_base64);
|
||||
CopyString(string_token_base64, token);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SessionIdManager::IsPaddingCorrect(const char * str, size_t len, char val)
|
||||
{
|
||||
if( len < 5 || len > 9 )
|
||||
return false;
|
||||
|
||||
for(size_t i=0 ; i<len ; ++i)
|
||||
if( str[i] != val )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SessionIdManager::DecodeAES(const char * str, size_t key)
|
||||
{
|
||||
if( !aes1[key].Decode((unsigned char*)str, 16) )
|
||||
{
|
||||
log << log1 << "SIM: I cannot AES decode the first block" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( !aes2[key].Decode((unsigned char*)str + 16, 16) )
|
||||
{
|
||||
log << log1 << "SIM: I cannot AES decode the second block" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SessionIdManager::CheckControlSums(const char * str)
|
||||
{
|
||||
char old_sum = *(str++);
|
||||
char old_xor = *(str++);
|
||||
|
||||
string_token.erase(string_token.size()-2);
|
||||
|
||||
AppendSum(string_token);
|
||||
AppendXor(string_token);
|
||||
|
||||
if( old_sum != string_token[string_token.size()-2] ||
|
||||
old_xor != string_token[string_token.size()-1] )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SessionIdManager::DecodeTokenA(size_t & id, unsigned int & index)
|
||||
{
|
||||
size_t pad_top_size;
|
||||
size_t pad_bottom_size;
|
||||
char pad_top_value;
|
||||
char pad_bottom_value;
|
||||
|
||||
const char * str = string_token.c_str() + 1;
|
||||
size_t key = (unsigned char)(*str);
|
||||
|
||||
str += 1;
|
||||
|
||||
if( !DecodeAES(str, key) )
|
||||
return false;
|
||||
|
||||
pad_top_value = *(str++);
|
||||
pad_bottom_value = *(str++);
|
||||
pad_top_size = (unsigned char)*(str++);
|
||||
pad_bottom_size = (unsigned char)*(str++);
|
||||
|
||||
if( pad_bottom_size != 14 - pad_top_size )
|
||||
return false;
|
||||
|
||||
if( !IsPaddingCorrect(str, pad_top_size, pad_top_value) )
|
||||
return false;
|
||||
|
||||
str += pad_top_size;
|
||||
|
||||
Read(str, id);
|
||||
str += 8; // sizeof(id), it's better to use constant '8' instead of sizeof() operator
|
||||
// because at the beginning we are making a test whether the string size is equal to 34
|
||||
// (in the future sizeof(size_t) can be different from 8)
|
||||
Read(str, index);
|
||||
str += 4; // sizeof(index)
|
||||
|
||||
if( !IsPaddingCorrect(str, pad_bottom_size, pad_bottom_value) )
|
||||
return false;
|
||||
|
||||
str += pad_bottom_size;
|
||||
|
||||
return CheckControlSums(str);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool SessionIdManager::DecodeToken(const std::wstring & token, size_t & id, unsigned int & index)
|
||||
{
|
||||
if( !was_inited )
|
||||
return false;
|
||||
|
||||
CopyString(token, string_token_base64);
|
||||
|
||||
if( !base64.Decode(string_token_base64, string_token) )
|
||||
return false;
|
||||
|
||||
if( string_token.size() != 34 )
|
||||
return false;
|
||||
|
||||
if( string_token[0] == 'a' )
|
||||
return DecodeTokenA(id, index);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
199
winixd/core/sessionidmanager.h
Normal file
199
winixd/core/sessionidmanager.h
Normal file
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_sessionidmanager
|
||||
#define headerfile_winix_core_sessionidmanager
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include "base64.h"
|
||||
#include "space/space.h"
|
||||
#include "aes.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
/*
|
||||
* this class is used to encode/decode the session cookie
|
||||
* in which there is a session id and a session's index
|
||||
*
|
||||
* session index is incremented each time a request comes to this session
|
||||
*
|
||||
* format of the encoded token:
|
||||
* 1 byte - algorithm type, currently only one algorithm: 'a'
|
||||
* 1 byte - an AES keys pair index
|
||||
* ---- below everything is encoded by AES (two 128 bits blocks, first block
|
||||
* is encoded by the first key and the second block is encoded by the second key) ------
|
||||
* 1 byte - top padding value
|
||||
* 1 byte - bottom padding value
|
||||
* 1 byte - top padding size
|
||||
* 1 byte - bottom padding size
|
||||
* 5-9 bytes - top padding
|
||||
* 8 bytes - the session id
|
||||
* 4 bytes - the session index
|
||||
* 5-9 bytes - bottom padding (bottom_padding_size = 14 - top_padding_size)
|
||||
* 1 byte - the sum of all previous bytes
|
||||
* 1 byte - the xor of all previous bytes (with the previous sum too)
|
||||
*
|
||||
* and at the end everyting is base64 encoded
|
||||
*
|
||||
*
|
||||
*/
|
||||
class SessionIdManager
|
||||
{
|
||||
public:
|
||||
|
||||
SessionIdManager();
|
||||
|
||||
/*
|
||||
* initialization
|
||||
* this method takes about 1MB memory more (for AES key expansions)
|
||||
* if you do not need the session cookie to be enrypted then don't call this method
|
||||
*
|
||||
*/
|
||||
void Init(const std::wstring & keys_file);
|
||||
|
||||
/*
|
||||
* how often a new AES key pairs should be generated
|
||||
*/
|
||||
void SetKeyRenewTime(time_t renew_time);
|
||||
|
||||
/*
|
||||
* encode/decode the session cookie
|
||||
* make sure the Init() method is called first
|
||||
*/
|
||||
bool EncodeToken(size_t id, unsigned int index, time_t cur_utc_time, std::wstring & token);
|
||||
bool DecodeToken(const std::wstring & token, size_t & id, unsigned int & index);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool was_inited;
|
||||
char algorithm_type;
|
||||
std::string string_token, string_token_base64;
|
||||
std::vector<std::string> key_tab1, key_tab2;
|
||||
size_t key_index;
|
||||
time_t last_key_generated;
|
||||
time_t key_renew_time;
|
||||
Tito::Base64 base64;
|
||||
size_t key_tab_size;
|
||||
std::ofstream out_file;
|
||||
std::string tmp_key_base64_encoded;
|
||||
std::wstring key_file_name;
|
||||
std::string file_name_ascii;
|
||||
std::vector<Tito::AES> aes1, aes2;
|
||||
|
||||
bool ReadKeysFromFile(const wchar_t * file);
|
||||
bool ReadKeysFromFile(const std::wstring & file);
|
||||
bool SaveKeysToFile(const wchar_t * file);
|
||||
bool SaveKeysToFile(const std::wstring & file);
|
||||
void ReadKey(const wchar_t * name, PT::Space & space, std::vector<std::string> & dest_key);
|
||||
bool AreKeysCorrect(size_t index);
|
||||
void GenerateKeys(size_t index, time_t cur_utc_time);
|
||||
void CheckKeys(time_t cur_utc_time);
|
||||
void GenerateKey(std::string & key, time_t cur_utc_time);
|
||||
void SaveKeysToFile(std::vector<std::string> & keys);
|
||||
void InitializeAesKeys();
|
||||
void RandPadding(size_t & pad_top_size, char & pad_top_value, size_t & pad_bottom_size, char & pad_bottom_value);
|
||||
void AppendSum(std::string & str);
|
||||
void AppendXor(std::string & str);
|
||||
void CopyString(const std::string & in, std::wstring & out);
|
||||
void CopyString(const std::wstring & in, std::string & out);
|
||||
bool Encode(std::string & str);
|
||||
bool DecodeTokenA(size_t & id, unsigned int & index);
|
||||
bool IsPaddingCorrect(const char * str, size_t len, char val);
|
||||
bool CheckControlSums(const char * str);
|
||||
void InitializeAesKeys(size_t index);
|
||||
bool DecodeAES(const char * str, size_t key);
|
||||
|
||||
template<typename Value>
|
||||
void Append(std::string & str, Value val);
|
||||
|
||||
template<typename Value>
|
||||
void Read(const char * str, Value & val);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<typename Value>
|
||||
void SessionIdManager::Append(std::string & str, Value val)
|
||||
{
|
||||
int move = sizeof(Value) * 8 - 8;
|
||||
int v;
|
||||
|
||||
do
|
||||
{
|
||||
if( move > 0 )
|
||||
v = ((val >> move) & 0xff);
|
||||
else
|
||||
v = val & 0xff;
|
||||
|
||||
str += (unsigned char)v;
|
||||
move -= 8;
|
||||
}
|
||||
while( move >= 0 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename Value>
|
||||
void SessionIdManager::Read(const char * str, Value & val)
|
||||
{
|
||||
val = 0;
|
||||
|
||||
for(size_t i=0 ; i<sizeof(Value) ; ++i)
|
||||
{
|
||||
val = val << 8;
|
||||
val = val | (unsigned char)str[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
874
winixd/core/sessionmanager.cpp
Normal file
874
winixd/core/sessionmanager.cpp
Normal file
@@ -0,0 +1,874 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <limits>
|
||||
#include "sessionmanager.h"
|
||||
#include "request.h"
|
||||
#include "log.h"
|
||||
#include "session.h"
|
||||
#include "sessionparser.h"
|
||||
#include "plugin.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
SessionManager::SessionManager()
|
||||
{
|
||||
temporary_session.id = 0;
|
||||
session = &temporary_session;
|
||||
session_tab.SetTmpSession(&temporary_session);
|
||||
|
||||
// thread work mode
|
||||
work_mode = 1;
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::SetCur(Cur * pcur)
|
||||
{
|
||||
cur = pcur;
|
||||
session_tab.SetCur(pcur);
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::SetConfig(Config * pconfig)
|
||||
{
|
||||
config = pconfig;
|
||||
session_tab.SetConfig(pconfig);
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::SetSystem(System * psystem)
|
||||
{
|
||||
system = psystem;
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::SetLastContainer(LastContainer * plast_container)
|
||||
{
|
||||
last_container = plast_container;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionManager::InitBanList()
|
||||
{
|
||||
ban_tab.SetMaxSize(config->ban_list_soft_max_size, config->ban_list_max_size);
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::InitCookieEncoding()
|
||||
{
|
||||
if( config->session_cookie_encode && !config->session_keys_file.empty() )
|
||||
session_id_manager.Init(config->session_keys_file);
|
||||
|
||||
session_id_manager.SetKeyRenewTime(config->session_key_renew_time);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
size_t SessionManager::Size()
|
||||
{
|
||||
return session_tab.Size();
|
||||
}
|
||||
|
||||
|
||||
bool SessionManager::IsSession(long id)
|
||||
{
|
||||
if( session_tab.FindById(id) == session_tab.End() )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SessionManager::EncodeSessionId(long id, unsigned int index, std::wstring & str)
|
||||
{
|
||||
return session_id_manager.EncodeToken((size_t)id, index, cur->request->start_time, str);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* IMPROVE ME we need a better algorithm
|
||||
*/
|
||||
long SessionManager::CreateSessionId()
|
||||
{
|
||||
long id;
|
||||
|
||||
// make sure to call std::srand() somewhere at the beginning
|
||||
// id must be != 0 (0 is reserved)
|
||||
do
|
||||
{
|
||||
if( sizeof(long) == 8 )
|
||||
{
|
||||
id = (((unsigned long)std::rand()) << 32) + std::rand();
|
||||
}
|
||||
else
|
||||
{
|
||||
id = std::rand();
|
||||
}
|
||||
|
||||
id += std::time(0);
|
||||
|
||||
if( id < 0 )
|
||||
id = -id;
|
||||
|
||||
}
|
||||
while( id == 0 ); // 0 reserved for a temporary session
|
||||
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void SessionManager::CreateSession()
|
||||
{
|
||||
int attempts = 100;
|
||||
SessionContainer::Iterator i = session_tab.End();
|
||||
|
||||
if( config->session_max == 0 || session_tab.Size() < config->session_max )
|
||||
{
|
||||
for( ; i == session_tab.End() && attempts > 0 ; --attempts )
|
||||
{
|
||||
long id = CreateSessionId();
|
||||
i = session_tab.AddSession(id);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log2 << "SM: sessions limit exceeded (" << config->session_max << ")" << logend;
|
||||
}
|
||||
|
||||
if( i != session_tab.End() )
|
||||
{
|
||||
is_session_set = true;
|
||||
session = &(*i);
|
||||
session->new_session = true;
|
||||
session->SetTimesTo(cur->request->start_time);
|
||||
session->id_index = (unsigned int)session->id;
|
||||
session->id_index += std::rand();
|
||||
|
||||
log << log2 << "SM: created a new session: " << session->id << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
// there is a problem with generating a new session id
|
||||
log << log1 << "SM: cannot create a session id" << logend;
|
||||
SetTemporarySession();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::SetTemporarySession()
|
||||
{
|
||||
is_session_set = true;
|
||||
|
||||
session = &temporary_session;
|
||||
session->Clear(false);
|
||||
session->SetTimesTo(cur->request->start_time);
|
||||
session->new_session = false; // temporary session was initialized at the beginning
|
||||
|
||||
log << log2 << "SM: using temporary session" << logend;
|
||||
}
|
||||
|
||||
|
||||
unsigned int SessionManager::CalculateIndexDifference(Session & ses, unsigned int index)
|
||||
{
|
||||
unsigned int difference;
|
||||
|
||||
if( index > ses.id_index )
|
||||
difference = std::numeric_limits<unsigned int>::max() - index + ses.id_index + 1;
|
||||
else
|
||||
difference = ses.id_index - index;
|
||||
|
||||
return difference;
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::SetSessionPutLogInfo(Session & ses, bool has_index, unsigned int difference)
|
||||
{
|
||||
log << log2 << "SM: session: " << ses.id;
|
||||
|
||||
if( has_index )
|
||||
log << ", index difference: " << (size_t)difference;
|
||||
|
||||
if( ses.puser )
|
||||
log << log2 << ", user: " << ses.puser->name << ", id: " << ses.puser->id;
|
||||
|
||||
log << log2 << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionManager::IncrementBanLevel(IPBan * ip_ban)
|
||||
{
|
||||
ip_ban->SetFlag(WINIX_IPBAN_FLAG_ACTIVE);
|
||||
|
||||
ip_ban->IncrementBanLevel(cur->request->start_time + (time_t)config->ban_level_1_delay,
|
||||
cur->request->start_time + (time_t)config->ban_level_2_delay,
|
||||
cur->request->start_time + (time_t)config->ban_level_3_delay);
|
||||
|
||||
PT::Date date(ip_ban->expires);
|
||||
log << log2 << "SM: this IP address has been banned to: " << date << " UTC" << logend;
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::SetFirstExpirationTime(IPBan * ip_ban)
|
||||
{
|
||||
time_t expiry = cur->request->start_time + (time_t)config->ban_level_1_delay;
|
||||
|
||||
if( ip_ban->expires < expiry )
|
||||
ip_ban->expires = expiry;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionManager::BrokenCookieCheckBan()
|
||||
{
|
||||
if( !current_ip_ban )
|
||||
current_ip_ban = &AddIPToBanList(cur->request->ip, cur->request->start_time);
|
||||
|
||||
if( current_ip_ban->broken_encoded_cookie_events < config->broken_encoded_cookie_treshold )
|
||||
{
|
||||
current_ip_ban->broken_encoded_cookie_events += 1;
|
||||
SetFirstExpirationTime(current_ip_ban);
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log2 << "SM: too many incorrect encoded cookies were sent from this IP" << logend;
|
||||
IncrementBanLevel(current_ip_ban);
|
||||
SetTemporarySession();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::IncorrectSessionCheckBan()
|
||||
{
|
||||
if( !current_ip_ban )
|
||||
current_ip_ban = &AddIPToBanList(cur->request->ip, cur->request->start_time);
|
||||
|
||||
if( current_ip_ban->session_hijacking_events < config->session_hijacking_treshold )
|
||||
{
|
||||
current_ip_ban->session_hijacking_events += 1;
|
||||
SetFirstExpirationTime(current_ip_ban);
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log2 << "SM: too many incorrect sessions identifiers were sent from this IP" << logend;
|
||||
IncrementBanLevel(current_ip_ban);
|
||||
SetTemporarySession();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionManager::NoSessionCookieCheckBan()
|
||||
{
|
||||
if( !current_ip_ban )
|
||||
current_ip_ban = &AddIPToBanList(cur->request->ip, cur->request->start_time);
|
||||
|
||||
if( current_ip_ban->no_session_cookie_events < config->no_session_cookie_treshold )
|
||||
{
|
||||
current_ip_ban->no_session_cookie_events += 1;
|
||||
SetFirstExpirationTime(current_ip_ban);
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log2 << "SM: too many times you have not sent a session cookie" << logend;
|
||||
|
||||
if( config->no_session_cookie_ban_mode == 1 )
|
||||
IncrementBanLevel(current_ip_ban);
|
||||
|
||||
SetTemporarySession();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool SessionManager::IsSessionCorrect(long id, bool has_index, unsigned int index,
|
||||
const SessionContainer::Iterator & s, unsigned int & difference)
|
||||
{
|
||||
difference = 0;
|
||||
|
||||
if( id == 0 )
|
||||
{
|
||||
log << log3 << "SM: id 0 is reserved for the temporary session" << logend;
|
||||
IncorrectSessionCheckBan();
|
||||
return false;
|
||||
}
|
||||
|
||||
if( s == session_tab.End() )
|
||||
{
|
||||
log << log3 << "SM: there is no a session with id: " << id << logend;
|
||||
IncorrectSessionCheckBan();
|
||||
return false;
|
||||
}
|
||||
|
||||
if( s->remove_me )
|
||||
{
|
||||
log << log3 << "SM: session: " << id << " is marked for removing" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( has_index )
|
||||
{
|
||||
difference = CalculateIndexDifference(*s, index);
|
||||
|
||||
if( (size_t)difference > config->session_allow_index_difference )
|
||||
{
|
||||
log << log2 << "SM: an incorrect session index for session: " << id
|
||||
<< ", index difference: " << (size_t)difference << logend;
|
||||
|
||||
IncorrectSessionCheckBan();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SessionManager::SetSessionFromCookie(long id, bool has_index, unsigned int index)
|
||||
{
|
||||
unsigned int difference;
|
||||
bool is_session_correct;
|
||||
|
||||
SessionContainer::Iterator s = session_tab.FindById(id);
|
||||
is_session_correct = IsSessionCorrect(id, has_index, index, s, difference);
|
||||
|
||||
if( is_session_correct )
|
||||
{
|
||||
is_session_set = true;
|
||||
session = &(*s);
|
||||
session->new_session = false;
|
||||
session->last_time = cur->request->start_time;
|
||||
session->last_date = cur->request->start_date;
|
||||
|
||||
if( session->id_index_changed + config->session_index_time_increment < cur->request->start_time )
|
||||
{
|
||||
session->id_index += 1;
|
||||
session->id_index_changed = cur->request->start_time;
|
||||
}
|
||||
|
||||
if( cur->request->method == Request::get )
|
||||
session->last_time_get = cur->request->start_time;
|
||||
|
||||
SetSessionPutLogInfo(*session, has_index, difference);
|
||||
}
|
||||
|
||||
return is_session_correct;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool SessionManager::SetSessionFromCookie(const std::wstring & cookie)
|
||||
{
|
||||
if( config->session_cookie_encode )
|
||||
{
|
||||
size_t id;
|
||||
unsigned int index;
|
||||
|
||||
if( !session_id_manager.DecodeToken(cookie, id, index) )
|
||||
{
|
||||
log << log2 << "SM: an incorrect cookie string was sent" << logend;
|
||||
BrokenCookieCheckBan();
|
||||
return false;
|
||||
}
|
||||
|
||||
return SetSessionFromCookie((long)id, true, index);
|
||||
}
|
||||
else
|
||||
{
|
||||
long id = Tol(cookie.c_str());
|
||||
return SetSessionFromCookie(id, false, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool SessionManager::IsIPBanned()
|
||||
{
|
||||
current_ip_ban = ban_tab.FindIP(cur->request->ip);
|
||||
|
||||
if( current_ip_ban )
|
||||
{
|
||||
current_ip_ban->last_used = cur->request->start_time;
|
||||
|
||||
if( current_ip_ban->expires != 0 && cur->request->start_time >= current_ip_ban->expires )
|
||||
{
|
||||
log << log2 << "SM: resetting events counters for this IP" << logend;
|
||||
current_ip_ban->ResetEventsCounters();
|
||||
}
|
||||
else
|
||||
if( current_ip_ban->IsIPBanned() )
|
||||
{
|
||||
PT::Date date = current_ip_ban->expires;
|
||||
log << log2 << "SM: this ip is bannned to: " << date << logend;
|
||||
SetTemporarySession();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::SetSession()
|
||||
{
|
||||
is_session_set = false;
|
||||
|
||||
if( !IsIPBanned() )
|
||||
{
|
||||
CookieTab::iterator i = cur->request->cookie_tab.find(config->http_session_id_name);
|
||||
|
||||
if( i != cur->request->cookie_tab.end() )
|
||||
{
|
||||
if( !SetSessionFromCookie(i->second) )
|
||||
cur->request->cookie_tab.erase(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
NoSessionCookieCheckBan();
|
||||
}
|
||||
}
|
||||
|
||||
if( !is_session_set )
|
||||
CreateSession();
|
||||
|
||||
session->ip_ban = current_ip_ban;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Session * SessionManager::FindSession(long id)
|
||||
{
|
||||
SessionContainer::Iterator i = session_tab.FindById(id);
|
||||
|
||||
if( i != session_tab.End() )
|
||||
return &*i;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
SessionContainer::Iterator SessionManager::SessionBegin()
|
||||
{
|
||||
return session_tab.Begin();
|
||||
}
|
||||
|
||||
|
||||
|
||||
SessionContainer::Iterator SessionManager::SessionEnd()
|
||||
{
|
||||
return session_tab.End();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void SessionManager::DeleteSessions()
|
||||
{
|
||||
SessionContainer::Iterator i;
|
||||
|
||||
for(i=session_tab.Begin() ; i!=session_tab.End() ; ++i)
|
||||
{
|
||||
if( i->puser && !i->remember_me )
|
||||
{
|
||||
plugin.Call(&(*i), WINIX_PREPARE_USER_TO_LOGOUT, i->puser);
|
||||
last_container->UserLogout(i->puser->id, i->id);
|
||||
}
|
||||
}
|
||||
|
||||
session_tab.Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
don't change a session's id when a user is logged
|
||||
the session id is in last_container and the user would not be
|
||||
correctly removed from the container
|
||||
*/
|
||||
bool SessionManager::ChangeSessionId(long old_id)
|
||||
{
|
||||
int attempts = 100;
|
||||
bool changed = false;
|
||||
long new_id;
|
||||
SessionContainer::Iterator i = session_tab.FindById(old_id);
|
||||
|
||||
if( i != session_tab.End() )
|
||||
{
|
||||
for( ; !changed && attempts > 0 ; --attempts )
|
||||
{
|
||||
new_id = CreateSessionId();
|
||||
changed = session_tab.ChangeSessionId(i, new_id);
|
||||
}
|
||||
|
||||
if( changed )
|
||||
plugin.Call(&(*i), WINIX_SESSION_CHANGED_ID, old_id, new_id);
|
||||
else
|
||||
log << log1 << "SM: I cannot create a new session id (still uses old one)" << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log2 << "SM: there is no a session with id: " << old_id << logend;
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::InitTmpSession()
|
||||
{
|
||||
Session * old_session = cur->session;
|
||||
|
||||
log << log4 << "SM: initializing temporary session" << logend;
|
||||
cur->session = &temporary_session;
|
||||
plugin.Call(WINIX_SESSION_CREATED);
|
||||
|
||||
cur->session = old_session;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionManager::UninitTmpSession()
|
||||
{
|
||||
Session * old_session = cur->session;
|
||||
|
||||
log << log4 << "SM: uninitializing temporary session" << logend;
|
||||
cur->session = &temporary_session;
|
||||
cur->session->plugin_data.DeleteAll(); // this will call plugin.Call(WINIX_PLUGIN_SESSION_DATA_REMOVE);
|
||||
cur->session->plugin_data.Resize(0);
|
||||
|
||||
cur->session = old_session;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionManager::LoadSessions()
|
||||
{
|
||||
SessionParser sp;
|
||||
SessionContainer::Iterator i;
|
||||
|
||||
// sessions will be overwritten (pointers are invalidated)
|
||||
cur->session = &temporary_session;
|
||||
|
||||
sp.SetUsers(&system->users);
|
||||
sp.Parse(config->session_file, session_tab);
|
||||
|
||||
for(i=session_tab.Begin() ; i != session_tab.End() ; ++i)
|
||||
{
|
||||
i->plugin_data.Resize(plugin.Size());
|
||||
plugin.Call(&(*i), WINIX_SESSION_CREATED);
|
||||
|
||||
/*
|
||||
!! IMPROVE ME
|
||||
we do not add it to the last_container (we don't have IP address stored yet)
|
||||
*/
|
||||
|
||||
if( i->puser )
|
||||
plugin.Call(&(*i), WINIX_USER_LOGGED);
|
||||
}
|
||||
|
||||
cur->session = &temporary_session;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void SessionManager::SaveSessions()
|
||||
{
|
||||
char file_path[WINIX_OS_PATH_SIZE];
|
||||
|
||||
if( config->session_file.empty() )
|
||||
return;
|
||||
|
||||
if( !WideToUTF8(config->session_file, file_path, WINIX_OS_PATH_SIZE) )
|
||||
return;
|
||||
|
||||
std::ofstream file(file_path);
|
||||
|
||||
if( !file )
|
||||
{
|
||||
log << log1 << "SM: cannot open the session file for writing - sessions lost" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
log << log2 << "SM: saving sessions" << logend;
|
||||
long len = 0;
|
||||
|
||||
SessionContainer::Iterator i = session_tab.Begin();
|
||||
|
||||
for( ; i!=session_tab.End() ; ++i )
|
||||
{
|
||||
if( i->id != 0 && i->puser && !i->remove_me )
|
||||
{
|
||||
file << i->id << ' ' << i->puser->id << ' ' << i->remember_me << ' ';
|
||||
file << (long)i->start_time << ' ' << (long)i->last_time << ' ';
|
||||
file << i->id_index << std::endl;
|
||||
|
||||
++len;
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
chmod(file_path, 0600);
|
||||
|
||||
log << log2 << "SM: saved " << len << " session(s)" << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Session * SessionManager::GetTmpSession()
|
||||
{
|
||||
return &temporary_session;
|
||||
}
|
||||
|
||||
|
||||
Session * SessionManager::GetCurSession()
|
||||
{
|
||||
return session;
|
||||
}
|
||||
|
||||
|
||||
// returns how many sessions was marked to remove
|
||||
size_t SessionManager::MarkAllSessionsToRemove(long user_id)
|
||||
{
|
||||
size_t how_many = 0;
|
||||
SessionContainer::Iterator i;
|
||||
|
||||
for(i=session_tab.Begin() ; i!=session_tab.End() ; ++i)
|
||||
{
|
||||
if( i->puser && i->puser->id == user_id )
|
||||
{
|
||||
plugin.Call(&(*i), WINIX_PREPARE_USER_TO_LOGOUT, i->puser);
|
||||
last_container->UserLogout(i->puser->id, i->id);
|
||||
i->remove_me = true;
|
||||
i->puser = 0;
|
||||
how_many += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return how_many;
|
||||
}
|
||||
|
||||
|
||||
IPBan & SessionManager::AddIPToBanList(int ip)
|
||||
{
|
||||
return ban_tab.AddIP(ip);
|
||||
}
|
||||
|
||||
|
||||
IPBan & SessionManager::AddIPToBanList(int ip, time_t last_used)
|
||||
{
|
||||
IPBan & ban = ban_tab.AddIP(ip);
|
||||
ban.last_used = last_used;
|
||||
|
||||
return ban;
|
||||
}
|
||||
|
||||
|
||||
size_t SessionManager::BanListSize()
|
||||
{
|
||||
return ban_tab.Size();
|
||||
}
|
||||
|
||||
|
||||
IPBan & SessionManager::GetIPBan(size_t index)
|
||||
{
|
||||
return ban_tab.GetIPBan(index);
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::RemoveIPBan(int ip)
|
||||
{
|
||||
ban_tab.RemoveIP(ip);
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::ClearIPBanList()
|
||||
{
|
||||
ban_tab.Clear();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
* sessions gc (second thread)
|
||||
* sessions are only removed here
|
||||
* SessionContainer::IndexId can be removed from the other thread
|
||||
* (when ChangeSessionId() method is called)
|
||||
*
|
||||
*/
|
||||
void SessionManager::Work()
|
||||
{
|
||||
bool exit = false;
|
||||
SessionContainer::Iterator i;
|
||||
|
||||
deleted = 0;
|
||||
|
||||
Lock();
|
||||
i = session_tab.Begin();
|
||||
Unlock();
|
||||
|
||||
while( !exit )
|
||||
{
|
||||
Lock();
|
||||
|
||||
CheckWheterIPListIsSorted();
|
||||
CheckSession(i);
|
||||
exit = synchro->was_stop_signal;
|
||||
|
||||
Unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// objects locked
|
||||
void SessionManager::CheckWheterIPListIsSorted()
|
||||
{
|
||||
if( !ban_tab.IsSorted() )
|
||||
{
|
||||
log << log4 << "SM: sorting the ban list" << logend;
|
||||
ban_tab.Sort();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// it's called from the other thread (with Lock and Unlock)
|
||||
void SessionManager::CheckSession(SessionContainer::Iterator & i)
|
||||
{
|
||||
const int deleted_max_at_once = 10;
|
||||
|
||||
if( i == session_tab.End() )
|
||||
{
|
||||
if( deleted > 0 )
|
||||
{
|
||||
deleted = 0;
|
||||
log << logsave;
|
||||
}
|
||||
|
||||
i = session_tab.Begin();
|
||||
WaitForSignalSleep(10);
|
||||
}
|
||||
else
|
||||
{
|
||||
if( i->remove_me || IsSessionOutdated(*i) )
|
||||
{
|
||||
Session * ses = &(*i);
|
||||
++i;
|
||||
DeleteSession(ses);
|
||||
++deleted;
|
||||
}
|
||||
else
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
if( deleted >= deleted_max_at_once )
|
||||
{
|
||||
log << logsave;
|
||||
WaitForSignalSleep(1);
|
||||
deleted = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// it's called from the other thread (with Lock and Unlock)
|
||||
bool SessionManager::IsSessionOutdated(const Session & s) const
|
||||
{
|
||||
bool outdated;
|
||||
|
||||
if( s.remember_me )
|
||||
{
|
||||
outdated = s.last_time < std::time(0) - config->session_remember_max_idle;
|
||||
}
|
||||
else
|
||||
{
|
||||
outdated = s.last_time < std::time(0) - config->session_max_idle;
|
||||
}
|
||||
|
||||
return outdated;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// it's called from the other thread (with Lock and Unlock)
|
||||
void SessionManager::DeleteSession(Session * del_session)
|
||||
{
|
||||
if( del_session->puser )
|
||||
{
|
||||
plugin.Call(del_session, WINIX_PREPARE_USER_TO_LOGOUT, del_session->puser);
|
||||
last_container->UserLogout(del_session->puser->id, del_session->id);
|
||||
del_session->puser = 0;
|
||||
}
|
||||
|
||||
long id = del_session->id;
|
||||
|
||||
plugin.Call(del_session, WINIX_PREPARE_SESSION_TO_REMOVE);
|
||||
session_tab.EraseById(del_session->id);
|
||||
plugin.Call((Session*)0, WINIX_SESSION_REMOVED, id);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
* end of sessions gc
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
155
winixd/core/sessionmanager.h
Normal file
155
winixd/core/sessionmanager.h
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_sessionmanager
|
||||
#define headerfile_winix_core_sessionmanager
|
||||
|
||||
#include <set>
|
||||
#include <ctime>
|
||||
|
||||
#include "sessioncontainer.h"
|
||||
#include "ipbancontainer.h"
|
||||
#include "config.h"
|
||||
#include "request.h"
|
||||
#include "lastcontainer.h"
|
||||
#include "system.h"
|
||||
#include "synchro.h"
|
||||
#include "basethread.h"
|
||||
#include "sessionidmanager.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class SessionManager : public BaseThread
|
||||
{
|
||||
public:
|
||||
|
||||
SessionManager();
|
||||
|
||||
void SetCur(Cur * pcur);
|
||||
void SetConfig(Config * pconfig);
|
||||
void SetSystem(System * psystem);
|
||||
void SetLastContainer(LastContainer * plast_container);
|
||||
|
||||
|
||||
// can return a null pointer
|
||||
Session * FindSession(long id);
|
||||
|
||||
void SetSession();
|
||||
void DeleteSessions(); // deleting all sessions
|
||||
bool ChangeSessionId(long old_id);
|
||||
|
||||
void IncrementBanLevel(IPBan * ip_ban);
|
||||
|
||||
void InitTmpSession();
|
||||
void InitBanList();
|
||||
void InitCookieEncoding();
|
||||
|
||||
void UninitTmpSession();
|
||||
|
||||
Session * GetTmpSession();
|
||||
Session * GetCurSession();
|
||||
|
||||
void LoadSessions();
|
||||
void SaveSessions();
|
||||
|
||||
SessionContainer::Iterator SessionBegin();
|
||||
SessionContainer::Iterator SessionEnd();
|
||||
|
||||
size_t Size();
|
||||
size_t MarkAllSessionsToRemove(long user_id);
|
||||
|
||||
IPBan & AddIPToBanList(int ip);
|
||||
IPBan & AddIPToBanList(int ip, time_t last_used);
|
||||
size_t BanListSize();
|
||||
IPBan & GetIPBan(size_t index);
|
||||
void RemoveIPBan(int ip);
|
||||
void ClearIPBanList();
|
||||
|
||||
|
||||
bool EncodeSessionId(long id, unsigned int index, std::wstring & str);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Config * config;
|
||||
Cur * cur;
|
||||
System * system;
|
||||
LastContainer * last_container;
|
||||
|
||||
Session * session;
|
||||
SessionContainer session_tab;
|
||||
IPBanContainer ban_tab;
|
||||
IPBan * current_ip_ban;
|
||||
bool is_session_set;
|
||||
Session temporary_session;
|
||||
SessionIdManager session_id_manager;
|
||||
|
||||
bool IsSession(long s);
|
||||
long CreateSessionId();
|
||||
void CreateSession();
|
||||
bool IsSessionCorrect(long id, bool has_index, unsigned int index, const SessionContainer::Iterator & s, unsigned int & difference);
|
||||
bool SetSessionFromCookie(long id, bool has_index, unsigned int index);
|
||||
bool SetSessionFromCookie(const std::wstring & cookie);
|
||||
void SetTemporarySession();
|
||||
unsigned int CalculateIndexDifference(Session & ses, unsigned int index);
|
||||
void SetSessionPutLogInfo(Session & ses, bool has_index, unsigned int difference);
|
||||
bool IsIPBanned();
|
||||
void SetFirstExpirationTime(IPBan * ip_ban);
|
||||
void BrokenCookieCheckBan();
|
||||
void IncorrectSessionCheckBan();
|
||||
void NoSessionCookieCheckBan();
|
||||
|
||||
|
||||
/*
|
||||
* second thread
|
||||
*/
|
||||
int deleted;
|
||||
virtual void Work();
|
||||
void CheckSession(SessionContainer::Iterator & i);
|
||||
bool IsSessionOutdated(const Session & s) const;
|
||||
void DeleteSession(Session * del_session);
|
||||
void CheckWheterIPListIsSorted();
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
208
winixd/core/sessionparser.cpp
Normal file
208
winixd/core/sessionparser.cpp
Normal file
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "sessionparser.h"
|
||||
#include "log.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
bool SessionParser::Parse(const std::wstring & path, SessionContainer & container)
|
||||
{
|
||||
return Parse(path.c_str(), container);
|
||||
}
|
||||
|
||||
|
||||
void SessionParser::SetUsers(Users * pusers)
|
||||
{
|
||||
users = pusers;
|
||||
}
|
||||
|
||||
|
||||
bool SessionParser::Parse(const wchar_t * path, SessionContainer & container)
|
||||
{
|
||||
char file_path[WINIX_OS_PATH_SIZE];
|
||||
|
||||
container.Clear();
|
||||
|
||||
if( !WideToUTF8(path, file_path, WINIX_OS_PATH_SIZE) )
|
||||
return false;
|
||||
|
||||
file.open(file_path, std::ios_base::in | std::ios_base::binary);
|
||||
|
||||
if( !file )
|
||||
{
|
||||
log << log1 << "SP: cannot open the session file for reading (file: '" << path << "')" << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool res = Parse(container);
|
||||
|
||||
file.close();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool SessionParser::Parse(SessionContainer & container)
|
||||
{
|
||||
/*
|
||||
file format, each rows:
|
||||
session_id(long) user_id(long) remember_me(0|1) time(long) last_time(long)
|
||||
*/
|
||||
last = file.get();
|
||||
|
||||
log << log2 << "SP: reading sessions from the session file" << logend;
|
||||
|
||||
while( true )
|
||||
{
|
||||
long id = ReadLong();
|
||||
|
||||
if( file.eof() )
|
||||
break;
|
||||
|
||||
long user_id = ReadLong();
|
||||
MakeSession(id, user_id, container);
|
||||
SkipLine();
|
||||
}
|
||||
|
||||
log << log2 << "SP: read " << container.Size() << " session(s)" << logend;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SessionParser::MakeSession(long id, long user_id, SessionContainer & container)
|
||||
{
|
||||
User * puser = users->GetUser(user_id);
|
||||
|
||||
if( !puser )
|
||||
{
|
||||
log << log1 << "SP: there is no a user with id: " << user_id << " (skipped)" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
SessionContainer::Iterator i = container.AddSession(id);
|
||||
|
||||
if( i != container.End() )
|
||||
{
|
||||
i->Clear();
|
||||
i->id = id;
|
||||
i->puser = puser;
|
||||
i->new_session = true;
|
||||
i->remember_me = ReadLong();
|
||||
i->start_time = ReadLong();
|
||||
i->last_time = ReadLong();
|
||||
i->id_index = ReadLong();
|
||||
i->start_date = i->start_time;
|
||||
i->last_date = i->last_time;
|
||||
// !! IMPROVE ME we do not save last_time_get
|
||||
|
||||
users->IncrementLoggedUsers();
|
||||
log << log2 << "SP: read session id: " << id << " for user: " << puser->name << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "SP: session with id: " << id << " already exists (skipping)" << logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool SessionParser::IsWhite(int c)
|
||||
{
|
||||
if( c==' ' || c=='\t' || c==13 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SessionParser::IsDigit(int c)
|
||||
{
|
||||
if( c>='0' && c<='9' )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void SessionParser::SkipWhite()
|
||||
{
|
||||
while( IsWhite(last) )
|
||||
last = file.get();
|
||||
}
|
||||
|
||||
|
||||
void SessionParser::SkipLine()
|
||||
{
|
||||
while( last != 10 )
|
||||
last = file.get();
|
||||
|
||||
last = file.get(); // first character from the new line
|
||||
}
|
||||
|
||||
|
||||
|
||||
long SessionParser::ReadLong()
|
||||
{
|
||||
long res = 0;
|
||||
bool is_sign = false;
|
||||
|
||||
SkipWhite();
|
||||
|
||||
if( last == '-' )
|
||||
{
|
||||
is_sign = true;
|
||||
last = file.get();
|
||||
}
|
||||
|
||||
while( IsDigit(last) )
|
||||
{
|
||||
res = res*10 + (last-'0');
|
||||
last = file.get();
|
||||
}
|
||||
|
||||
if( is_sign )
|
||||
res = -res;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
83
winixd/core/sessionparser.h
Normal file
83
winixd/core/sessionparser.h
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_sessionparser
|
||||
#define headerfile_winix_core_sessionparser
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "session.h"
|
||||
#include "sessioncontainer.h"
|
||||
#include "users.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class SessionParser
|
||||
{
|
||||
public:
|
||||
|
||||
bool Parse(const wchar_t * path, SessionContainer & container);
|
||||
bool Parse(const std::wstring & path, SessionContainer & container);
|
||||
void SetUsers(Users * pusers);
|
||||
|
||||
private:
|
||||
|
||||
Users * users;
|
||||
|
||||
bool Parse(SessionContainer & container);
|
||||
void MakeSession(long id, long user_id, SessionContainer & container);
|
||||
|
||||
bool IsWhite(int c);
|
||||
bool IsDigit(int c);
|
||||
void SkipWhite();
|
||||
void SkipLine();
|
||||
|
||||
long ReadLong();
|
||||
|
||||
std::ifstream file;
|
||||
int last; // last character
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
257
winixd/core/slog.cpp
Normal file
257
winixd/core/slog.cpp
Normal file
@@ -0,0 +1,257 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "slog.h"
|
||||
#include "utf8/utf8.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
SLog::SLog()
|
||||
{
|
||||
cur = 0;
|
||||
locale = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SLog::SetCur(Cur * pcur)
|
||||
{
|
||||
cur = pcur;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SLog::SetLocale(Locale * plocale)
|
||||
{
|
||||
locale = plocale;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
SLog & SLog::operator<<(const void * s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(const char * s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(const std::string * s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(const std::string & s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(const wchar_t * s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(const std::wstring * s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(const std::wstring & s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(int s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(long s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(char s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(wchar_t s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(size_t s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(double s)
|
||||
{
|
||||
return PutLog(s);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(const PT::Date & date)
|
||||
{
|
||||
return PutLog(date);
|
||||
}
|
||||
|
||||
|
||||
|
||||
SLog & SLog::operator<<(LogManipulators m)
|
||||
{
|
||||
if( cur && cur->session )
|
||||
{
|
||||
TextStream<std::wstring> & buf = cur->session->log_buffer;
|
||||
|
||||
switch(m)
|
||||
{
|
||||
case logend:
|
||||
buf << '\n';
|
||||
|
||||
if( buf.Size() > WINIX_SLOG_MAX_LOG_SIZE )
|
||||
{
|
||||
buf.Clear();
|
||||
(*this) << logwarning << T("slog_turn_over") << " " << WINIX_SLOG_MAX_LOG_SIZE << logend;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case loginfo:
|
||||
case logwarning:
|
||||
case logerror:
|
||||
buf << (wchar_t)(int)m;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
SLog & SLog::TranslateText(const char * str)
|
||||
{
|
||||
PT::UTF8ToWide(str, key_temp);
|
||||
return TranslateText(key_temp.c_str());
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::TranslateText(const wchar_t * str)
|
||||
{
|
||||
if( cur && cur->session )
|
||||
{
|
||||
const std::wstring * trans = 0;
|
||||
|
||||
if( locale )
|
||||
trans = &locale->Get(str);
|
||||
|
||||
// !! IMPROVE ME "Not translated" add to locale
|
||||
if( !trans || trans->empty() )
|
||||
cur->session->log_buffer << "Not translated: " << str;
|
||||
else
|
||||
cur->session->log_buffer << trans;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(const TranslateTextHelper<const char*> & raw)
|
||||
{
|
||||
return TranslateText(raw.par);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(const TranslateTextHelper<const wchar_t*> & raw)
|
||||
{
|
||||
return TranslateText(raw.par);
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(TranslateTextHelper<const std::string*> raw)
|
||||
{
|
||||
return TranslateText(raw.par->c_str());
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(TranslateTextHelper<const std::wstring*> raw)
|
||||
{
|
||||
return TranslateText(raw.par->c_str());
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(TranslateTextHelper<std::string> raw)
|
||||
{
|
||||
return TranslateText(raw.par.c_str());
|
||||
}
|
||||
|
||||
|
||||
SLog & SLog::operator<<(TranslateTextHelper<std::wstring> raw)
|
||||
{
|
||||
return TranslateText(raw.par.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
175
winixd/core/slog.h
Normal file
175
winixd/core/slog.h
Normal file
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_slog
|
||||
#define headerfile_winix_core_slog
|
||||
|
||||
#include "cur.h"
|
||||
#include "logmanipulators.h"
|
||||
#include "templates/locale.h"
|
||||
#include "textstream/textstream.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
#define WINIX_SLOG_MAX_LOG_SIZE 10240
|
||||
|
||||
|
||||
/*
|
||||
session logger
|
||||
|
||||
sample:
|
||||
#include "log.h" (or slog.h)
|
||||
slog << logerror << "message" << "something" << logend;
|
||||
slog << logwarning << T("message_to_translate") << x << logend;
|
||||
|
||||
if the latter example "message_to_translate" will be taken from locales
|
||||
currently following manipulators are taken into account:
|
||||
loginfo - the message in a normal info
|
||||
logwarning - this is a warning
|
||||
logerror - this is an error
|
||||
logend - end of a line -- we have one kind of a message (info, warning, error) per line
|
||||
|
||||
loginfo, logwarning, logerror should be specified at the beginning of a line
|
||||
(other manipulators are skipped)
|
||||
*/
|
||||
class SLog
|
||||
{
|
||||
public:
|
||||
|
||||
SLog();
|
||||
|
||||
void SetCur(Cur * pcur);
|
||||
void SetLocale(Locale * plocale);
|
||||
|
||||
template<class RawType>
|
||||
struct TranslateTextHelper
|
||||
{
|
||||
const RawType & par;
|
||||
|
||||
TranslateTextHelper(const TranslateTextHelper<RawType> & p) : par(p.par) {}
|
||||
TranslateTextHelper(const RawType & p) : par(p) {}
|
||||
};
|
||||
|
||||
SLog & operator<<(const void * s);
|
||||
SLog & operator<<(const char * s);
|
||||
SLog & operator<<(const std::string * s);
|
||||
SLog & operator<<(const std::string & s);
|
||||
SLog & operator<<(const wchar_t * s);
|
||||
SLog & operator<<(const std::wstring * s);
|
||||
SLog & operator<<(const std::wstring & s);
|
||||
SLog & operator<<(int s);
|
||||
SLog & operator<<(long s);
|
||||
SLog & operator<<(char s);
|
||||
SLog & operator<<(wchar_t s);
|
||||
SLog & operator<<(size_t s);
|
||||
SLog & operator<<(double s);
|
||||
SLog & operator<<(LogManipulators m);
|
||||
SLog & operator<<(const PT::Date & date);
|
||||
|
||||
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||
SLog & operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf);
|
||||
|
||||
|
||||
SLog & TranslateText(const char * str);
|
||||
SLog & TranslateText(const wchar_t * str);
|
||||
|
||||
template<size_t str_size>
|
||||
SLog & operator<<(const TranslateTextHelper<const char [str_size]> & raw) { return TranslateText(raw.par); }
|
||||
|
||||
template<size_t str_size>
|
||||
SLog & operator<<(const TranslateTextHelper<const wchar_t [str_size]> & raw){ return TranslateText(raw.par); }
|
||||
|
||||
template<size_t str_size>
|
||||
SLog & operator<<(const TranslateTextHelper<char [str_size]> & raw) { return TranslateText(raw.par); }
|
||||
|
||||
template<size_t str_size>
|
||||
SLog & operator<<(const TranslateTextHelper<wchar_t [str_size]> & raw){ return TranslateText(raw.par); }
|
||||
|
||||
SLog & operator<<(const TranslateTextHelper<const char*> & raw);
|
||||
SLog & operator<<(const TranslateTextHelper<const wchar_t*> & raw);
|
||||
SLog & operator<<(TranslateTextHelper<const std::string*> raw);
|
||||
SLog & operator<<(TranslateTextHelper<const std::wstring*> raw);
|
||||
SLog & operator<<(TranslateTextHelper<std::string> raw);
|
||||
SLog & operator<<(TranslateTextHelper<std::wstring> raw);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
template<class LogParam>
|
||||
SLog & PutLog(const LogParam & par);
|
||||
|
||||
Cur * cur;
|
||||
Locale * locale;
|
||||
std::wstring key_temp;
|
||||
|
||||
};
|
||||
|
||||
|
||||
template<class RawType>
|
||||
SLog::TranslateTextHelper<RawType> T(const RawType & par)
|
||||
{
|
||||
return SLog::TranslateTextHelper<RawType>(par);
|
||||
}
|
||||
|
||||
|
||||
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||
SLog & SLog::operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf)
|
||||
{
|
||||
return PutLog(buf);
|
||||
}
|
||||
|
||||
|
||||
template<class LogParam>
|
||||
SLog & SLog::PutLog(const LogParam & par)
|
||||
{
|
||||
if( cur && cur->session )
|
||||
cur->session->log_buffer << par;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
extern SLog slog;
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
107
winixd/core/synchro.cpp
Normal file
107
winixd/core/synchro.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include "synchro.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
Synchro::Synchro()
|
||||
{
|
||||
was_stop_signal = false;
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
/*
|
||||
* on FreeBSD a pthread's pthread_mutex_lock() is checking for deadlocks by default
|
||||
*/
|
||||
mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#else
|
||||
pthread_mutexattr_t attr;
|
||||
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
|
||||
pthread_mutex_init(&mutex, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Synchro::Lock()
|
||||
{
|
||||
int res = pthread_mutex_lock(&mutex);
|
||||
|
||||
if( res == 0 )
|
||||
{
|
||||
ref[pthread_self()] = 1;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
if( res == EDEADLK )
|
||||
{
|
||||
// Lock() method in this thread was called before
|
||||
ref[pthread_self()] += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Synchro::Unlock()
|
||||
{
|
||||
int & r = ref[pthread_self()];
|
||||
|
||||
if( r > 1 )
|
||||
{
|
||||
r -= 1;
|
||||
}
|
||||
else
|
||||
if( r == 1 )
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
76
winixd/core/synchro.h
Normal file
76
winixd/core/synchro.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_synchro
|
||||
#define headerfile_winix_core_synchro
|
||||
|
||||
#include <pthread.h>
|
||||
#include <map>
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
struct Synchro
|
||||
{
|
||||
// one global mutex
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
|
||||
// true when winix is closing
|
||||
volatile bool was_stop_signal;
|
||||
|
||||
|
||||
Synchro();
|
||||
|
||||
bool Lock();
|
||||
void Unlock();
|
||||
|
||||
private:
|
||||
|
||||
// deadlock counter for each thread
|
||||
// we can call Lock() more than one in the same thread
|
||||
std::map<pthread_t, int> ref;
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
1414
winixd/core/system.cpp
Normal file
1414
winixd/core/system.cpp
Normal file
File diff suppressed because it is too large
Load Diff
250
winixd/core/system.h
Normal file
250
winixd/core/system.h
Normal file
@@ -0,0 +1,250 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_system
|
||||
#define headerfile_winix_core_system
|
||||
|
||||
#include <ctime>
|
||||
#include "job.h"
|
||||
#include "dirs.h"
|
||||
#include "mounts.h"
|
||||
#include "db/db.h"
|
||||
#include "request.h"
|
||||
#include "config.h"
|
||||
#include "crypt.h"
|
||||
#include "users.h"
|
||||
#include "groups.h"
|
||||
#include "rebus.h"
|
||||
#include "loadavg.h"
|
||||
#include "synchro.h"
|
||||
#include "image.h"
|
||||
#include "threadmanager.h"
|
||||
#include "notify/notify.h"
|
||||
#include "timezones.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
class Functions;
|
||||
class SessionManager;
|
||||
|
||||
|
||||
// file system
|
||||
class System
|
||||
{
|
||||
public:
|
||||
|
||||
// contains current directories tree
|
||||
Dirs dirs;
|
||||
|
||||
// mount points
|
||||
Mounts mounts;
|
||||
|
||||
// users
|
||||
Users users;
|
||||
|
||||
// groups
|
||||
Groups groups;
|
||||
|
||||
// rebus (captcha)
|
||||
Rebus rebus;
|
||||
|
||||
// load averages
|
||||
LoadAvg load_avg;
|
||||
|
||||
// notifications (by emails)
|
||||
Notify notify;
|
||||
|
||||
// images (resizing, generating thumbnails)
|
||||
Image image;
|
||||
|
||||
// the time when the winix starts
|
||||
time_t system_start;
|
||||
|
||||
// cryptography and hashes
|
||||
Crypt crypt;
|
||||
|
||||
// thread management
|
||||
ThreadManager thread_manager;
|
||||
|
||||
// jobs
|
||||
Job job;
|
||||
|
||||
// time zones read from etc/time_zones.conf
|
||||
// when winix starts
|
||||
TimeZones time_zones;
|
||||
|
||||
|
||||
void SetCur(Cur * pcur);
|
||||
void SetConfig(Config * pconfig);
|
||||
void SetDb(Db * pdb);
|
||||
void SetSynchro(Synchro * psynchro);
|
||||
void SetFunctions(Functions * pfunctions);
|
||||
void SetSessionManager(SessionManager * sm);
|
||||
void Init();
|
||||
|
||||
void AddParams(const ParamTab & param_tab, std::wstring & str, bool clear_str = true);
|
||||
|
||||
void PutUrlProto(bool can_use_ssl, std::wstring & str, bool clear_str = true);
|
||||
void RedirectTo(const Item & item, const wchar_t * postfix = 0, bool use_reqtype = true);
|
||||
void RedirectTo(long item_id, const wchar_t * postfix = 0, bool use_reqtype = true);
|
||||
void RedirectTo(const wchar_t * url, bool use_reqtype = true);
|
||||
void RedirectTo(const std::wstring & url, bool use_reqtype = true);
|
||||
void RedirectWithFunctionAndParamsTo(const wchar_t * url);
|
||||
void RedirectWithFunctionAndParamsTo(const std::wstring & url);
|
||||
void RedirectToLastDir(const wchar_t * postfix = 0, bool use_reqtype = true);
|
||||
void RedirectToLastItem(const wchar_t * postfix = 0, bool use_reqtype = true); // redirect to an item if exists or to the last directory
|
||||
void RedirectToLastFunction(const wchar_t * postfix = 0, bool use_reqtype = true);
|
||||
|
||||
|
||||
bool CanChangeUser(const Item & item, long new_user_id);
|
||||
bool CanChangeGroup(const Item & item, long new_group_id);
|
||||
bool CanChangePrivileges(const Item & item, int new_priv);
|
||||
|
||||
|
||||
bool HasReadAccess(const Item & item);
|
||||
bool HasWriteAccess(const Item & item);
|
||||
bool HasReadWriteAccess(const Item & item);
|
||||
bool HasReadExecAccess(const Item & item);
|
||||
bool HasReadExecAccessToPath(long dir_id);
|
||||
bool HasReadExecAccessToPath(const std::vector<Item*> & dir_tab);
|
||||
bool DirsHaveReadExecPerm();
|
||||
void CheckAccessToItems(std::vector<Item> & item_tab);
|
||||
void CheckWriteAccessToItems(std::vector<Item> & item_tab);
|
||||
|
||||
/*
|
||||
this method checks the sticky bit and write permissions
|
||||
it returns true if we can remove/rename an item for the given child_item_user_id user id
|
||||
*/
|
||||
bool CanRemoveRenameChild(const Item & dir, long child_item_user_id);
|
||||
|
||||
int NewFilePrivileges();
|
||||
int NewDirPrivileges();
|
||||
|
||||
|
||||
bool CanUseHtml(long user_id);
|
||||
bool CanUseBBCode(long user_id);
|
||||
bool CanUseRaw(long user_id);
|
||||
|
||||
bool IsSuperUser(long user_id);
|
||||
bool IsMemberOfGroup(long user_id, const wchar_t * group_name);
|
||||
|
||||
// creating item.file_path and item.file_fs (the mountpoint where the item is located)
|
||||
bool CreateNewFile(Item & item);
|
||||
bool MakeFilePath(const Item & item, std::wstring & path, bool thumb = false, bool create_dir = false, int chmod = 0755, int group = -1);
|
||||
|
||||
bool MakePath(const Item & item, std::wstring & path, bool clear_path = true);
|
||||
|
||||
Error AddFile(Item & item, int notify_code = 0, bool call_plugins = true);
|
||||
Error EditFile(Item & item, bool with_url = true, int notify_code = 0, bool call_plugins = true);
|
||||
|
||||
|
||||
time_t ToLocal(time_t utc_time);
|
||||
PT::Date ToLocal(const PT::Date & utc_date);
|
||||
|
||||
time_t ToUTC(time_t local_time);
|
||||
PT::Date ToUTC(const PT::Date & local_date);
|
||||
|
||||
|
||||
int FollowLink(const std::vector<Item*> & current_dir_tab, const std::wstring & link_to,
|
||||
std::vector<Item*> & out_dir_tab, Item & out_item);
|
||||
|
||||
int FollowAllLinks(const std::vector<Item*> & current_dir_tab, const std::wstring & link_to,
|
||||
std::vector<Item*> & out_dir_tab, Item & out_item,
|
||||
bool follow_dir_default = false, bool stop_on_link_redirect = false, bool check_access = true);
|
||||
|
||||
// starting from root dir
|
||||
int FollowAllLinks(const std::wstring & link_to,
|
||||
std::vector<Item*> & out_dir_tab, Item & out_item,
|
||||
bool follow_dir_default = false, bool stop_on_link_redirect = false, bool check_access = true);
|
||||
|
||||
// using cur->request->dir_tab and cur->request->item
|
||||
bool FollowAllLinks(const std::wstring & link_to,
|
||||
bool follow_dir_default = false, bool stop_on_link_redirect = false, bool check_access = true);
|
||||
|
||||
|
||||
bool AddCommonFileToVar(const wchar_t * file_path, const wchar_t * url, bool overwrite_existing = true);
|
||||
|
||||
// reloading time zones
|
||||
void ReadTimeZones();
|
||||
|
||||
|
||||
void CreateItemLink(long parent_id, const std::wstring & url, const std::wstring & subdomain,
|
||||
std::wstring & link, bool clear_str = true);
|
||||
|
||||
void CreateItemLink(const Item & item, std::wstring & link, bool clear_str = true);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Cur * cur;
|
||||
Db * db;
|
||||
Config * config;
|
||||
Synchro * synchro;
|
||||
Functions * functions;
|
||||
SessionManager * session_manager;
|
||||
|
||||
Item item_temp;
|
||||
std::wstring link_to_temp, name_temp;
|
||||
std::wstring file_content, file_name;
|
||||
Item file_content_item;
|
||||
|
||||
// for FollowAllLinks
|
||||
std::vector<Item*> temp_follow_dir_tab;
|
||||
std::vector<Item*> root_follow_dir_tab;
|
||||
Item temp_follow_item;
|
||||
|
||||
bool HasAccess(const Item & item, int mask);
|
||||
int NewPrivileges(int creation_mask);
|
||||
|
||||
bool CreateNewFileSimpleFs(Item & item);
|
||||
bool CreateNewFileHashFs(Item & item);
|
||||
|
||||
bool FollowAllLinksDirFound(std::vector<Item*> & out_dir_tab,
|
||||
bool follow_dir_default, bool stop_on_link_redirect, bool check_access);
|
||||
|
||||
bool FollowAllLinksFileOrSymlinkFound(std::vector<Item*> & out_dir_tab, Item & out_item,
|
||||
bool stop_on_link_redirect, bool check_access);
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
556
winixd/core/textstream.h
Normal file
556
winixd/core/textstream.h
Normal file
@@ -0,0 +1,556 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_textstream
|
||||
#define headerfile_winix_core_textstream
|
||||
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
#include "misc.h"
|
||||
#include "space/space.h"
|
||||
#include "date/date.h"
|
||||
#include "textstream/textstream.h"
|
||||
#include "utf8/utf8.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
a special class representing a stream buffer
|
||||
similar to std::ostringstream
|
||||
|
||||
StringType can be either std::string or std::wstring
|
||||
|
||||
this class uses UTF-8 <-> wide characters conversions:
|
||||
if StringType is std::string:
|
||||
operator<<(const char*) only copies the input string
|
||||
operator<<(const wchar_t*) converts from wide characters to UTF-8
|
||||
(similary for an operator with std::string and std::wstring)
|
||||
if StringType is std::wstring:
|
||||
operator<<(const char*) converts from UTF-8 to wide characters
|
||||
operator<<(const wchar_t*) only copies the input string
|
||||
(similary for an operator with std::string and std::wstring)
|
||||
|
||||
*/
|
||||
template<class StringType>
|
||||
class TextStream
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename StringType::value_type CharType;
|
||||
typedef typename StringType::value_type char_type;
|
||||
|
||||
void Clear();
|
||||
bool Empty() const;
|
||||
size_t Size() const;
|
||||
void Reserve(size_t len);
|
||||
|
||||
const StringType & Str() const;
|
||||
const CharType * CStr() const;
|
||||
|
||||
void Str(const StringType & str);
|
||||
void Str(const StringType && str);
|
||||
|
||||
CharType operator[](size_t index);
|
||||
|
||||
TextStream & operator<<(const char * str);
|
||||
TextStream & operator<<(const std::string * str);
|
||||
TextStream & operator<<(const std::string & str);
|
||||
|
||||
TextStream & operator<<(const wchar_t * str);
|
||||
TextStream & operator<<(const std::wstring * str);
|
||||
TextStream & operator<<(const std::wstring & str);
|
||||
|
||||
TextStream & operator<<(char);
|
||||
TextStream & operator<<(wchar_t);
|
||||
TextStream & operator<<(int);
|
||||
TextStream & operator<<(long);
|
||||
TextStream & operator<<(unsigned int);
|
||||
TextStream & operator<<(unsigned long);
|
||||
TextStream & operator<<(double);
|
||||
TextStream & operator<<(const void *);// printing a pointer
|
||||
TextStream & operator<<(const PT::Space & space);
|
||||
TextStream & operator<<(const PT::Date & date);
|
||||
|
||||
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
||||
TextStream & operator<<(const PT::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg);
|
||||
|
||||
TextStream & Write(const char * buf, size_t len);
|
||||
TextStream & Write(const wchar_t * buf, size_t len);
|
||||
TextStream & write(const char * buf, size_t len); // for compatibility with standard library (Ezc uses it)
|
||||
TextStream & write(const wchar_t * buf, size_t len);
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
StringType buffer;
|
||||
|
||||
void Convert(wchar_t c, std::string & dst);
|
||||
void Convert(wchar_t c, std::wstring & dst);
|
||||
|
||||
void Convert(const char * src, size_t len, std::wstring & dst);
|
||||
void Convert(const char * src, std::wstring & dst);
|
||||
void Convert(const std::string & src, std::wstring & dst);
|
||||
|
||||
void Convert(const wchar_t * src, size_t len, std::string & dst);
|
||||
void Convert(const wchar_t * src, std::string & dst);
|
||||
void Convert(const std::wstring & src, std::string & dst);
|
||||
|
||||
void Convert(const char * src, size_t len, std::string & dst);
|
||||
void Convert(const char * src, std::string & dst);
|
||||
void Convert(const std::string & src, std::string & dst);
|
||||
|
||||
void Convert(const wchar_t * src, size_t len, std::wstring & dst);
|
||||
void Convert(const wchar_t * src, std::wstring & dst);
|
||||
void Convert(const std::wstring & src, std::wstring & dst);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Clear()
|
||||
{
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
template<class StringType>
|
||||
bool TextStream<StringType>::Empty() const
|
||||
{
|
||||
return buffer.empty();
|
||||
}
|
||||
|
||||
template<class StringType>
|
||||
size_t TextStream<StringType>::Size() const
|
||||
{
|
||||
return buffer.size();
|
||||
}
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Reserve(size_t len)
|
||||
{
|
||||
buffer.reserve(len);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
const StringType & TextStream<StringType>::Str() const
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
const typename TextStream<StringType>::CharType * TextStream<StringType>::CStr() const
|
||||
{
|
||||
return buffer.c_str();
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Str(const StringType & str)
|
||||
{
|
||||
buffer = str;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Str(const StringType && str)
|
||||
{
|
||||
buffer = str;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
typename TextStream<StringType>::CharType TextStream<StringType>::operator[](size_t index)
|
||||
{
|
||||
return buffer[index];
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(const char * str)
|
||||
{
|
||||
Convert(str, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(const std::string * str)
|
||||
{
|
||||
Convert(*str, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(const std::string & str)
|
||||
{
|
||||
Convert(str, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(const wchar_t * str)
|
||||
{
|
||||
Convert(str, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(const std::wstring * str)
|
||||
{
|
||||
Convert(*str, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(const std::wstring & str)
|
||||
{
|
||||
Convert(str, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(char v)
|
||||
{
|
||||
/*
|
||||
* there is no any possibility to treat 'v' as UTF-8 character if we have got
|
||||
* only one character so we only copy it
|
||||
*/
|
||||
buffer += v;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(wchar_t v)
|
||||
{
|
||||
Convert(v, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(int v)
|
||||
{
|
||||
wchar_t buf[50];
|
||||
size_t len = sizeof(buf) / sizeof(wchar_t);
|
||||
|
||||
Toa(v, buf, len);
|
||||
Convert(buf, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(long v)
|
||||
{
|
||||
wchar_t buf[50];
|
||||
size_t len = sizeof(buf) / sizeof(wchar_t);
|
||||
|
||||
Toa(v, buf, len);
|
||||
Convert(buf, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(unsigned int v)
|
||||
{
|
||||
wchar_t buf[50];
|
||||
size_t len = sizeof(buf) / sizeof(wchar_t);
|
||||
|
||||
Toa(v, buf, len);
|
||||
Convert(buf, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(unsigned long v)
|
||||
{
|
||||
wchar_t buf[50];
|
||||
size_t len = sizeof(buf) / sizeof(wchar_t);
|
||||
|
||||
Toa(v, buf, len);
|
||||
Convert(buf, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(double v)
|
||||
{
|
||||
char buf[50];
|
||||
|
||||
sprintf(buf, "%f", v);
|
||||
Convert(buf, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(const void * v)
|
||||
{
|
||||
wchar_t buf[50];
|
||||
size_t len = sizeof(buf) / sizeof(wchar_t);
|
||||
|
||||
buf[0] = '0';
|
||||
buf[1] = 'x';
|
||||
|
||||
Toa(reinterpret_cast<unsigned long>(v), buf+2, len-2, 16);
|
||||
Convert(buf, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::Write(const char * buf, size_t len)
|
||||
{
|
||||
Convert(buf, len, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::write(const char * buf, size_t len)
|
||||
{
|
||||
return Write(buf, len);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::Write(const wchar_t * buf, size_t len)
|
||||
{
|
||||
Convert(buf, len, buffer);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::write(const wchar_t * buf, size_t len)
|
||||
{
|
||||
return Write(buf, len);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(const PT::Space & space)
|
||||
{
|
||||
space.Serialize(*this, true, false);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(const PT::Date & date)
|
||||
{
|
||||
date.Serialize(*this);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(
|
||||
const PT::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg)
|
||||
{
|
||||
typename PT::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size>::const_iterator i;
|
||||
|
||||
for(i=arg.begin() ; i != arg.end() ; ++i)
|
||||
buffer += static_cast<char_type>(*i);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(wchar_t c, std::string & dst)
|
||||
{
|
||||
PT::IntToUTF8((int)c, dst, false);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(wchar_t c, std::wstring & dst)
|
||||
{
|
||||
dst += c;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const char * src, size_t len, std::wstring & dst)
|
||||
{
|
||||
PT::UTF8ToWide(src, len, dst, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const char * src, std::wstring & dst)
|
||||
{
|
||||
PT::UTF8ToWide(src, dst, false);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const std::string & src, std::wstring & dst)
|
||||
{
|
||||
PT::UTF8ToWide(src, dst, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const wchar_t * src, size_t len, std::string & dst)
|
||||
{
|
||||
PT::WideToUTF8(src, len, dst, false);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const wchar_t * src, std::string & dst)
|
||||
{
|
||||
PT::WideToUTF8(src, dst, false);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const std::wstring & src, std::string & dst)
|
||||
{
|
||||
PT::WideToUTF8(src, dst, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const char * src, size_t len, std::string & dst)
|
||||
{
|
||||
// we suppose that append is smart enough and we don't have to use reserve()
|
||||
dst.append(src, len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const char * src, std::string & dst)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
for(len=0 ; src[len] ; ++len){}
|
||||
|
||||
Convert(src, len, dst);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const std::string & src, std::string & dst)
|
||||
{
|
||||
dst.append(src);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const wchar_t * src, size_t len, std::wstring & dst)
|
||||
{
|
||||
// we suppose that append is smart enough and we don't have to use reserve()
|
||||
dst.append(src, len);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const wchar_t * src, std::wstring & dst)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
for(len=0 ; src[len] ; ++len){}
|
||||
|
||||
Convert(src, len, dst);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const std::wstring & src, std::wstring & dst)
|
||||
{
|
||||
dst.append(src);
|
||||
}
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
170
winixd/core/threadmanager.cpp
Normal file
170
winixd/core/threadmanager.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include "threadmanager.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
ThreadManager::ThreadManager()
|
||||
{
|
||||
were_started = false;
|
||||
}
|
||||
|
||||
|
||||
void ThreadManager::SetSynchro(Synchro * psynchro)
|
||||
{
|
||||
synchro = psynchro;
|
||||
}
|
||||
|
||||
|
||||
void ThreadManager::Init()
|
||||
{
|
||||
sigset_t set;
|
||||
|
||||
sigemptyset(&set);
|
||||
sigaddset(&set, SIGTERM);
|
||||
sigaddset(&set, SIGINT);
|
||||
|
||||
// blocking SIGTERM and SIGINT
|
||||
// new threads will have the signals blocked too
|
||||
pthread_sigmask(SIG_BLOCK, &set, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void ThreadManager::Add(BaseThread * pbase, const wchar_t * thread_name)
|
||||
{
|
||||
ThreadItem item;
|
||||
|
||||
item.object = pbase;
|
||||
item.name = thread_name;
|
||||
thread_tab.push_back(item);
|
||||
|
||||
if( were_started )
|
||||
Start(thread_tab.size() - 1);
|
||||
else
|
||||
log << log4 << "TM: added a thread to the queue, number: " << (thread_tab.size()-1)
|
||||
<< ", name: " << thread_name << logend;
|
||||
}
|
||||
|
||||
|
||||
void ThreadManager::Add(BaseThread & pbase, const wchar_t * thread_name)
|
||||
{
|
||||
Add(&pbase, thread_name);
|
||||
}
|
||||
|
||||
|
||||
void ThreadManager::Add(BaseThread * pbase, const std::wstring & thread_name)
|
||||
{
|
||||
Add(pbase, thread_name.c_str());
|
||||
}
|
||||
|
||||
void ThreadManager::Add(BaseThread & pbase, const std::wstring & thread_name)
|
||||
{
|
||||
Add(&pbase, thread_name.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ThreadManager::StartAll()
|
||||
{
|
||||
synchro->Lock();
|
||||
|
||||
for(size_t i=0 ; i<thread_tab.size() ; ++i)
|
||||
Start(i);
|
||||
|
||||
synchro->Unlock();
|
||||
|
||||
were_started = true;
|
||||
}
|
||||
|
||||
|
||||
void ThreadManager::Start(size_t i)
|
||||
{
|
||||
if( i < thread_tab.size() )
|
||||
{
|
||||
thread_tab[i].object->SetSynchro(synchro);
|
||||
|
||||
if( thread_tab[i].object->StartThread() )
|
||||
{
|
||||
log << log4 << "TM: thread " << i << " (" << thread_tab[i].object->ThreadId() << ", name: "
|
||||
<< thread_tab[i].name << ") started" << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log4 << "TM: cannot run a thread, thread number: " << i
|
||||
<< ", name: " << thread_tab[i].name << logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ThreadManager::StopAll()
|
||||
{
|
||||
if( !were_started )
|
||||
return;
|
||||
|
||||
// WakeUpThread() should be used with Lock/Unlock
|
||||
synchro->Lock();
|
||||
|
||||
for(size_t i=0 ; i<thread_tab.size() ; ++i)
|
||||
thread_tab[i].object->WakeUpThread();
|
||||
|
||||
synchro->Unlock();
|
||||
|
||||
|
||||
for(size_t i=0 ; i<thread_tab.size() ; ++i)
|
||||
{
|
||||
log << log4 << "TM: waiting for thread " << i << " (" << thread_tab[i].object->ThreadId()
|
||||
<< ", name: " << thread_tab[i].name << ")" << logend;
|
||||
|
||||
thread_tab[i].object->WaitForThread();
|
||||
|
||||
log << log4 << "TM: thread " << i << " terminated" << logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
99
winixd/core/threadmanager.h
Normal file
99
winixd/core/threadmanager.h
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_threadmanager
|
||||
#define headerfile_winix_core_threadmanager
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "basethread.h"
|
||||
#include "synchro.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
class ThreadManager
|
||||
{
|
||||
public:
|
||||
|
||||
ThreadManager();
|
||||
|
||||
// synchro object
|
||||
void SetSynchro(Synchro * psynchro);
|
||||
|
||||
// initializing
|
||||
void Init();
|
||||
|
||||
// adding a new thread to the queue
|
||||
// the thread will be running only if we call StartAll() before
|
||||
// otherwise the thread will be waiting for StartAll()
|
||||
void Add(BaseThread * pbase, const wchar_t * thread_name);
|
||||
void Add(BaseThread & pbase, const wchar_t * thread_name);
|
||||
void Add(BaseThread * pbase, const std::wstring & thread_name);
|
||||
void Add(BaseThread & pbase, const std::wstring & thread_name);
|
||||
|
||||
// starting all threads
|
||||
void StartAll();
|
||||
|
||||
// sending a stop signal to all threads
|
||||
// and waiting until they finish
|
||||
void StopAll();
|
||||
|
||||
private:
|
||||
|
||||
struct ThreadItem
|
||||
{
|
||||
BaseThread * object;
|
||||
std::wstring name;
|
||||
};
|
||||
|
||||
Synchro * synchro;
|
||||
typedef std::vector<ThreadItem> ThreadTab;
|
||||
ThreadTab thread_tab;
|
||||
bool were_started;
|
||||
|
||||
void Start(size_t i);
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
322
winixd/core/timezone.cpp
Normal file
322
winixd/core/timezone.cpp
Normal file
@@ -0,0 +1,322 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "timezone.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
TimeZone::Dst::Dst()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
void TimeZone::Dst::Clear()
|
||||
{
|
||||
has_dst = false;
|
||||
start.Clear();
|
||||
end.Clear();
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
|
||||
bool TimeZone::Dst::IsDstUsed(const PT::Date & date) const
|
||||
{
|
||||
if( !has_dst )
|
||||
return false;
|
||||
|
||||
if( Compare(start, date) <= 0 ) // !! CHECK ME <= or < ? (what about the one second?)
|
||||
if( Compare(date, end) < 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
int TimeZone::Dst::Compare(const PT::Date & date1, const PT::Date & date2) const
|
||||
{
|
||||
// year is ignored
|
||||
|
||||
if( date1.month != date2.month )
|
||||
return date1.month - date2.month;
|
||||
|
||||
if( date1.day != date2.day )
|
||||
return date1.day - date2.day;
|
||||
|
||||
if( date1.hour != date2.hour )
|
||||
return date1.hour - date2.hour;
|
||||
|
||||
if( date1.min != date2.min )
|
||||
return date1.min - date2.min;
|
||||
|
||||
if( date1.sec != date2.sec )
|
||||
return date1.sec - date2.sec;
|
||||
|
||||
// dates are equal
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
TimeZone::TimeZone()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void TimeZone::Clear()
|
||||
{
|
||||
name.clear();
|
||||
id = 0;
|
||||
offset = 0;
|
||||
dst_map.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
TimeZone::Dst * TimeZone::FindDst(int year)
|
||||
{
|
||||
if( dst_map.empty() )
|
||||
return 0;
|
||||
|
||||
DstMap::iterator i = dst_map.lower_bound(year);
|
||||
|
||||
if( i == dst_map.begin() && i->first > year )
|
||||
return 0;
|
||||
|
||||
if( i == dst_map.end() )
|
||||
return &(--i)->second;
|
||||
|
||||
if( i != dst_map.begin() && i->first > year )
|
||||
return &(--i)->second;
|
||||
|
||||
return &i->second;
|
||||
}
|
||||
|
||||
|
||||
|
||||
time_t TimeZone::CalcLocalOffset(const PT::Date & utc_date)
|
||||
{
|
||||
time_t dst_offset = 0;
|
||||
|
||||
Dst * dst = FindDst(utc_date.year);
|
||||
|
||||
if( dst && dst->IsDstUsed(utc_date) )
|
||||
dst_offset = dst->offset;
|
||||
|
||||
return offset + dst_offset;
|
||||
}
|
||||
|
||||
|
||||
|
||||
time_t TimeZone::ToLocal(time_t utc_time)
|
||||
{
|
||||
time_t offset = CalcLocalOffset(PT::Date(utc_time));
|
||||
|
||||
return utc_time + offset;
|
||||
}
|
||||
|
||||
|
||||
PT::Date TimeZone::ToLocal(const PT::Date & utc_date)
|
||||
{
|
||||
PT::Date local(utc_date);
|
||||
local += CalcLocalOffset(utc_date);
|
||||
|
||||
return local;
|
||||
}
|
||||
|
||||
|
||||
time_t TimeZone::CalcUTCOffset(const PT::Date & local_date)
|
||||
{
|
||||
time_t dst_offset = 0;
|
||||
|
||||
Dst * dst = FindDst(local_date.year);
|
||||
|
||||
if( dst && dst->has_dst )
|
||||
{
|
||||
// dst date ranges we have in UTC
|
||||
PT::Date utc(local_date);
|
||||
utc -= (offset + dst->offset);
|
||||
|
||||
if( dst->IsDstUsed(utc) )
|
||||
dst_offset = dst->offset;
|
||||
}
|
||||
|
||||
return offset + dst_offset;
|
||||
}
|
||||
|
||||
|
||||
|
||||
time_t TimeZone::ToUTC(time_t local_time)
|
||||
{
|
||||
time_t offset = CalcUTCOffset(PT::Date(local_time));
|
||||
|
||||
return local_time - offset;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PT::Date TimeZone::ToUTC(const PT::Date & local_date)
|
||||
{
|
||||
time_t offset;
|
||||
PT::Date utc(local_date);
|
||||
|
||||
offset = CalcUTCOffset(local_date);
|
||||
utc -= offset;
|
||||
|
||||
return utc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
time_t TimeZone::ParseStrOffset(const wchar_t * str)
|
||||
{
|
||||
PT::Date date;
|
||||
bool is_sign = false;
|
||||
time_t offset = 0;
|
||||
|
||||
str = SkipWhite(str);
|
||||
|
||||
if( *str == '-' )
|
||||
{
|
||||
is_sign = true;
|
||||
str += 1;
|
||||
}
|
||||
else
|
||||
if( *str == '+' )
|
||||
{
|
||||
str += 1;
|
||||
}
|
||||
|
||||
if( date.ParseTime(str) )
|
||||
{
|
||||
offset = date.hour * 60 * 60 + date.min * 60 + date.sec;
|
||||
|
||||
if( is_sign )
|
||||
offset = -offset;
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
|
||||
time_t TimeZone::GetOffset(PT::Space & space)
|
||||
{
|
||||
std::wstring * offset_str = space.GetValue(L"offset_str");
|
||||
|
||||
if( offset_str )
|
||||
return ParseStrOffset(offset_str->c_str());
|
||||
|
||||
return space.Long(L"offset");
|
||||
}
|
||||
|
||||
|
||||
bool TimeZone::SetTzDst(PT::Space & year)
|
||||
{
|
||||
time_t h24 = 60 * 60 * 24; // 24 hours
|
||||
bool result = true;
|
||||
Dst dst;
|
||||
|
||||
int year_int = Toi(year.name);
|
||||
|
||||
if( year_int < 1970 && year_int > 10000 )
|
||||
return false;
|
||||
|
||||
dst.has_dst = year.Bool(L"has_dst", false);
|
||||
|
||||
if( dst.has_dst )
|
||||
{
|
||||
dst.start.year = year_int;
|
||||
dst.end.year = year_int;
|
||||
|
||||
if( !dst.start.ParseMonthDayTime(year.Text(L"start")) )
|
||||
result = false;
|
||||
|
||||
if( !dst.end.ParseMonthDayTime(year.Text(L"end")) )
|
||||
result = false;
|
||||
|
||||
dst.offset = GetOffset(year);
|
||||
|
||||
if( dst.offset < -h24 || dst.offset > h24 )
|
||||
result = false;
|
||||
}
|
||||
|
||||
if( result )
|
||||
dst_map[year_int] = dst;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
bool TimeZone::SetTz(PT::Space & space)
|
||||
{
|
||||
bool result = true;
|
||||
name = space.name;
|
||||
id = space.Int(L"id", -1);
|
||||
offset = GetOffset(space);
|
||||
time_t h24 = 60 * 60 * 24; // 24 hours
|
||||
|
||||
if( offset < -h24 || offset > h24 )
|
||||
result = false;
|
||||
|
||||
PT::Space & dst = space.FindAddSpace(L"dst");
|
||||
|
||||
for(size_t i=0 ; i<dst.spaces.size() ; ++i)
|
||||
{
|
||||
PT::Space & year = *dst.spaces[i];
|
||||
|
||||
if( !SetTzDst(year) )
|
||||
{
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
170
winixd/core/timezone.h
Normal file
170
winixd/core/timezone.h
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_timezone
|
||||
#define headerfile_winix_core_timezone
|
||||
|
||||
#include <ctime>
|
||||
#include <map>
|
||||
#include "date/date.h"
|
||||
#include "space/space.h"
|
||||
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
class TimeZone
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
struct Dst
|
||||
{
|
||||
// true if a time zone has daylight saving time
|
||||
bool has_dst;
|
||||
|
||||
// time zone daylight saving time (used if has_dst is true)
|
||||
// the 'year' field is the same in 'start' and 'end'
|
||||
// start and end are represented in UTC time
|
||||
PT::Date start, end;
|
||||
|
||||
// time zone daylight saving time offset
|
||||
// used when has_dst is true and the date is whithin start and end
|
||||
// this offset should be added to time zone offset
|
||||
time_t offset;
|
||||
|
||||
Dst();
|
||||
void Clear();
|
||||
|
||||
// checking whether specified 'date' is in the range of <start, end>
|
||||
// the year field in date, start and end is ignored
|
||||
// has_dst must be true
|
||||
bool IsDstUsed(const PT::Date & date) const;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
// Compare returns zero if date1 and date2 are equal
|
||||
// return value less than zero if date1 is lower than date2
|
||||
// and a value greater than zero if date1 is greater than date2
|
||||
// the year field is ignored
|
||||
int Compare(const PT::Date & date1, const PT::Date & date2) const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
TimeZone();
|
||||
|
||||
|
||||
/*
|
||||
*/
|
||||
void Clear();
|
||||
|
||||
|
||||
/*
|
||||
reading zime zone values from Space struct (tz_id is skipped)
|
||||
the space struct should have:
|
||||
"tz_offset" (long)
|
||||
"tz_has_dst" (bool)
|
||||
if tz_has_dst is true then also:
|
||||
"tz_dst_start" date in the following format: MM:DD HH[:MM[:SS]]
|
||||
"tz_dst_end" the same as above
|
||||
"tz_dst_offset" (long)
|
||||
*/
|
||||
bool SetTz(PT::Space & space);
|
||||
|
||||
|
||||
/*
|
||||
converting from UTC to local time
|
||||
*/
|
||||
time_t CalcLocalOffset(const PT::Date & utc_date);
|
||||
time_t ToLocal(time_t utc_time);
|
||||
PT::Date ToLocal(const PT::Date & utc_date);
|
||||
|
||||
|
||||
/*
|
||||
converting from local time to UTC
|
||||
*/
|
||||
time_t CalcUTCOffset(const PT::Date & local_date);
|
||||
time_t ToUTC(time_t local_time);
|
||||
PT::Date ToUTC(const PT::Date & local_date);
|
||||
|
||||
// return a Dst structure for the specified year
|
||||
// or null if it not exists
|
||||
// this method can return a Dst structure for earlier year than 'year'
|
||||
// if 'year' doesn't exist
|
||||
Dst * FindDst(int year);
|
||||
|
||||
|
||||
|
||||
// a time zone name
|
||||
// this is a key to locale
|
||||
std::wstring name;
|
||||
|
||||
// each locale has its own identifier
|
||||
size_t id;
|
||||
|
||||
// time zone offset (in seconds)
|
||||
time_t offset;
|
||||
|
||||
// daylight saving time map
|
||||
// year -> Dst
|
||||
// if there is not a specified year we are taking the lower year, e.g.
|
||||
// if we are looking for 2010 and there is no such a year then we take 2009
|
||||
// (or 2008 if 2009 not exists etc)
|
||||
typedef std::map<int, Dst> DstMap;
|
||||
DstMap dst_map;
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
time_t ParseStrOffset(const wchar_t * str);
|
||||
time_t GetOffset(PT::Space & space);
|
||||
bool SetTzDst(PT::Space & year);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
206
winixd/core/timezones.cpp
Normal file
206
winixd/core/timezones.cpp
Normal file
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "timezones.h"
|
||||
#include "misc.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
TimeZones::TimeZones()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
void TimeZones::Clear()
|
||||
{
|
||||
zone_tab.clear();
|
||||
|
||||
for(size_t i=0 ; i<zone_indices.size() ; ++i)
|
||||
zone_indices[i] = size_t(-1);
|
||||
}
|
||||
|
||||
|
||||
void TimeZones::SetTimeZoneMaxId(size_t max_id)
|
||||
{
|
||||
if( max_id > 1000 )
|
||||
{
|
||||
max_id = 1000;
|
||||
log << log1 << "TZ: time_zone_max_id is too big (changed to 1000)" << logend;
|
||||
}
|
||||
|
||||
size_t old_size = zone_indices.size();
|
||||
zone_indices.resize(max_id + 1);
|
||||
|
||||
for(size_t i=old_size ; i<zone_indices.size() ; ++i)
|
||||
zone_indices[i] = size_t(-1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool TimeZones::HasZone(size_t zone_id)
|
||||
{
|
||||
if( zone_id < zone_indices.size() )
|
||||
return zone_indices[zone_id] < zone_tab.size();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
TimeZone * TimeZones::GetZone(size_t zone_id)
|
||||
{
|
||||
if( zone_id < zone_indices.size() )
|
||||
{
|
||||
size_t index = zone_indices[zone_id];
|
||||
|
||||
if( index < zone_tab.size() )
|
||||
return &zone_tab[index];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
TimeZone * TimeZones::GetZoneByIndex(size_t zone_index)
|
||||
{
|
||||
if( zone_index < zone_tab.size() )
|
||||
return &zone_tab[zone_index];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
size_t TimeZones::Size() const
|
||||
{
|
||||
return zone_tab.size();
|
||||
}
|
||||
|
||||
|
||||
bool TimeZones::Empty() const
|
||||
{
|
||||
return zone_tab.empty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void TimeZones::ParseZones()
|
||||
{
|
||||
for(size_t i=0 ; i<temp_space.spaces.size() ; ++i)
|
||||
{
|
||||
PT::Space & zone = *temp_space.spaces[i];
|
||||
temp_zone.Clear();
|
||||
|
||||
if( temp_zone.SetTz(zone) )
|
||||
{
|
||||
if( !HasZone(temp_zone.id) )
|
||||
{
|
||||
if( temp_zone.id < zone_indices.size() )
|
||||
{
|
||||
zone_tab.push_back(temp_zone);
|
||||
zone_indices[temp_zone.id] = zone_tab.size() - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "Tz: zone: " << temp_zone.name << " has too big id: "
|
||||
<< temp_zone.id << " (skipping)" << logend;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "Tz: zone with id: " << temp_zone.id
|
||||
<< " already exists (skipping)" << logend;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "System: problem with reading time zone info from time zone: "
|
||||
<< zone.name << " (skipping) " << logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// !! IMPROVE ME
|
||||
// in the future we do not have to read the whole file
|
||||
// just space by space (not implemented in Space at the moment)
|
||||
bool TimeZones::ReadTimeZones(const wchar_t * path)
|
||||
{
|
||||
parser.SetSpace(temp_space);
|
||||
zone_tab.clear();
|
||||
temp_space.Clear();
|
||||
|
||||
PT::SpaceParser::Status status = parser.Parse(path);
|
||||
|
||||
if( status == PT::SpaceParser::ok )
|
||||
{
|
||||
ParseZones();
|
||||
log << log2 << "Tz: time zones loaded, there are " << zone_tab.size() << " zones" << logend;
|
||||
}
|
||||
else
|
||||
if( status == PT::SpaceParser::syntax_error )
|
||||
{
|
||||
log << log1 << "TZ: error in time zone file, line: " << parser.line << logend;
|
||||
}
|
||||
else
|
||||
if( status == PT::SpaceParser::cant_open_file )
|
||||
{
|
||||
log << log1 << "TZ: I cannot open the time zone file: " << path << logend;
|
||||
}
|
||||
|
||||
temp_space.Clear();
|
||||
|
||||
return status == PT::SpaceParser::ok;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool TimeZones::ReadTimeZones(const std::wstring & path)
|
||||
{
|
||||
return ReadTimeZones(path.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
114
winixd/core/timezones.h
Normal file
114
winixd/core/timezones.h
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_timezones
|
||||
#define headerfile_winix_core_timezones
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "timezone.h"
|
||||
#include "space/spaceparser.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
class TimeZones
|
||||
{
|
||||
public:
|
||||
|
||||
TimeZones();
|
||||
|
||||
// maximum allowed time zone's identifier
|
||||
void SetTimeZoneMaxId(size_t max_id);
|
||||
|
||||
// reading time zone from a file
|
||||
bool ReadTimeZones(const wchar_t * path);
|
||||
bool ReadTimeZones(const std::wstring & path);
|
||||
|
||||
// returning true if there is a time zone with the zone_id identifier
|
||||
bool HasZone(size_t zone_id);
|
||||
|
||||
// returning a time zone by time zone's identifier
|
||||
// can return a null pointer if there is no such a zone
|
||||
TimeZone * GetZone(size_t zone_id);
|
||||
|
||||
// returning the number of all time zones
|
||||
size_t Size() const;
|
||||
|
||||
// returning a time zone by an internal index
|
||||
// usuful for iterating through all zones
|
||||
// this index is in a range of <0, Size()-1>
|
||||
// can return a null pointer if the index is out of range
|
||||
TimeZone * GetZoneByIndex(size_t zone_index);
|
||||
|
||||
// return true if there are not any time zones
|
||||
bool Empty() const;
|
||||
|
||||
// clears all time zones
|
||||
// this does not affect SetTimeZoneMaxId()
|
||||
// so the size of zone_indices is not changed but all indices are invalidated
|
||||
void Clear();
|
||||
|
||||
|
||||
private:
|
||||
|
||||
// indices to 'tab'
|
||||
// with this we have O(1) time to find a time zone in 'tab'
|
||||
// everywhere we use zone_id we refer to this table
|
||||
// SetTimeZoneMaxId(size_t max_id) sets size of this table to max_id+1
|
||||
std::vector<size_t> zone_indices;
|
||||
|
||||
// time zones
|
||||
// everywhere we use zone_index we refer to this table
|
||||
std::vector<TimeZone> zone_tab;
|
||||
|
||||
PT::SpaceParser parser;
|
||||
PT::Space temp_space;
|
||||
TimeZone temp_zone;
|
||||
|
||||
void ParseZones();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
310
winixd/core/ugcontainer.h
Normal file
310
winixd/core/ugcontainer.h
Normal file
@@ -0,0 +1,310 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfile_winix_core_ugcontainer
|
||||
#define headerfile_winix_core_ugcontainer
|
||||
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include "log.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
class UGContainer
|
||||
{
|
||||
public:
|
||||
|
||||
UGContainer();
|
||||
~UGContainer();
|
||||
|
||||
typedef typename std::list<Type> Table;
|
||||
typedef typename Table::iterator Iterator;
|
||||
typedef typename Table::size_type SizeType;
|
||||
|
||||
typedef typename std::map<long, Iterator> TableId;
|
||||
typedef typename std::map<std::wstring, Iterator> TableName;
|
||||
|
||||
|
||||
Iterator Begin();
|
||||
Iterator End();
|
||||
SizeType Size();
|
||||
bool Empty();
|
||||
Iterator PushBack(const Type & type); // can return End() if the user already exists
|
||||
void Clear();
|
||||
|
||||
bool Is(long id);
|
||||
bool Is(const std::wstring & name);
|
||||
|
||||
Iterator FindId(long id);
|
||||
Iterator FindName(const std::wstring & name);
|
||||
|
||||
bool Remove(long id);
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
// don't copy these objects
|
||||
UGContainer(const UGContainer<Type> &);
|
||||
UGContainer<Type> & operator=(const UGContainer<Type> &);
|
||||
|
||||
void AddIndexes(Iterator iter);
|
||||
void RebuildIndexes();
|
||||
|
||||
// main table
|
||||
Table table;
|
||||
|
||||
// table.size() has O(n) complexity
|
||||
size_t table_size;
|
||||
|
||||
// indexes
|
||||
TableId table_id;
|
||||
TableName table_name;
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
UGContainer<Type>::~UGContainer()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
UGContainer<Type>::UGContainer()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
// is private
|
||||
template<class Type>
|
||||
UGContainer<Type>::UGContainer(const UGContainer<Type> &)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
// is private
|
||||
template<class Type>
|
||||
UGContainer<Type> & UGContainer<Type>::operator=(const UGContainer<Type> &)
|
||||
{
|
||||
Clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
typename UGContainer<Type>::Iterator UGContainer<Type>::Begin()
|
||||
{
|
||||
return table.begin();
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
typename UGContainer<Type>::Iterator UGContainer<Type>::End()
|
||||
{
|
||||
return table.end();
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
typename UGContainer<Type>::SizeType UGContainer<Type>::Size()
|
||||
{
|
||||
return table_size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
bool UGContainer<Type>::Empty()
|
||||
{
|
||||
return table.empty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
typename UGContainer<Type>::Iterator UGContainer<Type>::PushBack(const Type & type)
|
||||
{
|
||||
if( Is(type.id) || Is(type.name) )
|
||||
return End();
|
||||
|
||||
table.push_back(type);
|
||||
table_size += 1;
|
||||
Iterator i = --table.end();
|
||||
log << log3 << "UGCont: added, id: " << type.id << ", name: " << type.name << logend;
|
||||
AddIndexes(i);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
void UGContainer<Type>::Clear()
|
||||
{
|
||||
table_size = 0;
|
||||
table.clear();
|
||||
table_id.clear();
|
||||
table_name.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
bool UGContainer<Type>::Is(long id)
|
||||
{
|
||||
typename TableId::iterator i = table_id.find(id);
|
||||
|
||||
if( i == table_id.end() )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
bool UGContainer<Type>::Is(const std::wstring & name)
|
||||
{
|
||||
typename TableName::iterator i = table_name.find(name);
|
||||
|
||||
if( i == table_name.end() )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
typename UGContainer<Type>::Iterator UGContainer<Type>::FindId(long id)
|
||||
{
|
||||
typename TableId::iterator i = table_id.find(id);
|
||||
|
||||
if( i == table_id.end() )
|
||||
return table.end();
|
||||
|
||||
return i->second;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
typename UGContainer<Type>::Iterator UGContainer<Type>::FindName(const std::wstring & name)
|
||||
{
|
||||
typename TableName::iterator i = table_name.find(name);
|
||||
|
||||
if( i == table_name.end() )
|
||||
return table.end();
|
||||
|
||||
return i->second;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
void UGContainer<Type>::AddIndexes(UGContainer<Type>::Iterator iter)
|
||||
{
|
||||
table_id.insert( std::make_pair(iter->id, iter) );
|
||||
table_name.insert( std::make_pair(iter->name, iter) );
|
||||
|
||||
log << log4 << "UGCont: added indexes to: id: " << iter->id << ", name: " << iter->name << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
void UGContainer<Type>::RebuildIndexes()
|
||||
{
|
||||
Iterator i;
|
||||
|
||||
log << log4 << "UGCont: rebuilding indexes" << logend;
|
||||
|
||||
table_id.clear();
|
||||
table_name.clear();
|
||||
|
||||
for(i=table.begin() ; i!=table.end() ; ++i)
|
||||
AddIndexes(i);
|
||||
|
||||
log << log4 << "UGCont: indexes rebuilt, table.size: " << table_size << ", table_id.size: "
|
||||
<< table_id.size() << ", table_name.size: " << table_name.size() << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<class Type>
|
||||
bool UGContainer<Type>::Remove(long id)
|
||||
{
|
||||
typename TableId::iterator i = table_id.find(id);
|
||||
typename TableName::iterator n;
|
||||
bool result = false;
|
||||
|
||||
if( i != table_id.end() )
|
||||
{
|
||||
for(n=table_name.begin() ; n != table_name.end() ; ++n)
|
||||
{
|
||||
if( n->second == i->second )
|
||||
{
|
||||
table_name.erase(n);
|
||||
log << log4 << "UGCont: removed index_id to: id: " << i->second->id << ", name: " << i->second->name << logend;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
log << log4 << "UGCont: removed index_name to: id: " << i->second->id << ", name: " << i->second->name << logend;
|
||||
log << log3 << "UGCont: removed: id: " << i->second->id << ", name: " << i->second->name << logend;
|
||||
table.erase(i->second);
|
||||
table_id.erase(i);
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
#endif
|
||||
83
winixd/core/user.cpp
Normal file
83
winixd/core/user.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2014, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. 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.
|
||||
*
|
||||
* 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "user.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
User::User()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
void User::Clear()
|
||||
{
|
||||
id = -1;
|
||||
name.clear();
|
||||
super_user = false;
|
||||
groups.clear();
|
||||
email.clear();
|
||||
notify = 0;
|
||||
env.Clear();
|
||||
aenv.Clear();
|
||||
status = WINIX_ACCOUNT_BLOCKED;
|
||||
locale_id = 0;
|
||||
time_zone_id = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool User::IsMemberOf(long group)
|
||||
{
|
||||
std::vector<long>::iterator i;
|
||||
|
||||
for(i=groups.begin() ; i!=groups.end() ; ++i)
|
||||
if( *i == group )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user