fix: set correctly request->session

The session cookie was not created because request->session pointed at
a temporary session.

While here:
- do some cleaning in App
- set request->mount too
- add fcgi_cannot_create_request_delay to the config
- fix: in mailregister plugin get model_connector from env (and not from the request)
This commit is contained in:
Tomasz Sowa 2022-07-30 18:37:50 +02:00
parent 4b46ba1fe3
commit 968660e8ca
7 changed files with 43 additions and 48 deletions

View File

@ -98,7 +98,7 @@ App::App()
config.SetFileLog(&file_log);
config.SetLogBuffer(&log_buffer);
cur.request = nullptr;
cur.request = nullptr; // they are first set in App::Init()
cur.session = nullptr;
cur.mount = nullptr;
@ -352,6 +352,12 @@ bool App::InitializePlugins()
bool App::Init()
{
cur.session = session_manager.GetTmpSession();
cur.mount = system.mounts.GetEmptyMount();;
system.req_tab.resize(1);
cur.request = &system.req_tab.back();
InitializeNewRequest(*cur.request); // this will set cur.request->session (from cur.session) and cur.request->mount (from cur.mount)
if( !config.db_conn_string.empty() )
postgresql_connector.set_conn_param(config.db_conn_string);
else
@ -617,9 +623,10 @@ void App::MakeRenameMeToABetterName()
cur.request->item.set_connector(model_connector);
cur.session = session_manager.PrepareSession();
cur.request->session = cur.session;
model_connector.set_winix_session(cur.session);
functions.CheckFunctionAndSymlink(); // here a function can be changed
functions.CheckFunctionAndSymlink(); // here a function can be changed (and mount point but cur.mount and cur->request->mount will be set)
if( cur.request->function )
{
@ -628,6 +635,7 @@ void App::MakeRenameMeToABetterName()
}
cur.session = session_manager.CheckIfFunctionRequireSession();
cur.request->session = cur.session;
model_connector.set_winix_session(cur.session);
SetLocale();
@ -736,6 +744,7 @@ void App::ProcessRequestThrow()
}
cur.mount = system.mounts.CalcCurMount(cur.request);
cur.request->mount = cur.mount;
if( cur.mount->type != system.mounts.MountTypeStatic() )
{
@ -811,20 +820,20 @@ void App::ClearAfterRequest()
}
bool App::InitializeRequestForFastCGI()
bool App::InitializeRequestForFastCGI(Request & request)
{
bool request_initialized = false;
while( !synchro.was_stop_signal && !request_initialized )
{
if( FCGX_InitRequest(&cur.request->fcgi_request, fcgi_socket, FCGI_FAIL_ACCEPT_ON_INTR) == 0 )
if( FCGX_InitRequest(&request.fcgi_request, fcgi_socket, FCGI_FAIL_ACCEPT_ON_INTR) == 0 )
{
request_initialized = true;
}
else
{
log << log1 << "App: FCGX_InitRequest fails, I cannot read a new request, I wait 3s and will try again..." << logend << logsave;
sleep(3); // IMPROVEME put me to config
sleep(config.fcgi_cannot_create_request_delay);
}
}
@ -832,44 +841,30 @@ bool App::InitializeRequestForFastCGI()
}
void App::SetRequestDependency()
void App::SetRequestDependency(Request & request)
{
cur.request->SetConfig(&config);
cur.request->SetTemplates(&templates);
cur.request->SetCompress(&compress);
cur.request->SetPlugin(&plugin);
cur.request->SetMounts(&system.mounts);
request.SetConfig(&config);
request.SetTemplates(&templates);
request.SetCompress(&compress);
request.SetPlugin(&plugin);
request.SetMounts(&system.mounts);
cur.request->session = cur.session;
cur.request->mount = cur.mount;
request.session = cur.session;
request.mount = cur.mount;
cur.request->set_connector(&model_connector);
cur.request->item.set_connector(&model_connector);
request.set_connector(&model_connector);
request.item.set_connector(&model_connector);
}
void App::InitializeNewRequest()
void App::InitializeNewRequest(Request & request)
{
SetRequestDependency();
cur.request->Clear(); // IMPROVE ME what about an 'id' in request? it should be unique?
InitializeRequestForFastCGI();
SetRequestDependency(request);
request.Clear();
InitializeRequestForFastCGI(request);
}
// use with lock/unlock
void App::PrepareRequest()
{
Winix::Lock lock(synchro);
if( system.req_tab.empty() )
{
system.req_tab.resize(1);
}
cur.request = &system.req_tab.back();
InitializeNewRequest();
}
void App::PutRequestToJob()
{
@ -903,12 +898,6 @@ void App::Start()
{
Winix::Lock lock(synchro);
was_stop_signal = synchro.was_stop_signal;
}
PrepareRequest();
{
Winix::Lock lock(synchro);
fcgi_request = &cur.request->fcgi_request;
}
@ -960,7 +949,7 @@ void App::Start()
*/
system.req_tab.resize(system.req_tab.size() + 1);
cur.request = &system.req_tab.back();
InitializeNewRequest();
InitializeNewRequest(*cur.request); // cur.request->session, cur.request->mount, cur.request->id etc. are set when the request starts
}
}

View File

@ -76,10 +76,9 @@ public:
Log & GetMainLog();
bool InitializePlugins();
bool Init();
bool InitializeRequestForFastCGI();
void SetRequestDependency();
void InitializeNewRequest();
void PrepareRequest();
bool InitializeRequestForFastCGI(Request & request);
void SetRequestDependency(Request & request);
void InitializeNewRequest(Request & request);
void PutRequestToJob();
void Start();
void Close();

View File

@ -136,6 +136,7 @@ void Config::AssignValues()
fcgi_socket_user = Text(L"fcgi_socket_user");
fcgi_socket_group = Text(L"fcgi_socket_group");
fcgi_socket_listen = Int(L"fcgi_socket_listen", 1024);
fcgi_cannot_create_request_delay = Size(L"fcgi_cannot_create_request_delay", 3);
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);

View File

@ -180,6 +180,12 @@ public:
// default: 100
int fcgi_socket_listen;
// in order to read a request we need to properly initialize a FastCGI structure by
// using FCGX_InitRequest function, if FCGX_InitRequest fails then we wait fcgi_cannot_create_request_delay
// seconds and trying again
// default: 3
size_t fcgi_cannot_create_request_delay;
std::wstring templates_dir;
std::wstring templates_dir_default; // html templates from winix

View File

@ -1445,7 +1445,8 @@ bool System::FollowAllLinks(const std::wstring & link_to,
log << log3 << "System: current directory changed and the new file loaded" << logend;
}
mounts.CalcCurMount(cur->request);
cur->mount = mounts.CalcCurMount(cur->request);
cur->request->mount = cur->mount;
}
else
{

View File

@ -172,6 +172,7 @@ Error Functions::CheckSpecialFile(const Item & item)
system->mounts.ReadMounts(item.item_content.content_raw);
system->mounts.CalcCurMount(cur->request);
cur->mount = system->mounts.pmount;
cur->request->mount = cur->mount;
templates->ReadNewIndexTemplates();
templates->ReadNewChangeTemplates();

View File

@ -123,9 +123,7 @@ void ConfigReload(PluginInfo & info)
// iq.SetAll(true, false);
// iq.WhereParentId(dir->id);
// CHECKME is it correct to get connector from the last item?
morm::ModelConnector * model_connector = info.cur->request->last_item->get_connector();
morm::Finder<Item> finder(model_connector);
morm::Finder<Item> finder(info.model_connector);
finder.
select().