]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Cgit.pm
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / Cgit.pm
1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
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.
7
8 package PublicInbox::Cgit;
9 use strict;
10 use PublicInbox::GitHTTPBackend;
11 use PublicInbox::Git;
12 # not bothering with Exporter for a one-off
13 *input_prepare = *PublicInbox::GitHTTPBackend::input_prepare;
14 *serve = *PublicInbox::GitHTTPBackend::serve;
15 use warnings;
16 use PublicInbox::Qspawn;
17 use PublicInbox::WwwStatic qw(r);
18
19 sub locate_cgit ($) {
20         my ($pi_config) = @_;
21         my $cgit_bin = $pi_config->{'publicinbox.cgitbin'};
22         my $cgit_data = $pi_config->{'publicinbox.cgitdata'};
23
24         # /var/www/htdocs/cgit is the default install path from cgit.git
25         # /usr/{lib,share}/cgit is where Debian puts cgit
26         # TODO: check other distros for common paths
27         unless (defined $cgit_bin) {
28                 foreach (qw(/var/www/htdocs/cgit /usr/lib/cgit)) {
29                         my $x = "$_/cgit.cgi";
30                         next unless -x $x;
31                         $cgit_bin = $x;
32                         last;
33                 }
34         }
35         unless (defined $cgit_data) {
36                 my @dirs = qw(/var/www/htdocs/cgit /usr/share/cgit);
37
38                 # local installs of cgit from source have
39                 # CGIT_SCRIPT_PATH==CGIT_DATA_PATH by default,
40                 # so we can usually infer the cgit_data path from cgit_bin
41                 if (defined($cgit_bin) && $cgit_bin =~ m!\A(.+?)/[^/]+\z!) {
42                         unshift @dirs, $1 if -d $1;
43                 }
44                 foreach my $d (@dirs) {
45                         my $f = "$d/cgit.css";
46                         next unless -f $f;
47                         $cgit_data = $d;
48                         last;
49                 }
50         }
51         ($cgit_bin, $cgit_data);
52 }
53
54 sub new {
55         my ($class, $pi_config) = @_;
56         my ($cgit_bin, $cgit_data) = locate_cgit($pi_config);
57
58         my $self = bless {
59                 cmd => [ $cgit_bin ],
60                 cgit_data => $cgit_data,
61                 pi_config => $pi_config,
62         }, $class;
63
64         $pi_config->fill_all; # fill in -code_repos mapped to inboxes
65
66         # some cgit repos may not be mapped to inboxes, so ensure those exist:
67         my $code_repos = $pi_config->{-code_repos};
68         foreach my $k (keys %$pi_config) {
69                 $k =~ /\Acoderepo\.(.+)\.dir\z/ or next;
70                 my $dir = $pi_config->{$k};
71                 $code_repos->{$1} ||= PublicInbox::Git->new($dir);
72         }
73         while (my ($nick, $repo) = each %$code_repos) {
74                 $self->{"\0$nick"} = $repo;
75         }
76         my $cgit_static = $pi_config->{-cgit_static};
77         my $static = join('|', map { quotemeta $_ } keys %$cgit_static);
78         $self->{static} = qr/\A($static)\z/;
79         $self;
80 }
81
82 # only what cgit cares about:
83 my @PASS_ENV = qw(
84         HTTP_HOST
85         QUERY_STRING
86         REQUEST_METHOD
87         SCRIPT_NAME
88         SERVER_NAME
89         SERVER_PORT
90         HTTP_COOKIE
91         HTTP_REFERER
92         CONTENT_LENGTH
93 );
94 # XXX: cgit filters may care about more variables...
95
96 my $parse_cgi_headers = \&PublicInbox::GitHTTPBackend::parse_cgi_headers;
97
98 sub call {
99         my ($self, $env) = @_;
100         my $path_info = $env->{PATH_INFO};
101         my $cgit_data;
102
103         # handle requests without spawning cgit iff possible:
104         if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
105                 my ($nick, $path) = ($1, $2);
106                 if (my PublicInbox::Git $git = $self->{"\0$nick"}) {
107                         return serve($env, $git, $path);
108                 }
109         } elsif ($path_info =~ m!$self->{static}! &&
110                  defined($cgit_data = $self->{cgit_data})) {
111                 my $f = $cgit_data.$1; # {static} only matches leading slash
112                 return PublicInbox::WwwStatic::response($env, [], $f);
113         }
114
115         my $cgi_env = { PATH_INFO => $path_info };
116         foreach (@PASS_ENV) {
117                 defined(my $v = $env->{$_}) or next;
118                 $cgi_env->{$_} = $v;
119         }
120         $cgi_env->{'HTTPS'} = 'on' if $env->{'psgi.url_scheme'} eq 'https';
121
122         my $rdr = input_prepare($env) or return r(500);
123         my $qsp = PublicInbox::Qspawn->new($self->{cmd}, $cgi_env, $rdr);
124         my $limiter = $self->{pi_config}->limiter('-cgit');
125         $qsp->psgi_return($env, $limiter, $parse_cgi_headers);
126 }
127
128 1;