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