Comparison

plugins/mod_compression.lua @ 1678:79eb903d0e67

Using pcall to make save the rest from zlib fails. Emit errors on those fails.
author Tobias Markmann <tm@ayena.de>
date Thu, 13 Aug 2009 13:20:32 +0200
parent 1677:f0961ce9830c
child 1679:e2eb0eaf2544
comparison
equal deleted inserted replaced
1677:f0961ce9830c 1678:79eb903d0e67
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";
9 local zlib = require "zlib"; 9 local zlib = require "zlib";
10 local print = print 10 local print = print;
11 local pcall = pcall;
11 12
12 local xmlns_compression_feature = "http://jabber.org/features/compress" 13 local xmlns_compression_feature = "http://jabber.org/features/compress"
13 local xmlns_compression_protocol = "http://jabber.org/protocol/compress" 14 local xmlns_compression_protocol = "http://jabber.org/protocol/compress"
14 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up(); 15 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up();
15 16
43 session.log("info", method.." compression selected."); 44 session.log("info", method.." compression selected.");
44 session.send(st.stanza("compressed", {xmlns=xmlns_compression_protocol})); 45 session.send(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
45 session:reset_stream(); 46 session:reset_stream();
46 47
47 -- create deflate and inflate streams 48 -- create deflate and inflate streams
48 local deflate_stream = zlib.deflate(compression_level); 49 local status, deflate_stream = pcall(zlib.deflate, compression_level);
49 local inflate_stream = zlib.inflate(); 50 if status == false then
51 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
52 session.send(error_st);
53 session:log("error", "Failed to create zlib.deflate filter.");
54 module:log("error", inflate_stream);
55 return
56 end
57
58 local status, inflate_stream = pcall(zlib.inflate);
59 if status == false then
60 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
61 session.send(error_st);
62 session:log("error", "Failed to create zlib.deflate filter.");
63 module:log("error", inflate_stream);
64 return
65 end
50 66
51 -- setup compression for session.w 67 -- setup compression for session.w
52 local old_send = session.send; 68 local old_send = session.send;
53 69
54 session.send = function(t) 70 session.send = function(t)
55 local compressed, eof = deflate_stream(tostring(t), 'sync'); 71 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync');
72 if status == false then
73 session:close({
74 condition = "undefined-condition";
75 text = compressed;
76 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
77 });
78 module:log("error", compressed);
79 return;
80 end
56 old_send(compressed); 81 old_send(compressed);
57 end; 82 end;
58 83
59 -- setup decompression for session.data 84 -- setup decompression for session.data
60 local function setup_decompression(session) 85 local function setup_decompression(session)
61 local old_data = session.data 86 local old_data = session.data
62 session.data = function(conn, data) 87 session.data = function(conn, data)
63 local decompressed, eof = inflate_stream(data); 88 local status, decompressed, eof = pcall(inflate_stream, data);
89 if status == false then
90 session:close({
91 condition = "undefined-condition";
92 text = compressed;
93 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
94 });
95 module:log("error", compressed);
96 return;
97 end
64 old_data(conn, decompressed); 98 old_data(conn, decompressed);
65 end; 99 end;
66 end 100 end
67 setup_decompression(session); 101 setup_decompression(session);
68 102