We can't rely on 3-element array response when calling
WwwCoderepo for ViewVCS endpoints since that uses Qspawn
internally. Thus, we have to allow two Qspawn objects to run in
parallel and ensure `qspawn.wcb' only gets called once, so we
end up duplicating the entire $ctx to ensure this.
my $parse_cgi_headers = \&PublicInbox::GitHTTPBackend::parse_cgi_headers;
sub call {
my $parse_cgi_headers = \&PublicInbox::GitHTTPBackend::parse_cgi_headers;
sub call {
+ my ($self, $env, $ctx) = @_; # $ctx is optional, used by WWW
my $path_info = $env->{PATH_INFO};
my $cgit_data;
my $path_info = $env->{PATH_INFO};
my $cgit_data;
my $rdr = input_prepare($env) or return r(500);
my $qsp = PublicInbox::Qspawn->new($self->{cmd}, $cgi_env, $rdr);
my $limiter = $self->{pi_cfg}->limiter('-cgit');
my $rdr = input_prepare($env) or return r(500);
my $qsp = PublicInbox::Qspawn->new($self->{cmd}, $cgi_env, $rdr);
my $limiter = $self->{pi_cfg}->limiter('-cgit');
- $qsp->psgi_return($env, $limiter, $parse_cgi_headers);
+ $qsp->psgi_return($env, $limiter, $parse_cgi_headers, $ctx);
-# Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) 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
# 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
}
sub parse_cgi_headers {
}
sub parse_cgi_headers {
+ my ($r, $bref, $ctx) = @_;
return r(500) unless defined $r && $r >= 0;
$$bref =~ s/\A(.*?)\r?\n\r?\n//s or return $r == 0 ? r(500) : undef;
my $h = $1;
return r(500) unless defined $r && $r >= 0;
$$bref =~ s/\A(.*?)\r?\n\r?\n//s or return $r == 0 ? r(500) : undef;
my $h = $1;
+
+ # fallback to WwwCoderepo if cgit 404s. Duplicating $ctx prevents
+ # ->finalize from the current Qspawn from using qspawn.wcb
+ if ($code == 404 && $ctx->{www} && !$ctx->{_coderepo_tried}++) {
+ my %ctx = %$ctx;
+ $ctx{env} = +{ %{$ctx->{env}} };
+ delete $ctx->{env}->{'qspawn.wcb'};
+ $ctx->{env}->{'plack.skip-deflater'} = 1; # prevent 2x gzip
+ my $res = $ctx->{www}->coderepo->srv(\%ctx);
+ $res->(delete $ctx{env}->{'qspawn.wcb'}) if ref($res) eq 'CODE';
+ $res; # non ARRAY ref for ->psgi_return_init_cb
+ } else {
+ [ $code, \@h ]
+ }
my ($self) = @_;
my $r = rd_hdr($self) or return;
my $env = $self->{psgi_env};
my ($self) = @_;
my $r = rd_hdr($self) or return;
my $env = $self->{psgi_env};
- my $filter = delete $env->{'qspawn.filter'} //
- PublicInbox::GzipFilter::qsp_maybe($r->[1], $env);
+ my $filter = delete($env->{'qspawn.filter'}) // (ref($r) eq 'ARRAY' ?
+ PublicInbox::GzipFilter::qsp_maybe($r->[1], $env) : undef);
my $wcb = delete $env->{'qspawn.wcb'};
my $async = delete $self->{async}; # PublicInbox::HTTPD::Async
my $wcb = delete $env->{'qspawn.wcb'};
my $async = delete $self->{async}; # PublicInbox::HTTPD::Async
- if (scalar(@$r) == 3) { # error
+ 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;
event_step($self);
}
if ($async) { # calls rpipe->close && ->event_step
$async->close; # PublicInbox::HTTPD::Async::close
} else {
$self->{rpipe}->close;
event_step($self);
}
+ $wcb->($r) if ref($r) eq 'ARRAY';
} elsif ($async) {
# done reading headers, handoff to read body
my $fh = $wcb->($r); # scalar @$r == 2
} elsif ($async) {
# done reading headers, handoff to read body
my $fh = $wcb->($r); # scalar @$r == 2