2a6c0871c5a4b0ff3548a7853fa635d3fcfdc384
[prosody.git] / plugins / mod_compression.lua
1 -- Prosody IM
2 -- Copyright (C) 2009 Tobias Markmann
3 -- 
4 -- This project is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
6 --
7
8 local st = require "util.stanza";
9 local zlib = require "zlib";
10 local pcall = pcall;
11 local xmlns_compression_feature = "http://jabber.org/features/compress"
12 local xmlns_compression_protocol = "http://jabber.org/protocol/compress"
13 local xmlns_stream = "http://etherx.jabber.org/streams";
14 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up();
15
16 local compression_level = module:get_option("compression_level");
17 -- if not defined assume admin wants best compression
18 if compression_level == nil then compression_level = 9 end;
19
20
21 compression_level = tonumber(compression_level);
22 if not compression_level or compression_level < 1 or compression_level > 9 then
23         module:log("warn", "Invalid compression level in config: %s", tostring(compression_level));
24         module:log("warn", "Module loading aborted. Compression won't be available.");
25         return;
26 end
27
28 module:add_event_hook("stream-features",
29                 function (session, features)
30                         if not session.compressed then
31                                 -- FIXME only advertise compression support when TLS layer has no compression enabled
32                                 features:add_child(compression_stream_feature);
33                         end
34                 end
35 );
36
37 module:hook("s2s-stream-features",
38                 function (data)
39                         local session, features = data.session, data.features;
40                         -- FIXME only advertise compression support when TLS layer has no compression enabled
41                         features:add_child(compression_stream_feature);
42                 end
43 );
44
45 -- S2Sout handling aka the client perspective in the S2S connection
46 module:hook_stanza(xmlns_stream, "features",
47                 function (session, stanza)
48                         if not session.compressed then
49                                 module:log("debug", "FEATURES: "..stanza:pretty_print())
50                                 -- does remote server support compression?
51                                 local comp_st = stanza:child_with_name("compression");
52                                 if comp_st then
53                                         -- do we support the mechanism
54                                         for a in comp_st:children() do
55                                                 local algorithm = a[1]
56                                                 if algorithm == "zlib" then
57                                                         session.sends2s(st.stanza("compress", {xmlns=xmlns_compression_protocol}):tag("method"):text("zlib"))
58                                                         session.log("info", "Enabled compression using zlib.")
59                                                         return true;
60                                                 end
61                                         end
62                                         session.log("debug", "Remote server supports no compression algorithm we support.")
63                                 end
64                         end
65                 end
66 , 250);
67
68 -- TODO Support compression on S2S level too.
69 module:add_handler({"s2sout_unauthed", "s2sout"}, "compressed", xmlns_compression_protocol, 
70                 function(session ,stanza)
71                         session.log("debug", "Activating compression...")
72                 end
73 );
74
75 module:add_handler({"c2s_unauthed", "c2s", "s2sin_unauthed", "s2sin"}, "compress", xmlns_compression_protocol,
76                 function(session, stanza)
77                         -- fail if we are already compressed
78                         if session.compressed then
79                                 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
80                                 session.send(error_st);
81                                 session.log("warn", "Tried to establish another compression layer.");
82                         end
83                         
84                         -- checking if the compression method is supported
85                         local method = stanza:child_with_name("method")[1];
86                         if method == "zlib" then
87                                 session.log("info", method.." compression selected.");
88                                 (session.sends2s or session.send)(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
89                                 session:reset_stream();
90                                 
91                                 -- create deflate and inflate streams
92                                 local status, deflate_stream = pcall(zlib.deflate, compression_level);
93                                 if status == false then
94                                         local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
95                                         (session.sends2s or session.send)(error_st);
96                                         session.log("error", "Failed to create zlib.deflate filter.");
97                                         module:log("error", deflate_stream);
98                                         return
99                                 end
100                                 
101                                 local status, inflate_stream = pcall(zlib.inflate);
102                                 if status == false then
103                                         local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
104                                         (session.sends2s or session.send)(error_st);
105                                         session.log("error", "Failed to create zlib.deflate filter.");
106                                         module:log("error", inflate_stream);
107                                         return
108                                 end
109                                 
110                                 -- setup compression for session.w
111                                 local function setup_compression(session)
112                                         local old_send = session.send;
113                                 
114                                         session.send = function(t)
115                                                         local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync');
116                                                         if status == false then
117                                                                 session:close({
118                                                                         condition = "undefined-condition";
119                                                                         text = compressed;
120                                                                         extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
121                                                                 });
122                                                                 module:log("warn", compressed);
123                                                                 return;
124                                                         end
125                                                         old_send(compressed);
126                                                 end;
127                                 end
128                                 setup_compression(session);
129                                         
130                                 -- setup decompression for session.data
131                                 local function setup_decompression(session)
132                                         local old_data = session.data
133                                         session.data = function(conn, data)
134                                                         local status, decompressed, eof = pcall(inflate_stream, data);
135                                                         if status == false then
136                                                                 session:close({
137                                                                         condition = "undefined-condition";
138                                                                         text = decompressed;
139                                                                         extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
140                                                                 });
141                                                                 module:log("warn", decompressed);
142                                                                 return;
143                                                         end
144                                                         old_data(conn, decompressed);
145                                                 end;
146                                 end
147                                 setup_decompression(session);
148                                 
149                                 local session_reset_stream = session.reset_stream;
150                                 session.reset_stream = function(session)
151                                                 session_reset_stream(session);
152                                                 setup_decompression(session);
153                                                 return true;
154                                         end;
155                                 session.compressed = true;
156                         else
157                                 session.log("info", method.." compression selected. But we don't support it.");
158                                 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
159                                 (session.sends2s or session.send)(error_st);
160                         end
161                 end
162 );
163