changed: added Cur structure

we have there two pointers: 
 Request * request;
 Session * session;
these are the current request and the current session


the session GC was moved to SessionManager (was in SessionContainer)



git-svn-id: svn://ttmath.org/publicrep/winix/trunk@708 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2011-01-23 14:15:30 +00:00
parent 61ac29b2de
commit 915cabdf97
171 changed files with 2822 additions and 2650 deletions

View File

@@ -31,33 +31,37 @@ App::App()
last_sessions_save = std::time(0);
fcgi_socket = -1;
// temporary there is only one request
cur.request = &req;
cur.session = session_manager.GetTmpSession();
db.SetConn(db_conn);
plugin.SetDb(&db);
plugin.SetConfig(&config);
plugin.SetRequest(&request);
plugin.SetCur(&cur);
plugin.SetSystem(&system);
plugin.SetFunctions(&functions);
plugin.SetTemplates(&templates);
plugin.SetSynchro(&synchro);
plugin.SetSessionManager(&session_manager);
request.SetConfig(&config);
req.SetConfig(&config);
functions.SetConfig(&config);
functions.SetRequest(&request);
functions.SetCur(&cur);
functions.SetDb(&db);
functions.SetSystem(&system);
functions.SetTemplates(&templates);
functions.SetSynchro(&synchro);
system.SetConfig(&config);
system.SetRequest(&request);
system.SetCur(&cur);
system.SetDb(&db);
system.SetSynchro(&synchro);
templates.SetConfig(&config);
templates.SetRequest(&request);
templates.SetCur(&cur);
templates.SetDb(&db);
templates.SetSystem(&system);
templates.SetFunctions(&functions);
@@ -65,7 +69,7 @@ App::App()
session_manager.SetLastContainer(&system.users.last);
session_manager.SetConfig(&config);
session_manager.SetRequest(&request);
session_manager.SetCur(&cur);
session_manager.SetSystem(&system);
session_manager.SetSynchro(&synchro);
@@ -131,7 +135,7 @@ bool App::Init()
db_conn.WaitForConnection();
db.LogQueries(config.log_db_query);
request.Clear();
cur.request->Clear();
compress.Init();
system.Init();
functions.Init();
@@ -143,6 +147,7 @@ bool App::Init()
// init notify after templates (it uses locales from templates)
system.notify.ReadTemplates();
session_manager.InitTmpSession();
session_manager.LoadSessions();
CreateStaticTree();
@@ -158,7 +163,8 @@ void App::Close()
{
session_manager.SaveSessions();
session_manager.DeleteSessions();
request.Clear();
cur.request->Clear();
session_manager.UninitTmpSession();
}
@@ -168,13 +174,13 @@ bool App::BaseUrlRedirect()
if( config.base_url_http_host.empty() )
return false;
if( Equal(config.base_url_http_host.c_str(), request.env_http_host) )
if( Equal(config.base_url_http_host.c_str(), cur.request->env_http_host) )
return false;
request.redirect_to = config.base_url;
AssignString(request.env_request_uri, request.redirect_to, false);
cur.request->redirect_to = config.base_url;
AssignString(cur.request->env_request_uri, cur.request->redirect_to, false);
log << log3 << "RC: BaseUrlRedirect from: " << request.env_http_host << logend;
log << log3 << "RC: BaseUrlRedirect from: " << cur.request->env_http_host << logend;
return true;
}
@@ -184,19 +190,23 @@ void App::ProcessRequestThrow()
{
ReadRequest();
// when BaseUrlRedirect() return true we didn't have to set everything in request.Read()
// in the future request.Read() can be split and at the beginning only environment variables will be read
// when BaseUrlRedirect() return true we didn't have to set everything in cur.request->Read()
// in the future cur.request->Read() can be split and at the beginning only environment variables will be read
// and then BaseUrlRedirect() will be called (for performance)
if( !BaseUrlRedirect() )
{
session_manager.SetSession(); // set request.session as well
// !! dac zeby zwracalo wskaznik (zawsze prawidlowy) na sesje
// i tutaj przypisanie do request.session
session_manager.SetSession();
cur.session = session_manager.GetCurSession();
// !! tutaj dodac to ustawianie request.session
if( cur.session->new_session )
{
cur.session->plugin_data.Resize(plugin.Size());
plugin.Call(WINIX_SESSION_CREATED);
}
plugin.Call(WINIX_SESSION_CHANGED);
functions.Parse();
system.mounts.CalcCurMount();
Make();
@@ -216,7 +226,6 @@ void App::ProcessRequest()
ProcessRequestThrow();
SaveSessionsIfNeeded(); // !! przerzucic to na watek sesji
request.Clear();
system.load_avg.StopRequest();
log << logendrequest;
}
@@ -232,6 +241,9 @@ void App::ProcessRequest()
{
log << log1 << "App: there was an unknown exception" << logend;
}
cur.request->Clear();
cur.session = session_manager.GetTmpSession();
}
@@ -270,20 +282,20 @@ void App::MakePage()
bool sent = false;
if( !request.redirect_to.empty() || !request.x_sendfile.empty() )
if( !cur.request->redirect_to.empty() || !cur.request->x_sendfile.empty() )
return;
if( request.is_item && request.item.file_type == WINIX_ITEM_FILETYPE_NONE &&
request.item.content_type == Item::ct_raw && request.status == WINIX_ERR_OK && request.function )
if( cur.request->is_item && cur.request->item.file_type == WINIX_ITEM_FILETYPE_NONE &&
cur.request->item.content_type == Item::ct_raw && cur.request->status == WINIX_ERR_OK && cur.request->function )
{
if( request.function == &functions.fun_cat )
if( cur.request->function == &functions.fun_cat )
{
request.page << request.item.content;
cur.request->page << cur.request->item.content;
sent = true;
}
else
if( request.function == &functions.fun_run )
if( cur.request->function == &functions.fun_run )
{
templates.GenerateRunRaw();
sent = true;
@@ -301,35 +313,35 @@ bool sent = false;
// !! ta nazwa chyba juz zajeta...
void App::Make()
{
if( request.dir_tab.empty() )
if( cur.request->dir_tab.empty() )
{
log << log1 << "Content: there is no a root dir (dir_tab is empty)" << logend;
return;
}
// request.status can be changed by function_parser
if( request.status == WINIX_ERR_OK )
// cur.request->status can be changed by function_parser
if( cur.request->status == WINIX_ERR_OK )
plugin.Call(WINIX_PREPARE_REQUEST);
if( request.status == WINIX_ERR_OK )
if( cur.request->status == WINIX_ERR_OK )
functions.CheckFunctionAndSymlink();
if( request.status == WINIX_ERR_OK )
if( cur.request->status == WINIX_ERR_OK )
functions.MakeFunction();
if( request.session->spam_score > 0 )
log << log1 << "App: spam score: " << request.session->spam_score << logend;
if( cur.session->spam_score > 0 )
log << log1 << "App: spam score: " << cur.session->spam_score << logend;
if( request.IsParam(L"noredirect") )
request.redirect_to.clear();
if( cur.request->IsParam(L"noredirect") )
cur.request->redirect_to.clear();
if( request.status == WINIX_ERR_OK )
if( cur.request->status == WINIX_ERR_OK )
plugin.Call(WINIX_PROCESS_REQUEST);
if( !request.redirect_to.empty() )
if( !cur.request->redirect_to.empty() )
return;
if( request.dir_tab.empty() )
if( cur.request->dir_tab.empty() )
{
log << log1 << "App: there is no a root dir (dir_tab is empty -- after calling a function)" << logend;
return;
@@ -342,8 +354,8 @@ void App::Make()
{
// !! dodac inne informacje (get, post, itp)
// jesli jest debug_info wlaczone to nie robic przekierowan
request.PrintGetTab();
//request.PrintEnv(); // !! PrintEnv() mozna przeniesc tutaj (do klasy App)
cur.request->PrintGetTab();
//cur.request->PrintEnv(); // !! PrintEnv() mozna przeniesc tutaj (do klasy App)
}
}
@@ -361,13 +373,13 @@ void App::ReadRequest()
LogAccess();
ReadGetPostVars();
cookie_parser.Parse(request.env_http_cookie, request.cookie_tab);
accept_encoding_parser.Parse(request.env_http_accept_encoding);
cookie_parser.Parse(cur.request->env_http_cookie, cur.request->cookie_tab);
accept_encoding_parser.Parse(cur.request->env_http_accept_encoding);
CheckIE();
CheckKonqueror();
if( request.role == Request::authorizer )
if( cur.request->role == Request::authorizer )
log << log3 << "Request: fast cgi role: authorizer" << logend;
}
@@ -380,7 +392,7 @@ const char * v = FCGX_GetParam(name, fcgi_request.envp);
if( v )
env = v;
// by default env is set to an empty string (in request.Clear() method)
// by default env is set to an empty string (in cur.request->Clear() method)
}
@@ -390,41 +402,41 @@ void App::ReadEnvVariables()
// we store that values because FCGX_GetParam has O(n) complexity
// with this variables (env_*) we have O(1)
SetEnv(request.env_request_method, "REQUEST_METHOD"); // !! mozna nie uzywac tego, teraz mamy w strukturze fcgi_request
SetEnv(request.env_request_uri, "REQUEST_URI");
SetEnv(request.env_http_cookie, "HTTP_COOKIE");
SetEnv(request.env_remote_addr, "REMOTE_ADDR");
SetEnv(request.env_http_host, "HTTP_HOST");
SetEnv(request.env_http_user_agent, "HTTP_USER_AGENT");
SetEnv(request.env_fcgi_role, "FCGI_ROLE");
SetEnv(request.env_content_type, "CONTENT_TYPE");
SetEnv(request.env_http_accept_encoding,"HTTP_ACCEPT_ENCODING");
SetEnv(cur.request->env_request_method, "REQUEST_METHOD"); // !! mozna nie uzywac tego, teraz mamy w strukturze fcgi_request
SetEnv(cur.request->env_request_uri, "REQUEST_URI");
SetEnv(cur.request->env_http_cookie, "HTTP_COOKIE");
SetEnv(cur.request->env_remote_addr, "REMOTE_ADDR");
SetEnv(cur.request->env_http_host, "HTTP_HOST");
SetEnv(cur.request->env_http_user_agent, "HTTP_USER_AGENT");
SetEnv(cur.request->env_fcgi_role, "FCGI_ROLE");
SetEnv(cur.request->env_content_type, "CONTENT_TYPE");
SetEnv(cur.request->env_http_accept_encoding,"HTTP_ACCEPT_ENCODING");
}
void App::CheckRequestMethod()
{
request.method = Request::none;
cur.request->method = Request::none;
if( ToSmall(request.env_request_method[0]) == 'g' )
request.method = Request::get;
if( ToSmall(cur.request->env_request_method[0]) == 'g' )
cur.request->method = Request::get;
else
if( ToSmall(request.env_request_method[0]) == 'p' )
request.method = Request::post;
if( ToSmall(cur.request->env_request_method[0]) == 'p' )
cur.request->method = Request::post;
else
if( ToSmall(request.env_request_method[0]) == 'h' )
request.method = Request::head;
if( ToSmall(cur.request->env_request_method[0]) == 'h' )
cur.request->method = Request::head;
}
void App::CheckFCGIRole()
{
// default we assume 'responder'
request.role = Request::responder;
cur.request->role = Request::responder;
if( ToSmall(request.env_fcgi_role[0]) == 'a' )
request.role = Request::authorizer;
if( ToSmall(cur.request->env_fcgi_role[0]) == 'a' )
cur.request->role = Request::authorizer;
}
@@ -433,11 +445,11 @@ void App::LogAccess()
{
log.PutDate(log1);
log << request.env_remote_addr << ' '
<< request.env_request_method << ' '
<< request.env_http_host
<< request.env_request_uri << ' '
<< request.env_http_user_agent << logend;
log << cur.request->env_remote_addr << ' '
<< cur.request->env_request_method << ' '
<< cur.request->env_http_host
<< cur.request->env_request_uri << ' '
<< cur.request->env_http_user_agent << logend;
}
@@ -447,22 +459,22 @@ void App::ReadGetPostVars()
{
// get parameters we have always
get_parser.UTF8(config.utf8);
get_parser.Parse(request.env_request_uri, request.get_tab);
get_parser.Parse(cur.request->env_request_uri, cur.request->get_tab);
if( request.method == Request::post )
if( cur.request->method == Request::post )
{
if( IsSubStringNoCase("multipart/form-data", request.env_content_type) )
if( IsSubStringNoCase("multipart/form-data", cur.request->env_content_type) )
{
log << log3 << "Request: post content type: multipart/form-data" << logend;
// !! dodac metode UTF8 do post_multi_parsera
// (narazie bierze bezposrednio z konfigu)
// w ogole wywalic zaleznosc od konfiga
post_multi_parser.Parse(fcgi_request.in, request.post_tab, request.post_file_tab);
post_multi_parser.Parse(fcgi_request.in, cur.request->post_tab, cur.request->post_file_tab);
}
else
{
post_parser.UTF8(config.utf8);
post_parser.Parse(fcgi_request.in, request.post_tab);
post_parser.Parse(fcgi_request.in, cur.request->post_tab);
}
}
}
@@ -472,24 +484,24 @@ void App::ReadGetPostVars()
void App::CheckIE()
{
char * msie = strstr(request.env_http_user_agent, "MSIE");
char * msie = strstr(cur.request->env_http_user_agent, "MSIE");
if( msie )
request.browser_msie = true;
cur.request->browser_msie = true;
else
request.browser_msie = false;
cur.request->browser_msie = false;
}
void App::CheckKonqueror()
{
char * kon = strstr(request.env_http_user_agent, "Konqueror");
char * kon = strstr(cur.request->env_http_user_agent, "Konqueror");
if( kon )
request.browser_konqueror = true;
cur.request->browser_konqueror = true;
else
request.browser_konqueror = false;
cur.request->browser_konqueror = false;
}
@@ -499,18 +511,18 @@ void App::CheckKonqueror()
void App::PrepareSessionCookie()
{
if( !request.session || request.session->id==0 )
if( !cur.session || cur.session->id==0 )
return;
if( !request.session->puser || !request.session->remember_me )
if( !cur.session->puser || !cur.session->remember_me )
{
request.SetCookie(config.http_session_id_name.c_str(), request.session->id);
cur.request->SetCookie(config.http_session_id_name.c_str(), cur.session->id);
}
else
{
time_t t = std::time(0) + config.session_remember_max_idle;
tm expires = Time(t);
request.SetCookie(config.http_session_id_name.c_str(), request.session->id, &expires);
cur.request->SetCookie(config.http_session_id_name.c_str(), cur.session->id, &expires);
}
}
@@ -521,27 +533,27 @@ void App::SendHeaders(bool compressing, Header header)
{
PrepareSessionCookie();
if( request.send_as_attachment )
if( cur.request->send_as_attachment )
FCGX_PutS("Content-Disposition: attachment\r\n", fcgi_request.out);
if( !request.redirect_to.empty() )
if( !cur.request->redirect_to.empty() )
{
FCGX_PutS("Status: 301 Moved Permanently\r\n", fcgi_request.out);
UrlEncode(request.redirect_to, request.aredirect_to);
FCGX_FPrintF(fcgi_request.out, "Location: %s\r\n", request.aredirect_to.c_str());
log << log2 << "Redirect to: " << request.aredirect_to << logend;
UrlEncode(cur.request->redirect_to, cur.request->aredirect_to);
FCGX_FPrintF(fcgi_request.out, "Location: %s\r\n", cur.request->aredirect_to.c_str());
log << log2 << "Redirect to: " << cur.request->aredirect_to << logend;
}
else
if( !request.x_sendfile.empty() )
if( !cur.request->x_sendfile.empty() )
{
static std::string temp, temp2; // !! wrzucic gdzies to
Ezc::WideToUTF8(config.http_header_send_file, temp);
Ezc::WideToUTF8(request.x_sendfile, temp2);
Ezc::WideToUTF8(cur.request->x_sendfile, temp2);
FCGX_FPrintF(fcgi_request.out, "%s: %s\r\n", temp.c_str(), temp2.c_str());
FCGX_PutS("Status: 200 OK\r\n", fcgi_request.out);
log << log2 << "Sending file: " << request.x_sendfile << logend;
log << log2 << "Sending file: " << cur.request->x_sendfile << logend;
}
else
{
@@ -562,7 +574,7 @@ void App::SendHeaders(bool compressing, Header header)
default:
FCGX_PutS("Status: 200 OK\r\n", fcgi_request.out);
if( request.role != Request::authorizer )
if( cur.request->role != Request::authorizer )
FCGX_PutS("Content-Type: text/html\r\n", fcgi_request.out);
}
}
@@ -570,7 +582,7 @@ void App::SendHeaders(bool compressing, Header header)
if( compressing )
FCGX_PutS("Content-Encoding: deflate\r\n", fcgi_request.out);
FCGX_PutS(request.headers.CStr(), fcgi_request.out);
FCGX_PutS(cur.request->headers.CStr(), fcgi_request.out);
FCGX_PutS("\r\n", fcgi_request.out);
}
@@ -593,8 +605,8 @@ void App::FilterCompressSend(bool compressing, const std::wstring & source_ref)
{
const std::wstring * source = &source_ref;
bool raw = request.is_item && request.item.content_type == Item::ct_raw && request.status == WINIX_ERR_OK &&
request.function && (request.function == &functions.fun_cat || request.function == &functions.fun_run);
bool raw = cur.request->is_item && cur.request->item.content_type == Item::ct_raw && cur.request->status == WINIX_ERR_OK &&
cur.request->function && (cur.request->function == &functions.fun_cat || cur.request->function == &functions.fun_run);
if( config.html_filter && !raw )
{
@@ -626,11 +638,11 @@ void App::FilterCompressSend(bool compressing, const std::wstring & source_ref)
bool App::IsCompressionAllowed(const std::wstring & source)
{
return( config.compression &&
request.role == Request::responder &&
request.redirect_to.empty() &&
request.x_sendfile.empty() &&
!request.browser_msie &&
!request.browser_konqueror &&
cur.request->role == Request::responder &&
cur.request->redirect_to.empty() &&
cur.request->x_sendfile.empty() &&
!cur.request->browser_msie &&
!cur.request->browser_konqueror &&
accept_encoding_parser.AcceptDeflate() &&
source.size() >= (size_t)config.compression_page_min_size );
}
@@ -638,11 +650,11 @@ bool App::IsCompressionAllowed(const std::wstring & source)
bool App::CanSendContent(Header header)
{
if( !request.redirect_to.empty() || !request.x_sendfile.empty() )
if( !cur.request->redirect_to.empty() || !cur.request->x_sendfile.empty() )
// if there is a redirect or a file to send then we do not send a content
return false;
if( header == h_200 && request.role == Request::authorizer && request.is_item && request.item.file_type != WINIX_ITEM_FILETYPE_NONE )
if( header == h_200 && cur.request->role == Request::authorizer && cur.request->is_item && cur.request->item.file_type != WINIX_ITEM_FILETYPE_NONE )
// if there is an item and the item has 'file' storage we do not send a content
return false;
@@ -650,7 +662,7 @@ bool App::CanSendContent(Header header)
we don't have to check the HEAD method
the server (lighttpd) doesn't send the body of its own
*/
if( request.method == Request::head )
if( cur.request->method == Request::head )
return false;
return true;
@@ -661,10 +673,10 @@ void App::AddDebugInfo(std::wstring & out)
{
if( config.debug_info )
{
if( !request.debug.Empty() )
if( !cur.request->debug.Empty() )
{
out += L"\n<!--\n";
out += request.debug.Str();
out += cur.request->debug.Str();
out += L"\n-->\n";
}
}
@@ -674,10 +686,10 @@ void App::AddDebugInfo(std::wstring & out)
void App::SendAnswer()
{
const std::wstring & source = request.page.Str();
const std::wstring & source = cur.request->page.Str();
Header header = h_200;
bool compressing = IsCompressionAllowed(source);
Error status = request.status;
Error status = cur.request->status;
if( status == WINIX_ERR_NO_ITEM || status == WINIX_ERR_NO_FUNCTION || status == WINIX_ERR_UNKNOWN_PARAM )
header = h_404;
@@ -964,7 +976,7 @@ void App::WaitForThreads()
//pthread_join(signal_thread, 0);
system.notify.WaitForThread();
session_manager.WaitForGC();
session_manager.WaitForThread();
system.thumb.WaitForThread();
}
@@ -1000,7 +1012,7 @@ sigset_t set;
FCGX_ShutdownPending();
Ezc::WideToUTF8(app->config.base_url, app->url_to_fetch_on_exit);
app->system.notify.PrepareToStopThread();
app->session_manager.PrepareToStopGC();
app->session_manager.WakeUpThread();
app->system.thumb.WakeUpThread();
app->Unlock();
@@ -1033,7 +1045,7 @@ sigset_t set;
system.notify.StartThread();
// gc for sessions
session_manager.StartGC();
session_manager.StartThread();
// thumbnails
system.thumb.StartThread();