util.xmppstream: Reject XML comments, processing instructions and (if supported by...
authorMatthew Wild <mwild1@gmail.com>
Wed, 1 Jun 2011 22:02:10 +0000 (23:02 +0100)
committerMatthew Wild <mwild1@gmail.com>
Wed, 1 Jun 2011 22:02:10 +0000 (23:02 +0100)
util/xmppstream.lua

index f92c5ffa29a26b2706ab8b6e083d026bb7475555..d1cb652d7a85af52dc5b34ce718793fd740fa671 100644 (file)
@@ -9,13 +9,10 @@
 
 local lxp = require "lxp";
 local st = require "util.stanza";
-local stanza_mt = st.stanza_mt;
 
 local tostring = tostring;
 local t_insert = table.insert;
 local t_concat = table.concat;
-local t_remove = table.remove;
-local setmetatable = setmetatable;
 
 local default_log = require "util.logger".init("xmppstream");
 
@@ -66,13 +63,12 @@ function new_sax_handlers(session, stream_callbacks)
        
        local stream_default_ns = stream_callbacks.default_ns;
        
-       local stack = {};
        local chardata, stanza = {};
        local non_streamns_depth = 0;
        function xml_handlers:StartElement(tagname, attr)
                if stanza and #chardata > 0 then
                        -- We have some character data in the buffer
-                       t_insert(stanza, t_concat(chardata));
+                       stanza:text(t_concat(chardata));
                        chardata = {};
                end
                local curr_ns,name = tagname:match(ns_pattern);
@@ -116,13 +112,9 @@ function new_sax_handlers(session, stream_callbacks)
                                cb_error(session, "invalid-top-level-element");
                        end
                        
-                       stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
+                       stanza = st.stanza(name, attr);
                else -- we are inside a stanza, so add a tag
-                       t_insert(stack, stanza);
-                       local oldstanza = stanza;
-                       stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
-                       t_insert(oldstanza, stanza);
-                       t_insert(oldstanza.tags, stanza);
+                       stanza:tag(name, attr);
                end
        end
        function xml_handlers:CharacterData(data)
@@ -137,11 +129,12 @@ function new_sax_handlers(session, stream_callbacks)
                if stanza then
                        if #chardata > 0 then
                                -- We have some character data in the buffer
-                               t_insert(stanza, t_concat(chardata));
+                               stanza:text(t_concat(chardata));
                                chardata = {};
                        end
                        -- Complete stanza
-                       if #stack == 0 then
+                       local last_add = stanza.last_add;
+                       if not last_add or #last_add == 0 then
                                if tagname ~= stream_error_tag then
                                        cb_handlestanza(session, stanza);
                                else
@@ -149,7 +142,7 @@ function new_sax_handlers(session, stream_callbacks)
                                end
                                stanza = nil;
                        else
-                               stanza = t_remove(stack);
+                               stanza:up();
                        end
                else
                        if tagname == stream_tag then
@@ -164,7 +157,6 @@ function new_sax_handlers(session, stream_callbacks)
                                cb_error(session, "parse-error", "unexpected-element-close", name);
                        end
                        stanza, chardata = nil, {};
-                       stack = {};
                end
        end
        
@@ -181,7 +173,6 @@ function new_sax_handlers(session, stream_callbacks)
        
        local function reset()
                stanza, chardata = nil, {};
-               stack = {};
        end
        
        local function set_session(stream, new_session)