1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # TODO: reduce duplication from PublicInbox::NNTPdeflate
6 package PublicInbox::IMAPdeflate;
10 use base qw(PublicInbox::IMAP);
11 use Compress::Raw::Zlib;
15 -WindowBits => -15, # RFC 1951
19 # global deflate context and buffer
20 my $zbuf = \(my $buf = '');
24 ($zout, $err) = Compress::Raw::Zlib::Deflate->new(
25 # nnrpd (INN) and Compress::Raw::Zlib favor MemLevel=9,
26 # the zlib C library and git use MemLevel=8 as the default
28 -Bufsize => 65536, # same as nnrpd
29 -WindowBits => -15, # RFC 1951
32 $err == Z_OK or die "Failed to initialize zlib deflate stream: $err";
36 my ($class, $self, $tag) = @_;
37 my ($in, $err) = Compress::Raw::Zlib::Inflate->new(%IN_OPT);
39 $self->err("Inflate->new failed: $err");
40 $self->write(\"$tag BAD failed to activate compression\r\n");
43 $self->write(\"$tag OK DEFLATE active\r\n");
48 # overrides PublicInbox::NNTP::compressed
52 my ($self, $rbuf, $len, $off) = @_;
54 my $zin = $self->{zin} or return; # closed
56 my $dbuf = delete($self->{dbuf}) // '';
57 $doff = length($dbuf);
58 my $r = PublicInbox::DS::do_read($self, \$dbuf, $len, $doff) or return;
60 # Workaround inflate bug appending to OOK scalars:
61 # <https://rt.cpan.org/Ticket/Display.html?id=132734>
62 # We only have $off if the client is pipelining, and pipelining
63 # is where our substr() OOK optimization in event_step makes sense.
70 # assert(length($$rbuf) == $off) as far as NNTP.pm is concerned
71 # -ConsumeInput is true, so $dbuf is automatically emptied
72 my $err = $zin->inflate($dbuf, $rbuf);
74 $self->{dbuf} = $dbuf if $dbuf ne '';
75 $r = length($$rbuf) and return $r;
76 # nothing ready, yet, get more, later
85 # override PublicInbox::DS::msg_more
89 # $_[1] may be a reference or not for ->deflate
90 my $err = $zout->deflate($_[1], $zbuf);
91 $err == Z_OK or die "->deflate failed $err";
99 $zbuf = \(my $next = '');
101 my $err = $zout->flush($deflated, Z_FULL_FLUSH);
102 $err == Z_OK or die "->flush failed $err";
104 # We can still let the lower socket layer do buffering:
105 PublicInbox::DS::msg_more($self, $$deflated);
108 # compatible with PublicInbox::DS::write, so $_[1] may be a reference or not
111 return PublicInbox::DS::write($self, $_[1]) if ref($_[1]) eq 'CODE';
113 my $deflated = $zbuf;
114 $zbuf = \(my $next = '');
116 # $_[1] may be a reference or not for ->deflate
117 my $err = $zout->deflate($_[1], $deflated);
118 $err == Z_OK or die "->deflate failed $err";
119 $err = $zout->flush($deflated, Z_FULL_FLUSH);
120 $err == Z_OK or die "->flush failed $err";
122 # We can still let the socket layer do buffering:
123 PublicInbox::DS::write($self, $deflated);