Source file eliommod_sessiongroups.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
open Eliom_lib
let make_full_named_group_name_ ~cookie_level sitedata g =
Eliom_common.get_site_dir_string sitedata, cookie_level, Left g
let make_full_group_name ~cookie_level ri site_dir_string ipv4mask ipv6mask
= function
| None ->
( site_dir_string
, cookie_level
, Right
(Eliom_common.network_of_ip
(Ocsigen_request.remote_ip_parsed ri)
ipv4mask ipv6mask) )
| Some g -> site_dir_string, cookie_level, Left g
let make_persistent_full_group_name =
Eliom_common.make_persistent_full_group_name
let getsessgrp a = a
let = Eliom_common.getperssessgrp
module type MEMTAB = sig
type group_of_group_data
val add :
?set_max:int
-> Eliom_common.sitedata
-> string
-> Eliom_common.cookie_level Eliom_common.sessgrp
-> string Ocsigen_cache.Dlist.node
val remove : 'a Ocsigen_cache.Dlist.node -> unit
val remove_group : Eliom_common.cookie_level Eliom_common.sessgrp -> unit
val find :
[< Eliom_common.cookie_level] Eliom_common.sessgrp
-> string Ocsigen_cache.Dlist.t
(** returns the dlist containing all session group elements *)
val find_node_in_group_of_groups :
Eliom_common.cookie_level Eliom_common.sessgrp
-> group_of_group_data option
(** Groups of browser sessions belongs to a group of groups.
As these groups are not associated to a cookie,
we put this information here. *)
val move :
?set_max:int
-> Eliom_common.sitedata
-> string Ocsigen_cache.Dlist.node
-> Eliom_common.cookie_level Eliom_common.sessgrp
-> string Ocsigen_cache.Dlist.node
val up : string Ocsigen_cache.Dlist.node -> unit
val nb_of_groups : unit -> int
val group_size : Eliom_common.cookie_level Eliom_common.sessgrp -> int
val set_max : 'a Ocsigen_cache.Dlist.node -> int -> unit
end
module GroupTable = Hashtbl.Make (struct
type t = Eliom_common.cookie_level Eliom_common.sessgrp
let equal = ( = )
let hash = Hashtbl.hash
end)
module Make (A : sig
type group_of_group_data
val table :
(group_of_group_data option * string Ocsigen_cache.Dlist.t) GroupTable.t
val close_session : Eliom_common.sitedata -> string -> unit
val max_tab_per_session : Eliom_common.sitedata -> int
val max_session_per_group : Eliom_common.sitedata -> int
val max_session_per_ip : Eliom_common.sitedata -> int
val clean_session :
Eliom_common.sitedata
-> GroupTable.key
-> (GroupTable.key -> group_of_group_data option)
-> (string Ocsigen_cache.Dlist.node -> unit)
-> (group_of_group_data -> unit)
-> unit
val node_of_group_of_group_data :
group_of_group_data
-> [`Session] Eliom_common.sessgrp Ocsigen_cache.Dlist.node
val create_group_of_group_data :
Eliom_common.sitedata
-> [`Session] Eliom_common.sessgrp Ocsigen_cache.Dlist.node
-> group_of_group_data
end) : MEMTAB with type group_of_group_data = A.group_of_group_data = struct
type group_of_group_data = A.group_of_group_data
let grouptable = A.table
let find g = snd (GroupTable.find grouptable g)
let find_node_in_group_of_groups g =
try fst (GroupTable.find grouptable g) with Not_found -> None
let remove node = Ocsigen_cache.Dlist.remove node
let remove_group sess_grp =
try
let cl = find sess_grp in
let rec close_all cl =
match Ocsigen_cache.Dlist.oldest cl with
| Some node ->
Ocsigen_cache.Dlist.remove node;
close_all cl
| None -> ()
in
close_all cl
with Not_found -> ()
let remove_if_empty sitedata sess_grp cl =
if Ocsigen_cache.Dlist.size cl = 0
then (
A.clean_session sitedata sess_grp find_node_in_group_of_groups remove
(fun n -> remove (A.node_of_group_of_group_data n));
GroupTable.remove grouptable sess_grp)
let get_cl ?set_max sitedata sess_grp =
try
let cl = find sess_grp in
(match set_max with
| None -> ()
| Some v -> ignore (Ocsigen_cache.Dlist.set_maxsize cl v));
cl
with Not_found ->
let size =
match set_max, sess_grp with
| None, (_, `Session, Left _) -> A.max_session_per_group sitedata
| None, (_, `Client_process, Left _) -> A.max_tab_per_session sitedata
| None, (_, `Session, Right _) -> A.max_session_per_ip sitedata
| None, _ -> assert false
| Some v, _ -> v
in
let cookie_level = Tuple3.snd sess_grp in
let cl = Ocsigen_cache.Dlist.create size in
Ocsigen_cache.Dlist.set_finaliser_after
(fun node ->
let name = Ocsigen_cache.Dlist.value node in
(match cookie_level with
| `Session ->
let subgrp =
make_full_named_group_name_ ~cookie_level:`Client_process
sitedata name
in
remove_group subgrp
| `Client_process -> ());
A.close_session sitedata name;
remove_if_empty sitedata sess_grp cl)
cl;
let node_in_group_of_group =
match cookie_level with
| `Session ->
ignore
(Ocsigen_cache.Dlist.add sess_grp
sitedata.Eliom_common.group_of_groups);
Ocsigen_cache.Dlist.newest sitedata.Eliom_common.group_of_groups
| _ -> None
in
let group_of_group_data =
Option.map
(A.create_group_of_group_data sitedata)
node_in_group_of_group
in
GroupTable.add grouptable sess_grp (group_of_group_data, cl);
cl
let add ?set_max sitedata sess_id sess_grp =
let cl = get_cl ?set_max sitedata sess_grp in
ignore (Ocsigen_cache.Dlist.add sess_id cl);
match Ocsigen_cache.Dlist.newest cl with
| Some v -> v
| None -> assert false
let up node = Ocsigen_cache.Dlist.up node
let move ?set_max sitedata node sess_grp =
let cl = get_cl ?set_max sitedata sess_grp in
ignore (Ocsigen_cache.Dlist.move node cl);
match Ocsigen_cache.Dlist.newest cl with
| Some v -> v
| None -> assert false
let nb_of_groups () = GroupTable.length grouptable
let group_size sess_grp =
try
let cl = find sess_grp in
Ocsigen_cache.Dlist.size cl
with Not_found -> 0
let set_max node i =
match Ocsigen_cache.Dlist.list_of node with
| None -> ()
| Some cl -> ignore (Ocsigen_cache.Dlist.set_maxsize cl i)
end
module Data = Make (struct
type group_of_group_data =
[`Session] Eliom_common.sessgrp Ocsigen_cache.Dlist.node
let table :
(group_of_group_data option * string Ocsigen_cache.Dlist.t) GroupTable.t
=
GroupTable.create 100
let close_session sitedata sess_id =
Eliom_common.SessionCookies.remove sitedata.Eliom_common.session_data
sess_id;
sitedata.Eliom_common.remove_session_data sess_id
let max_tab_per_session sitedata =
fst sitedata.Eliom_common.max_volatile_data_tab_sessions_per_group
let max_session_per_group sitedata =
fst sitedata.Eliom_common.max_volatile_data_sessions_per_group
let max_session_per_ip sitedata =
fst sitedata.Eliom_common.max_volatile_data_sessions_per_subnet
let clean_session sitedata sess_grp find_node_in_group_of_groups remove1
remove2
=
match (sess_grp : GroupTable.key) with
| _, `Client_process, Left sess_id -> (
try
let {Eliom_common.Data_cookie.session_group; session_group_node; _} =
Eliom_common.SessionCookies.find sitedata.Eliom_common.session_data
sess_id
in
match !session_group with
| _, `Session, Right _
when sitedata.Eliom_common.not_bound_in_data_tables sess_id ->
remove1 session_group_node
| _ -> ()
with Not_found -> ())
| _, `Session, _ -> (
match find_node_in_group_of_groups sess_grp with
| Some node -> remove2 node
| None -> ())
| _ -> ()
let node_of_group_of_group_data x = x
let create_group_of_group_data _ x = x
end)
module Serv = Make (struct
type group_of_group_data =
Eliom_common.tables ref
* [`Session] Eliom_common.sessgrp Ocsigen_cache.Dlist.node
let table :
(group_of_group_data option * string Ocsigen_cache.Dlist.t) GroupTable.t
=
GroupTable.create 100
let close_session sitedata sess_id =
Eliom_common.SessionCookies.remove sitedata.Eliom_common.session_services
sess_id
let max_tab_per_session sitedata =
fst sitedata.Eliom_common.max_service_tab_sessions_per_group
let max_session_per_group sitedata =
fst sitedata.Eliom_common.max_service_sessions_per_group
let max_session_per_ip sitedata =
fst sitedata.Eliom_common.max_service_sessions_per_subnet
let clean_session sitedata sess_grp find_node_in_group_of_groups remove1
remove2
=
match (sess_grp : GroupTable.key) with
| _, `Client_process, Left sess_id -> (
try
let { Eliom_common.Service_cookie.session_table = tables
; session_group_node
; _ }
=
Eliom_common.SessionCookies.find
sitedata.Eliom_common.session_services sess_id
in
if Eliom_common.service_tables_are_empty tables
then remove1 session_group_node
with Not_found -> ())
| _, `Session, _ -> (
match find_node_in_group_of_groups sess_grp with
| Some node -> remove2 node
| None -> ())
| _ -> ()
let node_of_group_of_group_data = snd
let create_group_of_group_data sitedata x =
ref (Eliom_common.new_service_session_tables sitedata), x
end)
type nbmax = Val of int | Default | Nolimit
let cut n l =
let rec aux n = function
| [] -> [], []
| l when n <= 1 -> [], l
| a :: l ->
let l1, l2 = aux (n - 1) l in
a :: l1, l2
in
match n with None -> l, [] | Some n -> aux n l
module Pers = struct
module Ocsipersist = Eliom_common.Ocsipersist.Polymorphic
let grouptable : (nbmax * string list) Ocsipersist.table Lwt.t Lazy.t =
lazy (Ocsipersist.open_table "__eliom_session_group_table")
let find g =
match g with
| None -> Lwt.return_nil
| Some g ->
Lwt.catch
(fun () ->
!!grouptable >>= fun grouptable ->
Ocsipersist.find grouptable (Eliom_common.string_of_perssessgrp g)
>>= fun (_, a) -> Lwt.return a)
(function Not_found -> Lwt.return_nil | e -> Lwt.fail e)
let add ?set_max defaultmax sess_id sess_grp =
match sess_grp with
| Some sg ->
let sg = Eliom_common.string_of_perssessgrp sg in
Lwt.catch
(fun () ->
!!grouptable >>= fun grouptable ->
Ocsipersist.find grouptable sg >>= fun (max2, cl) ->
let max, newmax =
match set_max with
| None ->
( (match max2 with
| Default -> defaultmax
| Nolimit -> None
| Val m -> Some m)
, max2 )
| Some None -> None, Nolimit
| Some (Some v) -> Some v, Val v
in
let cl, toclose = cut max cl in
Ocsipersist.replace_if_exists grouptable sg (newmax, sess_id :: cl)
>>= fun () -> Lwt.return toclose)
(function
| Not_found ->
let max =
match set_max with
| None -> Default
| Some None -> Nolimit
| Some (Some v) -> Val v
in
!!grouptable >>= fun grouptable ->
Ocsipersist.add grouptable sg (max, [sess_id]) >>= fun () ->
Lwt.return_nil
| e -> Lwt.fail e)
| None -> Lwt.return_nil
let rec remove_group ~cookie_level sitedata sess_grp =
Lwt.catch
(fun () ->
find sess_grp >>= fun cl ->
Lwt_list.iter_p
(close_persistent_session2
~cookie_level:
(match cookie_level with
| `Client_process _ -> `Client_process
| `Session -> `Session)
sitedata None)
cl
>>= fun () ->
(match sess_grp with
| None -> Lwt.return_unit
| Some sg -> (
match Eliom_common.getperssessgrp sg with
| _, _, Right _ ->
Lwt.return_unit
| _, _, Left group_name -> (
Eliom_common.Persistent_tables.remove_key_from_all_tables
group_name
>>= fun () ->
match cookie_level with
| `Client_process grp ->
remove sitedata group_name grp
| _ -> Lwt.return_unit)))
>>= fun () ->
match sess_grp with
| Some sg ->
let sg = Eliom_common.string_of_perssessgrp sg in
!!grouptable >>= fun grouptable -> Ocsipersist.remove grouptable sg
| None -> Lwt.return_unit)
(function Not_found -> Lwt.return_unit | e -> Lwt.fail e)
and close_persistent_session2 ~cookie_level sitedata fullsessgrp cookie =
Lwt.catch
(fun () ->
match cookie_level with
| `Client_process ->
remove sitedata cookie fullsessgrp >>= fun () ->
Eliom_common.Persistent_tables.remove_key_from_all_tables cookie
| `Session ->
remove_group ~cookie_level:(`Client_process fullsessgrp) sitedata
(Eliom_common.make_persistent_full_group_name
~cookie_level:`Client_process
(Eliom_common.get_site_dir_string sitedata)
(Some cookie)))
(function Not_found -> Lwt.return_unit | e -> Lwt.fail e)
and remove _sitedata sess_id sess_grp =
match sess_grp with
| Some sg0 ->
let sg = Eliom_common.string_of_perssessgrp sg0 in
Lwt.catch
(fun () ->
!!grouptable >>= fun grouptable ->
Ocsipersist.find grouptable sg >>= fun (max, cl) ->
let newcl = List.remove_first_if_any sess_id cl in
Ocsipersist.replace_if_exists grouptable sg (max, newcl))
(function Not_found -> Lwt.return_unit | e -> Lwt.fail e)
| None -> Lwt.return_unit
let up sess_id grp =
match grp with
| None -> Lwt.return_unit
| Some sg ->
let sg = Eliom_common.string_of_perssessgrp sg in
Lwt.catch
(fun () ->
!!grouptable >>= fun grouptable ->
Ocsipersist.find grouptable sg >>= fun (max, cl) ->
let newcl = List.remove_first_if_any sess_id cl in
Ocsipersist.replace_if_exists grouptable sg (max, sess_id :: newcl))
(function Not_found -> Lwt.return_unit | e -> Lwt.fail e)
let move sitedata ?set_max max sess_id grp1 grp2 =
if set_max <> None || grp1 <> grp2
then
remove sitedata sess_id grp1 >>= fun () -> add ?set_max max sess_id grp2
else Lwt.return_nil
let nb_of_groups () = !!grouptable >>= Ocsipersist.length
end