]> Sergey Matveev's repositories - public-inbox.git/blobdiff - public-inbox.cgi
add example configs for Apache2 mod_perl and CGI
[public-inbox.git] / public-inbox.cgi
index b63223f509fc24cb05905e05c163c621303b7077..3bc6eca3c574fbe31e67a07141c10a0269c7b2c8 100755 (executable)
@@ -15,10 +15,12 @@ use warnings;
 use PublicInbox::Config;
 use URI::Escape qw(uri_escape_utf8 uri_unescape);
 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
+our $NO_SCRIPT_NAME; # for prettier redirects with mod_perl2
 our $pi_config;
 BEGIN {
        $pi_config = PublicInbox::Config->new;
        # TODO: detect and reload config as needed
+       $NO_SCRIPT_NAME = 1 if $ENV{NO_SCRIPT_NAME};
        if ($ENV{MOD_PERL}) {
                require CGI;
                no warnings;
@@ -36,6 +38,7 @@ if ($ENV{PI_PLACKUP}) {
        # so nuke it since CGI.pm functions without it.
        require CGI;
        delete $ENV{REQUEST_URI};
+       $ENV{SCRIPT_NAME} = '' if $NO_SCRIPT_NAME;
        my $req = CGI->new;
        my $ret = main($req, $req->request_method);
        binmode STDOUT;
@@ -147,13 +150,20 @@ sub mid2blob {
        my $hex = Digest::SHA::sha1_hex($ctx->{mid});
        $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
                        die "BUG: not a SHA-1 hex: $hex";
-       require IPC::Run;
-       my ($in, $blob, $err);
-       open my $null, '+<', '/dev/null' or die "open: $!\n";
-       IPC::Run::run(['git', "--git-dir=$ctx->{git_dir}",
-                       qw(cat-file blob), "HEAD:$1/$2"],
-                       $null, \$blob, $null);
-       $? == 0 ? \$blob : undef;
+
+       my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
+                       qw(cat-file blob), "HEAD:$1/$2");
+       my $cmd = join(' ', @cmd);
+       my $pid = open my $fh, '-|';
+       defined $pid or die "fork failed: $!\n";
+       if ($pid == 0) {
+               open STDERR, '>', '/dev/null'; # ignore errors
+               exec @cmd or die "exec failed: $!\n";
+       } else {
+               my $blob = eval { local $/; <$fh> };
+               close $fh;
+               $? == 0 ? \$blob : undef;
+       }
 }
 
 # /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw original