Increase compatibility of SGI-CGI with CGI1.1 standard
[project/luci.git] / libs / sgi-cgi / luasrc / sgi / cgi.lua
1 --[[
2 LuCI - SGI-Module for CGI
3
4 Description:
5 Server Gateway Interface for CGI
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
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 
16
17         http://www.apache.org/licenses/LICENSE-2.0 
18
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.
24
25 ]]--
26 module("luci.sgi.cgi", package.seeall)
27 local ltn12 = require("luci.ltn12")
28 require("luci.http")
29 require("luci.sys")
30 require("luci.dispatcher")
31
32 -- Limited source to avoid endless blocking
33 local function limitsource(handle, limit)
34         limit = limit or 0
35         local BLOCKSIZE = ltn12.BLOCKSIZE
36
37         return function()
38                 if limit < 1 then
39                         handle:close()
40                         return nil
41                 else
42                         local read = (limit > BLOCKSIZE) and BLOCKSIZE or limit
43                         limit = limit - read
44
45                         local chunk = handle:read(read)
46                         if not chunk then handle:close() end
47                         return chunk
48                 end
49         end
50 end
51
52 function run()
53         local r = luci.http.Request(
54                 luci.sys.getenv(),
55                 limitsource(io.stdin, tonumber(luci.sys.getenv("CONTENT_LENGTH"))),
56                 ltn12.sink.file(io.stderr)
57         )
58         
59         local x = coroutine.create(luci.dispatcher.httpdispatch)
60         local hcache = ""
61         local active = true
62         
63         while coroutine.status(x) ~= "dead" do
64                 local res, id, data1, data2 = coroutine.resume(x, r)
65
66                 if not res then
67                         print("Status: 500 Internal Server Error")
68                         print("Content-Type: text/plain\n")
69                         print(id)
70                         break;
71                 end
72
73                 if active then
74                         if id == 1 then
75                                 io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
76                         elseif id == 2 then
77                                 hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"
78                         elseif id == 3 then
79                                 io.write(hcache)
80                                 io.write("\r\n")
81                         elseif id == 4 then
82                                 io.write(data1)
83                         elseif id == 5 then
84                                 io.flush()
85                                 io.close()
86                                 active = false
87                         end
88                 end
89         end
90 end