Annotate

plugins/mod_compression.lua @ 6518:c0d221b0c94c

mod_saslauth: Break out tls-unique channel binding callback so it is instantiated once
author Kim Alvefur <zash@zash.se>
date Wed, 19 Nov 2014 14:44:41 +0100
parent 6367:769a3577dd85
child 6984:0f633160464c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1669
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
1 -- Prosody IM
4898
010c01841ed0 mod_compression: advertise/activate compression only for authenticated sessions in accordance to XEP-0170. (Thanks fippo)
Tobias Markmann <tm@ayena.de>
parents: 4481
diff changeset
2 -- Copyright (C) 2009-2012 Tobias Markmann
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
3 --
1669
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
6 --
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
7
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
8 local st = require "util.stanza";
1671
d196ac213104 Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents: 1670
diff changeset
9 local zlib = require "zlib";
1678
79eb903d0e67 Using pcall to make save the rest from zlib fails. Emit errors on those fails.
Tobias Markmann <tm@ayena.de>
parents: 1677
diff changeset
10 local pcall = pcall;
2882
4e72048d4a24 mod_compression: Fixed various possible tracebacks in logging.
Waqas Hussain <waqas20@gmail.com>
parents: 2279
diff changeset
11 local tostring = tostring;
1669
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
12
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
13 local xmlns_compression_feature = "http://jabber.org/features/compress"
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
14 local xmlns_compression_protocol = "http://jabber.org/protocol/compress"
2280
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
15 local xmlns_stream = "http://etherx.jabber.org/streams";
1669
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
16 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up();
3148
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
17 local add_filter = require "util.filters".add_filter;
1669
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
18
4481
408c2f688e4e mod_compression: Change default compression level to 7
Kim Alvefur <zash@zash.se>
parents: 4480
diff changeset
19 local compression_level = module:get_option_number("compression_level", 7);
1676
719350956714 Add config option handling.
Tobias Markmann <tm@ayena.de>
parents: 1674
diff changeset
20
719350956714 Add config option handling.
Tobias Markmann <tm@ayena.de>
parents: 1674
diff changeset
21 if not compression_level or compression_level < 1 or compression_level > 9 then
719350956714 Add config option handling.
Tobias Markmann <tm@ayena.de>
parents: 1674
diff changeset
22 module:log("warn", "Invalid compression level in config: %s", tostring(compression_level));
719350956714 Add config option handling.
Tobias Markmann <tm@ayena.de>
parents: 1674
diff changeset
23 module:log("warn", "Module loading aborted. Compression won't be available.");
719350956714 Add config option handling.
Tobias Markmann <tm@ayena.de>
parents: 1674
diff changeset
24 return;
719350956714 Add config option handling.
Tobias Markmann <tm@ayena.de>
parents: 1674
diff changeset
25 end
1669
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
26
2609
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
27 module:hook("stream-features", function(event)
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
28 local origin, features = event.origin, event.features;
6056
29cbbe882441 mod_compression: Remove checks for impossible conditions
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
29 if not origin.compressed and origin.type == "c2s" then
2609
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
30 -- FIXME only advertise compression support when TLS layer has no compression enabled
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
31 features:add_child(compression_stream_feature);
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
32 end
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
33 end);
1669
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
34
2609
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
35 module:hook("s2s-stream-features", function(event)
2613
afa20941e098 s2smanager, mod_compression, mod_tls: Changed event.session to event.origin for s2s-stream-features event for consistency.
Waqas Hussain <waqas20@gmail.com>
parents: 2609
diff changeset
36 local origin, features = event.origin, event.features;
2609
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
37 -- FIXME only advertise compression support when TLS layer has no compression enabled
6056
29cbbe882441 mod_compression: Remove checks for impossible conditions
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
38 if not origin.compressed and origin.type == "s2sin" then
2609
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
39 features:add_child(compression_stream_feature);
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
40 end
2ee28cae530a mod_compression: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 2324
diff changeset
41 end);
2280
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
42
2291
c6bed51c6733 mod_compression: Some comment clean up.
Tobias Markmann <tm@ayena.de>
parents: 2289
diff changeset
43 -- Hook to activate compression if remote server supports it.
2280
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
44 module:hook_stanza(xmlns_stream, "features",
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
45 function (session, stanza)
6056
29cbbe882441 mod_compression: Remove checks for impossible conditions
Kim Alvefur <zash@zash.se>
parents: 6054
diff changeset
46 if not session.compressed and session.type == "s2sout" then
2280
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
47 -- does remote server support compression?
6057
a3d7b8f4d9c7 mod_compression: Use get_child() and get_child_text()
Kim Alvefur <zash@zash.se>
parents: 6056
diff changeset
48 local comp_st = stanza:get_child("compression", xmlns_compression_feature);
2280
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
49 if comp_st then
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
50 -- do we support the mechanism
6058
1607b03356ed mod_compression: Only iterate over correctly named and namespaced child tags of compression feature
Kim Alvefur <zash@zash.se>
parents: 6057
diff changeset
51 for a in comp_st:childtags("method") do
1607b03356ed mod_compression: Only iterate over correctly named and namespaced child tags of compression feature
Kim Alvefur <zash@zash.se>
parents: 6057
diff changeset
52 local algorithm = a:get_text();
2280
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
53 if algorithm == "zlib" then
2282
6f54dac3ec2d mod_compression: Prepare activating of compression on s2s.
Tobias Markmann <tm@ayena.de>
parents: 2280
diff changeset
54 session.sends2s(st.stanza("compress", {xmlns=xmlns_compression_protocol}):tag("method"):text("zlib"))
3295
8865d1b3dce2 mod_compression: Lowered a log level.
Waqas Hussain <waqas20@gmail.com>
parents: 3200
diff changeset
55 session.log("debug", "Enabled compression using zlib.")
2280
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
56 return true;
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
57 end
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
58 end
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
59 session.log("debug", "Remote server supports no compression algorithm we support.")
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
60 end
1672
614623f393c6 Add FIXME to remember TLS compression detection.
Tobias Markmann <tm@ayena.de>
parents: 1671
diff changeset
61 end
1669
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
62 end
2280
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
63 , 250);
0b0fe49e5251 Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents: 2279
diff changeset
64
2285
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
65
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
66 -- returns either nil or a fully functional ready to use inflate stream
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
67 local function get_deflate_stream(session)
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
68 local status, deflate_stream = pcall(zlib.deflate, compression_level);
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
69 if status == false then
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
70 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
71 (session.sends2s or session.send)(error_st);
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
72 session.log("error", "Failed to create zlib.deflate filter.");
2887
765e7070d0a8 Merge with 0.6 (into 0.7, namely mod_compression fixes)
Matthew Wild <mwild1@gmail.com>
parents: 2613 2886
diff changeset
73 module:log("error", "%s", tostring(deflate_stream));
2285
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
74 return
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
75 end
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
76 return deflate_stream
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
77 end
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
78
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
79 -- returns either nil or a fully functional ready to use inflate stream
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
80 local function get_inflate_stream(session)
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
81 local status, inflate_stream = pcall(zlib.inflate);
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
82 if status == false then
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
83 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
84 (session.sends2s or session.send)(error_st);
2887
765e7070d0a8 Merge with 0.6 (into 0.7, namely mod_compression fixes)
Matthew Wild <mwild1@gmail.com>
parents: 2613 2886
diff changeset
85 session.log("error", "Failed to create zlib.inflate filter.");
765e7070d0a8 Merge with 0.6 (into 0.7, namely mod_compression fixes)
Matthew Wild <mwild1@gmail.com>
parents: 2613 2886
diff changeset
86 module:log("error", "%s", tostring(inflate_stream));
2285
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
87 return
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
88 end
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
89 return inflate_stream
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
90 end
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
91
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
92 -- setup compression for a stream
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
93 local function setup_compression(session, deflate_stream)
3148
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
94 add_filter(session, "bytes/out", function(t)
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
95 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync');
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
96 if status == false then
3226
69e920d7c968 mod_compression: Move logging of compression/decompression errors to before the closing of the stream, to make logs a bit easier to follow
Matthew Wild <mwild1@gmail.com>
parents: 3223
diff changeset
97 module:log("warn", "%s", tostring(compressed));
3148
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
98 session:close({
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
99 condition = "undefined-condition";
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
100 text = compressed;
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
101 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
102 });
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
103 return;
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
104 end
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
105 return compressed;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
106 end);
2285
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
107 end
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
108
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
109 -- setup decompression for a stream
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
110 local function setup_decompression(session, inflate_stream)
3148
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
111 add_filter(session, "bytes/in", function(data)
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
112 local status, decompressed, eof = pcall(inflate_stream, data);
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
113 if status == false then
3226
69e920d7c968 mod_compression: Move logging of compression/decompression errors to before the closing of the stream, to make logs a bit easier to follow
Matthew Wild <mwild1@gmail.com>
parents: 3223
diff changeset
114 module:log("warn", "%s", tostring(decompressed));
3148
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
115 session:close({
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
116 condition = "undefined-condition";
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
117 text = decompressed;
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
118 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
119 });
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
120 return;
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
121 end
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
122 return decompressed;
a83a995fe5db mod_compression: Use filters! \o/
Matthew Wild <mwild1@gmail.com>
parents: 3127
diff changeset
123 end);
2285
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
124 end
3dd7fdee9035 mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents: 2284
diff changeset
125
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
126 module:hook("stanza/http://jabber.org/protocol/compress:compressed", function(event)
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
127 local session = event.origin;
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
128
6040
b3b1c9da38fb mod_compression: Only allow compression on authenticated streams
Kim Alvefur <zash@zash.se>
parents: 5535
diff changeset
129 if session.type == "s2sout" then
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
130 session.log("debug", "Activating compression...")
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
131 -- create deflate and inflate streams
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
132 local deflate_stream = get_deflate_stream(session);
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
133 if not deflate_stream then return true; end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
134
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
135 local inflate_stream = get_inflate_stream(session);
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
136 if not inflate_stream then return true; end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
137
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
138 -- setup compression for session.w
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
139 setup_compression(session, deflate_stream);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
140
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
141 -- setup decompression for session.data
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
142 setup_decompression(session, inflate_stream);
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
143 session:reset_stream();
5535
0df0afc041d7 mod_saslauth, mod_compression: Fix some cases where open_stream() was not being passed to/from (see df3c78221f26 and issue #338)
Matthew Wild <mwild1@gmail.com>
parents: 5351
diff changeset
144 session:open_stream(session.from_host, session.to_host);
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
145 session.compressed = true;
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
146 return true;
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
147 end
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
148 end);
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
149
6358
55fda5deb5d5 mod_compression: Handle compression setup errors by logging a warning about it (fixes #408)
Kim Alvefur <zash@zash.se>
parents: 6040
diff changeset
150 module:hook("stanza/http://jabber.org/protocol/compress:failure", function(event)
55fda5deb5d5 mod_compression: Handle compression setup errors by logging a warning about it (fixes #408)
Kim Alvefur <zash@zash.se>
parents: 6040
diff changeset
151 local err = event.stanza:get_child();
55fda5deb5d5 mod_compression: Handle compression setup errors by logging a warning about it (fixes #408)
Kim Alvefur <zash@zash.se>
parents: 6040
diff changeset
152 (event.origin.log or module._log)("warn", "Compression setup failed (%s)", err and err.name or "unknown reason");
55fda5deb5d5 mod_compression: Handle compression setup errors by logging a warning about it (fixes #408)
Kim Alvefur <zash@zash.se>
parents: 6040
diff changeset
153 return true;
55fda5deb5d5 mod_compression: Handle compression setup errors by logging a warning about it (fixes #408)
Kim Alvefur <zash@zash.se>
parents: 6040
diff changeset
154 end);
55fda5deb5d5 mod_compression: Handle compression setup errors by logging a warning about it (fixes #408)
Kim Alvefur <zash@zash.se>
parents: 6040
diff changeset
155
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
156 module:hook("stanza/http://jabber.org/protocol/compress:compress", function(event)
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
157 local session, stanza = event.origin, event.stanza;
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
158
6040
b3b1c9da38fb mod_compression: Only allow compression on authenticated streams
Kim Alvefur <zash@zash.se>
parents: 5535
diff changeset
159 if session.type == "c2s" or session.type == "s2sin" then
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
160 -- fail if we are already compressed
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
161 if session.compressed then
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
162 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
163 (session.sends2s or session.send)(error_st);
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
164 session.log("debug", "Client tried to establish another compression layer.");
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
165 return true;
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
166 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
167
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
168 -- checking if the compression method is supported
6057
a3d7b8f4d9c7 mod_compression: Use get_child() and get_child_text()
Kim Alvefur <zash@zash.se>
parents: 6056
diff changeset
169 local method = stanza:get_child_text("method");
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
170 if method == "zlib" then
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
171 session.log("debug", "zlib compression enabled.");
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
172
2286
e0b2d934f316 mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents: 2285
diff changeset
173 -- create deflate and inflate streams
2292
9c5941198719 mod_compression: Declaring the de-/compression pipes as local.
Tobias Markmann <tm@ayena.de>
parents: 2291
diff changeset
174 local deflate_stream = get_deflate_stream(session);
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
175 if not deflate_stream then return true; end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
176
2292
9c5941198719 mod_compression: Declaring the de-/compression pipes as local.
Tobias Markmann <tm@ayena.de>
parents: 2291
diff changeset
177 local inflate_stream = get_inflate_stream(session);
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
178 if not inflate_stream then return true; end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
179
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
180 (session.sends2s or session.send)(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
181 session:reset_stream();
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
182
2286
e0b2d934f316 mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents: 2285
diff changeset
183 -- setup compression for session.w
e0b2d934f316 mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents: 2285
diff changeset
184 setup_compression(session, deflate_stream);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
185
2286
e0b2d934f316 mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents: 2285
diff changeset
186 -- setup decompression for session.data
e0b2d934f316 mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents: 2285
diff changeset
187 setup_decompression(session, inflate_stream);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5535
diff changeset
188
2286
e0b2d934f316 mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents: 2285
diff changeset
189 session.compressed = true;
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
190 elseif method then
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
191 session.log("debug", "%s compression selected, but we don't support it.", tostring(method));
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
192 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
193 (session.sends2s or session.send)(error_st);
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
194 else
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
195 (session.sends2s or session.send)(st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"));
2282
6f54dac3ec2d mod_compression: Prepare activating of compression on s2s.
Tobias Markmann <tm@ayena.de>
parents: 2280
diff changeset
196 end
3532
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
197 return true;
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
198 end
4f2cd1c688e1 mod_compression: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3301
diff changeset
199 end);
1669
b8eec163a823 Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
200