]> 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 87cc6943b1b344abe75e38ea5d6e0c4cb5466690..3bc6eca3c574fbe31e67a07141c10a0269c7b2c8 100755 (executable)
 use 5.008;
 use strict;
 use warnings;
-use CGI qw(:cgi -nosticky); # PSGI/FastCGI/mod_perl compat
-use Encode qw(find_encoding);
 use PublicInbox::Config;
 use URI::Escape qw(uri_escape_utf8 uri_unescape);
-our $enc_utf8 = find_encoding('UTF-8');
 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;
+               $CGI::NOSTICKY = 1;
                CGI->compile;
        }
 }
@@ -30,7 +32,15 @@ BEGIN {
 if ($ENV{PI_PLACKUP}) {
        psgi_app();
 } else {
-       my $ret = main();
+       # some servers (Ruby webrick) include scheme://host[:port] here,
+       # which confuses CGI.pm when generating self_url.
+       # RFC 3875 does not mention REQUEST_URI at all,
+       # 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;
        if (@ARGV && $ARGV[0] eq 'static') {
                print $ret->[2]->[0];
@@ -42,21 +52,15 @@ if ($ENV{PI_PLACKUP}) {
 # private functions below
 
 sub main {
-       # some servers (Ruby webrick) include scheme://host[:port] here,
-       # which confuses CGI.pm when generating self_url.
-       # RFC 3875 does not mention REQUEST_URI at all,
-       # so nuke it since CGI.pm functions without it.
-       delete $ENV{REQUEST_URI};
-
-       my $cgi = CGI->new;
+       my ($cgi, $method) = @_;
        my %ctx;
-       if ($cgi->request_method !~ /\AGET|HEAD\z/) {
+       if ($method !~ /\AGET|HEAD\z/) {
                return r(405, 'Method Not Allowed');
        }
-       my $path_info = $enc_utf8->decode($cgi->path_info);
+       my $path_info = $cgi->path_info;
 
        # top-level indices and feeds
-       if ($path_info eq "/") {
+       if ($path_info eq '/') {
                r404();
        } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
                invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
@@ -142,13 +146,24 @@ sub get_index {
 # just returns a string ref for the blob in the current ctx
 sub mid2blob {
        my ($ctx) = @_;
-       local $ENV{GIT_DIR} = $ctx->{git_dir};
        require Digest::SHA;
        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";
-       my $blob = `git cat-file blob HEAD:$1/$2 2>/dev/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
@@ -184,14 +199,19 @@ sub get_full_html {
                [ PublicInbox::View->as_html(Email::MIME->new($$x))] ];
 }
 
+sub self_url {
+       my ($cgi) = @_;
+       ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
+}
+
 sub redirect_list_index {
        my ($ctx, $cgi) = @_;
-       do_redirect($cgi->self_url . "/");
+       do_redirect(self_url($cgi) . "/");
 }
 
 sub redirect_mid {
        my ($ctx, $cgi) = @_;
-       my $url = $cgi->self_url;
+       my $url = self_url($cgi);
        $url =~ s!/f/!/m/!;
        do_redirect($url . '.html');
 }
@@ -205,8 +225,6 @@ sub do_redirect {
 }
 
 sub psgi_app {
-       require CGI::Emulate::PSGI;
-
        # preload so we are CoW friendly
        require PublicInbox::Feed;
        require PublicInbox::View;
@@ -214,12 +232,11 @@ sub psgi_app {
        require Digest::SHA;
        require POSIX;
        require XML::Atom::SimpleFeed;
-       eval { require Git };
+       require Plack::Request;
        sub {
-               my ($e) = @_;
-               local %ENV = (%ENV, CGI::Emulate::PSGI->emulate_environment($e));
-               main();
-       }
+               my $req = Plack::Request->new(@_);
+               main($req, $req->method);
+       };
 }
 
 sub cgi_print {