Changed separator between attribute names and prefixes from '|' to '\1' (optimization and cleanup).

Thu, 12 Nov 2009 13:42:44 +0500

author
Waqas Hussain <waqas20@gmail.com>
date
Thu, 12 Nov 2009 13:42:44 +0500
changeset 2077
e33658f6052c
parent 2076
de2ae849b0b3
child 2078
a5f154548154

Changed separator between attribute names and prefixes from '|' to '\1' (optimization and cleanup).

core/xmlhandlers.lua file | annotate | diff | comparison | revisions
net/xmppclient_listener.lua file | annotate | diff | comparison | revisions
net/xmppcomponent_listener.lua file | annotate | diff | comparison | revisions
net/xmppserver_listener.lua file | annotate | diff | comparison | revisions
plugins/mod_bosh.lua file | annotate | diff | comparison | revisions
util/stanza.lua file | annotate | diff | comparison | revisions
--- a/core/xmlhandlers.lua	Thu Nov 12 13:34:38 2009 +0500
+++ b/core/xmlhandlers.lua	Thu Nov 12 13:42:44 2009 +0500
@@ -29,7 +29,6 @@
 
 function init_xmlhandlers(session, stream_callbacks)
 		local ns_stack = { "" };
-		local curr_ns, name = "";
 		local curr_tag;
 		local chardata = {};
 		local xml_handlers = {};
@@ -50,7 +49,7 @@
 				stanza:text(t_concat(chardata));
 				chardata = {};
 			end
-			local curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
+			local curr_ns,name = tagname:match("^([^\1]*)\1?(.*)$");
 			if not name then
 				curr_ns, name = "", curr_ns;
 			end
@@ -63,7 +62,7 @@
 			for i=1,#attr do
 				local k = attr[i];
 				attr[i] = nil;
-				local ns, nm = k:match("^([^|]+)|?([^|]-)$")
+				local ns, nm = k:match("^([^\1]*)\1?(.*)$");
 				if ns and nm then
 					ns = ns_prefixes[ns]; 
 					if ns then 
@@ -105,7 +104,7 @@
 			end
 		end
 		function xml_handlers:EndElement(tagname)
-			curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
+			local curr_ns,name = tagname:match("^([^\1]*)\1?(.*)$");
 			if not name then
 				curr_ns, name = "", curr_ns;
 			end
--- a/net/xmppclient_listener.lua	Thu Nov 12 13:34:38 2009 +0500
+++ b/net/xmppclient_listener.lua	Thu Nov 12 13:42:44 2009 +0500
@@ -27,7 +27,7 @@
 local sm_streamclosed = sessionmanager.streamclosed;
 local st = require "util.stanza";
 
-local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", 
+local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams\1stream", 
 		default_ns = "jabber:client",
 		streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
 
@@ -53,7 +53,7 @@
 
 local function session_reset_stream(session)
 	-- Reset stream
-		local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
+		local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1");
 		session.parser = parser;
 		
 		session.notopen = true;
@@ -70,7 +70,7 @@
 
 
 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
-local default_stream_attr = { ["xmlns:stream"] = stream_callbacks.stream_tag:gsub("%|[^|]+$", ""), xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
+local default_stream_attr = { ["xmlns:stream"] = stream_callbacks.stream_tag:match("[^\1]*"), xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
 local function session_close(session, reason)
 	local log = session.log or log;
 	if session.conn then
--- a/net/xmppcomponent_listener.lua	Thu Nov 12 13:34:38 2009 +0500
+++ b/net/xmppcomponent_listener.lua	Thu Nov 12 13:42:44 2009 +0500
@@ -32,7 +32,7 @@
 
 --- Callbacks/data for xmlhandlers to handle streams for us ---
 
-local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", default_ns = xmlns_component };
+local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams\1stream", default_ns = xmlns_component };
 
 function stream_callbacks.error(session, error, data, data2)
 	log("warn", "Error processing component stream: "..tostring(error));
@@ -87,7 +87,7 @@
 
 --- Closing a component connection
 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
-local default_stream_attr = { ["xmlns:stream"] = stream_callbacks.stream_tag:gsub("%|[^|]+$", ""), xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
+local default_stream_attr = { ["xmlns:stream"] = stream_callbacks.stream_tag:match("[^\1]*"), xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
 local function session_close(session, reason)
 	local log = session.log or log;
 	if session.conn then
@@ -138,7 +138,7 @@
 		
 		session.log("info", "Incoming Jabber component connection");
 		
-		local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
+		local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1");
 		session.parser = parser;
 		
 		session.notopen = true;
--- a/net/xmppserver_listener.lua	Thu Nov 12 13:34:38 2009 +0500
+++ b/net/xmppserver_listener.lua	Thu Nov 12 13:42:44 2009 +0500
@@ -17,7 +17,7 @@
 local s2s_streamclosed = require "core.s2smanager".streamclosed;
 local s2s_destroy_session = require "core.s2smanager".destroy_session;
 local s2s_attempt_connect = require "core.s2smanager".attempt_connection;
-local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", 
+local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams\1stream", 
 		default_ns = "jabber:server",
 		streamopened = s2s_streamopened, streamclosed = s2s_streamclosed, handlestanza =  core_process_stanza };
 
@@ -53,7 +53,7 @@
 
 local function session_reset_stream(session)
 	-- Reset stream
-		local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
+		local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1");
 		session.parser = parser;
 		
 		session.notopen = true;
@@ -70,7 +70,7 @@
 end
 
 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
-local default_stream_attr = { ["xmlns:stream"] = stream_callbacks.stream_tag:gsub("%|[^|]+$", ""), xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
+local default_stream_attr = { ["xmlns:stream"] = stream_callbacks.stream_tag:match("[^\1]*"), xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
 local function session_close(session, reason)
 	local log = session.log or log;
 	if session.conn then
--- a/plugins/mod_bosh.lua	Thu Nov 12 13:34:38 2009 +0500
+++ b/plugins/mod_bosh.lua	Thu Nov 12 13:42:44 2009 +0500
@@ -23,7 +23,7 @@
 local log = logger.init("mod_bosh");
 
 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
-local stream_callbacks = { stream_tag = "http://jabber.org/protocol/httpbind|body", default_ns = xmlns_bosh };
+local stream_callbacks = { stream_tag = "http://jabber.org/protocol/httpbind\1body", default_ns = xmlns_bosh };
 
 local BOSH_DEFAULT_HOLD = tonumber(module:get_option("bosh_default_hold")) or 1;
 local BOSH_DEFAULT_INACTIVITY = tonumber(module:get_option("bosh_max_inactivity")) or 60;
@@ -70,7 +70,7 @@
 	--log("debug", "Handling new request %s: %s\n----------", request.id, tostring(body));
 	request.notopen = true;
 	request.log = log;
-	local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "|");
+	local parser = lxp.new(init_xmlhandlers(request, stream_callbacks), "\1");
 	
 	parser:parse(body);
 	
--- a/util/stanza.lua	Thu Nov 12 13:34:38 2009 +0500
+++ b/util/stanza.lua	Thu Nov 12 13:42:44 2009 +0500
@@ -136,11 +136,11 @@
 	local name = t.name
 	t_insert(buf, "<"..name);
 	for k, v in pairs(t.attr) do
-		if s_find(k, "|", 1, true) then
-			local ns, attrk = s_match(k, "^([^|]+)|(.+)$");
+		if s_find(k, "\1", 1, true) then
+			local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
 			nsid = nsid + 1;
 			t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
-               elseif not(k == "xmlns" and v == parentns) then
+		elseif not(k == "xmlns" and v == parentns) then
 			t_insert(buf, " "..k.."='"..xml_escape(v).."'");
 		end
 	end
@@ -152,7 +152,7 @@
 		for n=1,len do
 			local child = t[n];
 			if child.name then
-                               self(child, buf, self, xml_escape, t.attr.xmlns);
+				self(child, buf, self, xml_escape, t.attr.xmlns);
 			else
 				t_insert(buf, xml_escape(child));
 			end
@@ -162,7 +162,7 @@
 end
 function stanza_mt.__tostring(t)
 	local buf = {};
-       _dostring(t, buf, _dostring, xml_escape, nil);
+	_dostring(t, buf, _dostring, xml_escape, nil);
 	return t_concat(buf);
 end
 
@@ -210,6 +210,17 @@
 	if stanza then
 		local attr = stanza.attr;
 		for i=1,#attr do attr[i] = nil; end
+		local attrx = {};
+		for att in pairs(attr) do
+			if s_find(att, "|", 1, true) and not s_find(k, "\1", 1, true) then
+				local ns,na = s_match(k, "^([^|]+)|(.+)$");
+				attrx[ns.."\1"..na] = attr[att];
+				attr[att] = nil;
+			end
+		end
+		for a,v in pairs(attrx) do
+			attr[x] = v;
+		end
 		setmetatable(stanza, stanza_mt);
 		for _, child in ipairs(stanza) do
 			if type(child) == "table" then

mercurial