Annotate

util/xmppstream.lua @ 4471:ea24d73e6c94

net.http: Pass response object to callbacks (feels hacky, should this be passed *instead of* the request?)
author Matthew Wild <mwild1@gmail.com>
date Tue, 17 Jan 2012 00:30:52 +0000
parent 4306:a101c59772e7
child 4425:6f5ed0f4a3e6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1414
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2261
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2261
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 355
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 355
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 355
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 355
diff changeset
9
2921
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
10 local lxp = require "lxp";
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
11 local st = require "util.stanza";
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
12 local stanza_mt = st.stanza_mt;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
13
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
14 local tostring = tostring;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
15 local t_insert = table.insert;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
16 local t_concat = table.concat;
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
17 local t_remove = table.remove;
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
18 local setmetatable = setmetatable;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
19
3696
a96f275c42b5 util.xmppstream: Fix logger name.
Waqas Hussain <waqas20@gmail.com>
parents: 3638
diff changeset
20 local default_log = require "util.logger".init("xmppstream");
145
fbb3a4ff9cf1 dialback keys now verified
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
21
4275
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
22 -- COMPAT: w/LuaExpat 1.1.0
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
23 local lxp_supports_doctype = pcall(lxp.new, { StartDoctypeDecl = false });
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
24
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
25 if not lxp_supports_doctype then
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
26 default_log("warn", "The version of LuaExpat on your system leaves Prosody "
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
27 .."vulnerable to denial-of-service attacks. You should upgrade to "
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
28 .."LuaExpat 1.1.1 or higher as soon as possible. See "
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
29 .."http://prosody.im/doc/depends#luaexpat for more information.");
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
30 end
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
31
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
32 local error = error;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
33
2920
f3335285b20d util.xmppstream: Initial commit
Matthew Wild <mwild1@gmail.com>
parents: 2506
diff changeset
34 module "xmppstream"
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
35
2921
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
36 local new_parser = lxp.new;
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
37
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
38 local ns_prefixes = {
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
39 ["http://www.w3.org/XML/1998/namespace"] = "xml";
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
40 };
2464
0b5f0ae7a6b1 xmlhandlers: More refactoring, split up stream_ns and stream_tag, add stream_error_tag so that callers don't need to be so worried about the separator we use
Matthew Wild <mwild1@gmail.com>
parents: 2463
diff changeset
41
0b5f0ae7a6b1 xmlhandlers: More refactoring, split up stream_ns and stream_tag, add stream_error_tag so that callers don't need to be so worried about the separator we use
Matthew Wild <mwild1@gmail.com>
parents: 2463
diff changeset
42 local xmlns_streams = "http://etherx.jabber.org/streams";
0b5f0ae7a6b1 xmlhandlers: More refactoring, split up stream_ns and stream_tag, add stream_error_tag so that callers don't need to be so worried about the separator we use
Matthew Wild <mwild1@gmail.com>
parents: 2463
diff changeset
43
2463
d9ff0190eb4a xmlhandlers: Define ns_separator and ns_pattern to save repeating it in literal form throughout the file
Matthew Wild <mwild1@gmail.com>
parents: 2261
diff changeset
44 local ns_separator = "\1";
d9ff0190eb4a xmlhandlers: Define ns_separator and ns_pattern to save repeating it in literal form throughout the file
Matthew Wild <mwild1@gmail.com>
parents: 2261
diff changeset
45 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
46
3708
2dcb86c7cee7 util.xmppstream: Expose ns_separator and ns_pattern
Matthew Wild <mwild1@gmail.com>
parents: 3696
diff changeset
47 _M.ns_separator = ns_separator;
2dcb86c7cee7 util.xmppstream: Expose ns_separator and ns_pattern
Matthew Wild <mwild1@gmail.com>
parents: 3696
diff changeset
48 _M.ns_pattern = ns_pattern;
2dcb86c7cee7 util.xmppstream: Expose ns_separator and ns_pattern
Matthew Wild <mwild1@gmail.com>
parents: 3696
diff changeset
49
2921
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
50 function new_sax_handlers(session, stream_callbacks)
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
51 local xml_handlers = {};
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
52
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
53 local log = session.log or default_log;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
54
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
55 local cb_streamopened = stream_callbacks.streamopened;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
56 local cb_streamclosed = stream_callbacks.streamclosed;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
57 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
58 local cb_handlestanza = stream_callbacks.handlestanza;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
59
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
60 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
3927
1b57e83266f0 util.xmppstream: Allow stream_ns = "" for parsing streams with no xmlns
Matthew Wild <mwild1@gmail.com>
parents: 3708
diff changeset
61 local stream_tag = stream_callbacks.stream_tag or "stream";
1b57e83266f0 util.xmppstream: Allow stream_ns = "" for parsing streams with no xmlns
Matthew Wild <mwild1@gmail.com>
parents: 3708
diff changeset
62 if stream_ns ~= "" then
1b57e83266f0 util.xmppstream: Allow stream_ns = "" for parsing streams with no xmlns
Matthew Wild <mwild1@gmail.com>
parents: 3708
diff changeset
63 stream_tag = stream_ns..ns_separator..stream_tag;
1b57e83266f0 util.xmppstream: Allow stream_ns = "" for parsing streams with no xmlns
Matthew Wild <mwild1@gmail.com>
parents: 3708
diff changeset
64 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
65 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
66
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
67 local stream_default_ns = stream_callbacks.default_ns;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
68
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
69 local stack = {};
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
70 local chardata, stanza = {};
3633
4069c37c54bc util.xmppstream: Preserve the stream content namespace on descendents of elements which are in another namespace.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
71 local non_streamns_depth = 0;
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
72 function xml_handlers:StartElement(tagname, attr)
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
73 if stanza and #chardata > 0 then
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
74 -- We have some character data in the buffer
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
75 t_insert(stanza, t_concat(chardata));
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
76 chardata = {};
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
77 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
78 local curr_ns,name = tagname:match(ns_pattern);
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
79 if name == "" then
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
80 curr_ns, name = "", curr_ns;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
81 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
82
3633
4069c37c54bc util.xmppstream: Preserve the stream content namespace on descendents of elements which are in another namespace.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
83 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
84 attr.xmlns = curr_ns;
3633
4069c37c54bc util.xmppstream: Preserve the stream content namespace on descendents of elements which are in another namespace.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
85 non_streamns_depth = non_streamns_depth + 1;
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
86 end
2464
0b5f0ae7a6b1 xmlhandlers: More refactoring, split up stream_ns and stream_tag, add stream_error_tag so that callers don't need to be so worried about the separator we use
Matthew Wild <mwild1@gmail.com>
parents: 2463
diff changeset
87
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
88 -- FIXME !!!!!
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
89 for i=1,#attr do
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
90 local k = attr[i];
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
91 attr[i] = nil;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
92 local ns, nm = k:match(ns_pattern);
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
93 if nm ~= "" then
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3444
diff changeset
94 ns = ns_prefixes[ns];
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3444
diff changeset
95 if ns then
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
96 attr[ns..":"..nm] = attr[k];
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
97 attr[k] = nil;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
98 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
99 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
100 end
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
101
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
102 if not stanza then --if we are not currently inside a stanza
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
103 if session.notopen then
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
104 if tagname == stream_tag then
3633
4069c37c54bc util.xmppstream: Preserve the stream content namespace on descendents of elements which are in another namespace.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
105 non_streamns_depth = 0;
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
106 if cb_streamopened then
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
107 cb_streamopened(session, attr);
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
108 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
109 else
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
110 -- Garbage before stream?
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
111 cb_error(session, "no-stream");
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
112 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
113 return;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
114 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
115 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
116 cb_error(session, "invalid-top-level-element");
1051
0327c569eb1a xmlhandlers: Fix tag pattern again for the default namespace
Matthew Wild <mwild1@gmail.com>
parents: 1003
diff changeset
117 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
118
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
119 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
120 else -- we are inside a stanza, so add a tag
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
121 t_insert(stack, stanza);
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
122 local oldstanza = stanza;
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
123 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
124 t_insert(oldstanza, stanza);
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
125 t_insert(oldstanza.tags, stanza);
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
126 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
127 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
128 function xml_handlers:CharacterData(data)
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
129 if stanza then
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
130 t_insert(chardata, data);
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
131 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
132 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
133 function xml_handlers:EndElement(tagname)
3633
4069c37c54bc util.xmppstream: Preserve the stream content namespace on descendents of elements which are in another namespace.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
134 if non_streamns_depth > 0 then
4069c37c54bc util.xmppstream: Preserve the stream content namespace on descendents of elements which are in another namespace.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
135 non_streamns_depth = non_streamns_depth - 1;
4069c37c54bc util.xmppstream: Preserve the stream content namespace on descendents of elements which are in another namespace.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
136 end
2493
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
137 if stanza then
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
138 if #chardata > 0 then
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
139 -- We have some character data in the buffer
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
140 t_insert(stanza, t_concat(chardata));
2493
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
141 chardata = {};
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
142 end
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
143 -- Complete stanza
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
144 if #stack == 0 then
2493
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
145 if tagname ~= stream_error_tag then
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
146 cb_handlestanza(session, stanza);
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
147 else
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
148 cb_error(session, "stream-error", stanza);
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
149 end
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
150 stanza = nil;
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
151 else
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
152 stanza = t_remove(stack);
2493
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
153 end
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
154 else
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
155 if tagname == stream_tag then
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
156 if cb_streamclosed then
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
157 cb_streamclosed(session);
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
158 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
159 else
2505
0849e349992d xmlhandlers: A little optimization.
Waqas Hussain <waqas20@gmail.com>
parents: 2493
diff changeset
160 local curr_ns,name = tagname:match(ns_pattern);
0849e349992d xmlhandlers: A little optimization.
Waqas Hussain <waqas20@gmail.com>
parents: 2493
diff changeset
161 if name == "" then
0849e349992d xmlhandlers: A little optimization.
Waqas Hussain <waqas20@gmail.com>
parents: 2493
diff changeset
162 curr_ns, name = "", curr_ns;
0849e349992d xmlhandlers: A little optimization.
Waqas Hussain <waqas20@gmail.com>
parents: 2493
diff changeset
163 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
164 cb_error(session, "parse-error", "unexpected-element-close", name);
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
165 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
166 stanza, chardata = nil, {};
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
167 stack = {};
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
168 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
169 end
4277
683523db4fe8 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2925 4276
diff changeset
170
4288
8fde6b6b4919 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 4281 4287
diff changeset
171 local function restricted_handler(parser)
4275
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
172 cb_error(session, "parse-error", "restricted-xml", "Restricted XML, see RFC 6120 section 11.1.");
4305
e3ffa91517cc util.xmppstream: Check to make sure parser.stop is present before calling it.
Waqas Hussain <waqas20@gmail.com>
parents: 4289
diff changeset
173 if not parser.stop or not parser:stop() then
4288
8fde6b6b4919 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 4281 4287
diff changeset
174 error("Failed to abort parsing");
4276
a37522bf6b1b xmlhandlers: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning. [Backport of 7cc426988bcc in trunk]
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
175 end
4275
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
176 end
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
177
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
178 if lxp_supports_doctype then
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
179 xml_handlers.StartDoctypeDecl = restricted_handler;
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
180 end
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
181 xml_handlers.Comment = restricted_handler;
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
182 xml_handlers.ProcessingInstruction = restricted_handler;
5305a665bdd4 util.xmppstream: Reject XML comments, processing instructions and (if supported by LuaExpat) DTDs. If not supported, log a warning.
Matthew Wild <mwild1@gmail.com>
parents: 3927
diff changeset
183
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
184 local function reset()
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
185 stanza, chardata = nil, {};
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
186 stack = {};
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
187 end
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
188
3424
9e0df614e5d0 util.xmppstream: Add set_session() method to change the session that a stream is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3032
diff changeset
189 local function set_session(stream, new_session)
9e0df614e5d0 util.xmppstream: Add set_session() method to change the session that a stream is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3032
diff changeset
190 session = new_session;
9e0df614e5d0 util.xmppstream: Add set_session() method to change the session that a stream is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3032
diff changeset
191 log = new_session.log or default_log;
9e0df614e5d0 util.xmppstream: Add set_session() method to change the session that a stream is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3032
diff changeset
192 end
9e0df614e5d0 util.xmppstream: Add set_session() method to change the session that a stream is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3032
diff changeset
193
9e0df614e5d0 util.xmppstream: Add set_session() method to change the session that a stream is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3032
diff changeset
194 return xml_handlers, { reset = reset, set_session = set_session };
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
195 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
196
2921
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
197 function new(session, stream_callbacks)
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
198 local handlers, meta = new_sax_handlers(session, stream_callbacks);
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
199 local parser = new_parser(handlers, ns_separator);
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
200 local parse = parser.parse;
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
201
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
202 return {
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
203 reset = function ()
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
204 parser = new_parser(handlers, ns_separator);
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
205 parse = parser.parse;
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
206 meta.reset();
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
207 end,
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
208 feed = function (self, data)
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
209 return parse(parser, data);
3424
9e0df614e5d0 util.xmppstream: Add set_session() method to change the session that a stream is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3032
diff changeset
210 end,
9e0df614e5d0 util.xmppstream: Add set_session() method to change the session that a stream is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3032
diff changeset
211 set_session = meta.set_session;
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
212 };
2921
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
213 end
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
214
2920
f3335285b20d util.xmppstream: Initial commit
Matthew Wild <mwild1@gmail.com>
parents: 2506
diff changeset
215 return _M;