Made SGIs even more standards compliant (respect EOF command from LuCI)
[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 local ltn12 = require("luci.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 local active = true
45
46 while id < 3 do
47 res, id, data1, data2 = coroutine.resume(x, r)
48
49 if not res then
50 status = 500
51 headers["Content-Type"] = "text/plain"
52 local err = {id}
53 return status, headers, function() local x = table.remove(err) return x end
54 end
55
56 if id == 1 then
57 status = data1
58 elseif id == 2 then
59 headers[data1] = data2
60 end
61 end
62
63 local function iter()
64 local res, id, data = coroutine.resume(x)
65 if id == 4 and active then
66 return data
67 elseif id == 5 then
68 active = false
69 return ""
70 else
71 return ""
72 end
73 if coroutine.status(x) == "dead" then
74 return nil
75 end
76 end
77
78 return status, headers, iter
79 end