mirror of https://github.com/dirtbags/moth.git
touch up cgi.lua
This commit is contained in:
parent
cf122a01c0
commit
1e6c2484ec
57
cgi/cgi.lua
57
cgi/cgi.lua
|
@ -3,7 +3,28 @@
|
||||||
local cgi = {}
|
local cgi = {}
|
||||||
|
|
||||||
cgi.POST_MAX = 512
|
cgi.POST_MAX = 512
|
||||||
local getc = {}
|
|
||||||
|
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
|
||||||
|
|
||||||
function cgi.http_error(code, name, info)
|
function cgi.http_error(code, name, info)
|
||||||
print(code .. " " .. name)
|
print(code .. " " .. name)
|
||||||
|
@ -22,19 +43,11 @@ function cgi.init()
|
||||||
cgi.http_error(415, "Unsupported content-type", "You are sending me data in a format I can't process")
|
cgi.http_error(415, "Unsupported content-type", "You are sending me data in a format I can't process")
|
||||||
end
|
end
|
||||||
|
|
||||||
local CL = tonumber(os.getenv("CONTENT_LENGTH")) or 0
|
|
||||||
if (CL > cgi.POST_MAX) then
|
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")
|
cgi.http_error(413, "Post Data Too Long", "You are sending me more data than I'm prepared to handle")
|
||||||
end
|
end
|
||||||
|
|
||||||
function getc()
|
getc = getc_post
|
||||||
if (CL > 0) then
|
|
||||||
CL = CL - 1
|
|
||||||
return io.read(1)
|
|
||||||
else
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
elseif (method == "GET") then
|
elseif (method == "GET") then
|
||||||
local query = os.getenv("QUERY_STRING") or ""
|
local query = os.getenv("QUERY_STRING") or ""
|
||||||
local query_pos = 0
|
local query_pos = 0
|
||||||
|
@ -43,14 +56,7 @@ function cgi.init()
|
||||||
cgi.http_error(413, "Query Data Too Long", "You are sending me more data than I'm prepared to handle")
|
cgi.http_error(413, "Query Data Too Long", "You are sending me more data than I'm prepared to handle")
|
||||||
end
|
end
|
||||||
|
|
||||||
function getc()
|
getc = 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
|
|
||||||
else
|
else
|
||||||
cgi.http_error(405, "Method not allowed", "I only do GET and POST.")
|
cgi.http_error(405, "Method not allowed", "I only do GET and POST.")
|
||||||
end
|
end
|
||||||
|
@ -79,6 +85,20 @@ function cgi.item()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function cgi.fields()
|
||||||
|
local ret = {}
|
||||||
|
|
||||||
|
while (true) do
|
||||||
|
local k = cgi.item()
|
||||||
|
local v = cgi.item()
|
||||||
|
|
||||||
|
if (k == "") then
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
ret[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function cgi.escape(s)
|
function cgi.escape(s)
|
||||||
s = string.gsub(s, "&", "&")
|
s = string.gsub(s, "&", "&")
|
||||||
s = string.gsub(s, "<", "<")
|
s = string.gsub(s, "<", "<")
|
||||||
|
@ -87,4 +107,3 @@ function cgi.escape(s)
|
||||||
end
|
end
|
||||||
|
|
||||||
return cgi
|
return cgi
|
||||||
|
|
||||||
|
|
|
@ -3,4 +3,9 @@
|
||||||
local cgi = require "cgi"
|
local cgi = require "cgi"
|
||||||
|
|
||||||
cgi.init()
|
cgi.init()
|
||||||
print(cgi.item())
|
fields = cgi.fields()
|
||||||
|
print("Content-type: text/html")
|
||||||
|
print()
|
||||||
|
print("<pre>")
|
||||||
|
print(fields["t"])
|
||||||
|
print("</pre>")
|
||||||
|
|
Loading…
Reference in New Issue