* libs/httpd: Introduced keep-alive and pipelining support
[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_head(self, ...)
14         local response, sourceout = self:handle_get(...)
15         return response
16 end
17
18 function Luci.handle_post(self, ...)
19         return self:handle_get(...)
20 end
21
22 function Luci.handle_get(self, request, sourcein, sinkerr)      
23         local r = luci.http.Request(
24                 request.env,
25                 sourcein,
26                 sinkerr
27         )
28                 
29         local res, id, data1, data2 = true, 0, nil, nil
30         local headers = {}
31         local status = 200
32         
33         local x = coroutine.create(luci.dispatcher.httpdispatch)
34         while not id or id < 3 do
35                 coroutine.yield()
36                 
37                 res, id, data1, data2 = coroutine.resume(x, r)
38                 
39                 if not res then
40                         status = 500
41                         headers["Content-Type"] = "text/plain"
42                         local err = {id}
43                         return status, headers, function() local x = table.remove(err) return x end
44                 end
45                 
46                 if id == 1 then
47                         status = data1
48                 elseif id == 2 then
49                         headers[data1] = data2
50                 end
51         end
52         
53         local function iter()
54                 local res, id, data = coroutine.resume(x)
55                 if not res then
56                         return nil, id
57                 elseif not id then
58                         return true
59                 elseif id == 5 then
60                         return nil
61                 else
62                         return data
63                 end
64         end
65         
66         return Response(status, headers), iter
67 end