]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Cgit.pm
config: support "inboxdir" in addition to "mainrepo"
[public-inbox.git] / lib / PublicInbox / Cgit.pm
1 # Copyright (C) 2019 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 # 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;
17 use warnings;
18 use PublicInbox::Qspawn;
19 use Plack::MIME;
20
21 sub locate_cgit ($) {
22         my ($pi_config) = @_;
23         my $cgit_bin = $pi_config->{'publicinbox.cgitbin'};
24         my $cgit_data = $pi_config->{'publicinbox.cgitdata'};
25
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";
32                         next unless -x $x;
33                         $cgit_bin = $x;
34                         last;
35                 }
36         }
37         unless (defined $cgit_data) {
38                 my @dirs = qw(/var/www/htdocs/cgit /usr/share/cgit);
39
40                 # local installs of cgit from source have
41                 # CGIT_SCRIPT_PATH==CGIT_DATA_PATH by default,
42                 # so we can usually infer the cgit_data path from cgit_bin
43                 if (defined($cgit_bin) && $cgit_bin =~ m!\A(.+?)/[^/]+\z!) {
44                         unshift @dirs, $1 if -d $1;
45                 }
46                 foreach my $d (@dirs) {
47                         my $f = "$d/cgit.css";
48                         next unless -f $f;
49                         $cgit_data = $d;
50                         last;
51                 }
52         }
53         ($cgit_bin, $cgit_data);
54 }
55
56 sub new {
57         my ($class, $pi_config) = @_;
58         my ($cgit_bin, $cgit_data) = locate_cgit($pi_config);
59
60         my $self = bless {
61                 cmd => [ $cgit_bin ],
62                 cgit_data => $cgit_data,
63                 pi_config => $pi_config,
64         }, $class;
65
66         $pi_config->each_inbox(sub {}); # fill in -code_repos mapped to inboxes
67
68         # some cgit repos may not be mapped to inboxes, so ensure those exist:
69         my $code_repos = $pi_config->{-code_repos};
70         foreach my $k (keys %$pi_config) {
71                 $k =~ /\Acoderepo\.(.+)\.dir\z/ or next;
72                 my $dir = $pi_config->{$k};
73                 $code_repos->{$1} ||= PublicInbox::Git->new($dir);
74         }
75         while (my ($nick, $repo) = each %$code_repos) {
76                 $self->{"\0$nick"} = $repo;
77         }
78         my $cgit_static = $pi_config->{-cgit_static};
79         my $static = join('|', map { quotemeta $_ } keys %$cgit_static);
80         $self->{static} = qr/\A($static)\z/;
81         $self;
82 }
83
84 # only what cgit cares about:
85 my @PASS_ENV = qw(
86         HTTP_HOST
87         QUERY_STRING
88         REQUEST_METHOD
89         SCRIPT_NAME
90         SERVER_NAME
91         SERVER_PORT
92         HTTP_COOKIE
93         HTTP_REFERER
94         CONTENT_LENGTH
95 );
96 # XXX: cgit filters may care about more variables...
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 $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 = $1;
112                 my $type = Plack::MIME->mime_type($f);
113                 return static_result($env, [], $cgit_data.$f, $type);
114         }
115
116         my $cgi_env = { PATH_INFO => $path_info };
117         foreach (@PASS_ENV) {
118                 defined(my $v = $env->{$_}) or next;
119                 $cgi_env->{$_} = $v;
120         }
121         $cgi_env->{'HTTPS'} = 'on' if $env->{'psgi.url_scheme'} eq 'https';
122
123         my $rdr = input_prepare($env) or return r(500);
124         my $qsp = PublicInbox::Qspawn->new($self->{cmd}, $cgi_env, $rdr);
125         my $limiter = $self->{pi_config}->limiter('-cgit');
126         $qsp->psgi_return($env, $limiter, sub {
127                 my ($r, $bref) = @_;
128                 my $res = parse_cgi_headers($r, $bref) or return; # incomplete
129                 $res;
130         });
131 }
132
133 1;