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