|
1 -- Prosody IM v0.3 |
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
|
4 -- |
|
5 -- This project is MIT/X11 licensed. Please see the |
|
6 -- COPYING file in the source package for more information. |
|
7 -- |
|
8 |
|
9 |
|
10 module.host = "*" -- Global module |
|
11 |
|
12 local httpserver = require "net.httpserver"; |
|
13 local st = require "util.stanza"; |
|
14 local pcall = pcall; |
|
15 local unpack = unpack; |
|
16 local tostring = tostring; |
|
17 |
|
18 local translate_request = require "util.xmlrpc".translate_request; |
|
19 local create_response = require "util.xmlrpc".create_response; |
|
20 local create_error_response = require "util.xmlrpc".create_error_response; |
|
21 |
|
22 local entity_map = setmetatable({ |
|
23 ["amp"] = "&"; |
|
24 ["gt"] = ">"; |
|
25 ["lt"] = "<"; |
|
26 ["apos"] = "'"; |
|
27 ["quot"] = "\""; |
|
28 }, {__index = function(_, s) |
|
29 if s:sub(1,1) == "#" then |
|
30 if s:sub(2,2) == "x" then |
|
31 return string.char(tonumber(s:sub(3), 16)); |
|
32 else |
|
33 return string.char(tonumber(s:sub(2))); |
|
34 end |
|
35 end |
|
36 end |
|
37 }); |
|
38 local function xml_unescape(str) |
|
39 return (str:gsub("&(.-);", entity_map)); |
|
40 end |
|
41 local function parse_xml(xml) |
|
42 local stanza = st.stanza("root"); |
|
43 local regexp = "<([^>]*)>([^<]*)"; |
|
44 for elem, text in xml:gmatch(regexp) do |
|
45 --print("[<"..elem..">|"..text.."]"); |
|
46 if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions |
|
47 elseif elem:sub(1,1) == "/" then -- end tag |
|
48 elem = elem:sub(2); |
|
49 stanza:up(); -- TODO check for start-end tag name match |
|
50 elseif elem:sub(-1,-1) == "/" then -- empty tag |
|
51 elem = elem:sub(1,-2); |
|
52 stanza:tag(elem):up(); |
|
53 else -- start tag |
|
54 stanza:tag(elem); |
|
55 end |
|
56 if #text ~= 0 then -- text |
|
57 stanza:text(xml_unescape(text)); |
|
58 end |
|
59 end |
|
60 return stanza.tags[1]; |
|
61 end |
|
62 |
|
63 local function get_method(method) |
|
64 return function(...) |
|
65 return {method = method; args = {...}}; |
|
66 end |
|
67 end |
|
68 |
|
69 local function handle_xmlrpc_request(method, args) |
|
70 method = get_method(method); |
|
71 if not method then return create_error_response(404, "method not found"); end |
|
72 args = args or {}; |
|
73 local success, result = pcall(method, unpack(args)); |
|
74 if success then |
|
75 success, result = pcall(create_response, result or "nil"); |
|
76 if success then |
|
77 return result; |
|
78 end |
|
79 return create_error_response(500, "Error in creating response: "..result); |
|
80 end |
|
81 return create_error_response(0, result or "nil"); |
|
82 end |
|
83 |
|
84 --[[local function handle_xmpp_request(origin, stanza) |
|
85 local query = stanza.tags[1]; |
|
86 if query.name == "query" then |
|
87 if #query.tags == 1 then |
|
88 local success, method, args = pcall(translate_request, query.tags[1]); |
|
89 if success then |
|
90 local result = handle_xmlrpc_request(method, args); |
|
91 origin.send(st.reply(stanza):tag('query', {xmlns='jabber:iq:rpc'}):add_child(result)); |
|
92 else |
|
93 origin.send(st.error_reply(stanza, "modify", "bad-request", method)); |
|
94 end |
|
95 else |
|
96 origin.send(st.error_reply(stanza, "modify", "bad-request", "No content in XML-RPC request")); |
|
97 end |
|
98 else |
|
99 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
|
100 end |
|
101 end |
|
102 module:add_iq_handler({"c2s", "s2sin"}, "jabber:iq:rpc", handle_xmpp_request);]] |
|
103 |
|
104 local function handle_http_request(method, body, request) |
|
105 local stanza = body and parse_xml(body); |
|
106 if (not stanza) or request.method ~= "POST" then |
|
107 return "<html><body>You really don't look like an XML-RPC client to me... what do you want?</body></html>"; |
|
108 end |
|
109 local success, method, args = pcall(translate_request, stanza); |
|
110 if success then |
|
111 return tostring(handle_xmlrpc_request(method, args)); |
|
112 end |
|
113 return "<html><body>You really don't look like an XML-RPC client to me... what do you want?</body></html>"; |
|
114 end |
|
115 httpserver.new{ port = 9000, base = "xmlrpc", handler = handle_http_request } |