~ubuntu-branches/ubuntu/quantal/genometools/quantal-backports

« back to all changes in this revision

Viewing changes to src/external/cgilua-5.1.3/src/cgilua/cookies.lua

  • Committer: Package Import Robot
  • Author(s): Sascha Steinbiss
  • Date: 2012-07-09 14:10:23 UTC
  • Revision ID: package-import@ubuntu.com-20120709141023-juuu4spm6chqsf9o
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
----------------------------------------------------------------------------
 
2
-- Cookies Library
 
3
--
 
4
-- @release $Id: cookies.lua,v 1.8 2008/04/24 13:42:04 mascarenhas Exp $
 
5
----------------------------------------------------------------------------
 
6
 
 
7
require"cgilua.urlcode"
 
8
 
 
9
local error = error
 
10
local format, gsub, strfind = string.format, string.gsub, string.find
 
11
local date = os.date
 
12
local escape, unescape = cgilua.urlcode.escape, cgilua.urlcode.unescape
 
13
local function header(...)
 
14
   return SAPI.Response.header(...)
 
15
end
 
16
local function write(...)
 
17
   return SAPI.Response.write(...)
 
18
end
 
19
local function servervariable(...)
 
20
   return SAPI.Request.servervariable(...)
 
21
end
 
22
 
 
23
module ("cgilua.cookies")
 
24
 
 
25
local function optional (what, name)
 
26
  if name ~= nil and name ~= "" then
 
27
    return format("; %s=%s", what, name)
 
28
  else
 
29
    return ""
 
30
  end
 
31
end
 
32
 
 
33
 
 
34
local function build (name, value, options)
 
35
  if not name or not value then
 
36
    error("cookie needs a name and a value")
 
37
  end
 
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)
 
43
  end
 
44
  cookie = cookie .. optional("path", options.path)
 
45
  cookie = cookie .. optional("domain", options.domain)
 
46
  cookie = cookie .. optional("secure", options.secure)
 
47
  return cookie
 
48
end
 
49
 
 
50
 
 
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).
 
57
 
 
58
function set (name, value, options)
 
59
  header("Set-Cookie", build(name, value, options))
 
60
end
 
61
 
 
62
 
 
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).
 
69
 
 
70
function sethtml (name, value, options)
 
71
  write(format('<meta http-equiv="Set-Cookie" content="%s">', 
 
72
                build(name, value, options)))
 
73
end
 
74
 
 
75
 
 
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.
 
80
 
 
81
function get (name)
 
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)
 
88
end
 
89
 
 
90
 
 
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).
 
95
 
 
96
function delete (name, options)
 
97
  options = options or {}
 
98
  options.expires = 1
 
99
  set(name, "xxx", options)
 
100
end