1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # wrapper for cgit(1) and git-http-backend(1) for browsing and
5 # serving git code repositories. Requires 'publicinbox.cgitrc'
6 # directive to be set in the public-inbox config file.
8 package PublicInbox::Cgit;
10 use PublicInbox::GitHTTPBackend;
11 # not bothering with Exporter for a one-off
12 *r = *PublicInbox::GitHTTPBackend::r;
13 *input_prepare = *PublicInbox::GitHTTPBackend::input_prepare;
14 *parse_cgi_headers = *PublicInbox::GitHTTPBackend::parse_cgi_headers;
15 *serve = *PublicInbox::GitHTTPBackend::serve;
17 use PublicInbox::Qspawn;
20 my ($class, $pi_config) = @_;
21 my $cgit_bin = $pi_config->{'publicinbox.cgitbin'} ||
22 # Debian default location:
23 '/usr/lib/cgit/cgit.cgi';
27 pi_config => $pi_config,
30 $pi_config->each_inbox(sub {}); # fill in -code_repos mapped to inboxes
32 # some cgit repos may not be mapped to inboxes, so ensure those exist:
33 my $code_repos = $pi_config->{-code_repos};
34 foreach my $k (keys %$pi_config) {
35 $k =~ /\Acoderepo\.(.+)\.dir\z/ or next;
36 my $dir = $pi_config->{$k};
37 $code_repos->{$1} ||= PublicInbox::Git->new($dir);
39 while (my ($nick, $repo) = each %$code_repos) {
40 $self->{"\0$nick"} = $repo;
45 # only what cgit cares about:
57 # XXX: cgit filters may care about more variables...
60 my ($self, $env) = @_;
61 my $path_info = $env->{PATH_INFO};
63 # handle requests without spawning cgit iff possible:
64 if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
65 my ($nick, $path) = ($1, $2);
66 if (my $git = $self->{"\0$nick"}) {
67 return serve($env, $git, $path);
71 my $cgi_env = { PATH_INFO => $path_info };
73 defined(my $v = $env->{$_}) or next;
76 $cgi_env->{'HTTPS'} = 'on' if $env->{'psgi.url_scheme'} eq 'https';
78 my $rdr = input_prepare($env) or return r(500);
79 my $qsp = PublicInbox::Qspawn->new($self->{cmd}, $cgi_env, $rdr);
80 my $limiter = $self->{pi_config}->limiter('-cgit');
81 $qsp->psgi_return($env, $limiter, sub {
83 my $res = parse_cgi_headers($r, $bref) or return; # incomplete