]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Cgit.pm
cgit: support running cgit as a standalone CGI
[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 use warnings;
17 use PublicInbox::Qspawn;
18
19 sub new {
20         my ($class, $pi_config) = @_;
21         my $cgit_bin = $pi_config->{'publicinbox.cgitbin'} ||
22                 # Debian default location:
23                 '/usr/lib/cgit/cgit.cgi';
24
25         my $self = bless {
26                 cmd => [ $cgit_bin ],
27                 pi_config => $pi_config,
28         }, $class;
29
30         $pi_config->each_inbox(sub {}); # fill in -code_repos mapped to inboxes
31
32         # some cgit repos may not be mapped to inboxes, so ensure those exist:
33         my $code_repos = $pi_config->{-code_repos};
34         foreach my $k (keys %$pi_config) {
35                 $k =~ /\Acoderepo\.(.+)\.dir\z/ or next;
36                 my $dir = $pi_config->{$k};
37                 $code_repos->{$1} ||= PublicInbox::Git->new($dir);
38         }
39         while (my ($nick, $repo) = each %$code_repos) {
40                 $self->{"\0$nick"} = $repo;
41         }
42         $self;
43 }
44
45 # only what cgit cares about:
46 my @PASS_ENV = qw(
47         HTTP_HOST
48         QUERY_STRING
49         REQUEST_METHOD
50         SCRIPT_NAME
51         SERVER_NAME
52         SERVER_PORT
53         HTTP_COOKIE
54         HTTP_REFERER
55         CONTENT_LENGTH
56 );
57 # XXX: cgit filters may care about more variables...
58
59 sub call {
60         my ($self, $env) = @_;
61         my $path_info = $env->{PATH_INFO};
62
63         # handle requests without spawning cgit iff possible:
64         if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
65                 my ($nick, $path) = ($1, $2);
66                 if (my $git = $self->{"\0$nick"}) {
67                         return serve($env, $git, $path);
68                 }
69         }
70
71         my $cgi_env = { PATH_INFO => $path_info };
72         foreach (@PASS_ENV) {
73                 defined(my $v = $env->{$_}) or next;
74                 $cgi_env->{$_} = $v;
75         }
76         $cgi_env->{'HTTPS'} = 'on' if $env->{'psgi.url_scheme'} eq 'https';
77
78         my $rdr = input_prepare($env) or return r(500);
79         my $qsp = PublicInbox::Qspawn->new($self->{cmd}, $cgi_env, $rdr);
80         $qsp->psgi_return($env, undef, sub {
81                 my ($r, $bref) = @_;
82                 my $res = parse_cgi_headers($r, $bref) or return; # incomplete
83                 $res;
84         });
85 }
86
87 1;