]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ExtMsg.pm
remove direct CGI.pm support
[public-inbox.git] / lib / PublicInbox / ExtMsg.pm
index 82f4c63dc5ddd8b19820b58bda131f87a535d68d..cf9b6d58ba8d3b9836300a28cf96cf2000c1fa8f 100644 (file)
@@ -16,8 +16,9 @@ use PublicInbox::MID qw/mid2path/;
 our @EXT_URL = (
        'http://mid.gmane.org/%s',
        'https://lists.debian.org/msgid-search/%s',
-       'http://mid.mail-archive.com/%s',
-       'http://marc.info/?i=%s',
+       # leading "//" denotes protocol-relative (http:// or https://)
+       '//mid.mail-archive.com/%s',
+       '//marc.info/?i=%s',
 );
 
 sub ext_msg {
@@ -72,21 +73,10 @@ sub ext_msg {
        my $path = "HEAD:" . mid2path($mid);
 
        foreach my $n (@nox) {
-               my @cmd = ('git', "--git-dir=$n->{git_dir}", 'cat-file',
-                          '-t', $path);
-               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 $type = eval { local $/; <$fh> };
-                       close $fh;
-                       if ($? == 0 && $type eq "blob\n") {
-                               return r302($n->{url}, $mid);
-                       }
-               }
+               # TODO: reuse existing PublicInbox::Git objects to save forks
+               my $git = PublicInbox::Git->new($n->{git_dir});
+               my (undef, $type, undef) = $git->check($path);
+               return r302($n->{url}, $mid) if ($type && $type eq 'blob');
        }
 
        # fall back to partial MID matching
@@ -95,13 +85,13 @@ sub ext_msg {
 
        eval { require PublicInbox::Msgmap };
        my $have_mm = $@ ? 0 : 1;
+       my $cgi = $ctx->{cgi};
+       my $base_url = $cgi->base->as_string;
        if ($have_mm) {
                my $tmp_mid = $mid;
+               my $url;
 again:
-               my $cgi = $ctx->{cgi};
-               my $url = ref($cgi) eq 'CGI' ? $cgi->url(-base) . '/'
-                                       : $cgi->base->as_string;
-               $url .= $listname;
+               $url = $base_url . $listname;
                unshift @pfx, { git_dir => $ctx->{git_dir}, url => $url };
                foreach my $pfx (@pfx) {
                        my $git_dir = delete $pfx->{git_dir} or next;
@@ -135,7 +125,7 @@ again:
                foreach my $pfx (@partial) {
                        my $u = $pfx->{url};
                        foreach my $m (@{$pfx->{res}}) {
-                               my $p = PublicInbox::Hval->new($m);
+                               my $p = PublicInbox::Hval->new_msgid($m);
                                my $r = $p->as_href;
                                my $t = $p->as_html;
                                $s .= qq{<a\nhref="$u/$r/">$u/$t/</a>\n};
@@ -147,7 +137,9 @@ again:
        if (@EXT_URL && index($mid, '@') >= 0) {
                $code = 300;
                $s .= "\nPerhaps try an external site:\n\n";
+               my $scheme = $cgi->scheme;
                foreach my $u (@EXT_URL) {
+                       $u = "$scheme:$u" if $u =~ m!\A//!;
                        my $r = sprintf($u, $href);
                        my $t = sprintf($u, $html);
                        $s .= qq{<a\nhref="$r">$t</a>\n};