Comparison

util/xmppstream.lua @ 6054:7a5ddbaf758d

Merge 0.9->0.10
author Matthew Wild <mwild1@gmail.com>
date Wed, 02 Apr 2014 17:41:38 +0100
parent 6053:2f93a04564b2
parent 5776:bd0ff8ae98a8
child 6063:e626ee2fe106
comparison
equal deleted inserted replaced
6053:2f93a04564b2 6054:7a5ddbaf758d
1 -- Prosody IM 1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild 2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain 3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
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 local lxp = require "lxp"; 9 local lxp = require "lxp";
45 45
46 local function dummy_cb() end 46 local function dummy_cb() end
47 47
48 function new_sax_handlers(session, stream_callbacks, cb_handleprogress) 48 function new_sax_handlers(session, stream_callbacks, cb_handleprogress)
49 local xml_handlers = {}; 49 local xml_handlers = {};
50 50
51 local cb_streamopened = stream_callbacks.streamopened; 51 local cb_streamopened = stream_callbacks.streamopened;
52 local cb_streamclosed = stream_callbacks.streamclosed; 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; 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; 54 local cb_handlestanza = stream_callbacks.handlestanza;
55 cb_handleprogress = cb_handleprogress or dummy_cb; 55 cb_handleprogress = cb_handleprogress or dummy_cb;
56 56
57 local stream_ns = stream_callbacks.stream_ns or xmlns_streams; 57 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
58 local stream_tag = stream_callbacks.stream_tag or "stream"; 58 local stream_tag = stream_callbacks.stream_tag or "stream";
59 if stream_ns ~= "" then 59 if stream_ns ~= "" then
60 stream_tag = stream_ns..ns_separator..stream_tag; 60 stream_tag = stream_ns..ns_separator..stream_tag;
61 end 61 end
62 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error"); 62 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
63 63
64 local stream_default_ns = stream_callbacks.default_ns; 64 local stream_default_ns = stream_callbacks.default_ns;
65 65
66 local stack = {}; 66 local stack = {};
67 local chardata, stanza = {}; 67 local chardata, stanza = {};
68 local stanza_size = 0; 68 local stanza_size = 0;
69 local non_streamns_depth = 0; 69 local non_streamns_depth = 0;
70 function xml_handlers:StartElement(tagname, attr) 70 function xml_handlers:StartElement(tagname, attr)
80 80
81 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then 81 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
82 attr.xmlns = curr_ns; 82 attr.xmlns = curr_ns;
83 non_streamns_depth = non_streamns_depth + 1; 83 non_streamns_depth = non_streamns_depth + 1;
84 end 84 end
85 85
86 for i=1,#attr do 86 for i=1,#attr do
87 local k = attr[i]; 87 local k = attr[i];
88 attr[i] = nil; 88 attr[i] = nil;
89 local xmlk = xml_namespace[k]; 89 local xmlk = xml_namespace[k];
90 if xmlk then 90 if xmlk then
91 attr[xmlk] = attr[k]; 91 attr[xmlk] = attr[k];
92 attr[k] = nil; 92 attr[k] = nil;
93 end 93 end
94 end 94 end
95 95
96 if not stanza then --if we are not currently inside a stanza 96 if not stanza then --if we are not currently inside a stanza
97 if lxp_supports_bytecount then 97 if lxp_supports_bytecount then
98 stanza_size = self:getcurrentbytecount(); 98 stanza_size = self:getcurrentbytecount();
99 end 99 end
100 if session.notopen then 100 if session.notopen then
114 return; 114 return;
115 end 115 end
116 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then 116 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
117 cb_error(session, "invalid-top-level-element"); 117 cb_error(session, "invalid-top-level-element");
118 end 118 end
119 119
120 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt); 120 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
121 else -- we are inside a stanza, so add a tag 121 else -- we are inside a stanza, so add a tag
122 if lxp_supports_bytecount then 122 if lxp_supports_bytecount then
123 stanza_size = stanza_size + self:getcurrentbytecount(); 123 stanza_size = stanza_size + self:getcurrentbytecount();
124 end 124 end
203 cb_error(session, "parse-error", "restricted-xml", "Restricted XML, see RFC 6120 section 11.1."); 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 204 if not parser.stop or not parser:stop() then
205 error("Failed to abort parsing"); 205 error("Failed to abort parsing");
206 end 206 end
207 end 207 end
208 208
209 if lxp_supports_doctype then 209 if lxp_supports_doctype then
210 xml_handlers.StartDoctypeDecl = restricted_handler; 210 xml_handlers.StartDoctypeDecl = restricted_handler;
211 end 211 end
212 xml_handlers.Comment = restricted_handler; 212 xml_handlers.Comment = restricted_handler;
213 xml_handlers.ProcessingInstruction = restricted_handler; 213 xml_handlers.ProcessingInstruction = restricted_handler;
214 214
215 local function reset() 215 local function reset()
216 stanza, chardata, stanza_size = nil, {}, 0; 216 stanza, chardata, stanza_size = nil, {}, 0;
217 stack = {}; 217 stack = {};
218 end 218 end
219 219
220 local function set_session(stream, new_session) 220 local function set_session(stream, new_session)
221 session = new_session; 221 session = new_session;
222 end 222 end
223 223
224 return xml_handlers, { reset = reset, set_session = set_session }; 224 return xml_handlers, { reset = reset, set_session = set_session };
225 end 225 end
226 226
227 function new(session, stream_callbacks, stanza_size_limit) 227 function new(session, stream_callbacks, stanza_size_limit)
228 -- Used to track parser progress (e.g. to enforce size limits) 228 -- Used to track parser progress (e.g. to enforce size limits)