* Introducing LuCI HTTPD as testing environment
[project/luci.git] / libs / httpd / luasrc / httpd / handler / luci.lua
1 module("luci.httpd.handler.luci", package.seeall)
2 require("luci.dispatcher")
3 require("luci.http")
4 require("ltn12")
5
6 Luci = luci.util.class(luci.httpd.module.Handler)
7 Response = luci.httpd.module.Response
8
9 function Luci.__init__(self)
10         luci.httpd.module.Handler.__init__(self)
11 end
12
13 function Luci.handle(self, request, sourcein, sinkerr)  
14         local r = luci.http.Request(
15                 request.env,
16                 sourcein,
17                 sinkerr
18         )
19                 
20         local res, id, data1, data2 = true, 0, nil, nil
21         local headers = {}
22         local status = 200
23         
24         local x = coroutine.create(luci.dispatcher.httpdispatch)
25         while id < 3 do
26                 coroutine.yield()
27                 
28                 res, id, data1, data2 = coroutine.resume(x, r)
29                 
30                 if not res then
31                         status = 500
32                         headers["Content-Type"] = "text/plain"
33                         local err = {id}
34                         return status, headers, function() local x = table.remove(err) return x end
35                 end
36                 
37                 if id == 1 then
38                         status = data1
39                 elseif id == 2 then
40                         headers[data1] = data2
41                 end
42         end
43         
44         local function iter()
45                 local res, id, data = coroutine.resume(x)
46                 if not res then
47                         return nil, id
48                 elseif id == 5 then
49                         return nil
50                 else
51                         return data
52                 end
53         end
54         
55         return Response(status, headers), iter
56 end