mod_compression: Return <setup-failed/> instead of <unsupported-method/> where applic...
[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 tostring = tostring;
12
13 local xmlns_compression_feature = "http://jabber.org/features/compress"
14 local xmlns_compression_protocol = "http://jabber.org/protocol/compress"
15 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up();
16
17 local compression_level = module:get_option("compression_level");
18 -- if not defined assume admin wants best compression
19 if compression_level == nil then compression_level = 9 end;
20
21
22 compression_level = tonumber(compression_level);
23 if not compression_level or compression_level < 1 or compression_level > 9 then
24         module:log("warn", "Invalid compression level in config: %s", tostring(compression_level));
25         module:log("warn", "Module loading aborted. Compression won't be available.");
26         return;
27 end
28
29 module:add_event_hook("stream-features",
30                 function (session, features)
31                         if not session.compressed then
32                                 -- FIXME only advertise compression support when TLS layer has no compression enabled
33                                 features:add_child(compression_stream_feature);
34                         end
35                 end
36 );
37
38 -- TODO Support compression on S2S level too.
39 module:add_handler({"c2s_unauthed", "c2s"}, "compress", xmlns_compression_protocol,
40                 function(session, stanza)
41                         -- fail if we are already compressed
42                         if session.compressed then
43                                 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
44                                 session.send(error_st);
45                                 session.log("warn", "Tried to establish another compression layer.");
46                                 return;
47                         end
48                         
49                         -- checking if the compression method is supported
50                         local method = stanza:child_with_name("method");
51                         method = method and (method[1] or "");
52                         if method == "zlib" then
53                                 -- create deflate and inflate streams
54                                 local status, deflate_stream = pcall(zlib.deflate, compression_level);
55                                 if status == false then
56                                         local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
57                                         session.send(error_st);
58                                         session.log("error", "Failed to create zlib.deflate filter.");
59                                         module:log("error", "%s", tostring(deflate_stream));
60                                         return
61                                 end
62                                 
63                                 local status, inflate_stream = pcall(zlib.inflate);
64                                 if status == false then
65                                         local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
66                                         session.send(error_st);
67                                         session.log("error", "Failed to create zlib.inflate filter.");
68                                         module:log("error", "%s", tostring(inflate_stream));
69                                         return
70                                 end
71                                 
72                                 session.log("info", method.." compression selected.");
73                                 session.send(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
74                                 session:reset_stream();
75
76                                 -- setup compression for session.w
77                                 local old_send = session.send;
78                                 
79                                 session.send = function(t)
80                                                 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync');
81                                                 if status == false then
82                                                         session:close({
83                                                                 condition = "undefined-condition";
84                                                                 text = compressed;
85                                                                 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
86                                                         });
87                                                         module:log("warn", "%s", tostring(compressed));
88                                                         return;
89                                                 end
90                                                 old_send(compressed);
91                                         end;
92                                         
93                                 -- setup decompression for session.data
94                                 local function setup_decompression(session)
95                                         local old_data = session.data
96                                         session.data = function(conn, data)
97                                                         local status, decompressed, eof = pcall(inflate_stream, data);
98                                                         if status == false then
99                                                                 session:close({
100                                                                         condition = "undefined-condition";
101                                                                         text = decompressed;
102                                                                         extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
103                                                                 });
104                                                                 module:log("warn", "%s", tostring(decompressed));
105                                                                 return;
106                                                         end
107                                                         old_data(conn, decompressed);
108                                                 end;
109                                 end
110                                 setup_decompression(session);
111                                 
112                                 local session_reset_stream = session.reset_stream;
113                                 session.reset_stream = function(session)
114                                                 session_reset_stream(session);
115                                                 setup_decompression(session);
116                                                 return true;
117                                         end;
118                                 session.compressed = true;
119                         elseif method then
120                                 session.log("info", "%s compression selected, but we don't support it.", tostring(method));
121                                 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
122                                 session.send(error_st);
123                         else
124                                 session.send(st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"));
125                         end
126                 end
127 );