plugins/mod_compression.lua
changeset 5776 bd0ff8ae98a8
parent 5535 0df0afc041d7
child 6054 7a5ddbaf758d
equal deleted inserted replaced
5775:a6c2b8933507 5776:bd0ff8ae98a8
     1 -- Prosody IM
     1 -- Prosody IM
     2 -- Copyright (C) 2009-2012 Tobias Markmann
     2 -- Copyright (C) 2009-2012 Tobias Markmann
     3 -- 
     3 --
     4 -- This project is MIT/X11 licensed. Please see the
     4 -- This project is MIT/X11 licensed. Please see the
     5 -- COPYING file in the source package for more information.
     5 -- COPYING file in the source package for more information.
     6 --
     6 --
     7 
     7 
     8 local st = require "util.stanza";
     8 local st = require "util.stanza";
   101 				extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
   101 				extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
   102 			});
   102 			});
   103 			return;
   103 			return;
   104 		end
   104 		end
   105 		return compressed;
   105 		return compressed;
   106 	end);	
   106 	end);
   107 end
   107 end
   108 
   108 
   109 -- setup decompression for a stream
   109 -- setup decompression for a stream
   110 local function setup_decompression(session, inflate_stream)
   110 local function setup_decompression(session, inflate_stream)
   111 	add_filter(session, "bytes/in", function(data)
   111 	add_filter(session, "bytes/in", function(data)
   123 	end);
   123 	end);
   124 end
   124 end
   125 
   125 
   126 module:hook("stanza/http://jabber.org/protocol/compress:compressed", function(event)
   126 module:hook("stanza/http://jabber.org/protocol/compress:compressed", function(event)
   127 	local session = event.origin;
   127 	local session = event.origin;
   128 	
   128 
   129 	if session.type == "s2sout_unauthed" or session.type == "s2sout" then
   129 	if session.type == "s2sout_unauthed" or session.type == "s2sout" then
   130 		session.log("debug", "Activating compression...")
   130 		session.log("debug", "Activating compression...")
   131 		-- create deflate and inflate streams
   131 		-- create deflate and inflate streams
   132 		local deflate_stream = get_deflate_stream(session);
   132 		local deflate_stream = get_deflate_stream(session);
   133 		if not deflate_stream then return true; end
   133 		if not deflate_stream then return true; end
   134 		
   134 
   135 		local inflate_stream = get_inflate_stream(session);
   135 		local inflate_stream = get_inflate_stream(session);
   136 		if not inflate_stream then return true; end
   136 		if not inflate_stream then return true; end
   137 		
   137 
   138 		-- setup compression for session.w
   138 		-- setup compression for session.w
   139 		setup_compression(session, deflate_stream);
   139 		setup_compression(session, deflate_stream);
   140 			
   140 
   141 		-- setup decompression for session.data
   141 		-- setup decompression for session.data
   142 		setup_decompression(session, inflate_stream);
   142 		setup_decompression(session, inflate_stream);
   143 		session:reset_stream();
   143 		session:reset_stream();
   144 		session:open_stream(session.from_host, session.to_host);
   144 		session:open_stream(session.from_host, session.to_host);
   145 		session.compressed = true;
   145 		session.compressed = true;
   156 			local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
   156 			local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
   157 			(session.sends2s or session.send)(error_st);
   157 			(session.sends2s or session.send)(error_st);
   158 			session.log("debug", "Client tried to establish another compression layer.");
   158 			session.log("debug", "Client tried to establish another compression layer.");
   159 			return true;
   159 			return true;
   160 		end
   160 		end
   161 		
   161 
   162 		-- checking if the compression method is supported
   162 		-- checking if the compression method is supported
   163 		local method = stanza:child_with_name("method");
   163 		local method = stanza:child_with_name("method");
   164 		method = method and (method[1] or "");
   164 		method = method and (method[1] or "");
   165 		if method == "zlib" then
   165 		if method == "zlib" then
   166 			session.log("debug", "zlib compression enabled.");
   166 			session.log("debug", "zlib compression enabled.");
   167 			
   167 
   168 			-- create deflate and inflate streams
   168 			-- create deflate and inflate streams
   169 			local deflate_stream = get_deflate_stream(session);
   169 			local deflate_stream = get_deflate_stream(session);
   170 			if not deflate_stream then return true; end
   170 			if not deflate_stream then return true; end
   171 			
   171 
   172 			local inflate_stream = get_inflate_stream(session);
   172 			local inflate_stream = get_inflate_stream(session);
   173 			if not inflate_stream then return true; end
   173 			if not inflate_stream then return true; end
   174 			
   174 
   175 			(session.sends2s or session.send)(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
   175 			(session.sends2s or session.send)(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
   176 			session:reset_stream();
   176 			session:reset_stream();
   177 			
   177 
   178 			-- setup compression for session.w
   178 			-- setup compression for session.w
   179 			setup_compression(session, deflate_stream);
   179 			setup_compression(session, deflate_stream);
   180 				
   180 
   181 			-- setup decompression for session.data
   181 			-- setup decompression for session.data
   182 			setup_decompression(session, inflate_stream);
   182 			setup_decompression(session, inflate_stream);
   183 			
   183 
   184 			session.compressed = true;
   184 			session.compressed = true;
   185 		elseif method then
   185 		elseif method then
   186 			session.log("debug", "%s compression selected, but we don't support it.", tostring(method));
   186 			session.log("debug", "%s compression selected, but we don't support it.", tostring(method));
   187 			local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
   187 			local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
   188 			(session.sends2s or session.send)(error_st);
   188 			(session.sends2s or session.send)(error_st);