mod_tls: Offer the host-specific cert (when there is one) to incoming c2s/s2s connect...
[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
12 local xmlns_compression_feature = "http://jabber.org/features/compress"
13 local xmlns_compression_protocol = "http://jabber.org/protocol/compress"
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
18 -- if not defined assume admin wants best compression
19 if compression_level == nil then compression_level = 9 end;
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 -- TODO Support compression on S2S level too.
38 module:add_handler({"c2s_unauthed", "c2s"}, "compress", xmlns_compression_protocol,
39                 function(session, stanza)
40                         -- fail if we are already compressed
41                         if session.compressed then
42                                 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
43                                 session.send(error_st);
44                                 session:log("warn", "Tried to establish another compression layer.");
45                         end
46                         
47                         -- checking if the compression method is supported
48                         local method = stanza:child_with_name("method")[1];
49                         if method == "zlib" then
50                                 session.log("info", method.." compression selected.");
51                                 session.send(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
52                                 session:reset_stream();
53                                 
54                                 -- create deflate and inflate streams
55                                 local status, deflate_stream = pcall(zlib.deflate, compression_level);
56                                 if status == false then
57                                         local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
58                                         session.send(error_st);
59                                         session:log("error", "Failed to create zlib.deflate filter.");
60                                         module:log("error", deflate_stream);
61                                         return
62                                 end
63                                 
64                                 local status, inflate_stream = pcall(zlib.inflate);
65                                 if status == false then
66                                         local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
67                                         session.send(error_st);
68                                         session:log("error", "Failed to create zlib.deflate filter.");
69                                         module:log("error", inflate_stream);
70                                         return
71                                 end
72                                 
73                                 -- setup compression for session.w
74                                 local old_send = session.send;
75                                 
76                                 session.send = function(t)
77                                                 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync');
78                                                 if status == false then
79                                                         session:close({
80                                                                 condition = "undefined-condition";
81                                                                 text = compressed;
82                                                                 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
83                                                         });
84                                                         module:log("warn", compressed);
85                                                         return;
86                                                 end
87                                                 old_send(compressed);
88                                         end;
89                                         
90                                 -- setup decompression for session.data
91                                 local function setup_decompression(session)
92                                         local old_data = session.data
93                                         session.data = function(conn, data)
94                                                         local status, decompressed, eof = pcall(inflate_stream, data);
95                                                         if status == false then
96                                                                 session:close({
97                                                                         condition = "undefined-condition";
98                                                                         text = decompressed;
99                                                                         extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
100                                                                 });
101                                                                 module:log("warn", decompressed);
102                                                                 return;
103                                                         end
104                                                         old_data(conn, decompressed);
105                                                 end;
106                                 end
107                                 setup_decompression(session);
108                                 
109                                 local session_reset_stream = session.reset_stream;
110                                 session.reset_stream = function(session)
111                                                 session_reset_stream(session);
112                                                 setup_decompression(session);
113                                                 return true;
114                                         end;
115                                 session.compressed = true;
116                         else
117                                 session.log("info", method.." compression selected. But we don't support it.");
118                                 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
119                                 session.send(error_st);
120                         end
121                 end
122 );