d36d43fb51973c3b50014a6b08ed97e37d09759d
[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 function run()
33         local r = luci.http.Request(
34                 luci.sys.getenv(),
35                 ltn12.source.file(io.stdin),
36                 ltn12.sink.file(io.stderr)
37         )
38         
39         local x = coroutine.create(luci.dispatcher.httpdispatch)
40         local hcache = ""
41         local active = true
42         
43         while coroutine.status(x) ~= "dead" do
44                 local res, id, data1, data2 = coroutine.resume(x, r)
45
46                 if not res then
47                         print("Status: 500 Internal Server Error")
48                         print("Content-Type: text/plain\n")
49                         print(id)
50                         break;
51                 end
52
53                 if active then
54                         if id == 1 then
55                                 io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
56                         elseif id == 2 then
57                                 hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"
58                         elseif id == 3 then
59                                 io.write(hcache)
60                                 io.write("\r\n")
61                         elseif id == 4 then
62                                 io.write(data1)
63                         elseif id == 5 then
64                                 active = false
65                         end
66                 end
67         end
68 end