mirror of https://github.com/dirtbags/moth.git
This CGI module is way better
This commit is contained in:
parent
1e6c2484ec
commit
975c97831c
80
cgi/cgi.lua
80
cgi/cgi.lua
|
@ -26,17 +26,30 @@ function getc_get()
|
|||
end
|
||||
end
|
||||
|
||||
function cgi.http_error(code, name, info)
|
||||
print(code .. " " .. name)
|
||||
print("Allow: GET POST")
|
||||
print("Content-type: text/html")
|
||||
print()
|
||||
print("<h1>" .. code .. " " .. name .. "</h1>")
|
||||
print("<p>" .. info .. "</p>")
|
||||
os.exit(0)
|
||||
function read_hex()
|
||||
local a = getc() or 0
|
||||
local b = getc() or 0
|
||||
|
||||
return string.char(tonumber(a, 16)*16 + tonumber(b, 16))
|
||||
end
|
||||
|
||||
function cgi.init()
|
||||
function item()
|
||||
local val = ""
|
||||
|
||||
while (true) do
|
||||
local c = getc()
|
||||
if ((c == nil) or (c == "=") or (c == "&")) then
|
||||
return val
|
||||
elseif (c == "%") then
|
||||
c = read_hex()
|
||||
elseif (c == "+") then
|
||||
c = " "
|
||||
end
|
||||
val = val .. c
|
||||
end
|
||||
end
|
||||
|
||||
function init()
|
||||
method = os.getenv("REQUEST_METHOD")
|
||||
if (method == "POST") then
|
||||
if (os.getenv("HTTP_CONTENT_TYPE") ~= "application/x-www-form-urlencoded") then
|
||||
|
@ -60,45 +73,30 @@ function cgi.init()
|
|||
else
|
||||
cgi.http_error(405, "Method not allowed", "I only do GET and POST.")
|
||||
end
|
||||
end
|
||||
|
||||
function cgi.read_hex()
|
||||
local a = getc() or 0
|
||||
local b = getc() or 0
|
||||
|
||||
return string.char(tonumber(a, 16)*16 + tonumber(b, 16))
|
||||
end
|
||||
|
||||
function cgi.item()
|
||||
local val = ""
|
||||
|
||||
while (true) do
|
||||
local c = getc()
|
||||
if ((c == nil) or (c == "=") or (c == "&")) then
|
||||
return val
|
||||
elseif (c == "%") then
|
||||
c = read_hex()
|
||||
elseif (c == "+") then
|
||||
c = " "
|
||||
end
|
||||
val = val .. c
|
||||
end
|
||||
end
|
||||
|
||||
function cgi.fields()
|
||||
local ret = {}
|
||||
|
||||
cgi.fields = {}
|
||||
while (true) do
|
||||
local k = cgi.item()
|
||||
local v = cgi.item()
|
||||
local k = item()
|
||||
local v = item()
|
||||
|
||||
if (k == "") then
|
||||
return ret
|
||||
break
|
||||
end
|
||||
ret[k] = v
|
||||
cgi.fields[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function cgi.http_error(code, name, info)
|
||||
print(code .. " " .. name)
|
||||
print("Allow: GET POST")
|
||||
print("Content-type: text/html")
|
||||
print()
|
||||
print("<h1>" .. code .. " " .. name .. "</h1>")
|
||||
print("<p>" .. info .. "</p>")
|
||||
os.exit(0)
|
||||
end
|
||||
|
||||
function cgi.escape(s)
|
||||
s = string.gsub(s, "&", "&")
|
||||
s = string.gsub(s, "<", "<")
|
||||
|
@ -106,4 +104,6 @@ function cgi.escape(s)
|
|||
return s
|
||||
end
|
||||
|
||||
init()
|
||||
|
||||
return cgi
|
||||
|
|
|
@ -2,10 +2,13 @@
|
|||
|
||||
local cgi = require "cgi"
|
||||
|
||||
cgi.init()
|
||||
fields = cgi.fields()
|
||||
|
||||
-- Read in team and answer
|
||||
|
||||
|
||||
|
||||
print("Content-type: text/html")
|
||||
print()
|
||||
print("<pre>")
|
||||
print(fields["t"])
|
||||
print(cgi.fields["t"])
|
||||
print("</pre>")
|
||||
|
|
Loading…
Reference in New Issue