1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
5 module("luci.sgi.cgi", package.seeall)
6 local ltn12 = require("luci.ltn12")
10 require("luci.dispatcher")
12 -- Limited source to avoid endless blocking
13 local function limitsource(handle, limit)
15 local BLOCKSIZE = ltn12.BLOCKSIZE
22 local read = (limit > BLOCKSIZE) and BLOCKSIZE or limit
25 local chunk = handle:read(read)
26 if not chunk then handle:close() end
33 local r = luci.http.Request(
35 limitsource(io.stdin, tonumber(luci.sys.getenv("CONTENT_LENGTH"))),
36 ltn12.sink.file(io.stderr)
39 local x = coroutine.create(luci.dispatcher.httpdispatch)
43 while coroutine.status(x) ~= "dead" do
44 local res, id, data1, data2 = coroutine.resume(x, r)
47 print("Status: 500 Internal Server Error")
48 print("Content-Type: text/plain\n")
55 io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
57 hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"
62 io.write(tostring(data1 or ""))
68 data1:copyz(nixio.stdout, data2)