Mercurial > prosody-modules
annotate mod_compression_unsafe/mod_compression_unsafe.lua @ 3656:3e0f4d727825
mod_vcard_muc: Add an alternative method of signaling avatar change
When the avatar has been changed, a signal is sent that the room
configuration has changed. Clients then do a disco#info query to find
the SHA-1 of the new avatar. They can then fetch it as before, or not if
they have it cached already.
This is meant to be less disruptive than signaling via presence, which
caused problems for some clients.
If clients transition to the new method, the old one can eventually be removed.
The namespace is made up while waiting for standardization.
Otherwise it is very close to what's described in
https://xmpp.org/extensions/inbox/muc-avatars.html
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 25 Aug 2019 20:46:43 +0200 |
parents | 226693a22fc9 |
children |
rev | line source |
---|---|
2482
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- Prosody IM |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- Copyright (C) 2009-2012 Tobias Markmann |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- This project is MIT/X11 licensed. Please see the |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- COPYING file in the source package for more information. |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 -- |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local st = require "util.stanza"; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local zlib = require "zlib"; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local pcall = pcall; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local tostring = tostring; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local xmlns_compression_feature = "http://jabber.org/features/compress" |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local xmlns_compression_protocol = "http://jabber.org/protocol/compress" |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local xmlns_stream = "http://etherx.jabber.org/streams"; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up(); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local add_filter = require "util.filters".add_filter; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local compression_level = module:get_option_number("compression_level", 7); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 if not compression_level or compression_level < 1 or compression_level > 9 then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 module:log("warn", "Invalid compression level in config: %s", tostring(compression_level)); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 module:log("warn", "Module loading aborted. Compression won't be available."); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 return; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 module:hook("stream-features", function(event) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 local origin, features = event.origin, event.features; |
2795
226693a22fc9
mod_compression_unsafe: Fix logic bug in previous commit
Matthew Wild <mwild1@gmail.com>
parents:
2794
diff
changeset
|
29 if not origin.compressed and (origin.type == "c2s" or origin.type == "c2s_unbound") then |
2482
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 features:add_child(compression_stream_feature); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 end); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 module:hook("s2s-stream-features", function(event) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 local origin, features = event.origin, event.features; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 if not origin.compressed and origin.type == "s2sin" then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 features:add_child(compression_stream_feature); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 end); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 -- Hook to activate compression if remote server supports it. |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 module:hook_stanza(xmlns_stream, "features", |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 function (session, stanza) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 if not session.compressed and session.type == "s2sout" then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 -- does remote server support compression? |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 local comp_st = stanza:get_child("compression", xmlns_compression_feature); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 if comp_st then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 -- do we support the mechanism |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 for a in comp_st:childtags("method") do |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 local algorithm = a:get_text(); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 if algorithm == "zlib" then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 session.sends2s(st.stanza("compress", {xmlns=xmlns_compression_protocol}):tag("method"):text("zlib")) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 session.log("debug", "Enabled compression using zlib.") |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 return true; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 session.log("debug", "Remote server supports no compression algorithm we support.") |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 , 250); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 -- returns either nil or a fully functional ready to use inflate stream |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 local function get_deflate_stream(session) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 local status, deflate_stream = pcall(zlib.deflate, compression_level); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 if status == false then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 (session.sends2s or session.send)(error_st); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 session.log("error", "Failed to create zlib.deflate filter."); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 module:log("error", "%s", tostring(deflate_stream)); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 return |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 return deflate_stream |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 -- returns either nil or a fully functional ready to use inflate stream |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 local function get_inflate_stream(session) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 local status, inflate_stream = pcall(zlib.inflate); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 if status == false then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 (session.sends2s or session.send)(error_st); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 session.log("error", "Failed to create zlib.inflate filter."); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 module:log("error", "%s", tostring(inflate_stream)); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 return |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 return inflate_stream |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 -- setup compression for a stream |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 local function setup_compression(session, deflate_stream) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 add_filter(session, "bytes/out", function(t) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync'); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 if status == false then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 module:log("warn", "%s", tostring(compressed)); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 session:close({ |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 condition = "undefined-condition"; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 text = compressed; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 }); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 return; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 return compressed; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 end); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 -- setup decompression for a stream |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 local function setup_decompression(session, inflate_stream) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 add_filter(session, "bytes/in", function(data) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 local status, decompressed, eof = pcall(inflate_stream, data); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 if status == false then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 module:log("warn", "%s", tostring(decompressed)); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 session:close({ |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 condition = "undefined-condition"; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 text = decompressed; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 }); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 return; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 return decompressed; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 end); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 module:hook("stanza/http://jabber.org/protocol/compress:compressed", function(event) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 local session = event.origin; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 if session.type == "s2sout" then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 session.log("debug", "Activating compression...") |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 -- create deflate and inflate streams |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 local deflate_stream = get_deflate_stream(session); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 if not deflate_stream then return true; end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 local inflate_stream = get_inflate_stream(session); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 if not inflate_stream then return true; end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 -- setup compression for session.w |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 setup_compression(session, deflate_stream); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 -- setup decompression for session.data |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 setup_decompression(session, inflate_stream); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 session:reset_stream(); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 session:open_stream(session.from_host, session.to_host); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 session.compressed = true; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 return true; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 end); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 module:hook("stanza/http://jabber.org/protocol/compress:failure", function(event) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 local err = event.stanza:get_child(); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 (event.origin.log or module._log)("warn", "Compression setup failed (%s)", err and err.name or "unknown reason"); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 return true; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 end); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 module:hook("stanza/http://jabber.org/protocol/compress:compress", function(event) |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 local session, stanza = event.origin, event.stanza; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 if session.type == "c2s" or session.type == "s2sin" then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 -- fail if we are already compressed |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 if session.compressed then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 (session.sends2s or session.send)(error_st); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 session.log("debug", "Client tried to establish another compression layer."); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 return true; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 -- checking if the compression method is supported |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 local method = stanza:get_child_text("method"); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 if method == "zlib" then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 session.log("debug", "zlib compression enabled."); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 -- create deflate and inflate streams |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 local deflate_stream = get_deflate_stream(session); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 if not deflate_stream then return true; end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 local inflate_stream = get_inflate_stream(session); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 if not inflate_stream then return true; end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 (session.sends2s or session.send)(st.stanza("compressed", {xmlns=xmlns_compression_protocol})); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 session:reset_stream(); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 -- setup compression for session.w |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 setup_compression(session, deflate_stream); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 -- setup decompression for session.data |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 setup_decompression(session, inflate_stream); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 session.compressed = true; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 elseif method then |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 session.log("debug", "%s compression selected, but we don't support it.", tostring(method)); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method"); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 (session.sends2s or session.send)(error_st); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 else |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 (session.sends2s or session.send)(st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed")); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 return true; |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 end |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 end); |
c96a53b0b820
mod_compression: Import from Prosody, revision 0c69305ab525
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 |