refactoring

git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@523 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2009-11-17 01:04:01 +00:00
parent 2ca44ec361
commit dc5f002de3
17 changed files with 101 additions and 370 deletions

View File

@ -110,7 +110,7 @@ void Content::MakeStandardFunction()
if( !request.pfunction )
SetDefaultFunction();
if( request.result == Request::redirect )
if( !request.redirect_to.empty() )
return;
@ -239,7 +239,7 @@ void Content::Make()
if( request.method == Request::post )
MakePost();
if( request.result == Request::redirect )
if( !request.redirect_to.empty() )
return;
if( request.status == Error::ok )
@ -249,7 +249,7 @@ void Content::Make()
request.status = Error::permision_denied;
}
if( request.result == Request::redirect )
if( !request.redirect_to.empty() )
return;
if( request.dir_table.empty() )
@ -275,23 +275,22 @@ void Content::RedirectTo(const Item & item)
{
std::string path;
request.result = Request::redirect;
request.str = data.base_url;
request.redirect_to = data.base_url;
if( item.type == Item::dir )
{
// item_id is pointing to a directory
data.dirs.MakePath(item.id, path);
request.str += path;
request.redirect_to += path;
}
else
{
if( !data.dirs.MakePath(item.parent_id, path) )
log << log1 << "Content: Can't redirect: no dirs for item id: " << item.id << logend;
request.str += path;
request.str += item.url;
request.redirect_to += path;
request.redirect_to += item.url;
}
}
@ -302,16 +301,15 @@ void Content::RedirectTo(long item_id)
std::string path;
Item * pdir;
request.result = Request::redirect;
request.str = data.base_url;
pdir = data.dirs.GetDir(item_id);
request.redirect_to = data.base_url;
pdir = data.dirs.GetDir(item_id);
if( pdir )
{
// item_id is pointing to a directory
data.dirs.MakePath(pdir->id, path);
request.str += path;
request.redirect_to += path;
}
else
{
@ -324,7 +322,7 @@ Item * pdir;
if( !data.dirs.MakePath(request.item_table[0].parent_id, path) )
log << log1 << "Content: Can't redirect: no dirs for item id: " << request.item_table[0].id << ", requested directory id: " << request.item_table[0].parent_id << logend;
request.str += path + request.item_table[0].url;
request.redirect_to += path + request.item_table[0].url;
}
else
{
@ -386,9 +384,9 @@ bool Content::CheckRebus()
try
{
std::string & answer = request.PostVar("rebus");
std::string * answer = request.PostVar("rebus");
if( data.rebus.IsAnswerOk(request.session->rebus_item, answer) )
if( answer && data.rebus.IsAnswerOk(request.session->rebus_item, *answer) )
return true;
}
catch(const Error &)
@ -401,23 +399,16 @@ return false;
}
void Content::SetUser()
void Content::SetUser(Item & item)
{
if( request.session->puser )
{
request.item.user_id = request.session->puser->id;
request.item.guest_name.clear();
item.user_id = request.session->puser->id;
item.guest_name.clear();
}
else
{
request.item.user_id = -1;
try
{
request.item.guest_name = request.PostVar("guestname");
}
catch(const Error &)
{
}
item.user_id = -1;
request.PostVar("guestname", item.guest_name);
}
}

View File

@ -36,7 +36,7 @@ class Content
void MakeStandardFunction();
void SetUser();
void SetUser(Item & item);
bool CheckRebus();
void MakePost();

View File

@ -37,29 +37,10 @@ void Content::PostFunCreateThread()
if( !CheckRebus() )
{
request.status = Error::rebus_bad_answer;
try
{
try
{
request.item.url = request.PostVar("url");
}
catch(...)
{
}
request.item.subject = request.PostVar("subject");
request.item.content = request.PostVar("content");
SetUser();
}
catch(...)
{
}
request.PostVar("url", request.item.url);
request.PostVar("subject", request.item.subject);
request.PostVar("content", request.item.content);
SetUser(request.item);
return;
}

View File

@ -22,13 +22,13 @@ long Content::PostFunDefaultParsePath()
Item * pdir, * pdir2;
long defaultid = -1;
std::string & path = request.PostVar("defaultitem");
std::string * path = request.PostVar("defaultitem");
if( !path.empty() )
if( path && !path->empty() )
{
std::string dir, file;
Dirs::SplitPath(path, dir, file);
Dirs::SplitPath(*path, dir, file);
pdir = data.dirs.GetDir(dir);

View File

@ -43,7 +43,7 @@ void Content::PostFunEmacsAdd()
request.is_item = true;
SetUser();
SetUser(request.item);
request.item.group_id = -1;
request.item.privileges = 0644; // !! tymczasowo, bedzie uzyte umask
@ -100,14 +100,8 @@ bool adding = true;
{
// these old values are ignored (if exists)
//!! tymczasowo, zabezpieczenie przed pustym url-em
try {
request.item.url = request.PostVar("url");
request.item.subject = request.PostVar("subject");
}
catch(...)
{
}
request.PostVar("url", request.item.url);
request.PostVar("subject", request.item.subject);
if( request.item.subject.empty() )
request.item.subject = request.dir_table.back()->subject;
@ -115,11 +109,12 @@ bool adding = true;
// !! nie bedzie obciazany modul wyszukujacy bardzo
////////
request.item.content = request.PostVar("content");
request.PostVar("content", request.item.content);
bool with_url = false;
if( !request.is_item || request.PostVar("old_url") != request.item.url )
std::string * old_url = request.PostVar("old_url");
if( !request.is_item || (old_url && *old_url!=request.item.url) )
with_url = true;
PrepareUrl(request.item);
@ -127,7 +122,7 @@ bool adding = true;
if( !CheckRebus() )
{
request.status = Error::rebus_bad_answer;
SetUser();
SetUser(request.item);
return;
}

View File

@ -33,11 +33,11 @@ void Content::PostFunLogin()
{
try
{
std::string & login = request.PostVar("login");
std::string & pass = request.PostVar("password");
std::string * login = request.PostVar("login");
std::string * pass = request.PostVar("password");
long user_id;
if( db.CheckUser(login, pass, user_id) )
if( login && pass && db.CheckUser(*login, *pass, user_id) )
{
request.session->puser = data.users.GetUser(user_id);
@ -47,7 +47,7 @@ void Content::PostFunLogin()
return;
}
data.last.UserLogin(user_id, login, inet_addr(request.env_remote_addr), request.session->id);
data.last.UserLogin(user_id, *login, inet_addr(request.env_remote_addr), request.session->id);
log << log2 << "User " << login << " (id: " << user_id << ") logged" << logend;
}

View File

@ -33,15 +33,12 @@ void Content::FunLogout()
request.session->puser = 0;
}
request.result = Request::redirect;
std::string path;
data.dirs.MakePath(request.dir_table.back()->id, path);
request.str = data.base_url + path;
request.redirect_to = data.base_url + path;
if( request.is_item )
request.str += request.item.url;
request.redirect_to += request.item.url;
request.session->done = Done::loggedout;
request.session->done_timer = 2;

View File

@ -28,22 +28,14 @@ void Content::PostFunMkdir(bool add_to_dir_table, int mask)
Item item;
request.session->done = Done::added_dir;
try
{
// try
// {
item.type = Item::dir;
item.subject = request.PostVar("subject");
item.url = request.PostVar("url");
request.PostVar("subject", item.subject);
request.PostVar("url", item.url);
item.parent_id = request.dir_table.back()->id;
if( request.session->puser )
{
item.user_id = request.session->puser->id;
}
else
{
item.user_id = -1;
item.guest_name = request.PostVar("guestname");
}
SetUser(item);
item.group_id = -1;
item.privileges = mask;
@ -55,12 +47,12 @@ void Content::PostFunMkdir(bool add_to_dir_table, int mask)
request.session->done_status = db.AddItem(item);
}
/* }
catch(const Error & e)
{
request.session->done_status = e;
}
*/
if( request.session->done_status == Error::ok )
{

View File

@ -18,9 +18,21 @@ void Content::PostFunPriv(Item & item)
{
try
{
long user_id = data.users.GetUserId( request.PostVar("user") );
long group_id = data.groups.GetGroupId( request.PostVar("group") );
int privileges = strtol( request.PostVar("privileges").c_str() , 0, 8);
std::string * user = request.PostVar("user");
std::string * group = request.PostVar("group");
std::string * priv = request.PostVar("privileges");
if( !user || !group || !priv )
{
log << log3 << "Content: PostFunPriv: there is no some post variables" << logend;
// some kind of error to report?
// !! wstawic nowy kod bledu i rzucic wyjatek jego (usunac powyzszy wpis log takze)
return;
}
long user_id = data.users.GetUserId( *user );
long group_id = data.groups.GetGroupId( *group );
int privileges = strtol( priv->c_str() , 0, 8);
if( user_id==item.user_id && group_id==item.group_id && privileges==item.privileges )
{

View File

@ -60,34 +60,6 @@ void Dirs::ReadDirs()
}
bool Dirs::GetRootDir(Item ** item)
{
DirContainer::Iterator root = dir_table.GetRoot();
if( root == dir_table.End() )
return false;
*item = &(*root);
return true;
}
bool Dirs::GetDir(const std::string & name, long parent, Item ** item)
{
DirContainer::ParentIterator i = dir_table.FindFirstParent(parent);
for( ; i!=dir_table.ParentEnd() ; i = dir_table.NextParent(i) )
if( i->second->url == name )
{
*item = &(*i->second);
return true;
}
return false;
}
bool Dirs::ExtractName(const char * & s, std::string & name)
{
name.clear();
@ -103,62 +75,6 @@ return !name.empty();
// !! moze lepiej zwracac wskaznik do Item i kiedy nie ma katalogu to zwracac 0 ?
bool Dirs::GetDir(const std::string & path, Item ** item)
{
DirContainer::Iterator root = dir_table.GetRoot();
if( root == dir_table.End() )
// ops, we do not have a root dir
return false;
Item * pitem = &(*root);
std::string name;
const char * s = path.c_str();
while( ExtractName(s, name) )
{
if( !GetDir(name, pitem->id, &pitem) )
return false;
}
*item = pitem;
return true;
}
// !! ten interfejs jes bylejaki
// !! moze lepiej zwracac id i kiedy nie ma katalogu to -1 (przeciez to jest wartosc ktora nie moze pojawic sie w indeksie)
bool Dirs::GetDirId(const std::string & path, long * id)
{
Item * pitem;
if( !GetDir(path, &pitem) )
return false;
*id = pitem->id;
return true;
}
bool Dirs::GetDirId(const std::string & name, long parent, long * id)
{
Item * pitem;
if( !GetDir(name, parent, &pitem) )
return false;
*id = pitem->id;
return true;
}
bool Dirs::IsDir(long id)
@ -174,20 +90,6 @@ return true;
bool Dirs::GetDirChilds(long parent, std::vector<Item> & childs_table)
{
if( parent != -1 && !IsDir(parent) )
return false;
DirContainer::ParentIterator i = dir_table.FindFirstParent(parent);
for( ; i != dir_table.ParentEnd() ; i = dir_table.NextParent(i) )
childs_table.push_back( *i->second );
return true;
}
bool Dirs::GetDirChilds(long parent, std::vector<Item*> & childs_table)
{
if( parent != -1 && !IsDir(parent) )
@ -232,55 +134,6 @@ DirContainer::Iterator i;
// with exceptions
Item * Dirs::GetDirT(const std::string & path)
{
Item * pitem;
if( !GetDir(path, &pitem) )
throw Error(Error::incorrect_dir);
return pitem;
}
Item * Dirs::GetDirT(const std::string & name, long parent)
{
Item * pitem;
if( !GetDir(name, parent, &pitem) )
throw Error(Error::incorrect_dir);
return pitem;
}
long Dirs::GetDirIdT(const std::string & path)
{
long id;
if( !GetDirId(path, &id) )
throw Error(Error::incorrect_dir);
return id;
}
long Dirs::GetDirIdT(const std::string & name, long parent)
{
long id;
if( !GetDirId(name, parent, &id) )
throw Error(Error::incorrect_dir);
return id;
}
// !! nowy interfejs
@ -417,8 +270,7 @@ void Dirs::DeleteDir(long id)
if( db.DelDirById(id) == Error::ok )
dir_table.DelById(id);
if( data.mounts.CurrentMountType() == Mount::thread )
db.RemoveThread(id);
db.RemoveThread(id);
}

View File

@ -19,6 +19,8 @@
#include "dircontainer.h"
// we do not support '..' in a path (for simplicity and security reasons)
class Dirs
{
@ -26,41 +28,17 @@ public:
void Clear();
void ReadDirs();
// without any exceptions
// these methods return false in a case the path or name (with a specific parent) are invalid
// we do not support '..' in a path (for security reason)
bool IsDir(long id);
bool GetRootDir(Item ** item);
bool GetDir(const std::string & path, Item ** item);
bool GetDir(const std::string & name, long parent, Item ** item);
bool GetDirId(const std::string & path, long * id);
bool GetDirId(const std::string & name, long parent, long * id);
//!! ta nie bedzie chyba potrzebna
bool GetDirChilds(long parent, std::vector<Item> & childs_table); // only returns dir-children
// 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_table);
bool MakePath(long dir_id, std::string & path);
bool GetDirChilds(long parent, std::vector<Item*> & childs_table); // only returns dir-children
bool MakePath(long id, std::string & path);
static void SplitPath(const std::string & path, std::string & dir, std::string & file);
// with an Error exception
// if the path or name are invalid these methods throw an exception
Item * GetDirT(const std::string & path);
Item * GetDirT(const std::string & name, long parent);
long GetDirIdT(const std::string & path);
long GetDirIdT(const std::string & name, long parent);
// !! nowy interfejs
// returns null if there is no a root dir
// these methods return null if there is no such a dir
Item * GetRootDir();
Item * GetEtcDir();
Item * GetDir(const std::string & name, long parent);
@ -68,13 +46,7 @@ public:
Item * GetDir(long id);
Item * AddDir(const Item & item);
static void SplitPath(const std::string & path, std::string & dir, std::string & file);
void DeleteDir(long id);
void CheckRootDir();

View File

@ -64,6 +64,14 @@ Log & Log::operator<<(const std::string & s)
}
Log & Log::operator<<(const std::string * s)
{
buffer << *s;
return *this;
}
Log & Log::operator<<(int s)
{
buffer << s;

View File

@ -38,6 +38,7 @@ public:
void PutDate(Manipulators m);
Log & operator<<(const char * s);
Log & operator<<(const std::string * s);
Log & operator<<(const std::string & s);
Log & operator<<(int s);
Log & operator<<(long s);

View File

@ -75,7 +75,7 @@ Mount Mounts::GetCurrentMountPoint()
Mount::Type Mounts::CurrentMountType()
{
return current_dir.type;
return current_dir.type;
}
bool Mounts::CurrentMountIsParam(Mount::Param p)

View File

@ -59,17 +59,9 @@ void Request::Clear()
env_http_accept_encoding = &char_empty;
session = 0;
result = err404; // !! tutaj moze cos lepszego, cos w stylu 'not implemented'
// dir = -1;
// cur_dir_table.clear();
item_table.clear();
// dir_table2.clear();
item.Clear();
str.clear();
dir_table.clear();
is_item = false;
pfunction = 0;
@ -79,35 +71,16 @@ void Request::Clear()
is_thread = false;
thread.Clear();
thread_tab.clear();
notify_code = 0;
browser_msie = false;
// should be always empty
// this is only for safety
//string_empty.clear();
redirect_to.clear();
plugin.Call(CMSLU_REQUEST_CLEAR);
}
void Request::CopyFirstItem()
{
if( !request.item_table.empty() )
request.item = request.item_table[0];
else
{
request.item.Clear();
log << log1 << "Request::CopyFirstItem: item_table is empty" << logend;
request.result = err_internal;
}
}
// value can be null
@ -148,27 +121,21 @@ return true;
std::string & Request::PostVar(const char * var)
std::string * Request::PostVar(const char * var)
{
PostTable::iterator p;
p = post_table.find(var);
PostTable::iterator p = post_table.find(var);
if( p == post_table.end() )
{
throw Error(Error::no_postvar);
}
return 0;
return p->second;
return &(p->second);
}
/*
bool Request::PostVar(const char * var, std::string & result)
{
PostTable::iterator p;
p = post_table.find(var);
PostTable::iterator p = post_table.find(var);
if( p == post_table.end() )
{
@ -180,7 +147,7 @@ bool Request::PostVar(const char * var, std::string & result)
return true;
}
*/
@ -333,12 +300,12 @@ void Request::Read()
void Request::SendAll()
{
if( result == redirect )
if( !redirect_to.empty() )
{
FCGX_PutS("Status: 301 Moved Permanently\r\n", out);
FCGX_FPrintF(out, "Location: %s\r\n", str.c_str());
FCGX_FPrintF(out, "Location: %s\r\n", redirect_to.c_str());
log << log2 << "Redirect to: " << str << logend;
log << log2 << "Redirect to: " << redirect_to << logend;
}
else
{
@ -355,7 +322,7 @@ void Request::SendAll()
FCGX_PutS("\r\n", out);
if( result == redirect )
if( !redirect_to.empty() )
// if there is a redirect we do not send a content
return;
//

View File

@ -51,7 +51,8 @@ struct Request
CookieTable cookie_table;
// environment variables
// they are not null -- when the server doesn't have such a variable it will be pointing into 'char_empty' which is default '\0'
// they are not null -- when the server doesn't have such a variable
// it will be pointing into 'char_empty' which is default '\0'
const char * env_request_method;
const char * env_request_uri;
const char * env_http_cookie;
@ -68,22 +69,7 @@ struct Request
// is set after calling session_manager.SetSession()
Session * session;
// 'done_status' is set if 'done' is different than 'nothing'
Error done_status; // !! wywalic - jest przeciez w session
// what to do
// !! wywalic?
enum Result { err_internal, err404, err_per_denied, show_dir, show_item, show_item_by_id, add_item, edit_item, del_item, del_item_confirm, confirm, redirect, logout } result; // zamienic na to_do
// !! nowe skladowe
// current directory
// !! zapewnic aby byl minimum jeden katalog (root)
// !! moze nazwac to poprostu dir?
std::vector<Item*> dir_table;
bool is_item;
@ -102,33 +88,17 @@ struct Request
// last notify
int notify_code;
// ------------------
// !! stare skladowe
// current directory e.g. /foo/bar
//std::vector<Item> cur_dir_table;
// id of the last directory (bar) or -1
//long dir;
// ------------------
// items in the current directory
// maybe without contents?
std::vector<Item> item_table;
// directories in the current directory
//std::vector<Item> dir_table2;
// current thread (if exists)
bool is_thread;
Thread thread;
std::vector<Thread> thread_tab;
// this string is used for many purposes such as redirecting
std::string str;
// if not empty means an address for redirecting to
std::string redirect_to;
// for debugging
void PrintGetTable();
@ -146,12 +116,8 @@ struct Request
void SetCookie(const char * name, long value);
bool IsPostVar(const char * var);
std::string & PostVar(const char * var); // with a throw !!! wywalic te wyjatki ztad, niech zwraca pusty string jak nie znajdzie nic, albo referencje na jakis statyczny pusty string
//bool PostVar(const char * var, std::string & result);
// item_table[0] -> item
// !! to tez nie bedzie potrzebne
void CopyFirstItem();
std::string * PostVar(const char * var); // it can return null when there is no such a post variable
bool PostVar(const char * var, std::string & result);
void ReadEnvVariables();
@ -187,8 +153,6 @@ private:
// it contains '\0'
const char char_empty;
// used when returning a reference to std::string (when the string doesn't exist)
const std::string string_empty;
const char * SetEnvVar(const char * var);

View File

@ -130,8 +130,7 @@ bool RequestController::BaseUrlRedirect()
if( data.base_url_http_host == request.env_http_host )
return false;
request.result = Request::redirect;
request.str = data.base_url + request.env_request_uri;
request.redirect_to = data.base_url + request.env_request_uri;
log << log3 << "RC: BaseUrlRedirect from: " << request.env_http_host << logend;