123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583(* Ocsigen
* http://www.ocsigen.org
* Module eliomsessiongroups.ml
* Copyright (C) 2007 Vincent Balat
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)openEliom_libletmake_full_named_group_name_~cookie_levelsitedatag=Eliom_common.get_site_dir_stringsitedata,cookie_level,Leftgletmake_full_group_name~cookie_levelrisite_dir_stringipv4maskipv6mask=function(* The scope is the scope of group members (`Session by default). *)|None->(site_dir_string,cookie_level,Right(Eliom_common.network_of_ip(Ocsigen_request.remote_ip_parsedri)ipv4maskipv6mask))|Someg->site_dir_string,cookie_level,Leftgletmake_persistent_full_group_name=Eliom_common.make_persistent_full_group_nameletgetsessgrpa=aletgetperssessgrp=Eliom_common.getperssessgrpmoduletypeMEMTAB=sigtypegroup_of_group_datavaladd:?set_max:int->Eliom_common.sitedata->string->Eliom_common.cookie_levelEliom_common.sessgrp->stringOcsigen_cache.Dlist.nodevalremove:'aOcsigen_cache.Dlist.node->unitvalremove_group:Eliom_common.cookie_levelEliom_common.sessgrp->unitvalfind:[<Eliom_common.cookie_level]Eliom_common.sessgrp->stringOcsigen_cache.Dlist.t(** returns the dlist containing all session group elements *)valfind_node_in_group_of_groups:Eliom_common.cookie_levelEliom_common.sessgrp->group_of_group_dataoption(** Groups of browser sessions belongs to a group of groups.
As these groups are not associated to a cookie,
we put this information here. *)valmove:?set_max:int->Eliom_common.sitedata->stringOcsigen_cache.Dlist.node->Eliom_common.cookie_levelEliom_common.sessgrp->stringOcsigen_cache.Dlist.nodevalup:stringOcsigen_cache.Dlist.node->unitvalnb_of_groups:unit->intvalgroup_size:Eliom_common.cookie_levelEliom_common.sessgrp->intvalset_max:'aOcsigen_cache.Dlist.node->int->unitendmoduleGroupTable=Hashtbl.Make(structtypet=Eliom_common.cookie_levelEliom_common.sessgrpletequal=(=)lethash=Hashtbl.hashend)moduleMake(A:sigtypegroup_of_group_datavaltable:(group_of_group_dataoption*stringOcsigen_cache.Dlist.t)GroupTable.tvalclose_session:Eliom_common.sitedata->string->unitvalmax_tab_per_session:Eliom_common.sitedata->intvalmax_session_per_group:Eliom_common.sitedata->intvalmax_session_per_ip:Eliom_common.sitedata->intvalclean_session:Eliom_common.sitedata->GroupTable.key->(GroupTable.key->group_of_group_dataoption)->(stringOcsigen_cache.Dlist.node->unit)->(group_of_group_data->unit)->unitvalnode_of_group_of_group_data:group_of_group_data->[`Session]Eliom_common.sessgrpOcsigen_cache.Dlist.nodevalcreate_group_of_group_data:Eliom_common.sitedata->[`Session]Eliom_common.sessgrpOcsigen_cache.Dlist.node->group_of_group_dataend):MEMTABwithtypegroup_of_group_data=A.group_of_group_data=structtypegroup_of_group_data=A.group_of_group_dataletgrouptable=A.tableletfindg=snd(GroupTable.findgrouptableg)letfind_node_in_group_of_groupsg=tryfst(GroupTable.findgrouptableg)withNot_found->Noneletremovenode=Ocsigen_cache.Dlist.removenodeletremove_groupsess_grp=tryletcl=findsess_grpinletrecclose_allcl=matchOcsigen_cache.Dlist.oldestclwith|Somenode->Ocsigen_cache.Dlist.removenode;close_allcl|None->()inclose_allcl(* will remove the group using finaliser *)withNot_found->()letremove_if_emptysitedatasess_grpcl=ifOcsigen_cache.Dlist.sizecl=0(* finaliser after *)then(A.clean_sessionsitedatasess_grpfind_node_in_group_of_groupsremove(funn->remove(A.node_of_group_of_group_datan));GroupTable.removegrouptablesess_grp)letget_cl?set_maxsitedatasess_grp=tryletcl=findsess_grpin(matchset_maxwith|None->()|Somev->ignore(Ocsigen_cache.Dlist.set_maxsizeclv));clwithNot_found->(* We create a group *)letsize=matchset_max,sess_grpwith|None,(_,`Session,Left_)->A.max_session_per_groupsitedata|None,(_,`Client_process,Left_)->A.max_tab_per_sessionsitedata|None,(_,`Session,Right_)->A.max_session_per_ipsitedata|None,_->assertfalse|Somev,_->vinletcookie_level=Tuple3.sndsess_grpinletcl=Ocsigen_cache.Dlist.createsizeinOcsigen_cache.Dlist.set_finaliser_after(funnode->(* Finaliser of sessions and client processes *)letname=Ocsigen_cache.Dlist.valuenodein(* First we close all subsessions
(that is, all sessions in the group associated to the session) *)(matchcookie_levelwith(* | `Session_group -> assert false
As there is no table of groups of groups
(only one group of groups for each site),
the finaliser for these groups is created in eliommod.ml *)|`Session(* We are closing a browser session *)->(* First we close all tab sessions in the session (subgrp): *)letsubgrp=make_full_named_group_name_~cookie_level:`Client_processsitedatanameinremove_groupsubgrp|`Client_process(* We are closing a tab session *)->());(* Then we close all session tables: *)A.close_sessionsitedataname;(* If the dlist is empty, we remove it from the group table
(and possibly close the group itself): *)remove_if_emptysitedatasess_grpcl)cl;letnode_in_group_of_group=matchcookie_levelwith|`Session->ignore(Ocsigen_cache.Dlist.addsess_grpsitedata.Eliom_common.group_of_groups);Ocsigen_cache.Dlist.newestsitedata.Eliom_common.group_of_groups|_->Noneinletgroup_of_group_data=Option.map(A.create_group_of_group_datasitedata)node_in_group_of_groupinGroupTable.addgrouptablesess_grp(group_of_group_data,cl);clletadd?set_maxsitedatasess_idsess_grp=letcl=get_cl?set_maxsitedatasess_grpinignore(Ocsigen_cache.Dlist.addsess_idcl);matchOcsigen_cache.Dlist.newestclwith|Somev->v|None->assertfalseletupnode=Ocsigen_cache.Dlist.upnodeletmove?set_maxsitedatanodesess_grp=(* if set_max <> None || grp1 <> grp2 then begin *)letcl=get_cl?set_maxsitedatasess_grpinignore(Ocsigen_cache.Dlist.movenodecl);matchOcsigen_cache.Dlist.newestclwith|Somev->v|None->assertfalse(* end
else [] *)letnb_of_groups()=GroupTable.lengthgrouptableletgroup_sizesess_grp=tryletcl=findsess_grpinOcsigen_cache.Dlist.sizeclwithNot_found->0letset_maxnodei=matchOcsigen_cache.Dlist.list_ofnodewith|None->()|Somecl->ignore(Ocsigen_cache.Dlist.set_maxsizecli)endmoduleData=Make(structtypegroup_of_group_data=[`Session]Eliom_common.sessgrpOcsigen_cache.Dlist.nodelettable:(group_of_group_dataoption*stringOcsigen_cache.Dlist.t)GroupTable.t=(* The table associates the dlist for a group
to a full session group name.
It work both for groups of tab sessions and
groups of browser sessions.
For groups of groups, we do not need that table,
as there is only one group of groups for each site
(the dlist is found in sitedata).
The dlist is automatically removed from the table
when it becomes empty, using the finaliser of nodes.
In the case of groups of browser sessions,
the session group is also associated to a node
which corresponds to the node of that group in the group
of groups (one group of groups for each site).
*)GroupTable.create100letclose_sessionsitedatasess_id=Eliom_common.SessionCookies.removesitedata.Eliom_common.session_datasess_id;(* iterate on all session data tables: *)sitedata.Eliom_common.remove_session_datasess_id(* see also in eliommod.ml if you modify this *)letmax_tab_per_sessionsitedata=fstsitedata.Eliom_common.max_volatile_data_tab_sessions_per_groupletmax_session_per_groupsitedata=fstsitedata.Eliom_common.max_volatile_data_sessions_per_groupletmax_session_per_ipsitedata=fstsitedata.Eliom_common.max_volatile_data_sessions_per_subnetletclean_sessionsitedatasess_grpfind_node_in_group_of_groupsremove1remove2=(* We removed the last session from a group.
Do we want to close the group completely?
- For volatile browser sessions, yes.
We do not keep group data when there is no session in the group.
We remove the group of groups from the site dlist.
-- Vincent 2011/08: This is not coherent with persistent group data!
But as I do not see any correct use of volatile group data for now.
And there is a risk of memory leak if we keep them.
Besides, volatile sessions are (hopefully) going to disappear soon.
- For tab sessions, yes if the browser cookie is not
bound in tables and is not in a group (like in Eliommod_gc)
(means that we do not use the browser session).
*)(*VVV See also in Eliommod_gc and
Eliom_state.close_volatile_session_if_empty.
Should we use this function here?
*)(*VVV remove is not polymorphic enough -> remove1 remove2 *)match(sess_grp:GroupTable.key)with|_,`Client_process,Leftsess_id->(trylet{Eliom_common.Data_cookie.session_group;session_group_node;_}=Eliom_common.SessionCookies.findsitedata.Eliom_common.session_datasess_idinmatch!session_groupwith|_,`Session,Right_(* no group *)whensitedata.Eliom_common.not_bound_in_data_tablessess_id->remove1session_group_node|_->()withNot_found->())|_,`Session,_->(matchfind_node_in_group_of_groupssess_grpwith|Somenode->remove2node|None->())|_->()letnode_of_group_of_group_datax=xletcreate_group_of_group_data_x=xend)moduleServ=Make(structtypegroup_of_group_data=Eliom_common.tablesref*[`Session]Eliom_common.sessgrpOcsigen_cache.Dlist.nodelettable:(group_of_group_dataoption*stringOcsigen_cache.Dlist.t)GroupTable.t=GroupTable.create100letclose_sessionsitedatasess_id=Eliom_common.SessionCookies.removesitedata.Eliom_common.session_servicessess_idletmax_tab_per_sessionsitedata=fstsitedata.Eliom_common.max_service_tab_sessions_per_groupletmax_session_per_groupsitedata=fstsitedata.Eliom_common.max_service_sessions_per_groupletmax_session_per_ipsitedata=fstsitedata.Eliom_common.max_service_sessions_per_subnetletclean_sessionsitedatasess_grpfind_node_in_group_of_groupsremove1remove2=(* We removed the last session from a group.
Do we want to close the group completely?
- For volatile browser sessions, yes.
We do not keep group data when there is no session in the group.
We remove the group of groups from the site dlist.
-- Vincent 2011/08: This is not coherent with persistent group data!
But as I do not see any correct use of volatile group data for now.
And there is a risk of memory leak if we keep them.
Besides, volatile sessions are (hopefully) going to disappear soon.
- For tab sessions, yes if there are no session services
in the browser service table.
(means that we do not use the browser session).
*)(*VVV We close even if browser session is in a group.
It is not coherent with data sessions. *)(*VVV See also in Eliommod_gc and
Eliom_state.close_service_session_if_empty.
Should we use this function here?
*)(*VVV remove is not polymorphic enough -> remove1 remove2 *)match(sess_grp:GroupTable.key)with|_,`Client_process,Leftsess_id->(trylet{Eliom_common.Service_cookie.session_table=tables;session_group_node;_}=Eliom_common.SessionCookies.findsitedata.Eliom_common.session_servicessess_idinifEliom_common.service_tables_are_emptytablesthenremove1session_group_nodewithNot_found->())|_,`Session,_->(matchfind_node_in_group_of_groupssess_grpwith|Somenode->remove2node|None->())|_->()letnode_of_group_of_group_data=sndletcreate_group_of_group_datasitedatax=ref(Eliom_common.new_service_session_tablessitedata),x(*VVV Check when the table is collected *)end)typenbmax=Valofint|Default|Nolimitletcutnl=letrecauxn=function|[]->[],[]|lwhenn<=1->[],l|a::l->letl1,l2=aux(n-1)lina::l1,l2inmatchnwithNone->l,[](* no limitation *)|Somen->auxnlmodulePers=struct(*VVV Verify this carefully! *)(*VVV VERIFY concurrent access *)moduleOcsipersist=Eliom_common.Ocsipersist.Polymorphicletgrouptable:(nbmax*stringlist)Ocsipersist.tableLwt.tLazy.t=lazy(Ocsipersist.open_table"__eliom_session_group_table")(* It is lazy because if the module is linked statically,
the creation of the table must happen after initialisation
of ocsipersist (after reading the configuration file to know
the location of the table) *)letfindg=matchgwith|None->Lwt.return_nil|Someg->Lwt.catch(fun()->!!grouptable>>=fungrouptable->Ocsipersist.findgrouptable(Eliom_common.string_of_perssessgrpg)>>=fun(_,a)->Lwt.returna)(functionNot_found->Lwt.return_nil|e->Lwt.faile)letadd?set_maxdefaultmaxsess_idsess_grp=matchsess_grpwith|Somesg->letsg=Eliom_common.string_of_perssessgrpsginLwt.catch(fun()->!!grouptable>>=fungrouptable->Ocsipersist.findgrouptablesg>>=fun(max2,cl)->letmax,newmax=matchset_maxwith|None->((matchmax2with|Default->defaultmax|Nolimit->None|Valm->Somem),max2)|SomeNone->None,Nolimit|Some(Somev)->Somev,Valvinletcl,toclose=cutmaxclinOcsipersist.replace_if_existsgrouptablesg(newmax,sess_id::cl)>>=fun()->Lwt.returntoclose)(function|Not_found->letmax=matchset_maxwith|None->Default|SomeNone->Nolimit|Some(Somev)->Valvin!!grouptable>>=fungrouptable->Ocsipersist.addgrouptablesg(max,[sess_id])>>=fun()->Lwt.return_nil|e->Lwt.faile)|None->Lwt.return_nilletrecremove_group~cookie_levelsitedatasess_grp=(* cookie_level is the scope of group members *)(*VVV NEW 201007 closing all sessions in the group and removing group data *)(*VVV VERIFY concurrent access *)(*VVV Check this carefully!!!! Verify the order of actions. *)Lwt.catch(fun()->(* First we close all sessions in the group *)findsess_grp>>=funcl->Lwt_list.iter_p(close_persistent_session2~cookie_level:(matchcookie_levelwith|`Client_process_->`Client_process|`Session->`Session)sitedataNone)cl(* None because we will close the group *)>>=fun()->(* Then, we remove group data: *)(matchsess_grpwith|None->Lwt.return_unit|Somesg->(matchEliom_common.getperssessgrpsgwith|_,_,Right_->(* No group has been set. No group table.
Data associated to default (automatic) groups
is removed when closing associated sessions. *)Lwt.return_unit|_,_,Leftgroup_name->(Eliom_common.Persistent_tables.remove_key_from_all_tablesgroup_name>>=fun()->(* If it is associated to a session,
we remove the session from its group,
and we remove cookie info: *)matchcookie_levelwith|`Client_processgrp->(* We are closing a browser session,
belonging to the group grp *)(* group_name is the cookie value *)removesitedatagroup_namegrp|_->Lwt.return_unit)))>>=fun()->(* Then, we remove group from group table: *)matchsess_grpwith|Somesg->letsg=Eliom_common.string_of_perssessgrpsgin!!grouptable>>=fungrouptable->Ocsipersist.removegrouptablesg|None->Lwt.return_unit)(functionNot_found->Lwt.return_unit|e->Lwt.faile)(* close a persistent session (tab or browser)
and the associated group (if browser session) by cookie value *)andclose_persistent_session2~cookie_levelsitedatafullsessgrpcookie=(*VVV Check this carefully!!!! *)(*VVV Optimize the number of marshal/unmarshal (getperssessgrp) *)Lwt.catch(fun()->matchcookie_levelwith|`Client_process->(* We remove the session from its group: *)removesitedatacookiefullsessgrp>>=fun()->(* Then, we remove session data: *)Eliom_common.Persistent_tables.remove_key_from_all_tablescookie|`Session->remove_group~cookie_level:(`Client_processfullsessgrp)sitedata(Eliom_common.make_persistent_full_group_name~cookie_level:`Client_process(Eliom_common.get_site_dir_stringsitedata)(Somecookie)))(functionNot_found->Lwt.return_unit|e->Lwt.faile)andremove_sitedatasess_idsess_grp=matchsess_grpwith|Somesg0->letsg=Eliom_common.string_of_perssessgrpsg0inLwt.catch(fun()->!!grouptable>>=fungrouptable->Ocsipersist.findgrouptablesg>>=fun(max,cl)->letnewcl=List.remove_first_if_anysess_idclin(* Before 2018-10-18, we were closing the session group
when newcl was empty (no more session in the group).
But persistent session groups are usually used to store persistent
information about users. It makes no sense cleaning this
information when user closes all their sessions.
I remove this. -- Vincent
*)Ocsipersist.replace_if_existsgrouptablesg(max,newcl))(functionNot_found->Lwt.return_unit|e->Lwt.faile)|None->Lwt.return_unitletupsess_idgrp=matchgrpwith|None->Lwt.return_unit|Somesg->letsg=Eliom_common.string_of_perssessgrpsginLwt.catch(fun()->!!grouptable>>=fungrouptable->Ocsipersist.findgrouptablesg>>=fun(max,cl)->letnewcl=List.remove_first_if_anysess_idclinOcsipersist.replace_if_existsgrouptablesg(max,sess_id::newcl))(functionNot_found->Lwt.return_unit|e->Lwt.faile)letmovesitedata?set_maxmaxsess_idgrp1grp2=ifset_max<>None||grp1<>grp2thenremovesitedatasess_idgrp1>>=fun()->add?set_maxmaxsess_idgrp2elseLwt.return_nilletnb_of_groups()=!!grouptable>>=Ocsipersist.lengthend