From af2bc9bc8da7fb53cad52b1ac481bfc9419eaa60 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Sat, 18 Apr 2015 14:24:00 -0600 Subject: [PATCH] This CGI module is way better --- cgi/cgi.lua | 80 ++++++++++++++++++++++++------------------------- cgi/puzzler.cgi | 9 ++++-- 2 files changed, 46 insertions(+), 43 deletions(-) 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("
")