1
----------------------------------------------------------------------------
4
-- @release $Id: cookies.lua,v 1.8 2008/04/24 13:42:04 mascarenhas Exp $
5
----------------------------------------------------------------------------
7
require"cgilua.urlcode"
10
local format, gsub, strfind = string.format, string.gsub, string.find
12
local escape, unescape = cgilua.urlcode.escape, cgilua.urlcode.unescape
13
local function header(...)
14
return SAPI.Response.header(...)
16
local function write(...)
17
return SAPI.Response.write(...)
19
local function servervariable(...)
20
return SAPI.Request.servervariable(...)
23
module ("cgilua.cookies")
25
local function optional (what, name)
26
if name ~= nil and name ~= "" then
27
return format("; %s=%s", what, name)
34
local function build (name, value, options)
35
if not name or not value then
36
error("cookie needs a name and a value")
38
local cookie = name .. "=" .. escape(value)
39
options = options or {}
40
if options.expires then
41
local t = date("!%A, %d-%b-%Y %H:%M:%S GMT", options.expires)
42
cookie = cookie .. optional("expires", t)
44
cookie = cookie .. optional("path", options.path)
45
cookie = cookie .. optional("domain", options.domain)
46
cookie = cookie .. optional("secure", options.secure)
51
----------------------------------------------------------------------------
52
-- Sets a value to a cookie, with the given options.
53
-- Generates a header "Set-Cookie", thus it can only be used in Lua Scripts.
54
-- @param name String with the name of the cookie.
55
-- @param value String with the value of the cookie.
56
-- @param options Table with the options (optional).
58
function set (name, value, options)
59
header("Set-Cookie", build(name, value, options))
63
----------------------------------------------------------------------------
64
-- Sets a value to a cookie, with the given options.
65
-- Generates an HTML META tag, thus it can be used in Lua Pages.
66
-- @param name String with the name of the cookie.
67
-- @param value String with the value of the cookie.
68
-- @param options Table with the options (optional).
70
function sethtml (name, value, options)
71
write(format('<meta http-equiv="Set-Cookie" content="%s">',
72
build(name, value, options)))
76
----------------------------------------------------------------------------
77
-- Gets the value of a cookie.
78
-- @param name String with the name of the cookie.
79
-- @return String with the value associated with the cookie.
82
local cookies = servervariable"HTTP_COOKIE" or ""
83
cookies = ";" .. cookies .. ";"
84
cookies = gsub(cookies, "%s*;%s*", ";") -- remove extra spaces
85
local pattern = ";" .. name .. "=(.-);"
86
local _, __, value = strfind(cookies, pattern)
87
return value and unescape(value)
91
----------------------------------------------------------------------------
92
-- Deletes a cookie, by setting its value to "xxx".
93
-- @param name String with the name of the cookie.
94
-- @param options Table with the options (optional).
96
function delete (name, options)
97
options = options or {}
99
set(name, "xxx", options)