Software /
code /
prosody
Annotate
plugins/mod_compression.lua @ 2302:2dc9bbf25447
net.server_select: Fix possibility of losing data sent from a socket just before it closes (thanks Sjoerd Simons)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 03 Dec 2009 02:28:34 +0000 |
parent | 2293:abd28ed5d8b0 |
child | 2324:9f7eb1054a2f |
rev | line source |
---|---|
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
1 -- Prosody IM |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
2 -- Copyright (C) 2009 Tobias Markmann |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
3 -- |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
4 -- This project is MIT/X11 licensed. Please see the |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
5 -- COPYING file in the source package for more information. |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
6 -- |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
7 |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
8 local st = require "util.stanza"; |
1671
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
9 local zlib = require "zlib"; |
1678
79eb903d0e67
Using pcall to make save the rest from zlib fails. Emit errors on those fails.
Tobias Markmann <tm@ayena.de>
parents:
1677
diff
changeset
|
10 local pcall = pcall; |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
11 local xmlns_compression_feature = "http://jabber.org/features/compress" |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
12 local xmlns_compression_protocol = "http://jabber.org/protocol/compress" |
2280
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
13 local xmlns_stream = "http://etherx.jabber.org/streams"; |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
14 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up(); |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
15 |
1676 | 16 local compression_level = module:get_option("compression_level"); |
17 -- if not defined assume admin wants best compression | |
18 if compression_level == nil then compression_level = 9 end; | |
19 | |
2279 | 20 |
1676 | 21 compression_level = tonumber(compression_level); |
22 if not compression_level or compression_level < 1 or compression_level > 9 then | |
23 module:log("warn", "Invalid compression level in config: %s", tostring(compression_level)); | |
24 module:log("warn", "Module loading aborted. Compression won't be available."); | |
25 return; | |
26 end | |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
27 |
1670 | 28 module:add_event_hook("stream-features", |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
29 function (session, features) |
1673
5f81cd6d4a92
Remove space at the end of a line.
Tobias Markmann <tm@ayena.de>
parents:
1672
diff
changeset
|
30 if not session.compressed then |
1674
250bddde4162
Add a TODO for s2s compression support.
Tobias Markmann <tm@ayena.de>
parents:
1673
diff
changeset
|
31 -- FIXME only advertise compression support when TLS layer has no compression enabled |
1672
614623f393c6
Add FIXME to remember TLS compression detection.
Tobias Markmann <tm@ayena.de>
parents:
1671
diff
changeset
|
32 features:add_child(compression_stream_feature); |
614623f393c6
Add FIXME to remember TLS compression detection.
Tobias Markmann <tm@ayena.de>
parents:
1671
diff
changeset
|
33 end |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
34 end |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
35 ); |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
36 |
2280
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
37 module:hook("s2s-stream-features", |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
38 function (data) |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
39 local session, features = data.session, data.features; |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
40 -- FIXME only advertise compression support when TLS layer has no compression enabled |
2286
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
41 if not session.compressed then |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
42 features:add_child(compression_stream_feature); |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
43 end |
2280
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
44 end |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
45 ); |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
46 |
2291
c6bed51c6733
mod_compression: Some comment clean up.
Tobias Markmann <tm@ayena.de>
parents:
2289
diff
changeset
|
47 -- Hook to activate compression if remote server supports it. |
2280
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
48 module:hook_stanza(xmlns_stream, "features", |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
49 function (session, stanza) |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
50 if not session.compressed then |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
51 -- does remote server support compression? |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
52 local comp_st = stanza:child_with_name("compression"); |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
53 if comp_st then |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
54 -- do we support the mechanism |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
55 for a in comp_st:children() do |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
56 local algorithm = a[1] |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
57 if algorithm == "zlib" then |
2282
6f54dac3ec2d
mod_compression: Prepare activating of compression on s2s.
Tobias Markmann <tm@ayena.de>
parents:
2280
diff
changeset
|
58 session.sends2s(st.stanza("compress", {xmlns=xmlns_compression_protocol}):tag("method"):text("zlib")) |
2280
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
59 session.log("info", "Enabled compression using zlib.") |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
60 return true; |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
61 end |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
62 end |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
63 session.log("debug", "Remote server supports no compression algorithm we support.") |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
64 end |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
65 end |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
66 end |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
67 , 250); |
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
68 |
2285
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
69 |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
70 -- returns either nil or a fully functional ready to use inflate stream |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
71 local function get_deflate_stream(session) |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
72 local status, deflate_stream = pcall(zlib.deflate, compression_level); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
73 if status == false then |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
74 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
75 (session.sends2s or session.send)(error_st); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
76 session.log("error", "Failed to create zlib.deflate filter."); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
77 module:log("error", deflate_stream); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
78 return |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
79 end |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
80 return deflate_stream |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
81 end |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
82 |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
83 -- returns either nil or a fully functional ready to use inflate stream |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
84 local function get_inflate_stream(session) |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
85 local status, inflate_stream = pcall(zlib.inflate); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
86 if status == false then |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
87 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
88 (session.sends2s or session.send)(error_st); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
89 session.log("error", "Failed to create zlib.deflate filter."); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
90 module:log("error", inflate_stream); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
91 return |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
92 end |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
93 return inflate_stream |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
94 end |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
95 |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
96 -- setup compression for a stream |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
97 local function setup_compression(session, deflate_stream) |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
98 local old_send = (session.sends2s or session.send); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
99 |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
100 local new_send = function(t) |
2286
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
101 --TODO: Better code injection in the sending process |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
102 session.log(t) |
2285
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
103 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync'); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
104 if status == false then |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
105 session:close({ |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
106 condition = "undefined-condition"; |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
107 text = compressed; |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
108 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
109 }); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
110 module:log("warn", compressed); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
111 return; |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
112 end |
2286
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
113 session.conn:write(compressed); |
2285
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
114 end; |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
115 |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
116 if session.sends2s then session.sends2s = new_send |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
117 elseif session.send then session.send = new_send end |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
118 end |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
119 |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
120 -- setup decompression for a stream |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
121 local function setup_decompression(session, inflate_stream) |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
122 local old_data = session.data |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
123 session.data = function(conn, data) |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
124 local status, decompressed, eof = pcall(inflate_stream, data); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
125 if status == false then |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
126 session:close({ |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
127 condition = "undefined-condition"; |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
128 text = decompressed; |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
129 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
130 }); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
131 module:log("warn", decompressed); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
132 return; |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
133 end |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
134 old_data(conn, decompressed); |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
135 end; |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
136 end |
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
137 |
2282
6f54dac3ec2d
mod_compression: Prepare activating of compression on s2s.
Tobias Markmann <tm@ayena.de>
parents:
2280
diff
changeset
|
138 module:add_handler({"s2sout_unauthed", "s2sout"}, "compressed", xmlns_compression_protocol, |
6f54dac3ec2d
mod_compression: Prepare activating of compression on s2s.
Tobias Markmann <tm@ayena.de>
parents:
2280
diff
changeset
|
139 function(session ,stanza) |
6f54dac3ec2d
mod_compression: Prepare activating of compression on s2s.
Tobias Markmann <tm@ayena.de>
parents:
2280
diff
changeset
|
140 session.log("debug", "Activating compression...") |
2286
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
141 -- create deflate and inflate streams |
2292
9c5941198719
mod_compression: Declaring the de-/compression pipes as local.
Tobias Markmann <tm@ayena.de>
parents:
2291
diff
changeset
|
142 local deflate_stream = get_deflate_stream(session); |
2286
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
143 if not deflate_stream then return end |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
144 |
2292
9c5941198719
mod_compression: Declaring the de-/compression pipes as local.
Tobias Markmann <tm@ayena.de>
parents:
2291
diff
changeset
|
145 local inflate_stream = get_inflate_stream(session); |
2286
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
146 if not inflate_stream then return end |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
147 |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
148 -- setup compression for session.w |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
149 setup_compression(session, deflate_stream); |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
150 |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
151 -- setup decompression for session.data |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
152 setup_decompression(session, inflate_stream); |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
153 local session_reset_stream = session.reset_stream; |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
154 session.reset_stream = function(session) |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
155 session_reset_stream(session); |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
156 setup_decompression(session, inflate_stream); |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
157 return true; |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
158 end; |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
159 session:reset_stream(); |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
160 local default_stream_attr = {xmlns = "jabber:server", ["xmlns:stream"] = "http://etherx.jabber.org/streams", |
2288
3c17fc919f7b
mod_compression: Define db (dialback) namespace in stream header.
Tobias Markmann <tm@ayena.de>
parents:
2286
diff
changeset
|
161 ["xmlns:db"] = 'jabber:server:dialback', version = "1.0", to = session.to_host, from = session.from_host}; |
2286
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
162 session.sends2s("<?xml version='1.0'?>"); |
2293
abd28ed5d8b0
mod_compression: Removing trailing whitespace.
Tobias Markmann <tm@ayena.de>
parents:
2292
diff
changeset
|
163 session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag()); |
2286
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
164 session.compressed = true; |
2282
6f54dac3ec2d
mod_compression: Prepare activating of compression on s2s.
Tobias Markmann <tm@ayena.de>
parents:
2280
diff
changeset
|
165 end |
6f54dac3ec2d
mod_compression: Prepare activating of compression on s2s.
Tobias Markmann <tm@ayena.de>
parents:
2280
diff
changeset
|
166 ); |
6f54dac3ec2d
mod_compression: Prepare activating of compression on s2s.
Tobias Markmann <tm@ayena.de>
parents:
2280
diff
changeset
|
167 |
2280
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
168 module:add_handler({"c2s_unauthed", "c2s", "s2sin_unauthed", "s2sin"}, "compress", xmlns_compression_protocol, |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
169 function(session, stanza) |
1719
cf103398e643
Don't allow double compression.
Tobias Markmann <tm@ayena.de>
parents:
1718
diff
changeset
|
170 -- fail if we are already compressed |
cf103398e643
Don't allow double compression.
Tobias Markmann <tm@ayena.de>
parents:
1718
diff
changeset
|
171 if session.compressed then |
cf103398e643
Don't allow double compression.
Tobias Markmann <tm@ayena.de>
parents:
1718
diff
changeset
|
172 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method"); |
2286
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
173 (session.sends2s or session.send)(error_st); |
2279 | 174 session.log("warn", "Tried to establish another compression layer."); |
1719
cf103398e643
Don't allow double compression.
Tobias Markmann <tm@ayena.de>
parents:
1718
diff
changeset
|
175 end |
cf103398e643
Don't allow double compression.
Tobias Markmann <tm@ayena.de>
parents:
1718
diff
changeset
|
176 |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
177 -- checking if the compression method is supported |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
178 local method = stanza:child_with_name("method")[1]; |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
179 if method == "zlib" then |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
180 session.log("info", method.." compression selected."); |
1671
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
181 |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
182 -- create deflate and inflate streams |
2292
9c5941198719
mod_compression: Declaring the de-/compression pipes as local.
Tobias Markmann <tm@ayena.de>
parents:
2291
diff
changeset
|
183 local deflate_stream = get_deflate_stream(session); |
2285
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
184 if not deflate_stream then return end |
1678
79eb903d0e67
Using pcall to make save the rest from zlib fails. Emit errors on those fails.
Tobias Markmann <tm@ayena.de>
parents:
1677
diff
changeset
|
185 |
2292
9c5941198719
mod_compression: Declaring the de-/compression pipes as local.
Tobias Markmann <tm@ayena.de>
parents:
2291
diff
changeset
|
186 local inflate_stream = get_inflate_stream(session); |
2285
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
187 if not inflate_stream then return end |
1671
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
188 |
2286
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
189 (session.sends2s or session.send)(st.stanza("compressed", {xmlns=xmlns_compression_protocol})); |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
190 session:reset_stream(); |
e0b2d934f316
mod_compression: Enabeling compression for outgoing s2s streams.
Tobias Markmann <tm@ayena.de>
parents:
2285
diff
changeset
|
191 |
1671
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
192 -- setup compression for session.w |
2285
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
193 setup_compression(session, deflate_stream); |
1671
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
194 |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
195 -- setup decompression for session.data |
2285
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
196 setup_decompression(session, inflate_stream); |
1671
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
197 |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
198 local session_reset_stream = session.reset_stream; |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
199 session.reset_stream = function(session) |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
200 session_reset_stream(session); |
2285
3dd7fdee9035
mod_compression: Some further refactoring.
Tobias Markmann <tm@ayena.de>
parents:
2284
diff
changeset
|
201 setup_decompression(session, inflate_stream); |
1671
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
202 return true; |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
203 end; |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
204 session.compressed = true; |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
205 else |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
206 session.log("info", method.." compression selected. But we don't support it."); |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
207 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method"); |
2280
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
208 (session.sends2s or session.send)(error_st); |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
209 end |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
210 end |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
211 ); |
2280
0b0fe49e5251
Enable one way stream compression on s2s links.
Tobias Markmann <tm@ayena.de>
parents:
2279
diff
changeset
|
212 |