cgi-bin/tek/class/cgi.lua
author Timm S. Mueller <tmueller@neoscientists.org>
Sat, 15 Dec 2007 17:16:32 +0100
changeset 224 47382ae7b824
parent 201 d52b05a9fe9c
child 243 ae976ed38923
permissions -rw-r--r--
Markup class wasn't correctly initialized
tmueller@66
     1
tmueller@0
     2
--
tmueller@190
     3
--	tek.class.cgi - tek cgi class
tmueller@0
     4
--	Written by Timm S. Mueller <tmueller at neoscientists.org>
tmueller@0
     5
--	See copyright notice in COPYRIGHT
tmueller@0
     6
--
tmueller@0
     7
tmueller@189
     8
local type, tonumber = type, tonumber
tmueller@24
     9
local insert, concat, char = table.insert, table.concat, string.char
tmueller@0
    10
tmueller@212
    11
module "tek.class.cgi"
tmueller@212
    12
_VERSION = "CGI 1.3"
tmueller@0
    13
tmueller@212
    14
-------------------------------------------------------------------------------
tmueller@212
    15
--	encodeurl: encode string to url; optionally specify a string with a
tmueller@212
    16
--	set of characters that should be left unmodified, from: $&+,/:;=?@
tmueller@212
    17
-------------------------------------------------------------------------------
tmueller@0
    18
tmueller@212
    19
local function encodefunc(c)
tmueller@212
    20
	return ("%%%02x"):format(c:byte())
tmueller@212
    21
end
tmueller@190
    22
tmueller@190
    23
function encodeurl(s, excl)
tmueller@212
    24
	-- reserved chars with special meaning:
tmueller@212
    25
	local matchset = "$&+,/:;=?@"
tmueller@190
    26
	if excl == true then
tmueller@201
    27
		matchset = ""
tmueller@190
    28
	elseif excl and type(excl) == "string" then
tmueller@212
    29
		matchset = matchset:gsub("[" .. excl:gsub(".", "%%%1") .. "]", "")
tmueller@0
    30
	end
tmueller@212
    31
	-- unsafe chars are always substituted:
tmueller@0
    32
	matchset = matchset .. '"<>#%{}|\\^~[]`]'
tmueller@0
    33
	matchset = "[%z\001-\032\127-\255" .. matchset:gsub(".", "%%%1") .. "]"
tmueller@212
    34
	return s:gsub(matchset, encodefunc)
tmueller@0
    35
end
tmueller@0
    36
tmueller@212
    37
-------------------------------------------------------------------------------
tmueller@212
    38
--	decodeurl
tmueller@212
    39
-------------------------------------------------------------------------------
tmueller@0
    40
tmueller@212
    41
local function decodefunc(h)
tmueller@212
    42
	return char(tonumber(h, 16))
tmueller@212
    43
end
tmueller@190
    44
tmueller@0
    45
function decodeurl(s)
tmueller@212
    46
	return (s:gsub("+", " ")):gsub("%%(%x%x)", decodefunc)
tmueller@0
    47
end
tmueller@189
    48