modules/freifunk: set ipv4 broadcast to 255.255.255.255 by default
[project/luci.git] / libs / httpd / luasrc / httpd / handler / luci.lua
1 --[[
2
3 HTTP server implementation for LuCI - luci handler
4 (c) 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 module("luci.httpd.handler.luci", package.seeall)
17
18 require("luci.dispatcher")
19 require("luci.http")
20 require("luci.http.protocol.date")
21 local ltn12 = require("luci.ltn12")
22
23 Luci = luci.util.class(luci.httpd.module.Handler)
24 Response = luci.httpd.module.Response
25
26 function Luci.__init__(self, limit)
27         luci.httpd.module.Handler.__init__(self)
28         self.limit = limit or 5
29         self.running = {}
30         setmetatable(self.running, {__mode = "k"})
31 end
32
33 function Luci.handle_head(self, ...)
34         local response, sourceout = self:handle_get(...)
35         return response
36 end
37
38 function Luci.handle_post(self, ...)
39         return self:handle_get(...)
40 end
41
42 function Luci.handle_get(self, request, sourcein, sinkerr)
43         local r = luci.http.Request(
44                 request.env,
45                 sourcein,
46                 sinkerr
47         )
48
49         local res, id, data1, data2 = true, 0, nil, nil
50         local headers = {}
51         local status = 200
52         local active = true
53
54         local x = coroutine.create(luci.dispatcher.httpdispatch)
55         while not id or id < 3 do
56                 coroutine.yield()
57
58                 res, id, data1, data2 = coroutine.resume(x, r)
59
60                 if not res then
61                         status = 500
62                         headers["Content-Type"] = "text/plain"
63                         local err = {id}
64                         return Response( status, headers ), function() return table.remove(err) end
65                 end
66
67                 if id == 1 then
68                         status = data1
69                 elseif id == 2 then
70                         headers[data1] = data2
71                 end
72         end
73
74         local function iter()
75                 local res, id, data = coroutine.resume(x)
76                 if not res then
77                         return nil, id
78                 elseif not id or not active then
79                         return true
80                 elseif id == 5 then
81                         active = false
82
83                         while (coroutine.resume(x)) do
84                         end
85
86                         return nil
87                 elseif id == 4 then
88                         return data
89                 end
90                 if coroutine.status(x) == "dead" then
91                         return nil
92                 end
93         end
94
95         return Response(status, headers), iter
96 end