The cookie module, used in Cookie Injection, provides the
following two methods:
Return value of the cookie name from the incoming request, or nil if no such cookie is present.
Set cookie name in the response. Second argument provides the
value of Max-Age.
Full text of cookie.lua follows:
local _M = {}
-- Return the value of cookie name or nil if it is not set.
function _M.get(name)
-- Return true if string s begins with the name
-- followed by an equals sign.
function is_name(s)
local i,j = s:find(name..'=', 1, true)
if j ~= nil then
return s:sub(j+1)
else
return nil
end
end
-- Given partial value of the Cookie header cookie,
-- find the cookie name and return its value.
local function find_cookie(cookie, name)
if cookie == nil then
return nil
end
local i, j = cookie:find(";%s*")
if i == nil then
return is_name(cookie)
else
local s = is_name(cookie:sub(1, i-1))
if s ~= nil then
return s
else
return find_cookie(cookie:sub(j+1), name)
end
end
end
return find_cookie(http.req.headers['Cookie'], name)
end
-- Set cookie name to the given value. Use ttl
-- as its Max-Age.
function _M.set(name, value, ttl)
cv = name .. '=' .. value .. '; Max-Age=' .. ttl
cookie = http.resp.headers['Set-cookie']
if cookie == nil then
cookie = { cv }
elseif type(cookie) == 'table' then
table.insert(cookie, cv)
else
cookie = { cookie, cv }
end
http.resp.headers['Set-cookie'] = cookie
end
return _M