From 1980849de01d6eac16e59e8d6986e7d1796c73fe Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 12 Jan 2009 04:09:02 +0000 Subject: Add mod_actions_http for executing actions through HTTP --- plugins/mod_actions_http.lua | 76 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 plugins/mod_actions_http.lua (limited to 'plugins/mod_actions_http.lua') diff --git a/plugins/mod_actions_http.lua b/plugins/mod_actions_http.lua new file mode 100644 index 00000000..327bd670 --- /dev/null +++ b/plugins/mod_actions_http.lua @@ -0,0 +1,76 @@ + +local httpserver = require "net.httpserver"; +local t_concat, t_insert = table.concat, table.insert; + +local log = log; + +local response_404 = { status = "404 Not Found", body = "

No such action

Sorry, I don't have the action you requested" }; + +local control = require "core.actions".actions; + + +local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = string.char(tonumber("0x"..k)); return t[k]; end }); + +local function urldecode(s) + return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); +end + +local function query_to_table(query) + if type(query) == "string" and #query > 0 then + if query:match("=") then + local params = {}; + for k, v in query:gmatch("&?([^=%?]+)=([^&%?]+)&?") do + if k and v then + params[urldecode(k)] = urldecode(v); + end + end + return params; + else + return urldecode(query); + end + end +end + + + +local http_path = { http_base }; +local function handle_request(method, body, request) + local path = request.url.path:gsub("^/[^/]+/", ""); + + local curr = control; + + for comp in path:gmatch("([^/]+)") do + curr = curr[comp]; + if not curr then + return response_404; + end + end + + if type(curr) == "table" then + local s = {}; + for k,v in pairs(curr) do + t_insert(s, tostring(k)); + t_insert(s, " = "); + if type(v) == "function" then + t_insert(s, "action") + else + t_insert(s, tostring(v)); + end + t_insert(s, "\n"); + end + return t_concat(s); + elseif type(curr) == "function" then + local params = query_to_table(request.url.query); + params.host = request.headers.host:gsub(":%d+", ""); + local ok, ret1, ret2 = pcall(curr, params); + if not ok then + return "EPIC FAIL: "..tostring(ret1); + elseif not ret1 then + return "FAIL: "..tostring(ret2); + else + return "OK: "..tostring(ret2); + end + end +end + +httpserver.new{ port = 5280, base = "control", handler = handle_request, ssl = false } \ No newline at end of file -- cgit v1.2.3 From 393b7385885eb8ec8e9e93daf31a365480b127a9 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 12 Jan 2009 04:13:05 +0000 Subject: mod_actions_http: Show tables as 'list's --- plugins/mod_actions_http.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'plugins/mod_actions_http.lua') diff --git a/plugins/mod_actions_http.lua b/plugins/mod_actions_http.lua index 327bd670..43370a41 100644 --- a/plugins/mod_actions_http.lua +++ b/plugins/mod_actions_http.lua @@ -53,6 +53,8 @@ local function handle_request(method, body, request) t_insert(s, " = "); if type(v) == "function" then t_insert(s, "action") + elseif type(v) == "table" then + t_insert(s, "list"); else t_insert(s, tostring(v)); end -- cgit v1.2.3