* Rewrote Luci to be coroutine-safe allowing the use of non-forking webservers
[project/luci.git] / libs / sgi-webuci / root / usr / lib / boa / luci.lua
1 module("luci-plugin", package.seeall)
2
3 function normalize(path)
4 local newpath
5 while newpath ~= path do
6 if (newpath) then
7 path = newpath
8 end
9 newpath = string.gsub(path, "/[^/]+/../", "/")
10 end
11 return newpath
12 end
13
14 function init(path)
15 -- NB: path points to ROOT/usr/lib/boa, change it to /usr/lib/lua
16 root = normalize(path .. '/../../../')
17 path = normalize(path .. '/../lua/')
18 package.cpath = path..'?.so;'..package.cpath
19 package.path = path..'?.lua;'..package.path
20
21 require("luci.dispatcher")
22 require("luci.sgi.webuci")
23 require("uci")
24
25 if (root ~= '/') then
26 -- Entering dummy mode
27 uci.set_savedir(root..'/tmp/.uci')
28 uci.set_confdir(root..'/etc/config')
29
30 luci.sys.hostname = function() return "" end
31 luci.sys.loadavg = function() return 0,0,0,0,0 end
32 luci.sys.reboot = function() return end
33 luci.sys.sysinfo = function() return "","","" end
34 luci.sys.syslog = function() return "" end
35
36 luci.sys.net.arptable = function() return {} end
37 luci.sys.net.devices = function() return {} end
38 luci.sys.net.routes = function() return {} end
39 luci.sys.wifi.getiwconfig = function() return {} end
40 luci.sys.wifi.iwscan = function() return {} end
41
42 luci.sys.user.checkpasswd = function() return true end
43 luci.http.basic_auth = function() return true end
44 end
45 end
46
47 function prepare_req(uri)
48 luci.dispatcher.createindex()
49 env = {}
50 env.REQUEST_URI = uri
51 -- TODO: setting luci-plugin.reload = true allows this function to trigger a context reload
52 end
53
54 function handle_req(context)
55 env.SERVER_PROTOCOL = context.server_proto
56 env.REMOTE_ADDR = context.remote_addr
57 env.REQUEST_METHOD = context.request_method
58 env.PATH_INFO = context.uri
59 env.REMOTE_PORT = context.remote_port
60 env.SERVER_ADDR = context.server_addr
61 env.SCRIPT_NAME = env.REQUEST_URI:sub(1, #env.REQUEST_URI - #env.PATH_INFO)
62
63 luci.sgi.webuci.run(env, vars)
64 end