author | Matthew Wild <mwild1@gmail.com> |
Wed, 01 May 2013 13:54:31 +0100 | |
branch | sasl |
changeset 5555 | 70a7ef4b6aaa |
parent 1522 | 569d58d21612 |
child 2923 | b7049746bd29 |
permissions | -rw-r--r-- |
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
700
diff
changeset
|
1 |
-- Prosody IM |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
700
diff
changeset
|
2 |
-- Copyright (C) 2008-2009 Matthew Wild |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
700
diff
changeset
|
3 |
-- Copyright (C) 2008-2009 Waqas Hussain |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
700
diff
changeset
|
4 |
-- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
700
diff
changeset
|
5 |
-- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
700
diff
changeset
|
6 |
-- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
700
diff
changeset
|
7 |
-- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
700
diff
changeset
|
8 |
|
699
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
local httpserver = require "net.httpserver"; |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
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
|
12 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
local log = log; |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
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
|
16 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
local control = require "core.actions".actions; |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
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
|
21 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
local function urldecode(s) |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
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
|
24 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
local function query_to_table(query) |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
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
|
28 |
if query:match("=") then |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
local params = {}; |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
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
|
31 |
if k and v then |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
params[urldecode(k)] = urldecode(v); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
return params; |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
else |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
return urldecode(query); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
end |
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 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
local http_path = { http_base }; |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
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
|
46 |
local path = request.url.path:gsub("^/[^/]+/", ""); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 |
local curr = control; |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
for comp in path:gmatch("([^/]+)") do |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 |
curr = curr[comp]; |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 |
if not curr then |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 |
return response_404; |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
if type(curr) == "table" then |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
local s = {}; |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 |
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
|
60 |
t_insert(s, tostring(k)); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 |
t_insert(s, " = "); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
if type(v) == "function" then |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 |
t_insert(s, "action") |
700
9666ad50a353
mod_actions_http: Show tables as 'list's
Matthew Wild <mwild1@gmail.com>
parents:
699
diff
changeset
|
64 |
elseif type(v) == "table" then |
9666ad50a353
mod_actions_http: Show tables as 'list's
Matthew Wild <mwild1@gmail.com>
parents:
699
diff
changeset
|
65 |
t_insert(s, "list"); |
699
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 |
else |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
t_insert(s, tostring(v)); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 |
t_insert(s, "\n"); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 |
return t_concat(s); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
elseif type(curr) == "function" then |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
if not ok then |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 |
return "EPIC FAIL: "..tostring(ret1); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 |
elseif not ret1 then |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 |
return "FAIL: "..tostring(ret2); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 |
else |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 |
return "OK: "..tostring(ret2); |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 |
end |
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 |
|
30f5dcb654bd
Add mod_actions_http for executing actions through HTTP
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
httpserver.new{ port = 5280, base = "control", handler = handle_request, ssl = false } |