2015-01-16 22:38:38 +00:00
|
|
|
-- Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
-- Licensed to the public under the Apache License 2.0.
|
2008-06-15 17:45:10 +00:00
|
|
|
|
2010-11-23 01:02:21 +00:00
|
|
|
exectime = os.clock()
|
2008-06-15 17:45:10 +00:00
|
|
|
module("luci.sgi.cgi", package.seeall)
|
2008-06-29 16:15:26 +00:00
|
|
|
local ltn12 = require("luci.ltn12")
|
2009-07-25 07:27:05 +00:00
|
|
|
require("nixio.util")
|
2008-06-15 17:45:10 +00:00
|
|
|
require("luci.http")
|
|
|
|
require("luci.sys")
|
|
|
|
require("luci.dispatcher")
|
|
|
|
|
2008-11-08 20:23:55 +00:00
|
|
|
-- Limited source to avoid endless blocking
|
|
|
|
local function limitsource(handle, limit)
|
|
|
|
limit = limit or 0
|
|
|
|
local BLOCKSIZE = ltn12.BLOCKSIZE
|
|
|
|
|
|
|
|
return function()
|
|
|
|
if limit < 1 then
|
|
|
|
handle:close()
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
local read = (limit > BLOCKSIZE) and BLOCKSIZE or limit
|
|
|
|
limit = limit - read
|
|
|
|
|
|
|
|
local chunk = handle:read(read)
|
|
|
|
if not chunk then handle:close() end
|
|
|
|
return chunk
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-15 17:45:10 +00:00
|
|
|
function run()
|
2008-06-20 19:57:57 +00:00
|
|
|
local r = luci.http.Request(
|
|
|
|
luci.sys.getenv(),
|
2008-11-08 20:23:55 +00:00
|
|
|
limitsource(io.stdin, tonumber(luci.sys.getenv("CONTENT_LENGTH"))),
|
2008-06-20 19:57:57 +00:00
|
|
|
ltn12.sink.file(io.stderr)
|
|
|
|
)
|
2008-06-15 17:45:10 +00:00
|
|
|
|
|
|
|
local x = coroutine.create(luci.dispatcher.httpdispatch)
|
2008-07-19 17:13:11 +00:00
|
|
|
local hcache = ""
|
2008-07-23 18:52:12 +00:00
|
|
|
local active = true
|
2008-06-15 17:45:10 +00:00
|
|
|
|
|
|
|
while coroutine.status(x) ~= "dead" do
|
|
|
|
local res, id, data1, data2 = coroutine.resume(x, r)
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
print("Status: 500 Internal Server Error")
|
|
|
|
print("Content-Type: text/plain\n")
|
|
|
|
print(id)
|
|
|
|
break;
|
|
|
|
end
|
2008-06-28 16:03:54 +00:00
|
|
|
|
2008-07-23 18:52:12 +00:00
|
|
|
if active then
|
|
|
|
if id == 1 then
|
|
|
|
io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
|
|
|
|
elseif id == 2 then
|
|
|
|
hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"
|
|
|
|
elseif id == 3 then
|
|
|
|
io.write(hcache)
|
|
|
|
io.write("\r\n")
|
|
|
|
elseif id == 4 then
|
2009-08-07 12:24:30 +00:00
|
|
|
io.write(tostring(data1 or ""))
|
2008-07-23 18:52:12 +00:00
|
|
|
elseif id == 5 then
|
2008-11-08 20:23:55 +00:00
|
|
|
io.flush()
|
|
|
|
io.close()
|
2008-07-23 18:52:12 +00:00
|
|
|
active = false
|
2009-07-25 07:27:05 +00:00
|
|
|
elseif id == 6 then
|
|
|
|
data1:copyz(nixio.stdout, data2)
|
2009-11-02 15:40:53 +00:00
|
|
|
data1:close()
|
2008-07-23 18:52:12 +00:00
|
|
|
end
|
2008-06-15 17:45:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|