- my $fail = sub {
- my ($e) = @_;
- if ($e eq 'EAGAIN') {
- select($vin, undef, undef, undef) if defined $vin;
- # $vin is undef on async, so this is a noop on EAGAIN
- return;
+
+ # Danga::Socket users, we queue up the read_enable callback to
+ # fire after pending writes are complete:
+ my $buf = '';
+ 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; # incomplete headers
+ $r->[0] == 403 ? serve_dumb($env, $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:
+ if ($async) {
+ $async->close; # calls rpipe->close
+ } else {
+ $rpipe->close;
+ $end->();
+ }
+ return $res->($r);
+ }
+ if ($async) {
+ $fh = $res->($r);
+ return $async->async_pass($io, $fh, \$buf);