Software /
code /
prosody
Comparison
plugins/mod_compression.lua @ 2887:765e7070d0a8
Merge with 0.6 (into 0.7, namely mod_compression fixes)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 17 Mar 2010 14:28:26 +0000 |
parent | 2613:afa20941e098 |
parent | 2886:3baee526d714 |
child | 2892:9f214431de29 |
comparison
equal
deleted
inserted
replaced
2878:9384ee36fc03 | 2887:765e7070d0a8 |
---|---|
6 -- | 6 -- |
7 | 7 |
8 local st = require "util.stanza"; | 8 local st = require "util.stanza"; |
9 local zlib = require "zlib"; | 9 local zlib = require "zlib"; |
10 local pcall = pcall; | 10 local pcall = pcall; |
11 local tostring = tostring; | |
12 | |
11 local xmlns_compression_feature = "http://jabber.org/features/compress" | 13 local xmlns_compression_feature = "http://jabber.org/features/compress" |
12 local xmlns_compression_protocol = "http://jabber.org/protocol/compress" | 14 local xmlns_compression_protocol = "http://jabber.org/protocol/compress" |
13 local xmlns_stream = "http://etherx.jabber.org/streams"; | 15 local xmlns_stream = "http://etherx.jabber.org/streams"; |
14 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(); |
15 | 17 |
69 local status, deflate_stream = pcall(zlib.deflate, compression_level); | 71 local status, deflate_stream = pcall(zlib.deflate, compression_level); |
70 if status == false then | 72 if status == false then |
71 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); | 73 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
72 (session.sends2s or session.send)(error_st); | 74 (session.sends2s or session.send)(error_st); |
73 session.log("error", "Failed to create zlib.deflate filter."); | 75 session.log("error", "Failed to create zlib.deflate filter."); |
74 module:log("error", deflate_stream); | 76 module:log("error", "%s", tostring(deflate_stream)); |
75 return | 77 return |
76 end | 78 end |
77 return deflate_stream | 79 return deflate_stream |
78 end | 80 end |
79 | 81 |
81 local function get_inflate_stream(session) | 83 local function get_inflate_stream(session) |
82 local status, inflate_stream = pcall(zlib.inflate); | 84 local status, inflate_stream = pcall(zlib.inflate); |
83 if status == false then | 85 if status == false then |
84 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); | 86 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
85 (session.sends2s or session.send)(error_st); | 87 (session.sends2s or session.send)(error_st); |
86 session.log("error", "Failed to create zlib.deflate filter."); | 88 session.log("error", "Failed to create zlib.inflate filter."); |
87 module:log("error", inflate_stream); | 89 module:log("error", "%s", tostring(inflate_stream)); |
88 return | 90 return |
89 end | 91 end |
90 return inflate_stream | 92 return inflate_stream |
91 end | 93 end |
92 | 94 |
102 session:close({ | 104 session:close({ |
103 condition = "undefined-condition"; | 105 condition = "undefined-condition"; |
104 text = compressed; | 106 text = compressed; |
105 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); | 107 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); |
106 }); | 108 }); |
107 module:log("warn", compressed); | 109 module:log("warn", "%s", tostring(compressed)); |
108 return; | 110 return; |
109 end | 111 end |
110 session.conn:write(compressed); | 112 session.conn:write(compressed); |
111 end; | 113 end; |
112 | 114 |
123 session:close({ | 125 session:close({ |
124 condition = "undefined-condition"; | 126 condition = "undefined-condition"; |
125 text = decompressed; | 127 text = decompressed; |
126 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"); |
127 }); | 129 }); |
128 module:log("warn", decompressed); | 130 module:log("warn", "%s", tostring(decompressed)); |
129 return; | 131 return; |
130 end | 132 end |
131 old_data(conn, decompressed); | 133 old_data(conn, decompressed); |
132 end; | 134 end; |
133 end | 135 end |
164 | 166 |
165 module:add_handler({"c2s_unauthed", "c2s", "s2sin_unauthed", "s2sin"}, "compress", xmlns_compression_protocol, | 167 module:add_handler({"c2s_unauthed", "c2s", "s2sin_unauthed", "s2sin"}, "compress", xmlns_compression_protocol, |
166 function(session, stanza) | 168 function(session, stanza) |
167 -- fail if we are already compressed | 169 -- fail if we are already compressed |
168 if session.compressed then | 170 if session.compressed then |
169 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method"); | 171 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
170 (session.sends2s or session.send)(error_st); | 172 (session.sends2s or session.send)(error_st); |
171 session.log("warn", "Tried to establish another compression layer."); | 173 session.log("warn", "Tried to establish another compression layer."); |
174 return; | |
172 end | 175 end |
173 | 176 |
174 -- checking if the compression method is supported | 177 -- checking if the compression method is supported |
175 local method = stanza:child_with_name("method")[1]; | 178 local method = stanza:child_with_name("method"); |
179 method = method and (method[1] or ""); | |
176 if method == "zlib" then | 180 if method == "zlib" then |
177 session.log("debug", method.." compression selected."); | 181 session.log("debug", "%s compression selected.", tostring(method)); |
178 | 182 |
179 -- create deflate and inflate streams | 183 -- create deflate and inflate streams |
180 local deflate_stream = get_deflate_stream(session); | 184 local deflate_stream = get_deflate_stream(session); |
181 if not deflate_stream then return end | 185 if not deflate_stream then return end |
182 | 186 |
197 session_reset_stream(session); | 201 session_reset_stream(session); |
198 setup_decompression(session, inflate_stream); | 202 setup_decompression(session, inflate_stream); |
199 return true; | 203 return true; |
200 end; | 204 end; |
201 session.compressed = true; | 205 session.compressed = true; |
202 else | 206 elseif method then |
203 session.log("warn", method.." compression selected. But we don't support it."); | 207 session.log("info", "%s compression selected, but we don't support it.", tostring(method)); |
204 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method"); | 208 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method"); |
205 (session.sends2s or session.send)(error_st); | 209 (session.sends2s or session.send)(error_st); |
210 else | |
211 (session.sends2s or session.send)(st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed")); | |
206 end | 212 end |
207 end | 213 end |
208 ); | 214 ); |
209 | 215 |