- if (my $async = $env->{'pi-httpd.async'}) {
- my $res;
- my $q = sub {
- $async->close;
- $end->();
- $res->(@_);
- };
- # $async is PublicInbox::HTTPD::Async->new($rpipe, $cb)
- $async = $async->($rpipe, sub {
- my $r = sysread($rpipe, $buf, 1024, length($buf));
- if (!defined $r || $r == 0) {
- return $q->(r(500, 'http-backend error'));
- }
- $r = parse_cgi_headers(\$buf) or return;
- if ($r->[0] == 403) {
- return $q->(serve_dumb($cgi, $git, $path));
- }
- my $fh = $res->($r);
- $fh->write($buf);
- $buf = undef;
- my $dst = Plack::Util::inline_object(
- write => sub { $fh->write(@_) },
- close => sub {
- $end->();
- $fh->close;
- });
- $async->async_pass($env->{'psgix.io'}, $dst);
- });
- sub { ($res) = @_ }; # let Danga::Socket handle the rest.
- } else { # getline + close for other PSGI servers
- my $r;
- do {
- $r = read($rpipe, $buf, 1024, length($buf));
- if (!defined $r || $r == 0) {
- return r(500, 'http-backend error');
- }
- $r = parse_cgi_headers(\$buf);
- } until ($r);
- return serve_dumb($cgi, $git, $path) if $r->[0] == 403;
+ my $rd_hdr = sub {
+ my $r = sysread($rpipe, $buf, 1024, length($buf));
+ return if !defined($r) && ($!{EINTR} || $!{EAGAIN});
+ return r(500, 'http-backend error') unless $r;
+ $r = parse_cgi_headers(\$buf) or return;
+ $r->[0] == 403 ? serve_dumb($cgi, $git, $path) : $r;
+ };
+ my $res;
+ my $async = $env->{'pi-httpd.async'};
+ my $io = $env->{'psgix.io'};
+ my $cb = sub {
+ my $r = $rd_hdr->() or return;
+ $rd_hdr = undef;
+ if (scalar(@$r) == 3) { # error:
+ $async->close if $async;
+ return $res->($r);
+ }
+ if ($async) {
+ $fh = $res->($r);
+ return $async->async_pass($io, $fh, \$buf);
+ }
+
+ # for synchronous PSGI servers