* Added preliminary support for WSAPI SGI
[project/luci.git] / libs / sgi-wsapi / luasrc / sgi / wsapi.lua
1 --[[
2 LuCI - SGI-Module for WSAPI
3
4 Description:
5 Server Gateway Interface for WSAPI
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.wsapi", package.seeall)
27 require("luci.http")
28 require("luci.dispatcher")
29 require("wsapi.request")
30
31 function run(wsapi_env)
32 local r = luci.http.Request()
33 r.env = wsapi_env
34 r.request = wsapi.request.parse_post_data(wsapi_env,
35 wsapi.request.parse_qs(wsapi_env.QUERY_STRING))
36
37 local res, id, data1, data2 = true, 0, nil, nil
38 local headers = {}
39 local status = 200
40
41 local x = coroutine.create(luci.dispatcher.httpdispatch)
42 while id < 3 do
43 res, id, data1, data2 = coroutine.resume(x, r)
44
45 if not res then
46 status = 500
47 headers["Content-Type"] = "text/plain"
48 local err = {id}
49 return status, headers, function() local x = table.remove(err) return x end
50 end
51
52 if id == 1 then
53 status = data1
54 elseif id == 2 then
55 headers[data1] = data2
56 end
57 end
58
59 local function iter()
60 local res, id, data1, data2 = coroutine.resume(x)
61 if not res or id == 5 then
62 return nil
63 else
64 return data1
65 end
66 end
67
68 return status, headers, iter
69 end