2 local httpserver = require "net.httpserver";
3 local t_concat, t_insert = table.concat, table.insert;
7 local response_404 = { status = "404 Not Found", body = "<h1>No such action</h1>Sorry, I don't have the action you requested" };
9 local control = require "core.actions".actions;
12 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = string.char(tonumber("0x"..k)); return t[k]; end });
14 local function urldecode(s)
15 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
18 local function query_to_table(query)
19 if type(query) == "string" and #query > 0 then
20 if query:match("=") then
22 for k, v in query:gmatch("&?([^=%?]+)=([^&%?]+)&?") do
24 params[urldecode(k)] = urldecode(v);
29 return urldecode(query);
36 local http_path = { http_base };
37 local function handle_request(method, body, request)
38 local path = request.url.path:gsub("^/[^/]+/", "");
42 for comp in path:gmatch("([^/]+)") do
49 if type(curr) == "table" then
51 for k,v in pairs(curr) do
52 t_insert(s, tostring(k));
54 if type(v) == "function" then
56 elseif type(v) == "table" then
59 t_insert(s, tostring(v));
64 elseif type(curr) == "function" then
65 local params = query_to_table(request.url.query);
66 params.host = request.headers.host:gsub(":%d+", "");
67 local ok, ret1, ret2 = pcall(curr, params);
69 return "EPIC FAIL: "..tostring(ret1);
71 return "FAIL: "..tostring(ret2);
73 return "OK: "..tostring(ret2);
78 httpserver.new{ port = 5280, base = "control", handler = handle_request, ssl = false }