2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local lxp = require "lxp";
11 local st = require "util.stanza";
12 local stanza_mt = st.stanza_mt;
15 local tostring = tostring;
16 local t_insert = table.insert;
17 local t_concat = table.concat;
18 local t_remove = table.remove;
19 local setmetatable = setmetatable;
21 -- COMPAT: w/LuaExpat 1.1.0
22 local lxp_supports_doctype = pcall(lxp.new, { StartDoctypeDecl = false });
26 local new_parser = lxp.new;
29 ["http://www.w3.org/XML/1998/namespace"] = "xml";
32 local xmlns_streams = "http://etherx.jabber.org/streams";
34 local ns_separator = "\1";
35 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
37 _M.ns_separator = ns_separator;
38 _M.ns_pattern = ns_pattern;
40 function new_sax_handlers(session, stream_callbacks)
41 local xml_handlers = {};
43 local cb_streamopened = stream_callbacks.streamopened;
44 local cb_streamclosed = stream_callbacks.streamclosed;
45 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
46 local cb_handlestanza = stream_callbacks.handlestanza;
48 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
49 local stream_tag = stream_callbacks.stream_tag or "stream";
50 if stream_ns ~= "" then
51 stream_tag = stream_ns..ns_separator..stream_tag;
53 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
55 local stream_default_ns = stream_callbacks.default_ns;
58 local chardata, stanza = {};
59 local non_streamns_depth = 0;
60 function xml_handlers:StartElement(tagname, attr)
61 if stanza and #chardata > 0 then
62 -- We have some character data in the buffer
63 t_insert(stanza, t_concat(chardata));
66 local curr_ns,name = tagname:match(ns_pattern);
68 curr_ns, name = "", curr_ns;
71 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
73 non_streamns_depth = non_streamns_depth + 1;
80 local ns, nm = k:match(ns_pattern);
84 attr[ns..":"..nm] = attr[k];
90 if not stanza then --if we are not currently inside a stanza
91 if session.notopen then
92 if tagname == stream_tag then
93 non_streamns_depth = 0;
94 if cb_streamopened then
95 cb_streamopened(session, attr);
98 -- Garbage before stream?
99 cb_error(session, "no-stream");
103 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
104 cb_error(session, "invalid-top-level-element");
107 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
108 else -- we are inside a stanza, so add a tag
109 t_insert(stack, stanza);
110 local oldstanza = stanza;
111 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
112 t_insert(oldstanza, stanza);
113 t_insert(oldstanza.tags, stanza);
116 function xml_handlers:CharacterData(data)
118 t_insert(chardata, data);
121 function xml_handlers:EndElement(tagname)
122 if non_streamns_depth > 0 then
123 non_streamns_depth = non_streamns_depth - 1;
126 if #chardata > 0 then
127 -- We have some character data in the buffer
128 t_insert(stanza, t_concat(chardata));
133 if tagname ~= stream_error_tag then
134 cb_handlestanza(session, stanza);
136 cb_error(session, "stream-error", stanza);
140 stanza = t_remove(stack);
143 if tagname == stream_tag then
144 if cb_streamclosed then
145 cb_streamclosed(session);
148 local curr_ns,name = tagname:match(ns_pattern);
150 curr_ns, name = "", curr_ns;
152 cb_error(session, "parse-error", "unexpected-element-close", name);
154 stanza, chardata = nil, {};
159 local function restricted_handler(parser)
160 cb_error(session, "parse-error", "restricted-xml", "Restricted XML, see RFC 6120 section 11.1.");
161 if not parser.stop or not parser:stop() then
162 error("Failed to abort parsing");
166 if lxp_supports_doctype then
167 xml_handlers.StartDoctypeDecl = restricted_handler;
169 xml_handlers.Comment = restricted_handler;
170 xml_handlers.ProcessingInstruction = restricted_handler;
172 local function reset()
173 stanza, chardata = nil, {};
177 local function set_session(stream, new_session)
178 session = new_session;
181 return xml_handlers, { reset = reset, set_session = set_session };
184 function new(session, stream_callbacks)
185 local handlers, meta = new_sax_handlers(session, stream_callbacks);
186 local parser = new_parser(handlers, ns_separator);
187 local parse = parser.parse;
191 parser = new_parser(handlers, ns_separator);
192 parse = parser.parse;
195 feed = function (self, data)
196 return parse(parser, data);
198 set_session = meta.set_session;