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