Annotate

util/xmppstream.lua @ 12073:4cbe7979a92a

mod_smacks: Fix traceback when bouncing unacked stanzas Errors sent from handle_unacked_stanzas() should usually not be sent to the session itself, but if one is, it should not be queued.
author Kim Alvefur <zash@zash.se>
date Sat, 18 Dec 2021 13:58:33 +0100
parent 11560:3bbb1af92514
child 12975:d10957394a3c
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
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
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
2921
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
9 local lxp = require "lxp";
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
10 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
11 local stanza_mt = st.stanza_mt;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
12
4425
6f5ed0f4a3e6 util.xmppstream: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4306
diff changeset
13 local error = error;
1
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
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
20 -- 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
21 local lxp_supports_doctype = pcall(lxp.new, { StartDoctypeDecl = false });
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
22 local lxp_supports_xmldecl = pcall(lxp.new, { XmlDecl = false });
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
23 local lxp_supports_bytecount = not not lxp.new({}).getcurrentbytecount;
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
24
11543
63fd4c8465fb util.xmppstream: Reduce default xmppstream limit to 1MB
Matthew Wild <mwild1@gmail.com>
parents: 9071
diff changeset
25 local default_stanza_size_limit = 1024*1024*1; -- 1MB
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
26
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
27 local _ENV = nil;
8555
4f0f5b49bb03 vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8382
diff changeset
28 -- luacheck: std none
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
29
2921
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
30 local new_parser = lxp.new;
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
31
4484
0da4e0f0f0ef util.xmppstream: Optimize attribute processing.
Waqas Hussain <waqas20@gmail.com>
parents: 4483
diff changeset
32 local xml_namespace = {
0da4e0f0f0ef util.xmppstream: Optimize attribute processing.
Waqas Hussain <waqas20@gmail.com>
parents: 4483
diff changeset
33 ["http://www.w3.org/XML/1998/namespace\1lang"] = "xml:lang";
0da4e0f0f0ef util.xmppstream: Optimize attribute processing.
Waqas Hussain <waqas20@gmail.com>
parents: 4483
diff changeset
34 ["http://www.w3.org/XML/1998/namespace\1space"] = "xml:space";
0da4e0f0f0ef util.xmppstream: Optimize attribute processing.
Waqas Hussain <waqas20@gmail.com>
parents: 4483
diff changeset
35 ["http://www.w3.org/XML/1998/namespace\1base"] = "xml:base";
0da4e0f0f0ef util.xmppstream: Optimize attribute processing.
Waqas Hussain <waqas20@gmail.com>
parents: 4483
diff changeset
36 ["http://www.w3.org/XML/1998/namespace\1id"] = "xml:id";
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
37 };
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
38
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
39 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
40
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
41 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
42 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
43
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
44 local function dummy_cb() end
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
45
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
46 local function new_sax_handlers(session, stream_callbacks, cb_handleprogress)
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
47 local xml_handlers = {};
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
48
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
49 local cb_streamopened = stream_callbacks.streamopened;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
50 local cb_streamclosed = stream_callbacks.streamclosed;
8382
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7240
diff changeset
51 local cb_error = stream_callbacks.error or
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7240
diff changeset
52 function(_, e, stanza)
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7240
diff changeset
53 error("XML stream error: "..tostring(e)..(stanza and ": "..tostring(stanza) or ""),2);
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7240
diff changeset
54 end;
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
55 local cb_handlestanza = stream_callbacks.handlestanza;
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
56 cb_handleprogress = cb_handleprogress or dummy_cb;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
57
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
58 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
59 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
60 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
61 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
62 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
63 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
64
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
65 local stream_default_ns = stream_callbacks.default_ns;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
66
10093
1266a63ba567 util.xmppstream: Inherit xml:lang from stream to stanzas (fixes #1401)
Kim Alvefur <zash@zash.se>
parents: 9071
diff changeset
67 local stream_lang = "en";
1266a63ba567 util.xmppstream: Inherit xml:lang from stream to stanzas (fixes #1401)
Kim Alvefur <zash@zash.se>
parents: 9071
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 = {};
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
71 local stanza_size = 0;
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
72 local non_streamns_depth = 0;
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
73 function xml_handlers:StartElement(tagname, attr)
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
74 if stanza and #chardata > 0 then
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
75 -- 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
76 t_insert(stanza, t_concat(chardata));
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
77 chardata = {};
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
78 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
79 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
80 if name == "" then
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
81 curr_ns, name = "", curr_ns;
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
82 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
83
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
84 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
85 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
86 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
87 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
88
2492
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;
4484
0da4e0f0f0ef util.xmppstream: Optimize attribute processing.
Waqas Hussain <waqas20@gmail.com>
parents: 4483
diff changeset
92 local xmlk = xml_namespace[k];
0da4e0f0f0ef util.xmppstream: Optimize attribute processing.
Waqas Hussain <waqas20@gmail.com>
parents: 4483
diff changeset
93 if xmlk then
0da4e0f0f0ef util.xmppstream: Optimize attribute processing.
Waqas Hussain <waqas20@gmail.com>
parents: 4483
diff changeset
94 attr[xmlk] = attr[k];
0da4e0f0f0ef util.xmppstream: Optimize attribute processing.
Waqas Hussain <waqas20@gmail.com>
parents: 4483
diff changeset
95 attr[k] = nil;
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
96 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
97 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
98
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
99 if not stanza then --if we are not currently inside a stanza
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
100 if lxp_supports_bytecount then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
101 stanza_size = self:getcurrentbytecount();
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
102 end
2492
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;
10093
1266a63ba567 util.xmppstream: Inherit xml:lang from stream to stanzas (fixes #1401)
Kim Alvefur <zash@zash.se>
parents: 9071
diff changeset
106 stream_lang = attr["xml:lang"] or stream_lang;
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
107 if cb_streamopened then
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
108 if lxp_supports_bytecount then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
109 cb_handleprogress(stanza_size);
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
110 stanza_size = 0;
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
111 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
112 cb_streamopened(session, attr);
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
113 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
114 else
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
115 -- Garbage before stream?
6363
ec446efc15e1 util.xmppstream: When error is 'no-stream', pass the received tagname to the error handler
Matthew Wild <mwild1@gmail.com>
parents: 6053
diff changeset
116 cb_error(session, "no-stream", tagname);
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
117 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
118 return;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
119 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
120 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
121 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
122 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
123
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
124 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
125 else -- we are inside a stanza, so add a tag
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
126 if lxp_supports_bytecount then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
127 stanza_size = stanza_size + self:getcurrentbytecount();
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
128 end
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
129 t_insert(stack, stanza);
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
130 local oldstanza = stanza;
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
131 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
132 t_insert(oldstanza, stanza);
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
133 t_insert(oldstanza.tags, stanza);
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
134 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
135 end
9020
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
136
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
137 function xml_handlers:StartCdataSection()
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
138 if lxp_supports_bytecount then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
139 if stanza then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
140 stanza_size = stanza_size + self:getcurrentbytecount();
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
141 else
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
142 cb_handleprogress(self:getcurrentbytecount());
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
143 end
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
144 end
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
145 end
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
146 function xml_handlers:EndCdataSection()
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
147 if lxp_supports_bytecount then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
148 if stanza then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
149 stanza_size = stanza_size + self:getcurrentbytecount();
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
150 else
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
151 cb_handleprogress(self:getcurrentbytecount());
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
152 end
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
153 end
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
154 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
155 function xml_handlers:CharacterData(data)
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
156 if stanza then
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
157 if lxp_supports_bytecount then
6052
ce3244c084f9 util.xmppstream: Disable LuaExpat's buffering (if possible)
Matthew Wild <mwild1@gmail.com>
parents: 6042
diff changeset
158 stanza_size = stanza_size + self:getcurrentbytecount();
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
159 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
160 t_insert(chardata, data);
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
161 elseif lxp_supports_bytecount then
6052
ce3244c084f9 util.xmppstream: Disable LuaExpat's buffering (if possible)
Matthew Wild <mwild1@gmail.com>
parents: 6042
diff changeset
162 cb_handleprogress(self:getcurrentbytecount());
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
163 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
164 end
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
165 function xml_handlers:EndElement(tagname)
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
166 if lxp_supports_bytecount then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
167 stanza_size = stanza_size + self:getcurrentbytecount()
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
168 end
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
169 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
170 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
171 end
2493
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
172 if stanza then
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
173 if #chardata > 0 then
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
174 -- 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
175 t_insert(stanza, t_concat(chardata));
2493
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
176 chardata = {};
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
177 end
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
178 -- Complete stanza
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
179 if #stack == 0 then
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
180 if lxp_supports_bytecount then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
181 cb_handleprogress(stanza_size);
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
182 end
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
183 stanza_size = 0;
10093
1266a63ba567 util.xmppstream: Inherit xml:lang from stream to stanzas (fixes #1401)
Kim Alvefur <zash@zash.se>
parents: 9071
diff changeset
184 if stanza.attr["xml:lang"] == nil then
1266a63ba567 util.xmppstream: Inherit xml:lang from stream to stanzas (fixes #1401)
Kim Alvefur <zash@zash.se>
parents: 9071
diff changeset
185 stanza.attr["xml:lang"] = stream_lang;
1266a63ba567 util.xmppstream: Inherit xml:lang from stream to stanzas (fixes #1401)
Kim Alvefur <zash@zash.se>
parents: 9071
diff changeset
186 end
2493
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
187 if tagname ~= stream_error_tag then
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
188 cb_handlestanza(session, stanza);
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
189 else
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
190 cb_error(session, "stream-error", stanza);
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
191 end
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
192 stanza = nil;
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
193 else
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
194 stanza = t_remove(stack);
2493
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
195 end
ec09d16a51e1 xmlhandlers: Rearranged a little code.
Waqas Hussain <waqas20@gmail.com>
parents: 2492
diff changeset
196 else
11546
b21d8c286973 util.xmppstream: Mark bytes for stream closure as handled
Matthew Wild <mwild1@gmail.com>
parents: 11543
diff changeset
197 if lxp_supports_bytecount then
b21d8c286973 util.xmppstream: Mark bytes for stream closure as handled
Matthew Wild <mwild1@gmail.com>
parents: 11543
diff changeset
198 cb_handleprogress(stanza_size);
b21d8c286973 util.xmppstream: Mark bytes for stream closure as handled
Matthew Wild <mwild1@gmail.com>
parents: 11543
diff changeset
199 end
4483
1dbd06eedaa4 util.xmppstream: Have faith in the XML parser matching start and end tags.
Waqas Hussain <waqas20@gmail.com>
parents: 4482
diff changeset
200 if cb_streamclosed then
1dbd06eedaa4 util.xmppstream: Have faith in the XML parser matching start and end tags.
Waqas Hussain <waqas20@gmail.com>
parents: 4482
diff changeset
201 cb_streamclosed(session);
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
202 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
203 end
2492
b5e2d1919ec3 xmlhandlers: Fixed indentation and added some semicolons.
Waqas Hussain <waqas20@gmail.com>
parents: 2468
diff changeset
204 end
4277
683523db4fe8 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2925 4276
diff changeset
205
4288
8fde6b6b4919 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 4281 4287
diff changeset
206 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
207 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
208 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
209 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
210 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
211 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
212
9020
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
213 if lxp_supports_xmldecl then
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
214 function xml_handlers:XmlDecl(version, encoding, standalone)
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
215 if lxp_supports_bytecount then
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
216 cb_handleprogress(self:getcurrentbytecount());
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
217 end
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
218 if (encoding and encoding:lower() ~= "utf-8")
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
219 or (standalone == "no")
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
220 or (version and version ~= "1.0") then
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
221 return restricted_handler(self);
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
222 end
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
223 end
19e51d8f8947 util.xmppstream: Perfom validation of XML declaration parameters
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
224 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
225 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
226 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
227 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
228 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
229 xml_handlers.ProcessingInstruction = restricted_handler;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
230
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
231 local function reset()
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
232 stanza, chardata, stanza_size = nil, {}, 0;
3987
8fbf57722368 util.xmppstream: Optimized stanza building by bypassing the stanza API.
Waqas Hussain <waqas20@gmail.com>
parents: 3927
diff changeset
233 stack = {};
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
234 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
235
8382
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7240
diff changeset
236 local function set_session(stream, new_session) -- luacheck: ignore 212/stream
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
237 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
238 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5311
diff changeset
239
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
240 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
241 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
242
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
243 local function new(session, stream_callbacks, stanza_size_limit)
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
244 -- Used to track parser progress (e.g. to enforce size limits)
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
245 local n_outstanding_bytes = 0;
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
246 local handle_progress;
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
247 if lxp_supports_bytecount then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
248 function handle_progress(n_parsed_bytes)
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
249 n_outstanding_bytes = n_outstanding_bytes - n_parsed_bytes;
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
250 end
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
251 stanza_size_limit = stanza_size_limit or default_stanza_size_limit;
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
252 elseif stanza_size_limit then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
253 error("Stanza size limits are not supported on this version of LuaExpat")
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
254 end
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
255
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
256 local handlers, meta = new_sax_handlers(session, stream_callbacks, handle_progress);
6052
ce3244c084f9 util.xmppstream: Disable LuaExpat's buffering (if possible)
Matthew Wild <mwild1@gmail.com>
parents: 6042
diff changeset
257 local parser = new_parser(handlers, ns_separator, false);
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
258 local parse = parser.parse;
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
259
8382
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7240
diff changeset
260 function session.open_stream(session, from, to) -- luacheck: ignore 432/session
6063
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
261 local send = session.sends2s or session.send;
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
262
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
263 local attr = {
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
264 ["xmlns:stream"] = "http://etherx.jabber.org/streams",
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
265 ["xml:lang"] = "en",
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
266 xmlns = stream_callbacks.default_ns,
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
267 version = session.version and (session.version > 0 and "1.0" or nil),
11358
ebbf8dca33d2 util.xmppstream: Ensure stream 'id' is always present (fix #1626)
Kim Alvefur <zash@zash.se>
parents: 10093
diff changeset
268 id = session.streamid or "",
6063
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
269 from = from or session.host, to = to,
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
270 };
6084
3c02a9ed399e util.xmppstream: Check for callback that may add stream header attributes
Kim Alvefur <zash@zash.se>
parents: 6063
diff changeset
271 if session.stream_attrs then
3c02a9ed399e util.xmppstream: Check for callback that may add stream header attributes
Kim Alvefur <zash@zash.se>
parents: 6063
diff changeset
272 session:stream_attrs(from, to, attr)
3c02a9ed399e util.xmppstream: Check for callback that may add stream header attributes
Kim Alvefur <zash@zash.se>
parents: 6063
diff changeset
273 end
11516
83822be7704d util.xmppstream: Send <?xml?> declaration in same call as stream open
Kim Alvefur <zash@zash.se>
parents: 11358
diff changeset
274 send("<?xml version='1.0'?>"..st.stanza("stream:stream", attr):top_tag());
6063
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
275 return true;
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
276 end
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
277
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
278 return {
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
279 reset = function ()
6053
2f93a04564b2 util.xmppstream: Also disable CharacterData merging after stream restarts
Matthew Wild <mwild1@gmail.com>
parents: 6052
diff changeset
280 parser = new_parser(handlers, ns_separator, false);
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
281 parse = parser.parse;
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
282 n_outstanding_bytes = 0;
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
283 meta.reset();
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
284 end,
8382
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7240
diff changeset
285 feed = function (self, data) -- luacheck: ignore 212/self
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
286 if lxp_supports_bytecount then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
287 n_outstanding_bytes = n_outstanding_bytes + #data;
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
288 end
9071
db61e33bbd41 util.xmppstream: Explicitly release old parser object on stream reset
Matthew Wild <mwild1@gmail.com>
parents: 9020
diff changeset
289 local _parser = parser;
db61e33bbd41 util.xmppstream: Explicitly release old parser object on stream reset
Matthew Wild <mwild1@gmail.com>
parents: 9020
diff changeset
290 local ok, err = parse(_parser, data);
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
291 if lxp_supports_bytecount and n_outstanding_bytes > stanza_size_limit then
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
292 return nil, "stanza-too-large";
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
293 end
9071
db61e33bbd41 util.xmppstream: Explicitly release old parser object on stream reset
Matthew Wild <mwild1@gmail.com>
parents: 9020
diff changeset
294 if parser ~= _parser then
db61e33bbd41 util.xmppstream: Explicitly release old parser object on stream reset
Matthew Wild <mwild1@gmail.com>
parents: 9020
diff changeset
295 _parser:parse();
db61e33bbd41 util.xmppstream: Explicitly release old parser object on stream reset
Matthew Wild <mwild1@gmail.com>
parents: 9020
diff changeset
296 _parser:close();
db61e33bbd41 util.xmppstream: Explicitly release old parser object on stream reset
Matthew Wild <mwild1@gmail.com>
parents: 9020
diff changeset
297 end
6042
1107d66d2ab2 util.xmppstream: Implement stanza size limiting, default limit 10MB
Matthew Wild <mwild1@gmail.com>
parents: 5311
diff changeset
298 return ok, err;
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
299 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
300 set_session = meta.set_session;
11547
dd21eb632f52 util.xmppstream: Allow dynamically configuring the stanza size limit for a stream
Matthew Wild <mwild1@gmail.com>
parents: 11546
diff changeset
301 set_stanza_size_limit = function (_, new_stanza_size_limit)
dd21eb632f52 util.xmppstream: Allow dynamically configuring the stanza size limit for a stream
Matthew Wild <mwild1@gmail.com>
parents: 11546
diff changeset
302 stanza_size_limit = new_stanza_size_limit;
dd21eb632f52 util.xmppstream: Allow dynamically configuring the stanza size limit for a stream
Matthew Wild <mwild1@gmail.com>
parents: 11546
diff changeset
303 end;
3032
38459cffaf67 util.xmppstream: Stream objects now just have feed/reset methods
Matthew Wild <mwild1@gmail.com>
parents: 2926
diff changeset
304 };
2921
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
305 end
f0ddfd7739ea util.xmppstream: new() now returns a parser object
Matthew Wild <mwild1@gmail.com>
parents: 2920
diff changeset
306
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
307 return {
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
308 ns_separator = ns_separator;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
309 ns_pattern = ns_pattern;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
310 new_sax_handlers = new_sax_handlers;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
311 new = new;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
312 };