* Generalized HTTP-API
[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("luci.http.protocol")
30
31 function run(wsapi_env)
32         local r = luci.http.Request(wsapi_env, wsapi_env.input, wsapi_env.error)
33         r.postds = function() return wsapi.request.parse_post_data(wsapi_env) end
34         r.getds  = function() return wsapi.request.parse_qs(wsapi_env.QUERY_STRING) end
35                 
36         local res, id, data1, data2 = true, 0, nil, nil
37         local headers = {}
38         local status = 200
39         
40         local x = coroutine.create(luci.dispatcher.httpdispatch)
41         while id < 3 do
42                 res, id, data1, data2 = coroutine.resume(x, r)
43                 
44                 if not res then
45                         status = 500
46                         headers["Content-Type"] = "text/plain"
47                         local err = {id}
48                         return status, headers, function() local x = table.remove(err) return x end
49                 end
50                 
51                 if id == 1 then
52                         status = data1
53                 elseif id == 2 then
54                         headers[data1] = data2
55                 end
56         end
57         
58         local function iter()
59                 local res, id, data1, data2 = coroutine.resume(x)
60                 if not res or id == 5 then
61                         return nil
62                 else
63                         return data1
64                 end
65         end
66         
67         return status, headers, iter
68 end