This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/docker/nginx/libs/utils.lua

46 lines
1.3 KiB
Lua
Raw Permalink Normal View History

local _M = {}
-- utility function for checking if table is empty
function _M.is_table_empty(check)
-- 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