]> Sergey Matveev's repositories - public-inbox.git/commitdiff
nntp: improve error reporting for COMPRESS
authorEric Wong <e@80x24.org>
Sun, 7 Jul 2019 06:57:43 +0000 (06:57 +0000)
committerEric Wong <e@80x24.org>
Sun, 7 Jul 2019 06:57:43 +0000 (06:57 +0000)
Add some checks for errors at initialization, though there's not
much that can be done with ENOMEM-type errors aside from
dropping clients.

We can also get rid of the scary FIXME for MemLevel=8.  It was a
stupid error on my part in the original per-client deflate
stream implementation calling C::R::Z::{Inflate,Deflate} in
array context and getting the extra dualvar error code as a
string result, causing the {zin}/{zout} array refs to have
extra array elements.

lib/PublicInbox/NNTP.pm
lib/PublicInbox/NNTPdeflate.pm

index 895858b754cd44e0a95e99889075a444b54393ee..6fee29f4819b384197bb64defb6f8a3fb7b6c560 100644 (file)
@@ -922,9 +922,8 @@ sub cmd_starttls ($) {
 # RFC 8054
 sub cmd_compress ($$) {
        my ($self, $alg) = @_;
-       return '503 Only the DEFLATE is supported' if uc($alg) ne 'DEFLATE';
+       return '503 Only DEFLATE is supported' if uc($alg) ne 'DEFLATE';
        return r502 if $self->compressed || !$have_deflate;
-       res($self, '206 Compression active');
        PublicInbox::NNTPdeflate->enable($self);
        $self->requeue;
        undef
index 78da2a588674fe59b48ab1793ab1b2cfbc9dc576..10e2337cda97ba1fa83fcecc28fdfd97592c92b3 100644 (file)
@@ -30,24 +30,33 @@ my %IN_OPT = (
 
 # global deflate context and buffer
 my $zbuf = \(my $buf = '');
-my $zout = Compress::Raw::Zlib::Deflate->new(
-       # nnrpd (INN) and Compress::Raw::Zlib favor MemLevel=9,
-       # but the zlib C library and git use MemLevel=8 as the default.
-       # FIXME: sometimes clients fail with 8, so we use 9
-       # -MemLevel => 9,
-
-       # needs more testing, nothing obviously different in terms of memory
-       -Bufsize => 65536,
+my $zout;
+{
+       my $err;
+       ($zout, $err) = Compress::Raw::Zlib::Deflate->new(
+               # nnrpd (INN) and Compress::Raw::Zlib favor MemLevel=9,
+               # the zlib C library and git use MemLevel=8 as the default
+               # -MemLevel => 9,
+               -Bufsize => 65536, # same as nnrpd
+               -WindowBits => -15, # RFC 1951
+               -AppendOutput => 1,
+       );
+       $err == Z_OK or die "Failed to initialize zlib deflate stream: $err";
+}
 
-       -WindowBits => -15, # RFC 1951
-       -AppendOutput => 1,
-);
 
 sub enable {
        my ($class, $self) = @_;
+       my ($in, $err) = Compress::Raw::Zlib::Inflate->new(%IN_OPT);
+       if ($err != Z_OK) {
+               $self->err("Inflate->new failed: $err");
+               $self->res('403 Unable to activate compression');
+               return;
+       }
        unlock_hash(%$self);
+       $self->res('206 Compression active');
        bless $self, $class;
-       $self->{zin} = [ Compress::Raw::Zlib::Inflate->new(%IN_OPT), '' ];
+       $self->{zin} = [ $in, '' ];
 }
 
 # overrides PublicInbox::NNTP::compressed