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.
9 local lxp = require "lxp";
10 local st = require "util.stanza";
11 local stanza_mt = st.stanza_mt;
14 local tostring = tostring;
15 local t_insert = table.insert;
16 local t_concat = table.concat;
17 local t_remove = table.remove;
18 local setmetatable = setmetatable;
20 -- COMPAT: w/LuaExpat 1.1.0
21 local lxp_supports_doctype = pcall(lxp.new, { StartDoctypeDecl = false });
22 local lxp_supports_xmldecl = pcall(lxp.new, { XmlDecl = false });
23 local lxp_supports_bytecount = not not lxp.new({}).getcurrentbytecount;
25 local default_stanza_size_limit = 1024*1024*10; -- 10MB
29 local new_parser = lxp.new;
31 local xml_namespace = {
32 ["http://www.w3.org/XML/1998/namespace\1lang"] = "xml:lang";
33 ["http://www.w3.org/XML/1998/namespace\1space"] = "xml:space";
34 ["http://www.w3.org/XML/1998/namespace\1base"] = "xml:base";
35 ["http://www.w3.org/XML/1998/namespace\1id"] = "xml:id";
38 local xmlns_streams = "http://etherx.jabber.org/streams";
40 local ns_separator = "\1";
41 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
43 _M.ns_separator = ns_separator;
44 _M.ns_pattern = ns_pattern;
46 local function dummy_cb() end
48 function new_sax_handlers(session, stream_callbacks, cb_handleprogress)
49 local xml_handlers = {};
51 local cb_streamopened = stream_callbacks.streamopened;
52 local cb_streamclosed = stream_callbacks.streamclosed;
53 local cb_error = stream_callbacks.error or function(session, e, stanza) error("XML stream error: "..tostring(e)..(stanza and ": "..tostring(stanza) or ""),2); end;
54 local cb_handlestanza = stream_callbacks.handlestanza;
55 cb_handleprogress = cb_handleprogress or dummy_cb;
57 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
58 local stream_tag = stream_callbacks.stream_tag or "stream";
59 if stream_ns ~= "" then
60 stream_tag = stream_ns..ns_separator..stream_tag;
62 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
64 local stream_default_ns = stream_callbacks.default_ns;
67 local chardata, stanza = {};
68 local stanza_size = 0;
69 local non_streamns_depth = 0;
70 function xml_handlers:StartElement(tagname, attr)
71 if stanza and #chardata > 0 then
72 -- We have some character data in the buffer
73 t_insert(stanza, t_concat(chardata));
76 local curr_ns,name = tagname:match(ns_pattern);
78 curr_ns, name = "", curr_ns;
81 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
83 non_streamns_depth = non_streamns_depth + 1;
89 local xmlk = xml_namespace[k];
96 if not stanza then --if we are not currently inside a stanza
97 if lxp_supports_bytecount then
98 stanza_size = self:getcurrentbytecount();
100 if session.notopen then
101 if tagname == stream_tag then
102 non_streamns_depth = 0;
103 if cb_streamopened then
104 if lxp_supports_bytecount then
105 cb_handleprogress(stanza_size);
108 cb_streamopened(session, attr);
111 -- Garbage before stream?
112 cb_error(session, "no-stream", tagname);
116 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
117 cb_error(session, "invalid-top-level-element");
120 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
121 else -- we are inside a stanza, so add a tag
122 if lxp_supports_bytecount then
123 stanza_size = stanza_size + self:getcurrentbytecount();
125 t_insert(stack, stanza);
126 local oldstanza = stanza;
127 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
128 t_insert(oldstanza, stanza);
129 t_insert(oldstanza.tags, stanza);
132 if lxp_supports_xmldecl then
133 function xml_handlers:XmlDecl(version, encoding, standalone)
134 if lxp_supports_bytecount then
135 cb_handleprogress(self:getcurrentbytecount());
139 function xml_handlers:StartCdataSection()
140 if lxp_supports_bytecount then
142 stanza_size = stanza_size + self:getcurrentbytecount();
144 cb_handleprogress(self:getcurrentbytecount());
148 function xml_handlers:EndCdataSection()
149 if lxp_supports_bytecount then
151 stanza_size = stanza_size + self:getcurrentbytecount();
153 cb_handleprogress(self:getcurrentbytecount());
157 function xml_handlers:CharacterData(data)
159 if lxp_supports_bytecount then
160 stanza_size = stanza_size + self:getcurrentbytecount();
162 t_insert(chardata, data);
163 elseif lxp_supports_bytecount then
164 cb_handleprogress(self:getcurrentbytecount());
167 function xml_handlers:EndElement(tagname)
168 if lxp_supports_bytecount then
169 stanza_size = stanza_size + self:getcurrentbytecount()
171 if non_streamns_depth > 0 then
172 non_streamns_depth = non_streamns_depth - 1;
175 if #chardata > 0 then
176 -- We have some character data in the buffer
177 t_insert(stanza, t_concat(chardata));
182 if lxp_supports_bytecount then
183 cb_handleprogress(stanza_size);
186 if tagname ~= stream_error_tag then
187 cb_handlestanza(session, stanza);
189 cb_error(session, "stream-error", stanza);
193 stanza = t_remove(stack);
196 if cb_streamclosed then
197 cb_streamclosed(session);
202 local function restricted_handler(parser)
203 cb_error(session, "parse-error", "restricted-xml", "Restricted XML, see RFC 6120 section 11.1.");
204 if not parser.stop or not parser:stop() then
205 error("Failed to abort parsing");
209 if lxp_supports_doctype then
210 xml_handlers.StartDoctypeDecl = restricted_handler;
212 xml_handlers.Comment = restricted_handler;
213 xml_handlers.ProcessingInstruction = restricted_handler;
215 local function reset()
216 stanza, chardata, stanza_size = nil, {}, 0;
220 local function set_session(stream, new_session)
221 session = new_session;
224 return xml_handlers, { reset = reset, set_session = set_session };
227 function new(session, stream_callbacks, stanza_size_limit)
228 -- Used to track parser progress (e.g. to enforce size limits)
229 local n_outstanding_bytes = 0;
230 local handle_progress;
231 if lxp_supports_bytecount then
232 function handle_progress(n_parsed_bytes)
233 n_outstanding_bytes = n_outstanding_bytes - n_parsed_bytes;
235 stanza_size_limit = stanza_size_limit or default_stanza_size_limit;
236 elseif stanza_size_limit then
237 error("Stanza size limits are not supported on this version of LuaExpat")
240 local handlers, meta = new_sax_handlers(session, stream_callbacks, handle_progress);
241 local parser = new_parser(handlers, ns_separator, false);
242 local parse = parser.parse;
244 function session.open_stream(session, from, to)
245 local send = session.sends2s or session.send;
248 ["xmlns:stream"] = "http://etherx.jabber.org/streams",
250 xmlns = stream_callbacks.default_ns,
251 version = session.version and (session.version > 0 and "1.0" or nil),
252 id = session.streamid,
253 from = from or session.host, to = to,
255 if session.stream_attrs then
256 session:stream_attrs(from, to, attr)
258 send("<?xml version='1.0'?>");
259 send(st.stanza("stream:stream", attr):top_tag());
265 parser = new_parser(handlers, ns_separator, false);
266 parse = parser.parse;
267 n_outstanding_bytes = 0;
270 feed = function (self, data)
271 if lxp_supports_bytecount then
272 n_outstanding_bytes = n_outstanding_bytes + #data;
274 local ok, err = parse(parser, data);
275 if lxp_supports_bytecount and n_outstanding_bytes > stanza_size_limit then
276 return nil, "stanza-too-large";
280 set_session = meta.set_session;