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;
28 local xml_namespace = {
29 ["http://www.w3.org/XML/1998/namespace\1lang"] = "xml:lang";
30 ["http://www.w3.org/XML/1998/namespace\1space"] = "xml:space";
31 ["http://www.w3.org/XML/1998/namespace\1base"] = "xml:base";
32 ["http://www.w3.org/XML/1998/namespace\1id"] = "xml:id";
35 local xmlns_streams = "http://etherx.jabber.org/streams";
37 local ns_separator = "\1";
38 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
40 _M.ns_separator = ns_separator;
41 _M.ns_pattern = ns_pattern;
43 function new_sax_handlers(session, stream_callbacks)
44 local xml_handlers = {};
46 local cb_streamopened = stream_callbacks.streamopened;
47 local cb_streamclosed = stream_callbacks.streamclosed;
48 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
49 local cb_handlestanza = stream_callbacks.handlestanza;
51 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
52 local stream_tag = stream_callbacks.stream_tag or "stream";
53 if stream_ns ~= "" then
54 stream_tag = stream_ns..ns_separator..stream_tag;
56 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
58 local stream_default_ns = stream_callbacks.default_ns;
61 local chardata, stanza = {};
62 local non_streamns_depth = 0;
63 function xml_handlers:StartElement(tagname, attr)
64 if stanza and #chardata > 0 then
65 -- We have some character data in the buffer
66 t_insert(stanza, t_concat(chardata));
69 local curr_ns,name = tagname:match(ns_pattern);
71 curr_ns, name = "", curr_ns;
74 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
76 non_streamns_depth = non_streamns_depth + 1;
82 local xmlk = xml_namespace[k];
89 if not stanza then --if we are not currently inside a stanza
90 if session.notopen then
91 if tagname == stream_tag then
92 non_streamns_depth = 0;
93 if cb_streamopened then
94 cb_streamopened(session, attr);
97 -- Garbage before stream?
98 cb_error(session, "no-stream");
102 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
103 cb_error(session, "invalid-top-level-element");
106 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
107 else -- we are inside a stanza, so add a tag
108 t_insert(stack, stanza);
109 local oldstanza = stanza;
110 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
111 t_insert(oldstanza, stanza);
112 t_insert(oldstanza.tags, stanza);
115 function xml_handlers:CharacterData(data)
117 t_insert(chardata, data);
120 function xml_handlers:EndElement(tagname)
121 if non_streamns_depth > 0 then
122 non_streamns_depth = non_streamns_depth - 1;
125 if #chardata > 0 then
126 -- We have some character data in the buffer
127 t_insert(stanza, t_concat(chardata));
132 if tagname ~= stream_error_tag then
133 cb_handlestanza(session, stanza);
135 cb_error(session, "stream-error", stanza);
139 stanza = t_remove(stack);
142 if cb_streamclosed then
143 cb_streamclosed(session);
148 local function restricted_handler(parser)
149 cb_error(session, "parse-error", "restricted-xml", "Restricted XML, see RFC 6120 section 11.1.");
150 if not parser.stop or not parser:stop() then
151 error("Failed to abort parsing");
155 if lxp_supports_doctype then
156 xml_handlers.StartDoctypeDecl = restricted_handler;
158 xml_handlers.Comment = restricted_handler;
159 xml_handlers.ProcessingInstruction = restricted_handler;
161 local function reset()
162 stanza, chardata = nil, {};
166 local function set_session(stream, new_session)
167 session = new_session;
170 return xml_handlers, { reset = reset, set_session = set_session };
173 function new(session, stream_callbacks)
174 local handlers, meta = new_sax_handlers(session, stream_callbacks);
175 local parser = new_parser(handlers, ns_separator);
176 local parse = parser.parse;
180 parser = new_parser(handlers, ns_separator);
181 parse = parser.parse;
184 feed = function (self, data)
185 return parse(parser, data);
187 set_session = meta.set_session;