util/xmppstream.lua

changeset 3989
692d221ef9bd
parent 3987
8fbf57722368
child 3995
7214dc7a5642
equal deleted inserted replaced
3988:a3104064e905 3989:692d221ef9bd
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 9
10 local lxp = require "lxp"; 10 local lxp = require "lxp";
11 local lex = require "util.xmllex";
11 local st = require "util.stanza"; 12 local st = require "util.stanza";
12 local stanza_mt = st.stanza_mt; 13 local stanza_mt = st.stanza_mt;
13 14
14 local tostring = tostring; 15 local tostring = tostring;
15 local t_insert = table.insert; 16 local t_insert = table.insert;
21 22
22 local error = error; 23 local error = error;
23 24
24 module "xmppstream" 25 module "xmppstream"
25 26
26 local new_parser = lxp.new;
27
28 local ns_prefixes = {
29 ["http://www.w3.org/XML/1998/namespace"] = "xml";
30 };
31
32 local xmlns_streams = "http://etherx.jabber.org/streams"; 27 local xmlns_streams = "http://etherx.jabber.org/streams";
33 28
34 local ns_separator = "\1"; 29 function new(session, stream_callbacks)
35 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$"; 30 local partial;
36
37 _M.ns_separator = ns_separator;
38 _M.ns_pattern = ns_pattern;
39
40 function new_sax_handlers(session, stream_callbacks)
41 local xml_handlers = {};
42 31
43 local log = session.log or default_log; 32 local function feed(stream, data)
44 33 index(data,
45 local cb_streamopened = stream_callbacks.streamopened;
46 local cb_streamclosed = stream_callbacks.streamclosed;
47 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
48 local cb_handlestanza = stream_callbacks.handlestanza;
49
50 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
51 local stream_tag = stream_callbacks.stream_tag or "stream";
52 if stream_ns ~= "" then
53 stream_tag = stream_ns..ns_separator..stream_tag;
54 end 34 end
55 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
56
57 local stream_default_ns = stream_callbacks.default_ns;
58
59 local stack = {};
60 local chardata, stanza = {};
61 local non_streamns_depth = 0;
62 function xml_handlers:StartElement(tagname, attr)
63 if stanza and #chardata > 0 then
64 -- We have some character data in the buffer
65 t_insert(stanza, t_concat(chardata));
66 chardata = {};
67 end
68 local curr_ns,name = tagname:match(ns_pattern);
69 if name == "" then
70 curr_ns, name = "", curr_ns;
71 end
72
73 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
74 attr.xmlns = curr_ns;
75 non_streamns_depth = non_streamns_depth + 1;
76 end
77
78 -- FIXME !!!!!
79 for i=1,#attr do
80 local k = attr[i];
81 attr[i] = nil;
82 local ns, nm = k:match(ns_pattern);
83 if nm ~= "" then
84 ns = ns_prefixes[ns];
85 if ns then
86 attr[ns..":"..nm] = attr[k];
87 attr[k] = nil;
88 end
89 end
90 end
91
92 if not stanza then --if we are not currently inside a stanza
93 if session.notopen then
94 if tagname == stream_tag then
95 non_streamns_depth = 0;
96 if cb_streamopened then
97 cb_streamopened(session, attr);
98 end
99 else
100 -- Garbage before stream?
101 cb_error(session, "no-stream");
102 end
103 return;
104 end
105 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
106 cb_error(session, "invalid-top-level-element");
107 end
108
109 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
110 else -- we are inside a stanza, so add a tag
111 t_insert(stack, stanza);
112 local oldstanza = stanza;
113 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
114 t_insert(oldstanza, stanza);
115 t_insert(oldstanza.tags, stanza);
116 end
117 end
118 function xml_handlers:CharacterData(data)
119 if stanza then
120 t_insert(chardata, data);
121 end
122 end
123 function xml_handlers:EndElement(tagname)
124 if non_streamns_depth > 0 then
125 non_streamns_depth = non_streamns_depth - 1;
126 end
127 if stanza then
128 if #chardata > 0 then
129 -- We have some character data in the buffer
130 t_insert(stanza, t_concat(chardata));
131 chardata = {};
132 end
133 -- Complete stanza
134 if #stack == 0 then
135 if tagname ~= stream_error_tag then
136 cb_handlestanza(session, stanza);
137 else
138 cb_error(session, "stream-error", stanza);
139 end
140 stanza = nil;
141 else
142 stanza = t_remove(stack);
143 end
144 else
145 if tagname == stream_tag then
146 if cb_streamclosed then
147 cb_streamclosed(session);
148 end
149 else
150 local curr_ns,name = tagname:match(ns_pattern);
151 if name == "" then
152 curr_ns, name = "", curr_ns;
153 end
154 cb_error(session, "parse-error", "unexpected-element-close", name);
155 end
156 stanza, chardata = nil, {};
157 stack = {};
158 end
159 end
160
161 local function reset()
162 stanza, chardata = nil, {};
163 stack = {};
164 end
165
166 local function set_session(stream, new_session)
167 session = new_session;
168 log = new_session.log or default_log;
169 end
170
171 return xml_handlers, { reset = reset, set_session = set_session };
172 end
173
174 function new(session, stream_callbacks)
175 local handlers, meta = new_sax_handlers(session, stream_callbacks);
176 local parser = new_parser(handlers, ns_separator);
177 local parse = parser.parse;
178 35
179 return { 36 return {
180 reset = function () 37 reset = function ()
181 parser = new_parser(handlers, ns_separator); 38 parser = new_parser(handlers, ns_separator);
182 parse = parser.parse; 39 parse = parser.parse;

mercurial