1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 package PublicInbox::GzipFilter;
8 use Compress::Raw::Zlib qw(Z_FINISH Z_OK);
9 my %OPT = (-WindowBits => 15 + 16, -AppendOutput => 1);
12 my ($gz, $err) = Compress::Raw::Zlib::Deflate->new(%OPT);
13 $err == Z_OK or die "Deflate->new failed: $err";
14 bless { gz => $gz }, shift;
17 # for Qspawn if using $env->{'pi-httpd.async'}
24 # for GetlineBody (via Qspawn) when NOT using $env->{'pi-httpd.async'}
27 my $zbuf = delete($self->{zbuf});
28 if (defined $_[1]) { # my $buf = $_[1];
29 my $err = $self->{gz}->deflate($_[1], $zbuf);
30 die "gzip->deflate: $err" if $err != Z_OK;
31 return $zbuf if length($zbuf) >= 8192;
33 $self->{zbuf} = $zbuf;
35 } else { # undef == EOF
36 my $err = $self->{gz}->flush($zbuf, Z_FINISH);
37 die "gzip->flush: $err" if $err != Z_OK;
43 # my $ret = bytes::length($_[1]); # XXX does anybody care?
44 $_[0]->{fh}->write(translate($_[0], $_[1]));
49 my $fh = delete $self->{fh};
50 $fh->write(translate($self, undef));