2 LuCI - SGI-Module for CGI
5 Server Gateway Interface for CGI
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
26 module("luci.sgi.cgi", package.seeall)
27 local ltn12 = require("luci.ltn12")
31 require("luci.dispatcher")
33 -- Limited source to avoid endless blocking
34 local function limitsource(handle, limit)
36 local BLOCKSIZE = ltn12.BLOCKSIZE
43 local read = (limit > BLOCKSIZE) and BLOCKSIZE or limit
46 local chunk = handle:read(read)
47 if not chunk then handle:close() end
54 local r = luci.http.Request(
56 limitsource(io.stdin, tonumber(luci.sys.getenv("CONTENT_LENGTH"))),
57 ltn12.sink.file(io.stderr)
60 local x = coroutine.create(luci.dispatcher.httpdispatch)
64 while coroutine.status(x) ~= "dead" do
65 local res, id, data1, data2 = coroutine.resume(x, r)
68 print("Status: 500 Internal Server Error")
69 print("Content-Type: text/plain\n")
76 io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
78 hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"
83 io.write(tostring(data1 or ""))
89 data1:copyz(nixio.stdout, data2)