unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/3] www: cgit: fall back to WwwCoderepo on 404s
Date: Wed,  5 Oct 2022 22:29:40 +0000	[thread overview]
Message-ID: <20221005222941.4098-3-e@80x24.org> (raw)
In-Reply-To: <20221005222941.4098-1-e@80x24.org>

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.
---
 lib/PublicInbox/Cgit.pm           |  4 ++--
 lib/PublicInbox/GitHTTPBackend.pm | 19 ++++++++++++++++---
 lib/PublicInbox/Qspawn.pm         |  8 ++++----
 3 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/lib/PublicInbox/Cgit.pm b/lib/PublicInbox/Cgit.pm
index 1112d9f8..298663c7 100644
--- a/lib/PublicInbox/Cgit.pm
+++ b/lib/PublicInbox/Cgit.pm
@@ -83,7 +83,7 @@ my @PASS_ENV = qw(
 my $parse_cgi_headers = \&PublicInbox::GitHTTPBackend::parse_cgi_headers;
 
 sub call {
-	my ($self, $env) = @_;
+	my ($self, $env, $ctx) = @_; # $ctx is optional, used by WWW
 	my $path_info = $env->{PATH_INFO};
 	my $cgit_data;
 
@@ -109,7 +109,7 @@ sub call {
 	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);
 }
 
 1;
diff --git a/lib/PublicInbox/GitHTTPBackend.pm b/lib/PublicInbox/GitHTTPBackend.pm
index ba3a8f20..61a13560 100644
--- a/lib/PublicInbox/GitHTTPBackend.pm
+++ b/lib/PublicInbox/GitHTTPBackend.pm
@@ -1,4 +1,4 @@
-# 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
@@ -132,7 +132,7 @@ sub input_prepare {
 }
 
 sub parse_cgi_headers {
-	my ($r, $bref) = @_;
+	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;
@@ -146,7 +146,20 @@ sub parse_cgi_headers {
 			push @h, $k, $v;
 		}
 	}
-	[ $code, \@h ]
+
+	# 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 ]
+	}
 }
 
 1;
diff --git a/lib/PublicInbox/Qspawn.pm b/lib/PublicInbox/Qspawn.pm
index cea34fc3..ef9db43e 100644
--- a/lib/PublicInbox/Qspawn.pm
+++ b/lib/PublicInbox/Qspawn.pm
@@ -225,19 +225,19 @@ sub psgi_return_init_cb {
 	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
-	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);
 		}
-		$wcb->($r);
+		$wcb->($r) if ref($r) eq 'ARRAY';
 	} elsif ($async) {
 		# done reading headers, handoff to read body
 		my $fh = $wcb->($r); # scalar @$r == 2

  parent reply	other threads:[~2022-10-05 22:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-05 22:29 [PATCH 0/3] www: configurable cgit fallback for coderepos Eric Wong
2022-10-05 22:29 ` [PATCH 1/3] www: do not call ->coderepo->srv on sub ref Eric Wong
2022-10-05 22:29 ` Eric Wong [this message]
2022-10-05 22:29 ` [PATCH 3/3] www: support publicinbox.cgit knob Eric Wong
2022-10-07  7:42   ` [squash 4/3] manpage fix (was: [PATCH 3/3] www: support publicinbox.cgit knob) Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221005222941.4098-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).