diff --git a/cgi/cgi.lua b/cgi/cgi.lua index 84da73a..5d47013 100644 --- a/cgi/cgi.lua +++ b/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("

" .. code .. " " .. name .. "

") - print("

" .. info .. "

") - 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("

" .. code .. " " .. name .. "

") + print("

" .. info .. "

") + 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 diff --git a/cgi/puzzler.cgi b/cgi/puzzler.cgi index fadf98c..56ea236 100755 --- a/cgi/puzzler.cgi +++ b/cgi/puzzler.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("
")
-print(fields["t"])
+print(cgi.fields["t"])
 print("
")