moth/www/cgi-bin/cgi.lua

110 lines
2.2 KiB
Lua
Raw Normal View History

2015-05-26 10:16:22 -06:00
#! /usr/bin/env lua
2015-04-16 22:29:05 -06:00
local cgi = {}
cgi.POST_MAX = 512
2015-04-17 17:13:34 -06:00
local CL = tonumber(os.getenv("CONTENT_LENGTH")) or 0
function getc_post()
if (CL > 0) then
CL = CL - 1
return io.read(1)
else
return nil
end
end
local query = os.getenv("QUERY_STRING") or ""
local query_len = query:len()
local query_pos = 0
function getc_get()
if (query_pos < query_len) then
query_pos = query_pos + 1
return string.sub(query, query_pos, query_pos)
else
return nil
end
end
2015-04-16 22:29:05 -06:00
2015-04-18 14:24:00 -06:00
function read_hex()
local a = getc() or 0
local b = getc() or 0
return string.char(tonumber(a, 16)*16 + tonumber(b, 16))
2015-04-16 22:29:05 -06:00
end
2015-04-18 14:24:00 -06:00
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()
2015-04-16 22:29:05 -06:00
method = os.getenv("REQUEST_METHOD")
if (method == "POST") then
if (os.getenv("HTTP_CONTENT_TYPE") ~= "application/x-www-form-urlencoded") then
cgi.http_error(415, "Unsupported content-type", "You are sending me data in a format I can't process")
end
if (CL > cgi.POST_MAX) then
cgi.http_error(413, "Post Data Too Long", "You are sending me more data than I'm prepared to handle")
end
2015-04-17 17:13:34 -06:00
getc = getc_post
2015-04-16 22:29:05 -06:00
elseif (method == "GET") then
local query = os.getenv("QUERY_STRING") or ""
local query_pos = 0
local query_len = string.len(query)
if (query_len > cgi.POST_MAX) then
cgi.http_error(413, "Query Data Too Long", "You are sending me more data than I'm prepared to handle")
end
2015-04-17 17:13:34 -06:00
getc = getc_get
2015-04-16 22:29:05 -06:00
else
cgi.http_error(405, "Method not allowed", "I only do GET and POST.")
end
2015-04-17 17:13:34 -06:00
2015-04-18 14:24:00 -06:00
cgi.fields = {}
2015-04-17 17:13:34 -06:00
while (true) do
2015-04-18 14:24:00 -06:00
local k = item()
local v = item()
2015-04-17 17:13:34 -06:00
if (k == "") then
2015-04-18 14:24:00 -06:00
break
2015-04-17 17:13:34 -06:00
end
2015-04-18 14:24:00 -06:00
cgi.fields[k] = v
2015-04-17 17:13:34 -06:00
end
end
2015-04-18 14:24:00 -06:00
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
2015-04-16 22:29:05 -06:00
function cgi.escape(s)
s = string.gsub(s, "&", "&amp;")
s = string.gsub(s, "<", "&lt;")
s = string.gsub(s, ">", "&gt;")
return s
end
2015-04-18 14:24:00 -06:00
init()
2015-04-16 22:29:05 -06:00
return cgi