* libs/core: Added garbage collector to luci.util.threadlocal to avoid memory leaks
[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("ltn12")
28 require("luci.http")
29 require("luci.dispatcher")
30 require("luci.http.protocol")
31
32 function run(wsapi_env)
33         local r = luci.http.Request(
34                 wsapi_env,
35                 ltn12.source.file(wsapi_env.input),
36                 ltn12.sink.file(wsapi_env.error)
37         )
38                 
39         local res, id, data1, data2 = true, 0, nil, nil
40         local headers = {}
41         local status = 200
42         
43         local x = coroutine.create(luci.dispatcher.httpdispatch)
44         while id < 3 do
45                 res, id, data1, data2 = coroutine.resume(x, r)
46                 
47                 if not res then
48                         status = 500
49                         headers["Content-Type"] = "text/plain"
50                         local err = {id}
51                         return status, headers, function() local x = table.remove(err) return x end
52                 end
53                 
54                 if id == 1 then
55                         status = data1
56                 elseif id == 2 then
57                         headers[data1] = data2
58                 end
59         end
60         
61         local function iter()
62                 local res, id, data1, data2 = coroutine.resume(x)
63                 if not res or id == 5 then
64                         return nil
65                 else
66                         return data1
67                 end
68         end
69         
70         return status, headers, iter
71 end