2022-03-15 21:53:31 +00:00
|
|
|
local _M = {}
|
|
|
|
|
|
|
|
-- utility function for checking if table is empty
|
2022-03-15 21:56:24 +00:00
|
|
|
function _M.is_table_empty(check)
|
2022-03-15 21:53:31 +00:00
|
|
|
-- bind next to local variable to achieve ultimate efficiency
|
|
|
|
-- https://stackoverflow.com/a/1252776
|
|
|
|
local next = next
|
|
|
|
|
|
|
|
return next(check) == nil
|
|
|
|
end
|
|
|
|
|
2022-03-21 13:23:26 +00:00
|
|
|
-- extract full cookie name and value by its name from cookie string
|
|
|
|
-- note: name matcher argument is a pattern so you will need to escape
|
|
|
|
-- any special characters, read more https://www.lua.org/pil/20.2.html
|
|
|
|
function _M.extract_cookie(cookie_string, name_matcher)
|
2022-03-21 13:38:57 +00:00
|
|
|
-- nil cookie string safeguard
|
|
|
|
if cookie_string == nil then
|
|
|
|
return nil
|
|
|
|
end
|
2022-03-21 13:23:26 +00:00
|
|
|
|
|
|
|
local start, stop = string.find(cookie_string, name_matcher .. "=[^;]+")
|
|
|
|
|
|
|
|
if start then
|
|
|
|
return string.sub(cookie_string, start, stop)
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- extract just the cookie value by its name from cookie string
|
|
|
|
-- note: name matcher argument is a pattern so you will need to escape
|
|
|
|
-- any special characters, read more https://www.lua.org/pil/20.2.html
|
|
|
|
function _M.extract_cookie_value(cookie_string, name_matcher)
|
|
|
|
local cookie = _M.extract_cookie(cookie_string, name_matcher)
|
|
|
|
|
2022-03-21 13:38:57 +00:00
|
|
|
if cookie == nil then
|
|
|
|
return nil
|
|
|
|
end
|
2022-03-21 13:23:26 +00:00
|
|
|
|
|
|
|
local value_start = string.find(cookie, "=") + 1
|
|
|
|
|
|
|
|
return string.sub(cookie, value_start)
|
|
|
|
end
|
|
|
|
|
2022-03-21 12:18:20 +00:00
|
|
|
return _M
|