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;
12 # not bothering with Exporter for a one-off
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;
18 use PublicInbox::WwwStatic qw(r);
22 my $cgit_bin = $pi_config->{'publicinbox.cgitbin'};
23 my $cgit_data = $pi_config->{'publicinbox.cgitdata'};
25 # /var/www/htdocs/cgit is the default install path from cgit.git
26 # /usr/{lib,share}/cgit is where Debian puts cgit
27 # TODO: check other distros for common paths
28 unless (defined $cgit_bin) {
29 foreach (qw(/var/www/htdocs/cgit /usr/lib/cgit)) {
30 my $x = "$_/cgit.cgi";
36 unless (defined $cgit_data) {
37 my @dirs = qw(/var/www/htdocs/cgit /usr/share/cgit);
39 # local installs of cgit from source have
40 # CGIT_SCRIPT_PATH==CGIT_DATA_PATH by default,
41 # so we can usually infer the cgit_data path from cgit_bin
42 if (defined($cgit_bin) && $cgit_bin =~ m!\A(.+?)/[^/]+\z!) {
43 unshift @dirs, $1 if -d $1;
45 foreach my $d (@dirs) {
46 my $f = "$d/cgit.css";
52 ($cgit_bin, $cgit_data);
56 my ($class, $pi_config) = @_;
57 my ($cgit_bin, $cgit_data) = locate_cgit($pi_config);
61 cgit_data => $cgit_data,
62 pi_config => $pi_config,
65 $pi_config->fill_all; # fill in -code_repos mapped to inboxes
67 # some cgit repos may not be mapped to inboxes, so ensure those exist:
68 my $code_repos = $pi_config->{-code_repos};
69 foreach my $k (keys %$pi_config) {
70 $k =~ /\Acoderepo\.(.+)\.dir\z/ or next;
71 my $dir = $pi_config->{$k};
72 $code_repos->{$1} ||= PublicInbox::Git->new($dir);
74 while (my ($nick, $repo) = each %$code_repos) {
75 $self->{"\0$nick"} = $repo;
77 my $cgit_static = $pi_config->{-cgit_static};
78 my $static = join('|', map { quotemeta $_ } keys %$cgit_static);
79 $self->{static} = qr/\A($static)\z/;
83 # only what cgit cares about:
95 # XXX: cgit filters may care about more variables...
97 sub cgit_parse_hdr { # {parse_hdr} for Qspawn
99 my $res = parse_cgi_headers($r, $bref) or return; # incomplete
104 my ($self, $env) = @_;
105 my $path_info = $env->{PATH_INFO};
108 # handle requests without spawning cgit iff possible:
109 if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
110 my ($nick, $path) = ($1, $2);
111 if (my PublicInbox::Git $git = $self->{"\0$nick"}) {
112 return serve($env, $git, $path);
114 } elsif ($path_info =~ m!$self->{static}! &&
115 defined($cgit_data = $self->{cgit_data})) {
116 my $f = $cgit_data.$1; # {static} only matches leading slash
117 return PublicInbox::WwwStatic::response($env, [], $f);
120 my $cgi_env = { PATH_INFO => $path_info };
121 foreach (@PASS_ENV) {
122 defined(my $v = $env->{$_}) or next;
125 $cgi_env->{'HTTPS'} = 'on' if $env->{'psgi.url_scheme'} eq 'https';
127 my $rdr = input_prepare($env) or return r(500);
128 my $qsp = PublicInbox::Qspawn->new($self->{cmd}, $cgi_env, $rdr);
129 my $limiter = $self->{pi_config}->limiter('-cgit');
130 $qsp->psgi_return($env, $limiter, \&cgit_parse_hdr);