]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Qspawn.pm
httpd/async: retry reads properly when parsing headers
[public-inbox.git] / lib / PublicInbox / Qspawn.pm
index ef9db43e637d6c51113d0d04d1c1bd56e5b9b3fc..7984e35a80510ffdf09cbb7f5e325ecd9eb764a0 100644 (file)
@@ -40,7 +40,7 @@ my $def_limiter;
 # $cmd_env is the environ for the child process (not PSGI env)
 # $opt can include redirects and perhaps other process spawning options
 # {qsp_err} is an optional error buffer callers may access themselves
-sub new ($$$;) {
+sub new {
        my ($class, $cmd, $cmd_env, $opt) = @_;
        bless { args => [ $cmd, $cmd_env, $opt ] }, $class;
 }
@@ -106,10 +106,12 @@ sub finalize ($$) {
                return unless $@;
                warn "E: $@"; # hope qspawn.wcb can handle it
        }
+       return if $self->{passed}; # another command chained it
        if (my $wcb = delete $env->{'qspawn.wcb'}) {
                # have we started writing, yet?
+               my $code = delete $env->{'qspawn.fallback'} // 500;
                require PublicInbox::WwwStatic;
-               $wcb->(PublicInbox::WwwStatic::r(500));
+               $wcb->(PublicInbox::WwwStatic::r($code));
        }
 }
 
@@ -132,7 +134,7 @@ sub start ($$$) {
        }
 }
 
-sub psgi_qx_init_cb {
+sub psgi_qx_init_cb { # this may be PublicInbox::HTTPD::Async {cb}
        my ($self) = @_;
        my $async = delete $self->{async}; # PublicInbox::HTTPD::Async
        my ($r, $buf);
@@ -187,7 +189,7 @@ sub event_step {
        my ($self, $err) = @_; # $err: $!
        warn "psgi_{return,qx} $err" if defined($err);
        finish($self);
-       my ($fh, $qx_fh) = delete(@$self{qw(fh qx_fh)});
+       my ($fh, $qx_fh) = delete(@$self{qw(qfh qx_fh)});
        $fh->close if $fh; # async-only (psgi_return)
 }
 
@@ -221,11 +223,16 @@ sub rd_hdr ($) {
        $ret;
 }
 
-sub psgi_return_init_cb {
+sub psgi_return_init_cb { # this may be PublicInbox::HTTPD::Async {cb}
        my ($self) = @_;
        my $r = rd_hdr($self) or return;
        my $env = $self->{psgi_env};
-       my $filter = delete($env->{'qspawn.filter'}) // (ref($r) eq 'ARRAY' ?
+       my $filter;
+       if (ref($r) eq 'ARRAY' && Scalar::Util::blessed($r->[2]) &&
+                       $r->[2]->can('attach')) {
+               $filter = pop @$r;
+       }
+       $filter //= delete($env->{'qspawn.filter'}) // (ref($r) eq 'ARRAY' ?
                PublicInbox::GzipFilter::qsp_maybe($r->[1], $env) : undef);
 
        my $wcb = delete $env->{'qspawn.wcb'};
@@ -233,23 +240,30 @@ sub psgi_return_init_cb {
        if (ref($r) ne 'ARRAY' || scalar(@$r) == 3) { # error
                if ($async) { # calls rpipe->close && ->event_step
                        $async->close; # PublicInbox::HTTPD::Async::close
-               } else {
-                       $self->{rpipe}->close;
+               } else { # generic PSGI:
+                       delete($self->{rpipe})->close;
                        event_step($self);
+                       waitpid_err($self);
+               }
+               if (ref($r) eq 'ARRAY') { # error
+                       $wcb->($r)
+               } elsif (ref($r) eq 'CODE') { # chain another command
+                       $r->($wcb);
+                       $self->{passed} = 1;
                }
-               $wcb->($r) if ref($r) eq 'ARRAY';
+               # else do nothing
        } elsif ($async) {
                # done reading headers, handoff to read body
                my $fh = $wcb->($r); # scalar @$r == 2
                $fh = $filter->attach($fh) if $filter;
-               $self->{fh} = $fh;
+               $self->{qfh} = $fh;
                $async->async_pass($env->{'psgix.io'}, $fh,
                                        delete($self->{hdr_buf}));
        } else { # for synchronous PSGI servers
                require PublicInbox::GetlineBody;
+               my $buf = delete $self->{hdr_buf};
                $r->[2] = PublicInbox::GetlineBody->new($self->{rpipe},
-                                       \&event_step, $self,
-                                       ${$self->{hdr_buf}}, $filter);
+                                       \&event_step, $self, $$buf, $filter);
                $wcb->($r);
        }
 }