import the first version of cmslu
git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@460 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
161
core/sessionmanager.cpp
Executable file
161
core/sessionmanager.cpp
Executable file
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* This file is a part of CMSLU -- Content Management System like Unix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "sessionmanager.h"
|
||||
|
||||
|
||||
bool SessionManager::IsSession(long s)
|
||||
{
|
||||
SessionTable::iterator i;
|
||||
|
||||
Session temp;
|
||||
|
||||
temp.id = s;
|
||||
|
||||
i = session_table.find(temp);
|
||||
|
||||
if( i == session_table.end() )
|
||||
return false;
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
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::CreateTemporarySession()
|
||||
{
|
||||
Session s;
|
||||
|
||||
s.id = 0;
|
||||
SessionTable::iterator i = session_table.find( s ); // looking for id=0
|
||||
|
||||
if( i == session_table.end() )
|
||||
{
|
||||
std::pair<SessionTable::iterator,bool> res = session_table.insert(s);
|
||||
request.session = const_cast<Session*>( &(*res.first) );
|
||||
}
|
||||
else
|
||||
{
|
||||
request.session = const_cast<Session*>( &(*i) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SessionManager::CreateSession()
|
||||
{
|
||||
Session s;
|
||||
int attempts = 100;
|
||||
|
||||
for( ; attempts > 0 ; --attempts )
|
||||
{
|
||||
s.id = CreateSessionId();
|
||||
|
||||
std::pair<SessionTable::iterator,bool> res = session_table.insert(s);
|
||||
|
||||
if( res.second == true )
|
||||
{
|
||||
// the insertion took place
|
||||
|
||||
request.session = const_cast<Session*>( &(*res.first) );
|
||||
request.SetCookie(data.http_session_id_name.c_str(), request.session->id);
|
||||
log << log1 << "created a new session: " << s.id << logend;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// there is a problem with generating a new session id
|
||||
// we do not set a session cookie
|
||||
CreateTemporarySession();
|
||||
log << log1 << "cannot create a session id (temporary used: with id 0)" << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionManager::SetSession()
|
||||
{
|
||||
CookieTable::iterator i = request.cookie_table.find(data.http_session_id_name);
|
||||
|
||||
if( i == request.cookie_table.end() )
|
||||
{
|
||||
CreateSession();
|
||||
}
|
||||
else
|
||||
{
|
||||
Session temp;
|
||||
temp.id = atol(i->second.c_str());
|
||||
SessionTable::iterator s = session_table.find(temp);
|
||||
|
||||
if( s != session_table.end() )
|
||||
{
|
||||
// that session is in the table
|
||||
request.session = const_cast<Session*>( &(*s) );
|
||||
log << log1 << "session: " << s->id << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
// there is no such a session
|
||||
// deleting the old cookie
|
||||
request.cookie_table.erase(i);
|
||||
|
||||
// and creating a new one
|
||||
CreateSession();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// request.session is set now
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user