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;
16 *static_result = *PublicInbox::GitHTTPBackend::static_result;
18 use PublicInbox::Qspawn;
23 my $cgit_bin = $pi_config->{'publicinbox.cgitbin'};
24 my $cgit_data = $pi_config->{'publicinbox.cgitdata'};
26 # /var/www/htdocs/cgit is the default install path from cgit.git
27 # /usr/{lib,share}/cgit is where Debian puts cgit
28 # TODO: check other distros for common paths
29 unless (defined $cgit_bin) {
30 foreach (qw(/var/www/htdocs/cgit /usr/lib/cgit)) {
31 my $x = "$_/cgit.cgi";
37 unless (defined $cgit_data) {
38 foreach my $d (qw(/var/www/htdocs/cgit /usr/share/cgit)) {
39 my $f = "$d/cgit.css";
45 ($cgit_bin, $cgit_data);
49 my ($class, $pi_config) = @_;
50 my ($cgit_bin, $cgit_data) = locate_cgit($pi_config);
54 cgit_data => $cgit_data,
55 pi_config => $pi_config,
58 $pi_config->each_inbox(sub {}); # fill in -code_repos mapped to inboxes
60 # some cgit repos may not be mapped to inboxes, so ensure those exist:
61 my $code_repos = $pi_config->{-code_repos};
62 foreach my $k (keys %$pi_config) {
63 $k =~ /\Acoderepo\.(.+)\.dir\z/ or next;
64 my $dir = $pi_config->{$k};
65 $code_repos->{$1} ||= PublicInbox::Git->new($dir);
67 while (my ($nick, $repo) = each %$code_repos) {
68 $self->{"\0$nick"} = $repo;
70 my $cgit_static = $pi_config->{-cgit_static};
71 my $static = join('|', map { quotemeta $_ } keys %$cgit_static);
72 $self->{static} = qr/\A($static)\z/;
76 # only what cgit cares about:
88 # XXX: cgit filters may care about more variables...
91 my ($self, $env) = @_;
92 my $path_info = $env->{PATH_INFO};
94 # handle requests without spawning cgit iff possible:
95 if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
96 my ($nick, $path) = ($1, $2);
97 if (my $git = $self->{"\0$nick"}) {
98 return serve($env, $git, $path);
100 } elsif ($path_info =~ m!$self->{static}!) {
102 my $type = Plack::MIME->mime_type($f);
103 return static_result($env, [], "$self->{cgit_data}$f", $type);
106 my $cgi_env = { PATH_INFO => $path_info };
107 foreach (@PASS_ENV) {
108 defined(my $v = $env->{$_}) or next;
111 $cgi_env->{'HTTPS'} = 'on' if $env->{'psgi.url_scheme'} eq 'https';
113 my $rdr = input_prepare($env) or return r(500);
114 my $qsp = PublicInbox::Qspawn->new($self->{cmd}, $cgi_env, $rdr);
115 my $limiter = $self->{pi_config}->limiter('-cgit');
116 $qsp->psgi_return($env, $limiter, sub {
118 my $res = parse_cgi_headers($r, $bref) or return; # incomplete