-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgi.lua
54 lines (39 loc) · 1.3 KB
/
cgi.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function split (s, delim)
assert (type (delim) == "string" and string.len (delim) > 0,
"bad delimiter")
local start = 1
local t = {} -- results table
-- find each instance of a string followed by the delimiter
while true do
local pos = string.find (s, delim, start, true) -- plain find
if not pos then
break
end
table.insert (t, string.sub (s, start, pos - 1))
start = pos + string.len (delim)
end -- while
-- insert final one (after last delimiter)
table.insert (t, string.sub (s, start))
return t
end -- function split
-- trim leading and trailing spaces from a string
function trim (s)
return (string.gsub (s, "^%s*(.-)%s*$", "%1"))
end -- trim
-- convert + to space
-- convert %xx where xx is hex characters, to the equivalent byte
function urldecode (s)
return (string.gsub (string.gsub (s, "+", " "),
"%%(%x%x)",
function (str)
return string.char (tonumber (str, 16))
end ))
end -- function urldecode
-- process a single key=value pair from a GET line (or cookie, etc.)
function assemble_value (s, t)
assert (type (t) == "table")
local _, _, key, value = string.find (s, "(.-)=(.+)")
if key then
t [trim (urldecode (key))] = trim (urldecode (value))
end -- if we had key=value
end -- assemble_value