Software /
code /
prosody
Annotate
plugins/mod_compression.lua @ 1674:250bddde4162
Add a TODO for s2s compression support.
author | Tobias Markmann <tm@ayena.de> |
---|---|
date | Thu, 13 Aug 2009 10:57:56 +0200 |
parent | 1673:5f81cd6d4a92 |
child | 1676:719350956714 |
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"; |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
10 local print = print |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
11 |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
12 local xmlns_compression_feature = "http://jabber.org/features/compress" |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
13 local xmlns_compression_protocol = "http://jabber.org/protocol/compress" |
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 |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
16 |
1670 | 17 module:add_event_hook("stream-features", |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
18 function (session, features) |
1673
5f81cd6d4a92
Remove space at the end of a line.
Tobias Markmann <tm@ayena.de>
parents:
1672
diff
changeset
|
19 if not session.compressed then |
1674
250bddde4162
Add a TODO for s2s compression support.
Tobias Markmann <tm@ayena.de>
parents:
1673
diff
changeset
|
20 -- 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
|
21 features:add_child(compression_stream_feature); |
614623f393c6
Add FIXME to remember TLS compression detection.
Tobias Markmann <tm@ayena.de>
parents:
1671
diff
changeset
|
22 end |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
23 end |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
24 ); |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
25 |
1674
250bddde4162
Add a TODO for s2s compression support.
Tobias Markmann <tm@ayena.de>
parents:
1673
diff
changeset
|
26 -- TODO Support compression on S2S level too. |
1670 | 27 module:add_handler("c2s_unauthed", "compress", xmlns_compression_protocol, |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
28 function(session, stanza) |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
29 -- checking if the compression method is supported |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
30 local method = stanza:child_with_name("method")[1]; |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
31 if method == "zlib" then |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
32 session.log("info", method.." compression selected."); |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
33 session.send(st.stanza("compressed", {xmlns=xmlns_compression_protocol})); |
1671
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
34 session:reset_stream(); |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
35 |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
36 -- create deflate and inflate streams |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
37 local deflate_stream = zlib.deflate(9); |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
38 local inflate_stream = zlib.inflate(); |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
39 |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
40 -- setup compression for session.w |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
41 local old_send = session.send; |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
42 |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
43 session.send = function(t) |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
44 local compressed, eof = deflate_stream(tostring(t), 'sync'); |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
45 old_send(compressed); |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
46 end; |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
47 |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
48 -- setup decompression for session.data |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
49 local function setup_decompression(session) |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
50 local old_data = session.data |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
51 session.data = function(conn, data) |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
52 local decompressed, eof = inflate_stream(data); |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
53 old_data(conn, decompressed); |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
54 end; |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
55 end |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
56 setup_decompression(session); |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
57 |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
58 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
|
59 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
|
60 session_reset_stream(session); |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
61 setup_decompression(session); |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
62 return true; |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
63 end; |
d196ac213104
Actually inject de- and compression into the reading/writing functions.
Tobias Markmann <tm@ayena.de>
parents:
1670
diff
changeset
|
64 session.compressed = true; |
1669
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
65 else |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
66 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
|
67 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method"); |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
68 session.send(error_st); |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
69 end |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
70 end |
b8eec163a823
Commit initial version of mod_compression.
Tobias Markmann <tm@ayena.de>
parents:
diff
changeset
|
71 ); |