e20dc5de02f5b2ad302c06f94abe77d725b2d584
[project/luci.git] / libs / sgi-webuci / luasrc / sgi / webuci.lua
1 --[[
2 LuCI - SGI-Module for Webuci
3
4 Description:
5 Server Gateway Interface for Webuci
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.webuci", package.seeall)
27 local ltn12 = require("luci.ltn12")
28 require("luci.http")
29 require("luci.util")
30 require("luci.dispatcher")
31
32 function run(env, vars)
33         local r = luci.http.Request(
34                 env,
35                 ltn12.source.empty(),
36                 ltn12.sink.file(io.stderr)
37         )
38         
39         r.message.params = vars
40         
41         local x = coroutine.create(luci.dispatcher.httpdispatch)
42         local status = ""
43         local headers = {}
44         local active = true
45         
46         while coroutine.status(x) ~= "dead" do
47                 local res, id, data1, data2 = coroutine.resume(x, r)
48
49                 if not res then
50                         print(env.SERVER_PROTOCOL .. " 500 Internal Server Error")
51                         print("Content-Type: text/plain\n")
52                         print(id)
53                         break;
54                 end
55                 
56                 if active then
57                         if id == 1 then
58                                 status = env.SERVER_PROTOCOL .. " " .. tostring(data1) .. " " .. data2 .. "\r\n"
59                         elseif id == 2 then
60                                 headers[data1] = data2
61                         elseif id == 3 then
62                                 io.write(status)
63                                 for k, v in pairs(headers) do
64                                         io.write(k .. ": " .. v .. "\r\n")
65                                 end
66                                 io.write("\r\n")
67                         elseif id == 4 then
68                                 io.write(data1)
69                         elseif id == 5 then
70                                 active = false
71                         end
72                 end
73         end
74 end