* Generalized HTTP-API
[project/luci.git] / libs / sgi-haserl / luasrc / sgi / haserl.lua
1 --[[
2 LuCI - SGI-Module for Haserl
3
4 Description:
5 Server Gateway Interface for Haserl
6
7 FileId:
8 $Id: haserl.lua 2027 2008-05-07 21:16:35Z Cyrus $
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.haserl", package.seeall)
27 require("luci.http")
28 require("luci.util")
29 require("luci.dispatcher")
30
31 function run()
32         local r = luci.http.Request(ENV, nil, io.stderr)
33         r.get = normalize_table(FORM)
34         r.post = r.get
35         
36         local x = coroutine.create(luci.dispatcher.httpdispatch)
37         while coroutine.status(x) ~= "dead" do
38                 local res, id, data1, data2 = coroutine.resume(x, r)
39                 
40                 if not res then
41                         print("Status: 500 Internal Server Error")
42                         print("Content-Type: text/plain\n")
43                         print(id)
44                         break;
45                 end
46                 
47                 if id == 1 then
48                         io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\n")
49                 elseif id == 2 then
50                         io.write(data1 .. ": " .. data2 .. "\n")
51                 elseif id == 3 then
52                         io.write("\n")
53                 elseif id == 4 then
54                         io.write(data1)
55                 end
56         end
57 end
58
59 function normalize_table(table, prefix)
60         prefix = prefix and prefix .. "." or ""
61         local new = {}
62         
63         for k,v in pairs(table) do
64                 if type(v) == "table" and #v == 0 then
65                         luci.util.update(new, normalize_table(v, prefix .. k))
66                 else
67                         new[prefix .. k] = v
68                 end
69         end
70         
71         return new
72 end