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