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