Software /
code /
prosody
Comparison
plugins/mod_actions_http.lua @ 699:30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 12 Jan 2009 04:09:02 +0000 |
child | 700:9666ad50a353 |
comparison
equal
deleted
inserted
replaced
698:d8a678e40a0a | 699:30f5dcb654bd |
---|---|
1 | |
2 local httpserver = require "net.httpserver"; | |
3 local t_concat, t_insert = table.concat, table.insert; | |
4 | |
5 local log = log; | |
6 | |
7 local response_404 = { status = "404 Not Found", body = "<h1>No such action</h1>Sorry, I don't have the action you requested" }; | |
8 | |
9 local control = require "core.actions".actions; | |
10 | |
11 | |
12 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = string.char(tonumber("0x"..k)); return t[k]; end }); | |
13 | |
14 local function urldecode(s) | |
15 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); | |
16 end | |
17 | |
18 local function query_to_table(query) | |
19 if type(query) == "string" and #query > 0 then | |
20 if query:match("=") then | |
21 local params = {}; | |
22 for k, v in query:gmatch("&?([^=%?]+)=([^&%?]+)&?") do | |
23 if k and v then | |
24 params[urldecode(k)] = urldecode(v); | |
25 end | |
26 end | |
27 return params; | |
28 else | |
29 return urldecode(query); | |
30 end | |
31 end | |
32 end | |
33 | |
34 | |
35 | |
36 local http_path = { http_base }; | |
37 local function handle_request(method, body, request) | |
38 local path = request.url.path:gsub("^/[^/]+/", ""); | |
39 | |
40 local curr = control; | |
41 | |
42 for comp in path:gmatch("([^/]+)") do | |
43 curr = curr[comp]; | |
44 if not curr then | |
45 return response_404; | |
46 end | |
47 end | |
48 | |
49 if type(curr) == "table" then | |
50 local s = {}; | |
51 for k,v in pairs(curr) do | |
52 t_insert(s, tostring(k)); | |
53 t_insert(s, " = "); | |
54 if type(v) == "function" then | |
55 t_insert(s, "action") | |
56 else | |
57 t_insert(s, tostring(v)); | |
58 end | |
59 t_insert(s, "\n"); | |
60 end | |
61 return t_concat(s); | |
62 elseif type(curr) == "function" then | |
63 local params = query_to_table(request.url.query); | |
64 params.host = request.headers.host:gsub(":%d+", ""); | |
65 local ok, ret1, ret2 = pcall(curr, params); | |
66 if not ok then | |
67 return "EPIC FAIL: "..tostring(ret1); | |
68 elseif not ret1 then | |
69 return "FAIL: "..tostring(ret2); | |
70 else | |
71 return "OK: "..tostring(ret2); | |
72 end | |
73 end | |
74 end | |
75 | |
76 httpserver.new{ port = 5280, base = "control", handler = handle_request, ssl = false } |