Software /
code /
prosody
Comparison
plugins/mod_compression.lua @ 3148:a83a995fe5db
mod_compression: Use filters! \o/
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 02 Jun 2010 18:24:56 +0100 |
parent | 3127:94639081f327 |
child | 3202:1a8a5b89a5ad |
comparison
equal
deleted
inserted
replaced
3147:bc8c31399520 | 3148:a83a995fe5db |
---|---|
12 | 12 |
13 local xmlns_compression_feature = "http://jabber.org/features/compress" | 13 local xmlns_compression_feature = "http://jabber.org/features/compress" |
14 local xmlns_compression_protocol = "http://jabber.org/protocol/compress" | 14 local xmlns_compression_protocol = "http://jabber.org/protocol/compress" |
15 local xmlns_stream = "http://etherx.jabber.org/streams"; | 15 local xmlns_stream = "http://etherx.jabber.org/streams"; |
16 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up(); | 16 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up(); |
17 local add_filter = require "util.filters".add_filter; | |
17 | 18 |
18 local compression_level = module:get_option("compression_level"); | 19 local compression_level = module:get_option("compression_level"); |
19 -- if not defined assume admin wants best compression | 20 -- if not defined assume admin wants best compression |
20 if compression_level == nil then compression_level = 9 end; | 21 if compression_level == nil then compression_level = 9 end; |
21 | 22 |
92 return inflate_stream | 93 return inflate_stream |
93 end | 94 end |
94 | 95 |
95 -- setup compression for a stream | 96 -- setup compression for a stream |
96 local function setup_compression(session, deflate_stream) | 97 local function setup_compression(session, deflate_stream) |
97 local old_send = (session.sends2s or session.send); | 98 add_filter(session, "bytes/out", function(t) |
98 | 99 session.log(t) |
99 local new_send = function(t) | 100 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync'); |
100 --TODO: Better code injection in the sending process | 101 if status == false then |
101 session.log(t) | 102 session:close({ |
102 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync'); | 103 condition = "undefined-condition"; |
103 if status == false then | 104 text = compressed; |
104 session:close({ | 105 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); |
105 condition = "undefined-condition"; | 106 }); |
106 text = compressed; | 107 module:log("warn", "%s", tostring(compressed)); |
107 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); | 108 return; |
108 }); | 109 end |
109 module:log("warn", "%s", tostring(compressed)); | 110 return compressed; |
110 return; | 111 end); |
111 end | |
112 session.conn:write(compressed); | |
113 end; | |
114 | |
115 if session.sends2s then session.sends2s = new_send | |
116 elseif session.send then session.send = new_send end | |
117 end | 112 end |
118 | 113 |
119 -- setup decompression for a stream | 114 -- setup decompression for a stream |
120 local function setup_decompression(session, inflate_stream) | 115 local function setup_decompression(session, inflate_stream) |
121 local old_data = session.data | 116 add_filter(session, "bytes/in", function(data) |
122 session.data = function(data) | 117 local status, decompressed, eof = pcall(inflate_stream, data); |
123 local status, decompressed, eof = pcall(inflate_stream, data); | 118 if status == false then |
124 if status == false then | 119 session:close({ |
125 session:close({ | 120 condition = "undefined-condition"; |
126 condition = "undefined-condition"; | 121 text = decompressed; |
127 text = decompressed; | 122 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); |
128 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); | 123 }); |
129 }); | 124 module:log("warn", "%s", tostring(decompressed)); |
130 module:log("warn", "%s", tostring(decompressed)); | 125 return; |
131 return; | 126 end |
132 end | 127 return decompressed; |
133 old_data(decompressed); | 128 end); |
134 end; | |
135 end | 129 end |
136 | 130 |
137 module:add_handler({"s2sout_unauthed", "s2sout"}, "compressed", xmlns_compression_protocol, | 131 module:add_handler({"s2sout_unauthed", "s2sout"}, "compressed", xmlns_compression_protocol, |
138 function(session ,stanza) | 132 function(session ,stanza) |
139 session.log("debug", "Activating compression...") | 133 session.log("debug", "Activating compression...") |