core/xmlhandlers.lua

branch
s2s
changeset 145
fbb3a4ff9cf1
parent 99
ba08b8a4eeef
child 146
3826ca244eb6
equal deleted inserted replaced
144:ed78c1a0401e 145:fbb3a4ff9cf1
9 local t_remove = table.remove; 9 local t_remove = table.remove;
10 local t_concat = table.concat; 10 local t_concat = table.concat;
11 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end 11 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end
12 local sm_destroy_session = import("core.sessionmanager", "destroy_session"); 12 local sm_destroy_session = import("core.sessionmanager", "destroy_session");
13 13
14 local default_log = require "util.logger".init("xmlhandlers");
15
14 local error = error; 16 local error = error;
15 17
16 module "xmlhandlers" 18 module "xmlhandlers"
17 19
18 function init_xmlhandlers(session, streamopened) 20 function init_xmlhandlers(session, streamopened)
19 local ns_stack = { "" }; 21 local ns_stack = { "" };
20 local curr_ns = ""; 22 local curr_ns = "";
21 local curr_tag; 23 local curr_tag;
22 local chardata = {}; 24 local chardata = {};
23 local xml_handlers = {}; 25 local xml_handlers = {};
24 local log = session.log; 26 local log = session.log or default_log;
25 local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end 27 local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
26 28
27 local send = session.send; 29 local send = session.send;
28 30
29 local stanza 31 local stanza
31 if stanza and #chardata > 0 then 33 if stanza and #chardata > 0 then
32 -- We have some character data in the buffer 34 -- We have some character data in the buffer
33 stanza:text(t_concat(chardata)); 35 stanza:text(t_concat(chardata));
34 chardata = {}; 36 chardata = {};
35 end 37 end
36 curr_ns,name = name:match("^(.+):(%w+)$"); 38 log("debug", "Start element: %s", tostring(name));
37 if not stanza then 39 curr_ns,name = name:match("^(.+):([%w%-]+)$");
40 attr.xmlns = curr_ns;
41
42 if not stanza then --if we are not currently inside a stanza
38 if session.notopen then 43 if session.notopen then
39 if name == "stream" then 44 if name == "stream" then
40 streamopened(session, attr); 45 streamopened(session, attr);
41 return; 46 return;
42 end 47 end
43 error("Client failed to open stream successfully"); 48 error("Client failed to open stream successfully");
44 end 49 end
45 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then 50 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
46 error("Client sent invalid top-level stanza"); 51 error("Client sent invalid top-level stanza");
47 end 52 end
48 attr.xmlns = curr_ns; 53
49 stanza = st.stanza(name, attr); --{ to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns }); 54 stanza = st.stanza(name, attr); --{ to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns });
50 curr_tag = stanza; 55 curr_tag = stanza;
51 else 56 else -- we are inside a stanza, so add a tag
52 attr.xmlns = curr_ns; 57 attr.xmlns = curr_ns;
53 stanza:tag(name, attr); 58 stanza:tag(name, attr);
54 end 59 end
55 end 60 end
56 function xml_handlers:CharacterData(data) 61 function xml_handlers:CharacterData(data)
57 if stanza then 62 if stanza then
58 t_insert(chardata, data); 63 t_insert(chardata, data);
59 end 64 end
60 end 65 end
61 function xml_handlers:EndElement(name) 66 function xml_handlers:EndElement(name)
62 curr_ns,name = name:match("^(.+):(%w+)$"); 67 curr_ns,name = name:match("^(.+):([%w%-]+)$");
63 if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 68 if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then
64 if name == "stream" then 69 if name == "stream" then
65 log("debug", "Stream closed"); 70 log("debug", "Stream closed");
66 sm_destroy_session(session); 71 sm_destroy_session(session);
67 return; 72 return;
73 elseif name == "error" then
74 error("Stream error: "..tostring(name)..": "..tostring(stanza));
68 else 75 else
69 error("XML parse error in client stream"); 76 error("XML parse error in client stream");
70 end 77 end
71 end 78 end
72 if stanza and #chardata > 0 then 79 if stanza and #chardata > 0 then

mercurial