Annotate

plugins/compression.lua @ 443:89526c890363

libs.hashes: Add back pure Lua sha1 implementation This one is slow so it gets to be first so it gets replaced if a better lib is found.
author Kim Alvefur <zash@zash.se>
date Wed, 17 Feb 2021 14:57:59 +0100
parent 380:0891b4e27766
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Copyright (C) 2009-2010 Matthew Wild
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2009-2010 Tobias Markmann
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
3 --
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
250
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 197
diff changeset
8 local verse = require "verse";
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local zlib = require "zlib";
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local xmlns_compression_feature = "http://jabber.org/features/compress"
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local xmlns_compression_protocol = "http://jabber.org/protocol/compress"
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local xmlns_stream = "http://etherx.jabber.org/streams";
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local compression_level = 9;
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 -- returns either nil or a fully functional ready to use inflate stream
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local function get_deflate_stream(session)
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local status, deflate_stream = pcall(zlib.deflate, compression_level);
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 if status == false then
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 176
diff changeset
21 local error_st = verse.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 session:send(error_st);
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 session:error("Failed to create zlib.deflate filter: %s", tostring(deflate_stream));
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return deflate_stream
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 -- returns either nil or a fully functional ready to use inflate stream
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local function get_inflate_stream(session)
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local status, inflate_stream = pcall(zlib.inflate);
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if status == false then
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 176
diff changeset
33 local error_st = verse.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 session:send(error_st);
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 session:error("Failed to create zlib.inflate filter: %s", tostring(inflate_stream));
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 return
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 return inflate_stream
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 -- setup compression for a stream
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local function setup_compression(session, deflate_stream)
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 function session:send(t)
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 --TODO: Better code injection in the sending process
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync');
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if status == false then
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 session:close({
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 condition = "undefined-condition";
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 text = compressed;
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 176
diff changeset
50 extra = verse.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("processing-failed");
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 });
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 session:warn("Compressed send failed: %s", tostring(compressed));
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 return;
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 session.conn:write(compressed);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
56 end;
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 -- setup decompression for a stream
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 local function setup_decompression(session, inflate_stream)
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local old_data = session.data
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 session.data = function(conn, data)
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 session:debug("Decompressing data...");
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local status, decompressed, eof = pcall(inflate_stream, data);
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if status == false then
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 session:close({
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 condition = "undefined-condition";
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 text = decompressed;
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 176
diff changeset
69 extra = verse.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("processing-failed");
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 });
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 stream:warn("%s", tostring(decompressed));
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return;
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 return old_data(conn, decompressed);
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end;
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 function verse.plugins.compression(stream)
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local function handle_features(features)
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if not stream.compressed then
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 -- does remote server support compression?
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local comp_st = features:child_with_name("compression");
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if comp_st then
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 -- do we support the mechanism
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 for a in comp_st:children() do
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local algorithm = a[1]
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 if algorithm == "zlib" then
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 176
diff changeset
88 stream:send(verse.stanza("compress", {xmlns=xmlns_compression_protocol}):tag("method"):text("zlib"))
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 stream:debug("Enabled compression using zlib.")
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 return true;
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 session:debug("Remote server supports no compression algorithm we support.")
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 local function handle_compressed(stanza)
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 if stanza.name == "compressed" then
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 stream:debug("Activating compression...")
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 -- create deflate and inflate streams
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 local deflate_stream = get_deflate_stream(stream);
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 if not deflate_stream then return end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
104
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 local inflate_stream = get_inflate_stream(stream);
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 if not inflate_stream then return end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
107
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 -- setup compression for stream.w
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 setup_compression(stream, deflate_stream);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
110
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 -- setup decompression for stream.data
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 setup_decompression(stream, inflate_stream);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
113
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 stream.compressed = true;
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 stream:reopen();
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 elseif stanza.name == "failure" then
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 stream:warn("Failed to establish compression");
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end
72
1649a6022adb verse.plugins.compression: Bump priority of stream-features handler to negotiate compression before resource binding
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
120 stream:hook("stream-features", handle_features, 250);
71
5b74d155036d verse.plugins.compression: Support for stream compression (requires brimworks lua-zlib)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 stream:hook("stream/"..xmlns_compression_protocol, handle_compressed);
79
da06d4996992 plugins.compression: Add newline at end of file (helps old versions of squish for one thing)
Matthew Wild <mwild1@gmail.com>
parents: 74
diff changeset
122 end