Skip to content

Commit 9381ad9

Browse files
committed
http/cookie: Refactor moving add_to_store to own function
1 parent a58c807 commit 9381ad9

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

http/cookie.lua

+44-40
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,49 @@ local function new_store()
165165
}, store_mt)
166166
end
167167

168+
local function add_to_store(self, cookie, req_is_http, now)
169+
if cookie.expiry_time < now then
170+
-- This was all just a trigger to delete the old cookie
171+
self:remove(cookie.domain, cookie.path, cookie.name)
172+
else
173+
-- Insert the newly created cookie into the cookie store.
174+
local domain_cookies = self.domains[cookie.domain]
175+
if domain_cookies == nil then
176+
domain_cookies = {}
177+
self.domains[cookie.domain] = domain_cookies
178+
end
179+
local path_cookies = domain_cookies[cookie.path]
180+
if path_cookies == nil then
181+
path_cookies = {}
182+
domain_cookies[cookie.path] = path_cookies
183+
end
184+
185+
local old_cookie = path_cookies[cookie.name]
186+
-- If the cookie store contains a cookie with the same name,
187+
-- domain, and path as the newly created cookie:
188+
if old_cookie then
189+
-- If the newly created cookie was received from a "non-HTTP"
190+
-- API and the old-cookie's http-only-flag is set, abort these
191+
-- steps and ignore the newly created cookie entirely.
192+
if not req_is_http and old_cookie.http_only then
193+
return false
194+
end
195+
196+
-- Update the creation-time of the newly created cookie to
197+
-- match the creation-time of the old-cookie.
198+
cookie.creation_time = old_cookie.creation_time
199+
200+
-- Remove the old-cookie from the cookie store.
201+
self.expiry_heap:remove(old_cookie)
202+
end
203+
204+
path_cookies[cookie.name] = cookie
205+
self.expiry_heap:insert(cookie.expiry_time, cookie)
206+
end
207+
208+
return true
209+
end
210+
168211
function store_methods:store(req_domain, req_path, req_is_http, req_is_secure, req_site_for_cookies, name, value, params)
169212
assert(type(req_domain) == "string")
170213
assert(type(req_path) == "string")
@@ -371,46 +414,7 @@ function store_methods:store(req_domain, req_path, req_is_http, req_is_secure, r
371414
return false
372415
end
373416

374-
if cookie.expiry_time < now then
375-
-- This was all just a trigger to delete the old cookie
376-
self:remove(cookie.domain, cookie.path, cookie.name)
377-
else
378-
-- Insert the newly created cookie into the cookie store.
379-
local domain_cookies = self.domains[cookie.domain]
380-
if domain_cookies == nil then
381-
domain_cookies = {}
382-
self.domains[cookie.domain] = domain_cookies
383-
end
384-
local path_cookies = domain_cookies[cookie.path]
385-
if path_cookies == nil then
386-
path_cookies = {}
387-
domain_cookies[cookie.path] = path_cookies
388-
end
389-
390-
local old_cookie = path_cookies[cookie.name]
391-
-- If the cookie store contains a cookie with the same name,
392-
-- domain, and path as the newly created cookie:
393-
if old_cookie then
394-
-- If the newly created cookie was received from a "non-HTTP"
395-
-- API and the old-cookie's http-only-flag is set, abort these
396-
-- steps and ignore the newly created cookie entirely.
397-
if not req_is_http and old_cookie.http_only then
398-
return false
399-
end
400-
401-
-- Update the creation-time of the newly created cookie to
402-
-- match the creation-time of the old-cookie.
403-
cookie.creation_time = old_cookie.creation_time
404-
405-
-- Remove the old-cookie from the cookie store.
406-
self.expiry_heap:remove(old_cookie)
407-
end
408-
409-
path_cookies[cookie.name] = cookie
410-
self.expiry_heap:insert(cookie.expiry_time, cookie)
411-
end
412-
413-
return true
417+
return add_to_store(self, cookie, req_is_http, now)
414418
end
415419

416420
function store_methods:store_from_request(req_headers, resp_headers, req_host, req_site_for_cookies)

0 commit comments

Comments
 (0)