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";
13 local tostring = tostring;
14 local t_insert = table.insert;
15 local t_concat = table.concat;
17 local default_log = require "util.logger".init("xmppstream");
19 -- COMPAT: w/LuaExpat 1.1.0
20 local lxp_supports_doctype = pcall(lxp.new, { StartDoctypeDecl = false });
22 if not lxp_supports_doctype then
23 default_log("warn", "The version of LuaExpat on your system leaves Prosody "
24 .."vulnerable to denial-of-service attacks. You should upgrade to "
25 .."LuaExpat 1.1.1 or higher as soon as possible. See "
26 .."http://prosody.im/doc/depends#luaexpat for more information.");
33 local new_parser = lxp.new;
36 ["http://www.w3.org/XML/1998/namespace"] = "xml";
39 local xmlns_streams = "http://etherx.jabber.org/streams";
41 local ns_separator = "\1";
42 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
44 _M.ns_separator = ns_separator;
45 _M.ns_pattern = ns_pattern;
47 function new_sax_handlers(session, stream_callbacks)
48 local xml_handlers = {};
50 local log = session.log or default_log;
52 local cb_streamopened = stream_callbacks.streamopened;
53 local cb_streamclosed = stream_callbacks.streamclosed;
54 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
55 local cb_handlestanza = stream_callbacks.handlestanza;
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;
66 local chardata, stanza = {};
67 local non_streamns_depth = 0;
68 function xml_handlers:StartElement(tagname, attr)
69 if stanza and #chardata > 0 then
70 -- We have some character data in the buffer
71 stanza:text(t_concat(chardata));
74 local curr_ns,name = tagname:match(ns_pattern);
76 curr_ns, name = "", curr_ns;
79 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
81 non_streamns_depth = non_streamns_depth + 1;
88 local ns, nm = k:match(ns_pattern);
92 attr[ns..":"..nm] = attr[k];
98 if not stanza then --if we are not currently inside a stanza
99 if session.notopen then
100 if tagname == stream_tag then
101 non_streamns_depth = 0;
102 if cb_streamopened then
103 cb_streamopened(session, attr);
106 -- Garbage before stream?
107 cb_error(session, "no-stream");
111 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
112 cb_error(session, "invalid-top-level-element");
115 stanza = st.stanza(name, attr);
116 else -- we are inside a stanza, so add a tag
117 stanza:tag(name, attr);
120 function xml_handlers:CharacterData(data)
122 t_insert(chardata, data);
125 function xml_handlers:EndElement(tagname)
126 if non_streamns_depth > 0 then
127 non_streamns_depth = non_streamns_depth - 1;
130 if #chardata > 0 then
131 -- We have some character data in the buffer
132 stanza:text(t_concat(chardata));
136 local last_add = stanza.last_add;
137 if not last_add or #last_add == 0 then
138 if tagname ~= stream_error_tag then
139 cb_handlestanza(session, stanza);
141 cb_error(session, "stream-error", stanza);
148 if tagname == stream_tag then
149 if cb_streamclosed then
150 cb_streamclosed(session);
153 local curr_ns,name = tagname:match(ns_pattern);
155 curr_ns, name = "", curr_ns;
157 cb_error(session, "parse-error", "unexpected-element-close", name);
159 stanza, chardata = nil, {};
163 local function restricted_handler()
164 cb_error(session, "parse-error", "restricted-xml", "Restricted XML, see RFC 6120 section 11.1.");
167 if lxp_supports_doctype then
168 xml_handlers.StartDoctypeDecl = restricted_handler;
170 xml_handlers.Comment = restricted_handler;
171 xml_handlers.StartCdataSection = restricted_handler;
172 xml_handlers.ProcessingInstruction = restricted_handler;
174 local function reset()
175 stanza, chardata = nil, {};
178 local function set_session(stream, new_session)
179 session = new_session;
180 log = new_session.log or default_log;
183 return xml_handlers, { reset = reset, set_session = set_session };
186 function new(session, stream_callbacks)
187 local handlers, meta = new_sax_handlers(session, stream_callbacks);
188 local parser = new_parser(handlers, ns_separator);
189 local parse = parser.parse;
193 parser = new_parser(handlers, ns_separator);
194 parse = parser.parse;
197 feed = function (self, data)
198 return parse(parser, data);
200 set_session = meta.set_session;