2 -- Copyright (C) 2009 Tobias Markmann
4 -- This project is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
8 local st = require "util.stanza";
9 local zlib = require "zlib";
11 local tostring = tostring;
13 local xmlns_compression_feature = "http://jabber.org/features/compress"
14 local xmlns_compression_protocol = "http://jabber.org/protocol/compress"
15 local xmlns_stream = "http://etherx.jabber.org/streams";
16 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up();
17 local add_filter = require "util.filters".add_filter;
19 local compression_level = module:get_option("compression_level");
20 -- if not defined assume admin wants best compression
21 if compression_level == nil then compression_level = 9 end;
24 compression_level = tonumber(compression_level);
25 if not compression_level or compression_level < 1 or compression_level > 9 then
26 module:log("warn", "Invalid compression level in config: %s", tostring(compression_level));
27 module:log("warn", "Module loading aborted. Compression won't be available.");
31 module:hook("stream-features", function(event)
32 local origin, features = event.origin, event.features;
33 if not origin.compressed then
34 -- FIXME only advertise compression support when TLS layer has no compression enabled
35 features:add_child(compression_stream_feature);
39 module:hook("s2s-stream-features", function(event)
40 local origin, features = event.origin, event.features;
41 -- FIXME only advertise compression support when TLS layer has no compression enabled
42 if not origin.compressed then
43 features:add_child(compression_stream_feature);
47 -- Hook to activate compression if remote server supports it.
48 module:hook_stanza(xmlns_stream, "features",
49 function (session, stanza)
50 if not session.compressed then
51 -- does remote server support compression?
52 local comp_st = stanza:child_with_name("compression");
54 -- do we support the mechanism
55 for a in comp_st:children() do
56 local algorithm = a[1]
57 if algorithm == "zlib" then
58 session.sends2s(st.stanza("compress", {xmlns=xmlns_compression_protocol}):tag("method"):text("zlib"))
59 session.log("info", "Enabled compression using zlib.")
63 session.log("debug", "Remote server supports no compression algorithm we support.")
70 -- returns either nil or a fully functional ready to use inflate stream
71 local function get_deflate_stream(session)
72 local status, deflate_stream = pcall(zlib.deflate, compression_level);
73 if status == false then
74 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
75 (session.sends2s or session.send)(error_st);
76 session.log("error", "Failed to create zlib.deflate filter.");
77 module:log("error", "%s", tostring(deflate_stream));
83 -- returns either nil or a fully functional ready to use inflate stream
84 local function get_inflate_stream(session)
85 local status, inflate_stream = pcall(zlib.inflate);
86 if status == false then
87 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
88 (session.sends2s or session.send)(error_st);
89 session.log("error", "Failed to create zlib.inflate filter.");
90 module:log("error", "%s", tostring(inflate_stream));
96 -- setup compression for a stream
97 local function setup_compression(session, deflate_stream)
98 add_filter(session, "bytes/out", function(t)
100 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync');
101 if status == false then
103 condition = "undefined-condition";
105 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
107 module:log("warn", "%s", tostring(compressed));
114 -- setup decompression for a stream
115 local function setup_decompression(session, inflate_stream)
116 add_filter(session, "bytes/in", function(data)
117 local status, decompressed, eof = pcall(inflate_stream, data);
118 if status == false then
120 condition = "undefined-condition";
122 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
124 module:log("warn", "%s", tostring(decompressed));
131 module:add_handler({"s2sout_unauthed", "s2sout"}, "compressed", xmlns_compression_protocol,
132 function(session ,stanza)
133 session.log("debug", "Activating compression...")
134 -- create deflate and inflate streams
135 local deflate_stream = get_deflate_stream(session);
136 if not deflate_stream then return end
138 local inflate_stream = get_inflate_stream(session);
139 if not inflate_stream then return end
141 -- setup compression for session.w
142 setup_compression(session, deflate_stream);
144 -- setup decompression for session.data
145 setup_decompression(session, inflate_stream);
146 local session_reset_stream = session.reset_stream;
147 session.reset_stream = function(session)
148 session_reset_stream(session);
149 setup_decompression(session, inflate_stream);
152 session:reset_stream();
153 local default_stream_attr = {xmlns = "jabber:server", ["xmlns:stream"] = "http://etherx.jabber.org/streams",
154 ["xmlns:db"] = 'jabber:server:dialback', version = "1.0", to = session.to_host, from = session.from_host};
155 session.sends2s("<?xml version='1.0'?>");
156 session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
157 session.compressed = true;
161 module:add_handler({"c2s_unauthed", "c2s", "s2sin_unauthed", "s2sin"}, "compress", xmlns_compression_protocol,
162 function(session, stanza)
163 -- fail if we are already compressed
164 if session.compressed then
165 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
166 (session.sends2s or session.send)(error_st);
167 session.log("debug", "Client tried to establish another compression layer.");
171 -- checking if the compression method is supported
172 local method = stanza:child_with_name("method");
173 method = method and (method[1] or "");
174 if method == "zlib" then
175 session.log("debug", "zlib compression enabled.");
177 -- create deflate and inflate streams
178 local deflate_stream = get_deflate_stream(session);
179 if not deflate_stream then return end
181 local inflate_stream = get_inflate_stream(session);
182 if not inflate_stream then return end
184 (session.sends2s or session.send)(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
185 session:reset_stream();
187 -- setup compression for session.w
188 setup_compression(session, deflate_stream);
190 -- setup decompression for session.data
191 setup_decompression(session, inflate_stream);
193 local session_reset_stream = session.reset_stream;
194 session.reset_stream = function(session)
195 session_reset_stream(session);
196 setup_decompression(session, inflate_stream);
199 session.compressed = true;
201 session.log("debug", "%s compression selected, but we don't support it.", tostring(method));
202 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
203 (session.sends2s or session.send)(error_st);
205 (session.sends2s or session.send)(st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"));