]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/GitHTTPBackend.pm
githttpbackend: avoid infinite loop on generic PSGI servers
[public-inbox.git] / lib / PublicInbox / GitHTTPBackend.pm
index 4ad3fd1ee3aca9ef0b3c44e3a9ac6598163ba825..6efe5b31d485086b0f4c4825a98b52c5180d9e60 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # when no endpoints match, fallback to this and serve a static file
@@ -225,7 +225,6 @@ sub serve_smart {
        };
        my $res;
        my $async = $env->{'pi-httpd.async'}; # XXX unstable API
-       my $io = $env->{'psgix.io'};
        my $cb = sub {
                my $r = $rd_hdr->() or return;
                $rd_hdr = undef;
@@ -236,17 +235,16 @@ sub serve_smart {
                                $rpipe->close;
                                $end->();
                        }
-                       return $res->($r);
-               }
-               if ($async) {
+                       $res->($r);
+               } elsif ($async) {
                        $fh = $res->($r);
-                       return $async->async_pass($io, $fh, \$buf);
+                       $async->async_pass($env->{'psgix.io'}, $fh, \$buf);
+               } else { # for synchronous PSGI servers
+                       require PublicInbox::GetlineBody;
+                       $r->[2] = PublicInbox::GetlineBody->new($rpipe, $end,
+                                                               $buf);
+                       $res->($r);
                }
-
-               # for synchronous PSGI servers
-               require PublicInbox::GetlineBody;
-               $r->[2] = PublicInbox::GetlineBody->new($rpipe, $end, $buf);
-               $res->($r);
        };
        sub {
                ($res) = @_;
@@ -282,6 +280,7 @@ sub input_to_file {
                        err($env, "error reading input: $!");
                        return;
                }
+               last if $r == 0;
                my $off = 0;
                while ($r > 0) {
                        my $w = syswrite($in, $buf, $r, $off);