Comparison

plugins/mod_compression.lua @ 3532:4f2cd1c688e1

mod_compression: Updated to use the new events API.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 16 Oct 2010 06:53:59 +0500
parent 3301:772fb30b28fb
child 3540:bc139431830b
comparison
equal deleted inserted replaced
3531:f41e1cfe92f4 3532:4f2cd1c688e1
125 end 125 end
126 return decompressed; 126 return decompressed;
127 end); 127 end);
128 end 128 end
129 129
130 module:add_handler({"s2sout_unauthed", "s2sout"}, "compressed", xmlns_compression_protocol, 130 module:hook("stanza/http://jabber.org/protocol/compress:compressed", function(event)
131 function(session ,stanza) 131 local session = event.origin;
132 session.log("debug", "Activating compression...") 132
133 if session.type == "s2sout_unauthed" or session.type == "s2sout" then
134 session.log("debug", "Activating compression...")
135 -- create deflate and inflate streams
136 local deflate_stream = get_deflate_stream(session);
137 if not deflate_stream then return true; end
138
139 local inflate_stream = get_inflate_stream(session);
140 if not inflate_stream then return true; end
141
142 -- setup compression for session.w
143 setup_compression(session, deflate_stream);
144
145 -- setup decompression for session.data
146 setup_decompression(session, inflate_stream);
147 session:reset_stream();
148 local default_stream_attr = {xmlns = "jabber:server", ["xmlns:stream"] = "http://etherx.jabber.org/streams",
149 ["xmlns:db"] = 'jabber:server:dialback', version = "1.0", to = session.to_host, from = session.from_host};
150 session.sends2s("<?xml version='1.0'?>");
151 session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
152 session.compressed = true;
153 return true;
154 end
155 end);
156
157 module:hook("stanza/http://jabber.org/protocol/compress:compress", function(event)
158 local session, stanza = event.origin, event.stanza;
159
160 if session.type == "c2s" or session.type == "s2sin" or session.type == "c2s_unauthed" or session.type == "s2sin_unauthed" then
161 -- fail if we are already compressed
162 if session.compressed then
163 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
164 (session.sends2s or session.send)(error_st);
165 session.log("debug", "Client tried to establish another compression layer.");
166 return true;
167 end
168
169 -- checking if the compression method is supported
170 local method = stanza:child_with_name("method");
171 method = method and (method[1] or "");
172 if method == "zlib" then
173 session.log("debug", "zlib compression enabled.");
174
133 -- create deflate and inflate streams 175 -- create deflate and inflate streams
134 local deflate_stream = get_deflate_stream(session); 176 local deflate_stream = get_deflate_stream(session);
135 if not deflate_stream then return end 177 if not deflate_stream then return true; end
136 178
137 local inflate_stream = get_inflate_stream(session); 179 local inflate_stream = get_inflate_stream(session);
138 if not inflate_stream then return end 180 if not inflate_stream then return true; end
181
182 (session.sends2s or session.send)(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
183 session:reset_stream();
139 184
140 -- setup compression for session.w 185 -- setup compression for session.w
141 setup_compression(session, deflate_stream); 186 setup_compression(session, deflate_stream);
142 187
143 -- setup decompression for session.data 188 -- setup decompression for session.data
144 setup_decompression(session, inflate_stream); 189 setup_decompression(session, inflate_stream);
145 session:reset_stream(); 190
146 local default_stream_attr = {xmlns = "jabber:server", ["xmlns:stream"] = "http://etherx.jabber.org/streams",
147 ["xmlns:db"] = 'jabber:server:dialback', version = "1.0", to = session.to_host, from = session.from_host};
148 session.sends2s("<?xml version='1.0'?>");
149 session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
150 session.compressed = true; 191 session.compressed = true;
151 end 192 elseif method then
152 ); 193 session.log("debug", "%s compression selected, but we don't support it.", tostring(method));
153 194 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
154 module:add_handler({"c2s_unauthed", "c2s", "s2sin_unauthed", "s2sin"}, "compress", xmlns_compression_protocol, 195 (session.sends2s or session.send)(error_st);
155 function(session, stanza) 196 else
156 -- fail if we are already compressed 197 (session.sends2s or session.send)(st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"));
157 if session.compressed then 198 end
158 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); 199 return true;
159 (session.sends2s or session.send)(error_st); 200 end
160 session.log("debug", "Client tried to establish another compression layer."); 201 end);
161 return; 202
162 end
163
164 -- checking if the compression method is supported
165 local method = stanza:child_with_name("method");
166 method = method and (method[1] or "");
167 if method == "zlib" then
168 session.log("debug", "zlib compression enabled.");
169
170 -- create deflate and inflate streams
171 local deflate_stream = get_deflate_stream(session);
172 if not deflate_stream then return end
173
174 local inflate_stream = get_inflate_stream(session);
175 if not inflate_stream then return end
176
177 (session.sends2s or session.send)(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
178 session:reset_stream();
179
180 -- setup compression for session.w
181 setup_compression(session, deflate_stream);
182
183 -- setup decompression for session.data
184 setup_decompression(session, inflate_stream);
185
186 session.compressed = true;
187 elseif method then
188 session.log("debug", "%s compression selected, but we don't support it.", tostring(method));
189 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
190 (session.sends2s or session.send)(error_st);
191 else
192 (session.sends2s or session.send)(st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"));
193 end
194 end
195 );
196