Skip to content

Commit 4a9d3a4

Browse files
committed
http/cookie: Add store:remove method
1 parent 638a409 commit 4a9d3a4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

http/cookie.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,52 @@ function store_methods:get(domain, path, name)
357357
return nil
358358
end
359359

360+
function store_methods:remove(domain, path, name)
361+
assert(type(domain) == "string")
362+
assert(type(path) == "string" or (path == nil and name == nil))
363+
assert(type(name) == "string" or name == nil)
364+
local domain_cookies = self.domains[domain]
365+
if not domain_cookies then
366+
return
367+
end
368+
if path == nil then
369+
-- Delete whole domain
370+
for _, path_cookies in pairs(domain_cookies) do
371+
for _, cookie in pairs(path_cookies) do
372+
self.expiry_heap:remove(cookie)
373+
end
374+
end
375+
self.domains[domain] = nil
376+
else
377+
local path_cookies = domain_cookies[path]
378+
if path_cookies then
379+
if name == nil then
380+
-- Delete all names at path
381+
for _, cookie in pairs(path_cookies) do
382+
self.expiry_heap:remove(cookie)
383+
end
384+
domain_cookies[path] = nil
385+
if next(domain_cookies) == nil then
386+
self.domains[domain] = nil
387+
end
388+
else
389+
-- Delete singular cookie
390+
local cookie = path_cookies[name]
391+
if cookie then
392+
self.expiry_heap:remove(cookie)
393+
path_cookies[name] = nil
394+
if next(path_cookies) == nil then
395+
domain_cookies[path] = nil
396+
if next(domain_cookies) == nil then
397+
self.domains[domain] = nil
398+
end
399+
end
400+
end
401+
end
402+
end
403+
end
404+
end
405+
360406
--[[ The user agent SHOULD sort the cookie-list in the following order:
361407
- Cookies with longer paths are listed before cookies with shorter paths.
362408
- Among cookies that have equal-length path fields, cookies with earlier

0 commit comments

Comments
 (0)