]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Cgit.pm
use "\&" where possible when referring to subroutines
[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 PublicInbox::Qspawn;
16 use PublicInbox::WwwStatic qw(r);
17
18 sub locate_cgit ($) {
19         my ($pi_config) = @_;
20         my $cgit_bin = $pi_config->{'publicinbox.cgitbin'};
21         my $cgit_data = $pi_config->{'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                 foreach my $d (@dirs) {
44                         my $f = "$d/cgit.css";
45                         next unless -f $f;
46                         $cgit_data = $d;
47                         last;
48                 }
49         }
50         ($cgit_bin, $cgit_data);
51 }
52
53 sub new {
54         my ($class, $pi_config) = @_;
55         my ($cgit_bin, $cgit_data) = locate_cgit($pi_config);
56
57         my $self = bless {
58                 cmd => [ $cgit_bin ],
59                 cgit_data => $cgit_data,
60                 pi_config => $pi_config,
61         }, $class;
62
63         $pi_config->fill_all; # fill in -code_repos mapped to inboxes
64
65         # some cgit repos may not be mapped to inboxes, so ensure those exist:
66         my $code_repos = $pi_config->{-code_repos};
67         foreach my $k (keys %$pi_config) {
68                 $k =~ /\Acoderepo\.(.+)\.dir\z/ or next;
69                 my $dir = $pi_config->{$k};
70                 $code_repos->{$1} ||= PublicInbox::Git->new($dir);
71         }
72         while (my ($nick, $repo) = each %$code_repos) {
73                 $self->{"\0$nick"} = $repo;
74         }
75         my $cgit_static = $pi_config->{-cgit_static};
76         my $static = join('|', map { quotemeta $_ } keys %$cgit_static);
77         $self->{static} = qr/\A($static)\z/;
78         $self;
79 }
80
81 # only what cgit cares about:
82 my @PASS_ENV = qw(
83         HTTP_HOST
84         QUERY_STRING
85         REQUEST_METHOD
86         SCRIPT_NAME
87         SERVER_NAME
88         SERVER_PORT
89         HTTP_COOKIE
90         HTTP_REFERER
91         CONTENT_LENGTH
92 );
93 # XXX: cgit filters may care about more variables...
94
95 my $parse_cgi_headers = \&PublicInbox::GitHTTPBackend::parse_cgi_headers;
96
97 sub call {
98         my ($self, $env) = @_;
99         my $path_info = $env->{PATH_INFO};
100         my $cgit_data;
101
102         # handle requests without spawning cgit iff possible:
103         if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
104                 my ($nick, $path) = ($1, $2);
105                 if (my PublicInbox::Git $git = $self->{"\0$nick"}) {
106                         return serve($env, $git, $path);
107                 }
108         } elsif ($path_info =~ m!$self->{static}! &&
109                  defined($cgit_data = $self->{cgit_data})) {
110                 my $f = $cgit_data.$1; # {static} only matches leading slash
111                 return PublicInbox::WwwStatic::response($env, [], $f);
112         }
113
114         my $cgi_env = { PATH_INFO => $path_info };
115         foreach (@PASS_ENV) {
116                 defined(my $v = $env->{$_}) or next;
117                 $cgi_env->{$_} = $v;
118         }
119         $cgi_env->{'HTTPS'} = 'on' if $env->{'psgi.url_scheme'} eq 'https';
120
121         my $rdr = input_prepare($env) or return r(500);
122         my $qsp = PublicInbox::Qspawn->new($self->{cmd}, $cgi_env, $rdr);
123         my $limiter = $self->{pi_config}->limiter('-cgit');
124         $qsp->psgi_return($env, $limiter, $parse_cgi_headers);
125 }
126
127 1;