[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Simple CGI in Lua
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Wed, 17 Sep 2008 17:46:42 +0200
2008/9/16 Robert Raschke <rtrlists@googlemail.com>:
> I think it's actually worthwhile to learn the CGI spec. It's not very
> much. Probably faster to learn than a cgi wrapper API.
>
> The really hard bit is trying to get multipart mime forms right. I
> gave up when I reached that level of complexity.
>
> But parsing your cgi args (PATH_INFO and QUERY_STRING) is really very
> trivial. And there's a few neat html builder libs for Lua out there
> you can use to assemble your page.
I finally did just that, taking the QUERY_STRING as is. I borrowed the
url_decode function from WSAPI to unescape special characters. Below
is the final script for those interested. Thanks for all the advice
and propositions.
#!/usr/bin/env lua
-- Decode an URL-encoded string (see RFC 2396)
function url_decode(str)
if not str then return nil end
str = string.gsub (str, "+", " ")
str = string.gsub (str, "%%(%x%x)", function(h) return
string.char(tonumber(h,16)) end)
str = string.gsub (str, "\r\n", "\n")
return str
end
local command = "tail "..url_decode(os.getenv'QUERY_STRING')
local tail = assert(io.popen(command, "r"))
io.write("Status: 200 OK\r\n")
io.write("Content-Type: text/html; charset=UTF-8\r\n")
io.write("\r\n")
io.write([[
<html>
<head><title>]]..command..[[</title></head>
<body>
<h1>]]..command..[[</h1>
<pre>
]])
io.flush()
repeat
local data = tail:read("*l")
if data then
io.write(data.."\n")
io.flush()
end
until not data
io.write([[
</pre>
</body>
</html>
]])
tail:close()