3 -- |
3 -- |
4 -- This project is MIT/X11 licensed. Please see the |
4 -- This project is MIT/X11 licensed. Please see the |
5 -- COPYING file in the source package for more information. |
5 -- COPYING file in the source package for more information. |
6 -- |
6 -- |
7 |
7 |
8 local st = require "util.stanza"; |
|
9 local zlib = require "zlib"; |
8 local zlib = require "zlib"; |
10 |
9 |
11 local xmlns_compression_feature = "http://jabber.org/features/compress" |
10 local xmlns_compression_feature = "http://jabber.org/features/compress" |
12 local xmlns_compression_protocol = "http://jabber.org/protocol/compress" |
11 local xmlns_compression_protocol = "http://jabber.org/protocol/compress" |
13 local xmlns_stream = "http://etherx.jabber.org/streams"; |
12 local xmlns_stream = "http://etherx.jabber.org/streams"; |
16 |
15 |
17 -- returns either nil or a fully functional ready to use inflate stream |
16 -- returns either nil or a fully functional ready to use inflate stream |
18 local function get_deflate_stream(session) |
17 local function get_deflate_stream(session) |
19 local status, deflate_stream = pcall(zlib.deflate, compression_level); |
18 local status, deflate_stream = pcall(zlib.deflate, compression_level); |
20 if status == false then |
19 if status == false then |
21 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
20 local error_st = verse.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
22 session:send(error_st); |
21 session:send(error_st); |
23 session:error("Failed to create zlib.deflate filter: %s", tostring(deflate_stream)); |
22 session:error("Failed to create zlib.deflate filter: %s", tostring(deflate_stream)); |
24 return |
23 return |
25 end |
24 end |
26 return deflate_stream |
25 return deflate_stream |
28 |
27 |
29 -- returns either nil or a fully functional ready to use inflate stream |
28 -- returns either nil or a fully functional ready to use inflate stream |
30 local function get_inflate_stream(session) |
29 local function get_inflate_stream(session) |
31 local status, inflate_stream = pcall(zlib.inflate); |
30 local status, inflate_stream = pcall(zlib.inflate); |
32 if status == false then |
31 if status == false then |
33 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
32 local error_st = verse.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"); |
34 session:send(error_st); |
33 session:send(error_st); |
35 session:error("Failed to create zlib.inflate filter: %s", tostring(inflate_stream)); |
34 session:error("Failed to create zlib.inflate filter: %s", tostring(inflate_stream)); |
36 return |
35 return |
37 end |
36 end |
38 return inflate_stream |
37 return inflate_stream |
45 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync'); |
44 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync'); |
46 if status == false then |
45 if status == false then |
47 session:close({ |
46 session:close({ |
48 condition = "undefined-condition"; |
47 condition = "undefined-condition"; |
49 text = compressed; |
48 text = compressed; |
50 extra = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("processing-failed"); |
49 extra = verse.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("processing-failed"); |
51 }); |
50 }); |
52 session:warn("Compressed send failed: %s", tostring(compressed)); |
51 session:warn("Compressed send failed: %s", tostring(compressed)); |
53 return; |
52 return; |
54 end |
53 end |
55 session.conn:write(compressed); |
54 session.conn:write(compressed); |
64 local status, decompressed, eof = pcall(inflate_stream, data); |
63 local status, decompressed, eof = pcall(inflate_stream, data); |
65 if status == false then |
64 if status == false then |
66 session:close({ |
65 session:close({ |
67 condition = "undefined-condition"; |
66 condition = "undefined-condition"; |
68 text = decompressed; |
67 text = decompressed; |
69 extra = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("processing-failed"); |
68 extra = verse.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("processing-failed"); |
70 }); |
69 }); |
71 stream:warn("%s", tostring(decompressed)); |
70 stream:warn("%s", tostring(decompressed)); |
72 return; |
71 return; |
73 end |
72 end |
74 return old_data(conn, decompressed); |
73 return old_data(conn, decompressed); |
83 if comp_st then |
82 if comp_st then |
84 -- do we support the mechanism |
83 -- do we support the mechanism |
85 for a in comp_st:children() do |
84 for a in comp_st:children() do |
86 local algorithm = a[1] |
85 local algorithm = a[1] |
87 if algorithm == "zlib" then |
86 if algorithm == "zlib" then |
88 stream:send(st.stanza("compress", {xmlns=xmlns_compression_protocol}):tag("method"):text("zlib")) |
87 stream:send(verse.stanza("compress", {xmlns=xmlns_compression_protocol}):tag("method"):text("zlib")) |
89 stream:debug("Enabled compression using zlib.") |
88 stream:debug("Enabled compression using zlib.") |
90 return true; |
89 return true; |
91 end |
90 end |
92 end |
91 end |
93 session:debug("Remote server supports no compression algorithm we support.") |
92 session:debug("Remote server supports no compression algorithm we support.") |