4
4
]]
5
5
6
6
local http_patts = require " lpeg_patterns.http"
7
+ local binaryheap = require " binaryheap"
7
8
local has_psl , psl = pcall (require , " psl" )
8
9
9
10
local EOF = require " lpeg" .P (- 1 )
@@ -96,6 +97,7 @@ local store_mt = {
96
97
local function new_store ()
97
98
return setmetatable ({
98
99
domains = {};
100
+ expiry_heap = binaryheap .minUnique ();
99
101
}, store_mt )
100
102
end
101
103
@@ -317,9 +319,11 @@ function store_methods:store(req_domain, req_path, req_is_http, req_is_secure, n
317
319
cookie .creation_time = old_cookie .creation_time
318
320
319
321
-- Remove the old-cookie from the cookie store.
322
+ self .expiry_heap :remove (old_cookie )
320
323
end
321
324
322
325
path_cookies [cookie .name ] = cookie
326
+ self .expiry_heap :insert (cookie .expiry_time , cookie )
323
327
end
324
328
325
329
return true
@@ -352,12 +356,20 @@ function store_methods:remove(domain, path, name)
352
356
end
353
357
if path == nil then
354
358
-- Delete whole domain
359
+ for _ , path_cookies in pairs (domain_cookies ) do
360
+ for _ , cookie in pairs (path_cookies ) do
361
+ self .expiry_heap :remove (cookie )
362
+ end
363
+ end
355
364
self .domains [domain ] = nil
356
365
else
357
366
local path_cookies = domain_cookies [path ]
358
367
if path_cookies then
359
368
if name == nil then
360
369
-- Delete all names at path
370
+ for _ , cookie in pairs (path_cookies ) do
371
+ self .expiry_heap :remove (cookie )
372
+ end
361
373
domain_cookies [path ] = nil
362
374
if next (domain_cookies ) == nil then
363
375
self .domains [domain ] = nil
@@ -366,6 +378,7 @@ function store_methods:remove(domain, path, name)
366
378
-- Delete singular cookie
367
379
local cookie = path_cookies [name ]
368
380
if cookie then
381
+ self .expiry_heap :remove (cookie )
369
382
path_cookies [name ] = nil
370
383
if next (path_cookies ) == nil then
371
384
domain_cookies [path ] = nil
@@ -463,19 +476,20 @@ end
463
476
464
477
function store_methods :clean ()
465
478
local now = self .time ()
466
- for domain , domain_cookies in pairs (self .domains ) do
467
- for path , path_cookies in pairs (domain_cookies ) do
468
- for name , cookie in pairs (path_cookies ) do
469
- if cookie .expiry_time < now then
470
- path_cookies [name ] = nil
479
+ while self .expiry_heap :peek ().expiry_time < now do
480
+ local cookie = self .expiry_heap :pop ()
481
+ local domain_cookies = self .domains [cookie .domain ]
482
+ if domain_cookies then
483
+ local path_cookies = domain_cookies [cookie .path ]
484
+ if path_cookies then
485
+ path_cookies [cookie .name ] = nil
486
+ if next (path_cookies ) == nil then
487
+ domain_cookies [cookie .path ] = nil
488
+ if next (domain_cookies ) == nil then
489
+ self .domains [cookie .domain ] = nil
490
+ end
471
491
end
472
492
end
473
- if next (path_cookies ) == nil then
474
- domain_cookies [path ] = nil
475
- end
476
- end
477
- if next (domain_cookies ) == nil then
478
- self .domains [domain ] = nil
479
493
end
480
494
end
481
495
return true
0 commit comments